unit uGUI;

interface

uses
  dfHRenderer, dfMath;

const
  DISAPPEAR_TIME = 3.0;

  HEALTHBAR_X = 30;
  HEALTHBAR_Y = 40;
  FORCEBAR_X  = 30;
  FORCEBAR_Y  = 60;

  TEXT_CANUSE  = '[Enter] — использовать спец. способность';
  TEXT_CANTUSE = 'Недостаточно энергии';

type
  TglrInGameGUI = class
  protected
    Ft: Single;
  public
    FShowText, FPlayerText, FPlayer2Text, FCanUseAbility: IdfText;
    FPlayerHealthBar, FPlayer2HealthBar,
    FPlayerForceBar, FPlayer2ForceBar: IdfGUISlider;
    //FSignal: IdfSprite;
    constructor Create(aScene: Idf2DScene); virtual;
    destructor Destroy(); override;

    procedure Update(const dt: Double);
    procedure UpdateSliders();

    procedure ShowText(aText: WideString);
    procedure ShowAbilityText(aText: WideString; aTime: Single);
  end;

implementation

uses
  SysUtils,
  uGlobal, uPopup, uGameScreen.Game,
  dfHUtility, dfTweener;

procedure UpdateSliderValue(aSlider: IdfTweenObject; aValue: Single);
begin
  with IdfGUISlider(aSlider) do
    Value := Floor(aValue);
end;

{ TglrInGameGUI }

constructor TglrInGameGUI.Create(aScene: Idf2DScene);
begin
  inherited Create();

  FPlayerText := dfCreateText();
  with FPlayerText do
  begin
    Position := dfVec2f(HEALTHBAR_X, 10);
    PivotPoint := ppTopLeft;
    Font := fontCooper;
    Material.MaterialOptions.Diffuse := dfVec4f(0.2, 0.2, 0.2, 1.0);
    Z := Z_HUD;
    case game.GameMode of
      gmSingle:         Text := 'Игрок';
      gmTwoPlayersVs:   Text := 'Игрок 1';
    end;
  end;

  FPlayer2Text := dfCreateText();
  with FPlayer2Text do
  begin
    Position := dfVec2f(R.WindowWidth - HEALTHBAR_X, 10);
    PivotPoint := ppTopRight;
    Font := fontCooper;
    Material.MaterialOptions.Diffuse := dfVec4f(0.2, 0.2, 0.2, 1.0);
    Z := Z_HUD;
    case game.GameMode of
      gmSingle:       Text := 'Противник';
      gmTwoPlayersVs: Text := 'Игрок 2';
    end;
  end;

  FShowText := dfCreateText();
  with FShowText do
  begin
    Position := dfVec2f(R.WindowWidth div 2, R.WindowHeight div 2);
    PivotPoint := ppCenter;
    Font := fontCooper;
    Material.MaterialOptions.Diffuse := dfVec4f(0.2, 0.2, 0.2, 1.0);
    Z := Z_HUD;
  end;

  FCanUseAbility := dfCreateText();
  with FCanUseAbility do
  begin
    Position := dfVec2f(R.WindowWidth div 2, 70);
    PivotPoint := ppCenter;
    Font := fontCooper;
    Material.MaterialOptions.Diffuse := dfVec4f(0.2, 0.2, 0.2, 1.0);
    Z := Z_HUD;
  end;

  FPlayerHealthBar := dfCreateGUISlider();
  with FPlayerHealthBar do
  begin
    Material.Texture := atlasMain.LoadTexture(HEALTHSLIDER_BACK);
    UpdateTexCoords();
    SetSizeToTextureSize();
    with SliderOver do
    begin
      Material.Texture := atlasMain.LoadTexture(HEALTHSLIDER_OVER);
      UpdateTexCoords();
      SetSizeToTextureSize();
    end;
    SliderButton.Visible := False;
    Z := Z_HUD;
    Position := dfVec2f(HEALTHBAR_X , HEALTHBAR_Y);
  end;

  FPlayer2HealthBar := dfCreateGUISlider();
  with FPlayer2HealthBar do
  begin
    Material.Texture := atlasMain.LoadTexture(HEALTHSLIDER_BACK);
    UpdateTexCoords();
    SetSizeToTextureSize();
    with SliderOver do
    begin
      Material.Texture := atlasMain.LoadTexture(HEALTHSLIDER_OVER);
      UpdateTexCoords();
      SetSizeToTextureSize();
    end;
    SliderButton.Visible := False;
    Z := Z_HUD;
    Position := dfVec2f(R.WindowWidth - HEALTHBAR_X - Width, HEALTHBAR_Y);
  end;

  FPlayerForceBar := dfCreateGUISlider();
  with FPlayerForceBar do
  begin
    Material.Texture := atlasMain.LoadTexture(SLIDER_BACK);
    Material.MaterialOptions.Diffuse := dfVec4f(0.0, 0.0, 0.0, 1.0);
    UpdateTexCoords();
    SetSizeToTextureSize();
    with SliderOver do
    begin
      Material.Texture := atlasMain.LoadTexture(SLIDER_OVER);
      Material.MaterialOptions.Diffuse := dfVec4f(0.2, 0.2, 0.6, 1.0);
      UpdateTexCoords();
      SetSizeToTextureSize();
    end;
    SliderButton.Visible := False;
    Z := Z_HUD;
    Position := dfVec2f(FORCEBAR_X , FORCEBAR_Y);
  end;

  FPlayer2ForceBar := dfCreateGUISlider();
  with FPlayer2ForceBar do
  begin
    Material.Texture := atlasMain.LoadTexture(SLIDER_BACK);
    Material.MaterialOptions.Diffuse := dfVec4f(0.0, 0.0, 0.0, 1.0);
    UpdateTexCoords();
    SetSizeToTextureSize();
    with SliderOver do
    begin
      Material.Texture := atlasMain.LoadTexture(SLIDER_OVER);
      Material.MaterialOptions.Diffuse := dfVec4f(0.2, 0.2, 0.6, 1.0);
      UpdateTexCoords();
      SetSizeToTextureSize();
    end;
    SliderButton.Visible := False;
    Z := Z_HUD;
    Position := dfVec2f(R.WindowWidth - FORCEBAR_X - Width, FORCEBAR_Y);
  end;

  aScene.RegisterElement(FShowText);
  aScene.RegisterElement(FPlayerText);
  aScene.RegisterElement(FPlayerHealthBar);
  aScene.RegisterElement(FPlayerForceBar);
  aScene.RegisterElement(FPlayer2Text);
  aScene.RegisterElement(FPlayer2HealthBar);
  aScene.RegisterElement(FPlayer2ForceBar);
  aScene.RegisterElement(FCanUseAbility)
