unit iz_GUI;

interface
uses
  iz_gfx,iz_types,iz_data,iz_cfg,iz_Utils,iz_Options,
  GPAHeader;
type
  zGUIState = (GUI_ALL, GUI_MENU, GUI_GAME);

  PzComponent = ^zComponent;
  zComponent = class
    Name : string;
    Pos : izPosition_f;
    Size,Frame_Size : izSize_w;
    Tex,Spr,Ani : word;
    State : zGUIState;
    Visible : bool;
    Rect : izRect;
  public
    constructor Create(); virtual;
    destructor Destroy(); virtual;
  end;

  PzPicture = ^zPicture;
  zPicture = class(zComponent)
  Animation : byte;
  public
    constructor Create(); override;
    procedure Update();
    function OnClick : bool;
    procedure Draw();
    destructor Destroy(); override;
  end;

  PzGUI = ^zGUI;
  zGUI = class
  PictureCount : word;
  Picture : array of zPicture;
  public
    constructor Create();
    procedure Load(FileName : string);
    procedure AddPicture(FileName,Name : string);
    procedure Update(State : zGUIState);
    procedure Draw(State : zGUIState);
    destructor Destroy();
  end;

implementation
uses
  g_Data;

{ zComponent }

constructor zComponent.Create;
begin

end;

destructor zComponent.Destroy;
begin

end;

{ zGUI }

constructor zGUI.Create;
begin
  PictureCount := 0;
end;

procedure zGUI.Load(FileName: string);
var
  i,count : byte;
begin
  count := cfg_readInt(FileName,'GUI_COMPONNENT_COUNT',2);
  i := 0;
  while i < count do begin
    AddPicture(FileName,Cfg_ReadStr(FileName, 'GUI_NAME_'+inttostr(i+1),'CUR'));
    inc(i);
  end;
end;

procedure zGUI.AddPicture(FileName,Name : string);
var
  sID,eID,cId,delay,state,Allign,SpaceX,SpaceY : word;
  TexName : string;
  Visible : byte;
begin
  inc(PictureCount);
  Setlength(Picture,PictureCount);
  Picture[PictureCount-1] := zPicture.Create();
  Picture[PictureCount-1].Name := Name;
  State := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_STATE',0);
  case State of
    0 : Picture[PictureCount-1].State := GUI_ALL;
    1 : Picture[PictureCount-1].State := GUI_MENU;
    2 : Picture[PictureCount-1].State := GUI_GAME;
  end;
  Visible := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_VISIBLE',1);
  if Visible = 1 then Picture[PictureCount-1].Visible := true else Picture[PictureCount-1].Visible := false;
  Picture[PictureCount-1].Frame_Size[0] := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_FRAME_WIDTH',100);
  Picture[PictureCount-1].Frame_Size[1] := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_FRAME_HEIGHT',100);
  Picture[PictureCount-1].Size[0] := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_WIDTH',100);
  Picture[PictureCount-1].Size[1] := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_HEIGHT',100);
  SpaceX := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_SPACE_X',0);
  SpaceY := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_SPACE_Y',0);
  Allign := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_ALLIGN',0);
  case Allign of
  0 : begin
        Picture[PictureCount-1].Pos[0] := Options.ScreenWidth div 2;
        Picture[PictureCount-1].Pos[1] := Picture[PictureCount-1].Size[1] div 2+SpaceY;
      end;
  1 : begin
        Picture[PictureCount-1].Pos[0] := Options.ScreenWidth - Picture[PictureCount-1].Size[0] div 2-SpaceX;
        Picture[PictureCount-1].Pos[1] := Picture[PictureCount-1].Size[1] div 2+SpaceY;
      end;
  2 : begin
        Picture[PictureCount-1].Pos[0] := Options.ScreenWidth - Picture[PictureCount-1].Size[0] div 2-SpaceX;
        Picture[PictureCount-1].Pos[1] := Options.ScreenHeight div 2;
      end;
  3 : begin
        Picture[PictureCount-1].Pos[0] := Options.ScreenWidth - Picture[PictureCount-1].Size[0] div 2-SpaceX;
        Picture[PictureCount-1].Pos[1] := Options.ScreenHeight - Picture[PictureCount-1].Size[1] div 2-SpaceY;
      end;
  4 : begin
        Picture[PictureCount-1].Pos[0] := Options.ScreenWidth div 2;
        Picture[PictureCount-1].Pos[1] := Options.ScreenHeight - Picture[PictureCount-1].Size[1] div 2-SpaceY;
      end;
  5 : begin
        Picture[PictureCount-1].Pos[0] := Picture[PictureCount-1].Size[0] div 2+SpaceX;
        Picture[PictureCount-1].Pos[1] := Options.ScreenHeight - Picture[PictureCount-1].Size[1] div 2;
      end;
  6 : begin
        Picture[PictureCount-1].Pos[0] := Picture[PictureCount-1].Size[0] div 2+SpaceX;
        Picture[PictureCount-1].Pos[1] := Options.ScreenHeight div 2;
      end;
  7 : begin
        Picture[PictureCount-1].Pos[0] := Picture[PictureCount-1].Size[0] div 2+SpaceX;
        Picture[PictureCount-1].Pos[1] := Picture[PictureCount-1].Size[1] div 2+SpaceY;
      end;
  255 : begin
          Picture[PictureCount-1].Pos[0] := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_X',0);
          Picture[PictureCount-1].Pos[1] := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_Y',0);
      end;
  end;
  Picture[PictureCount-1].Rect.Left := Picture[PictureCount-1].Pos[0] - Picture[PictureCount-1].Size[0] div 2;
  Picture[PictureCount-1].Rect.Right := Picture[PictureCount-1].Pos[0] + Picture[PictureCount-1].Size[0] div 2;
  Picture[PictureCount-1].Rect.Top := Picture[PictureCount-1].Pos[1] - Picture[PictureCount-1].Size[1] div 2;
  Picture[PictureCount-1].Rect.Bottom := Picture[PictureCount-1].Pos[1] + Picture[PictureCount-1].Size[1] div 2;
  if Picture[PictureCount-1].Size[0] = 0 then begin
     Picture[PictureCount-1].Size[0] := options.ScreenWidth;
     Picture[PictureCount-1].Size[1] := options.ScreenHeight;
     Picture[PictureCount-1].Pos[0] := options.ScreenWidth div 2;
     Picture[PictureCount-1].Pos[1] := options.ScreenHeight div 2;
  end;


  delay := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_DELAY',0);
  TexName := Cfg_ReadStr(FileName, Picture[PictureCount-1].Name + '_TEX',Name+'.tga');
  GPAExtractToFileByID(GPAGetIDByName(PCHAR(TexName),true),PChar(iz_dir_local_temp_gfx+TexName));
  Picture[PictureCount-1].Tex := gfx.TexMan.Load(iz_dir_local_temp_gfx+TexName);
  Picture[PictureCount-1].Animation := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_ANIMATION',255);
  sId := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_sID',0);
  eId := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_eID',0);
  cId := Cfg_ReadInt(FileName, Picture[PictureCount-1].Name + '_cID',0);
  if Picture[PictureCount-1].Animation = 1 then begin
    Picture[PictureCount-1].Spr := gfx.SprMan.Add(gfx.TexMan.GetSize(Picture[PictureCount-1].Tex),Picture[PictureCount-1].Size);
    Picture[PictureCount-1].Ani := gfx.SprMan.Add_Anim(Picture[PictureCount-1].Tex,sID,eID,cID,delay)
  end
  else begin
    Picture[PictureCount-1].Spr := gfx.SprMan.Add(gfx.TexMan.GetSize(Picture[PictureCount-1].Tex),Picture[PictureCount-1].Frame_Size);
    Picture[PictureCount-1].Ani := cID;
  end;
