unit uPage;

interface
uses
  LexUtils, zglHeader, uScene;

type
  TPageButton = class;

  TPage = class
    pX, pY, rX, rY : Integer;
    PageX, PageY : PInt;
    Color,
    W, H, Wd2 : Integer;
    Enabled, bgDraw,
    Visible : Boolean;
    PScene : TScene;
    APageButton : array of TPageButton;
    constructor Create(const X, Y : Integer; const Scene : TScene); overload;
    constructor Create(const X, Y, W_, H_, Color_ : Integer; const Scene : TScene); overload;
    procedure AddButton(PageButton : TPageButton);
    procedure Draw; virtual;
    procedure Update; virtual;
    destructor Free;
  end;

  TPageButton = class
    Enabled,
    Clicked,
    MouseIn : Boolean;
    PageX, PageY : PInt;
    pX, pY, W, H,  pX1, pY1,
    cpX1, cpY1, cpX2, cpY2, cW, cH,
    Alpha : Integer;
    Text : String;
    Texture, TextureGlow : zglPTexture;
    constructor Create(X, Y, W_, H_, cW_, cH_ : Integer; Text_: String;
                Enabled_: Boolean); virtual;
    function  Update(X, Y : Single) : Boolean;
    procedure Click; virtual;
    procedure Draw; virtual;
  end;

implementation
uses
  uMaterials;
{ TPage }

procedure TPage.AddButton(PageButton : TPageButton);
var
  L : Integer;
begin
  L := Length(APageButton);
  SetLength(APageButton, L + 1);
  APageButton[L] := PageButton;
  APageButton[L].PageX := @pX;
  APageButton[L].PageY := @pY;
end;

constructor TPage.Create(const X, Y : Integer; const Scene : TScene);
begin
  rX := X;
  rY := Y;
  PScene := Scene;
  if PScene <> nil then
  begin
    PageX := @PScene.pX;
    PageY := @PScene.pX;
  end
  else
  begin
    PageX := @c_nul;
    PageY := @c_nul;
  end;
  pX := rX + PageX^;
  pY := rY + PageY^;
  bgDraw := False;
end;

constructor TPage.Create(const X, Y, W_, H_, Color_: Integer; const Scene : TScene);
begin
  Create(X, Y, Scene);
  W := W_;
  H := H_;
  Wd2 := W div 2;
  Color := Color_;
  bgDraw := True;
end;


procedure TPage.Draw;
var
  I : Integer;
begin
  if bgDraw then
  begin
    pr2d_Rect(pX - Wd2 - 16, pY - 16, W + 32, H + 32, Color, 128, PR2D_FILL);
    pr2d_Rect(pX - Wd2, pY, W, H, Color, 160, PR2D_FILL);
  end;
  for I := 0 to Length(APageButton) - 1 do
  if APageButton[I].Enabled then
    APageButton[I].Draw;
end;

destructor TPage.Free;
var
  I : Integer;
begin
  for I := 0 to Length(APageButton) - 1 do
  if APageButton[I] <> nil then
    APageButton[I].Free;
end;

procedure TPage.Update;
var
  I : Integer;
  X, Y : Single;
begin
  pX := rX + PageX^;
  pY := rY + PageY^;

  X := mouse_X - pX;
  Y := mouse_Y - pY;

  for I := 0 to Length(APageButton) - 1 do
  if APageButton[I].Enabled then
    APageButton[I].Update(X, Y);
end;

{ TPageButton }
constructor TPageButton.Create(X, Y, W_, H_, cW_, cH_ : Integer; Text_: String;
                Enabled_: Boolean);
begin
  pX := X;
  pY := Y;
  W := W_;
  H := H_;
  pX1 := pX - W div 2;
  pY1 := pY - H div 2;
  cW := cW_;
  cH := cH_;
  cpX1 := pX - cW div 2;
  cpY1 := pY - cH div 2;
  cpX2 := cpX1 + cW;
  cpY2 := cpY1 + cH;

  PageX := @c_nul;
  PageY := @c_nul;
  Enabled := Enabled_;
  Text := Text_;
  Alpha := 0;

  Texture := mPBT1[1];
  TextureGlow := mPBT1[2];
end;

procedure TPageButton.Draw;
begin
 // pr2d_Rect(PageX^ + cpX1, PageY^ + cpY1, cW, cH);
  ssprite2d_Draw(Texture, PageX^ + pX1, PageY^ + pY1, W, H, 0);
  ssprite2d_Draw(TextureGlow, PageX^ + pX1, PageY^ + pY1, W, H, 0, Alpha);
  text_DrawEx(mFont_1,  PageX^ + pX, PageY^ + pY, 1, 0, Text,  252, $FFFFFF, TEXT_HALIGN_CENTER or TEXT_VALIGN_CENTER);
end;

function TPageButton.Update(X, Y : Single) : Boolean;
begin
  if  (X > cpX1)
  and (X < cpX2)
  and (Y > cpY1)
  and (Y < cpY2) then
  begin
    MouseIn := True;

    if Alpha < 245 then
    Alpha := Alpha + 7
    else
      Alpha := 255;

    if (mouse_Click(M_BLEFT)) then
    begin
      Click;
      Result := True;
      Clicked := True;
    end
    else
      Result := False;
  end
  else
  begin
    if Alpha > 7 then
      Alpha := Alpha - 7
    else
      Alpha := 0;
    MouseIn := False;
    Result := False;
    Clicked := False;
  end;
end;

procedure TPageButton.Click;
begin
end;

end.