end;

destructor TglrInGameGUI.Destroy;
begin
  inherited;
end;

procedure TglrInGameGUI.ShowAbilityText(aText: WideString; aTime: Single);
begin

end;

procedure TglrInGameGUI.ShowText(aText: WideString);
begin
  FShowText.Text := aText;
  Ft := DISAPPEAR_TIME;
end;

procedure TglrInGameGUI.Update(const dt: Double);
begin
  if Ft > 0 then
  begin
    Ft := Ft - dt;
    FShowText.Material.MaterialOptions.PDiffuse.w := Ft / DISAPPEAR_TIME;
  end
end;

procedure TglrInGameGUI.UpdateSliders;
begin
  Tweener.AddTweenInterface(FPlayerHealthBar, @UpdateSliderValue, tsExpoEaseIn, FPlayerHealthBar.Value, player.Health, 1.5, 0.0);
  Tweener.AddTweenInterface(FPlayer2HealthBar, @UpdateSliderValue, tsExpoEaseIn, FPlayer2HealthBar.Value, player2.Health, 1.5, 0.0);
//  FPlayerHealthBar.Value  := player.Health;
//  FPlayer2HealthBar.Value := player2.Health;

  Tweener.AddTweenInterface(FPlayerForceBar, @UpdateSliderValue, tsExpoEaseIn, FPlayerForceBar.Value, player.Force, 1.5, 0.0);
  Tweener.AddTweenInterface(FPlayer2ForceBar, @UpdateSliderValue, tsExpoEaseIn, FPlayer2ForceBar.Value, player2.Force, 1.5, 0.0);
//  FPlayerForceBar.Value  := player.Force;
//  FPlayer2ForceBar.Value := player2.Force;
end;

end.