end;

destructor zGUI.Destroy;
begin
  PictureCount := 0;
  Setlength(Picture,PictureCount);
end;

procedure zGUI.Update(State : zGUIState);
var i : word;
begin
  i := 0;
  while i < PictureCount do begin
    if ((Picture[i].State = State) or (Picture[i].State = GUI_ALL)) then Picture[i].Update;
    inc(i);
  end;
end;

procedure zGUI.Draw(State : zGUIState);
var i : int;
begin
  i := PictureCount-1;
  while i > -1 do begin
    if (((Picture[i].State = State) or (Picture[i].State = GUI_ALL)) and Picture[i].Visible) then Picture[i].Draw;
    dec(i);
  end;
end;

{ zPicture }

constructor zPicture.Create;
begin
  inherited;

end;

destructor zPicture.Destroy;
begin

  inherited;
end;

procedure zPicture.Draw;
var
  t_pos : izPosition_f;
begin
  if ((State <> GUI_MENU)) then begin
    t_pos[0] := pos[0]-Camera.pos[0];
    t_pos[1] := pos[1]-Camera.pos[1];
  end else t_pos := pos;
    gfx.TexMan.Bind(Tex);
    if Animation = 1 then gfx.SprMan.Draw_Anim_Scale_Alpha(Spr,Ani,t_pos,size,0) else
     gfx.SprMan.Draw_Alpha_Scale(Spr,Ani,t_pos,size,0);
end;

function zPicture.OnClick : bool;
var t_Cur,temp_r : izRect;
begin
  result := false;
  t_Cur.Left := gfx.Input.X-1;
  t_Cur.Right := gfx.Input.X+1;
  t_Cur.Top := gfx.Input.Y-1;
  t_Cur.Bottom := gfx.Input.Y+1;

  if gfx.Input.mKeys[0] then
  if iz_IntersectRect(temp_R,Rect,t_Cur) then begin
    result := true;
    gfx.Input.mKeys[0] := false;
  end;
end;

procedure zPicture.Update;
begin
  if name = 'CUR' then pos := Cur.Pos;
  if Animation = 1 then gfx.SprMan.Anim_Update(Spr,Ani,DeltaTime*10);
end;

end.
