unit u_Button;

interface
uses
   zglHeader;
type
  T_Button = class
    T, ST : Integer;
    Gl, ifS : single;
    X2, Y2,
    X, Y, W, H : Integer;
    Rect : zglTRect;
    S : string;
    constructor Create(inS : string; inT : Integer; inX, inY, inW, inH : Integer; fS : single = 0.7);
    procedure   SetP(inS : string; inT : Integer; inX, inY, inW, inH : Integer; fS : single = 0.7);
    procedure   CalcXY;
    function    Work({inX, inY : Integer}) : Boolean;
    procedure   Draw;
  end;

const
  BtnColor : array[1..2] of Integer = ( $FFFFFF, $777777);

implementation
uses
  GSound, uMaterials;

constructor T_Button.Create;
begin
  SetP(inS, inT, inX, inY, inW, inH, fS);
end;

procedure T_Button.SetP;
begin
  X := inX;
  Y := inY;
  W := inW;
  H := inH;
  S := inS;
  T := inT;
  ST := 1;

  ifS := fS;

  CalcXY;

  Rect.X := X;
  Rect.Y := Y;
  Rect.W := W;
  Rect.H := H;

end;

procedure T_Button.CalcXY;
begin
  X2 := X + W;
  Y2 := Y + H;
end;

function T_Button.Work;
begin
  Result := False;
  if  (mouse_X > X  + 2)
  and (mouse_X < X2 - 2)
  and (mouse_Y > Y  + 2)
  and (mouse_Y < Y2 - 2) then
  begin
    ST := 2;
    if GL < 245 then
    GL := GL + 7
    else
      GL := 255;
    if (mouse_Click(M_BLEFT)) then
    begin
      Result := True;
      PlaySound(2);
    end;
  end
  else
  begin
    ST := 1;
    if GL > 7 then
      GL := GL - 7
    else
      GL := 0;
  end;
end;

procedure T_Button.Draw;
begin
  ssprite2d_Draw(mButton[1], X, Y, W, H, 0);
//  if ST > 1 then
  ssprite2d_Draw(mButton[2], X, Y, W, H, 0, trunc(GL));

//  Text_DrawEx(mFont_1, TX, Y + (H - 24)/2, ifS, 0, AnsiToUtf8(S),  225, $110101);

  text_DrawInRectEx(mFont_1, Rect, ifS, 0, AnsiToUtf8(S), 225, $110101, TEXT_HALIGN_CENTER or TEXT_VALIGN_CENTER);

end;

end.
