///////////////////////////////////////////////////////////
////////    Загрузчик фалов Hud&Gui Fast Editor   /////////
////////  www.cdmlex.ru 2008                      /////////
////////  cdmlex (Трофимов Алексей)               /////////
////////  Упрощенная версия для игры quod         /////////
///////////////////////////////////////////////////////////

unit ghload;
interface
uses
  GLScene, SysUtils, Classes, GLHUDObjects,
  GLGui, GLWindows, GLObjects, GLCrossPlatform;

type
////// Объекты
  TGGForm=record
    O : TGLForm;
    P, F : byte;
    M : boolean;
  end;
  TGGPanel=record
    O : TGLPanel;
    P, F : byte;
    M : boolean;
  end;
  TGGButton=record
    O : TGLButton;
    P, F : byte;
    M : boolean;
  end;
  TGGCheckBox=record
    O : TGLCheckBox;
    P, F : byte;
    M : boolean;
  end;
  TGGEdit=record
    O : TGLEdit;
    P, F : byte;
    M : boolean;
  end;
  TGGLabel=record
    O : TGLLabel;
    P, F : byte;
    M : boolean;
  end;
  TGGCustomControl=record
    O : TGLCustomControl;
    P, F : byte;
    M : boolean;
    I : String;
  end;

var
  F : textfile;
  //// Объекты <
  GLForm: array[1..100] of TGGForm;
  GLPanel: array[1..100] of TGGPanel;
  GLButton: array[1..100] of TGGButton;
  GLCheckBox: array[1..100] of TGGCheckBox;
  GLEdit: array[1..100] of TGGEdit;
  GLLabel: array[1..100] of TGGLabel;
  GLCustomControl: array[1..100] of TGGCustomControl;
//// >
  ObjCaption : string;
  MofLayer, MofF, MofP, MofB, MofCh, MofE, MofL, MofCC : byte;
  UseLayer : byte=1;
  ParObj, GLGUIWork : TGLBaseSceneObject;
  Obj : TGLBaseGuiObject;
  GLGLayout : TGLGuiLayout;

  Layer : array[1..20] of TGLDummyCube;
  Width, Height : word;

  ParForm : boolean;
  ParFormN : byte;

  St, T : string;

function itisgui: boolean;
procedure glgLoad(FN: string);

/////////////////////////////////////////////////////
procedure GGLChange;
procedure GGLMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);

procedure GGLMouseDown(Sender: TObject; Button: TMouseButton;
                               Shift:TShiftState; X, Y: Integer);
procedure GGLMouseUp(Sender: TObject; Button: TMouseButton;
                               Shift:TShiftState; X, Y: Integer);
////////////////////////////////////////////////////

implementation

////////////////////////////////////////////////////
////// проверка GUI
function itisgui: boolean;
begin
  if (ParObj<>nil)
  and ((copy(ParObj.Name,0,3)='GFO')
   or (copy(ParObj.Name,0,3)='GPA')
   or (copy(ParObj.Name,0,3)='GBU')
   or (copy(ParObj.Name,0,3)='GCH')
   or (copy(ParObj.Name,0,3)='GED')
   or (copy(ParObj.Name,0,3)='GLA')
   or (copy(ParObj.Name,0,3)='GBM')
   ) then
   result:=true
   else result:=false;
end;
///////////////////////////////////////////////////
procedure addParent;
begin
  If ((ParObj<>nil) and (copy(ParObj.Name,0,3)='GFO'))
  then
    begin
      ParForm:=false;
      ParFormN:=strtoint(copy(ParObj.Name,5,3));
    end
  else
    begin
      ParObj:=Layer[UseLayer];
      ParForm:=true;
      ParFormN:=0;
    end;
end;

procedure PtoO(P : TGLBaseSceneObject; var O : TGLBaseGuiObject);
begin
  if P<>nil then
    begin
      if copy(P.Name,0,3)='GFO' then
          O:=GLForm[strtoint(copy(P.Name,5,3))].O;
      if copy(P.Name,0,3)='GPA' then
          O:=GLPanel[strtoint(copy(P.Name,5,3))].O;
      if copy(P.Name,0,3)='GBU' then
          O:=GLButton[strtoint(copy(P.Name,5,3))].O;
      if copy(P.Name,0,3)='GCH' then
          O:=GLCheckBox[strtoint(copy(P.Name,5,3))].O;
      if copy(P.Name,0,3)='GED' then
          O:=GLEdit[strtoint(copy(P.Name,5,3))].O;
      if copy(P.Name,0,3)='GLA' then
          O:=GLLabel[strtoint(copy(P.Name,5,3))].O;
      if copy(P.Name,0,3)='GBM' then
          O:=GLCustomControl[strtoint(copy(P.Name,5,3))].O;
    end;
end;

////////////////////////////////////////////////////
////////////////// Очистка
procedure allclear;
var
  i : byte;
begin
  for i:=0 to MofLayer do
  if layer[i]<>nil then freeandnil(layer[i]);
  MofLayer:=0;
  MofF:=0;
  MofP:=0;
  MofB:=0;
  MofCh:=0;
  MofE:=0;
  MofL:=0;
  MofCC:=0;
  UseLayer:=1;
  ParObj :=nil;
  Obj :=nil;
end;
/////////////////////////////////////////////////////
///// Создание объектов
procedure CrGLForm;
begin
  MofF:=MofF+1;
  GLForm[MofF].O:=TGLForm(ParObj.AddNewChild(TGLForm));
  GLForm[MofF].O.Name:='GFO'+inttostr(1000+MofF);
  GLForm[MofF].O.Caption:=ObjCaption;
  GLForm[MofF].O.GuiLayout:=GLGLayout;
  GLForm[MofF].O.GuiLayoutName:=St;
  GLForm[MofF].M:=True;
  GLForm[MofF].P:=UseLayer;
  GLForm[MofF].F:=ParFormN;
  Obj:=GLForm[MofF].O;
end;

procedure CrGLPanel;
begin
  MofP:=MofP+1;
  GLPanel[MofP].O:=TGLPanel(ParObj.AddNewChild(TGLPanel));
  GLPanel[MofP].O.Name:='GPA'+inttostr(1000+MofP);
  GLPanel[MofP].O.GuiLayout:=GLGLayout;
  GLPanel[MofP].O.GuiLayoutName:=St;
  GLPanel[MofP].M:=ParForm;
  GLPanel[MofP].P:=UseLayer;
  GLPanel[MofP].F:=ParFormN;
  Obj:=GLPanel[MofP].O;
end;

procedure CrGLButton;
begin
  MofB:=MofB+1;
  GLButton[MofB].O:=TGLButton(ParObj.AddNewChild(TGLButton));
  GLButton[MofB].O.Name:='GBU'+inttostr(1000+MofB);
  GLButton[MofB].O.Caption:=ObjCaption;
  GLButton[MofB].O.GuiLayout:=GLGLayout;
  GLButton[MofB].O.GuiLayoutName:=St+'_up';
  GLButton[MofB].O.GuiLayoutNamePressed:=St+'_down';
  GLButton[MofB].M:=ParForm;
  GLButton[MofB].P:=UseLayer;
  GLButton[MofB].F:=ParFormN;
  Obj:=GLButton[MofB].O;
end;

procedure CrGLCheckBox;
begin
  MofCH:=MofCH+1;
  GLCheckBox[MofCH].O:=TGLCheckBox(ParObj.AddNewChild(TGLCheckBox));
  GLCheckBox[MofCH].O.Name:='GCH'+inttostr(1000+MofCH);
  GLCheckBox[MofCH].O.GuiLayout:=GLGLayout;
  GLCheckBox[MofCH].O.GuiLayoutName:=St+'_checked';
  GLCheckBox[MofCH].O.GuiLayoutNameChecked:=St+'_unchecked';
  GLCheckBox[MofCH].M:=ParForm;
  GLCheckBox[MofCH].P:=UseLayer;
  if ObjCaption='TRUE' then
  GLCheckBox[MofCH].O.Checked:=true
  else
  GLCheckBox[MofCH].O.Checked:=false;
  GLCheckBox[MofCH].F:=ParFormN;
  Obj:=GLCheckBox[MofCH].O;
end;

procedure CrGLEdit;
begin
  MofE:=MofE+1;
  GLEdit[MofE].O:=TGLEdit(ParObj.AddNewChild(TGLEdit));
  GLEdit[MofE].O.Name:='GED'+inttostr(1000+MofE);
  GLEdit[MofE].O.Caption:=ObjCaption;
  GLEdit[MofE].O.GuiLayout:=GLGLayout;
  GLEdit[MofE].O.GuiLayoutName:=St;
  GLEdit[MofE].M:=ParForm;
  GLEdit[MofE].P:=UseLayer;
  GLEdit[MofE].F:=ParFormN;
  Obj:=GLEdit[MofE].O;
end;

procedure CrGLLabel;
begin
  MofL:=MofL+1;
  GLLabel[MofL].O:=TGLLabel(ParObj.AddNewChild(TGLLabel));
  GLLabel[MofL].O.Name:='GLA'+inttostr(1000+MofL);
  GLLabel[MofL].O.Caption:=ObjCaption;
  GLLabel[MofL].O.GuiLayout:=GLGLayout;
  GLLabel[MofL].O.GuiLayoutName:=St;
  GLLabel[MofL].M:=ParForm;
  GLLabel[MofL].P:=UseLayer;
  GLLabel[MofL].F:=ParFormN;
  Obj:=GLLabel[MofL].O;
end;

Procedure CrBitMap;
begin
  MofCC:=MofCC+1;
  GLCustomControl[MofCC].O:=TGLCustomControl(ParObj.AddNewChild(TGLCustomControl));
  GLCustomControl[MofCC].O.Name:='GBM'+inttostr(1000+MofCC);
  GLCustomControl[MofCC].O.GuiLayout:=GLGLayout;
  GLCustomControl[MofCC].O.GuiLayoutName:=St;
  GLCustomControl[MofCC].O.Caption:=ObjCaption;
  GLCustomControl[MofCC].M:=ParForm;
  GLCustomControl[MofCC].P:=UseLayer;
  GLCustomControl[MofCC].F:=ParFormN;
  Obj:=GLCustomControl[MofCC].O;
end;
/// Общее создание
procedure CreatObj;
begin
  addParent;
  Obj:=nil;
  if T='Form' then CrGLForm;
  if T='Panel' then CrGLPanel;
  if T='Button' then CrGLButton;
  if T='CheckBox' then CrGLCheckBox;
  if T='Edit' then CrGLEdit;
  if T='Label' then CrGLLabel;
  if T='BitMap' then CrBitMap;
end;
////
////////////////////////////////////////////////////

procedure layercreat(n : byte);
var
  i, u : byte;
begin
  for i:=1 to n do
    begin
      Readln(F, U);
      Layer[u]:=TGLDummyCube(GLGUIWork.AddNewChild(TGLDummyCube));
      Layer[u].Name:='GLGlayer'+inttostr(u);
    end;
end;

procedure loadobj(O : TGLBaseGuiObject);
var
 L : real;
begin
  readln(F, L );
  if L>=0 then O.Left:=L else O.Left:=Width + L;
  readln(F, L );
  if L>=0 then O.Top:=L else O.Top:=Height + L;
  readln(F, L );
  O.Width:=L;
  readln(F, L );
  O.Height:=L;
end;

procedure crload;
var
  S : string;
  i, n, l : byte;
begin
  readln(F, T);
  T:=copy(T,2,length(T)-2);
  readln(F, N);
  for i:=1 to n do
    begin
      readln(F, UseLayer);
      readln(F, l);
      if l>0 then ParObj:=GLForm[l].O
      else ParObj:=Layer[UseLayer];
      readln(F, S);
      if S='TRUE' then ParForm:=true;
      readln(F, St);
      readln(F, ObjCaption);
      CreatObj;
      loadobj(Obj);
    end;
end;

procedure layerload;
var
  n : byte;
begin
  readln(F);
  readln(F, N);
  layercreat(n);
  for n:=1 to 7 do
  crload;
end;
/////////////////////////////////////////////////////////
////// загрузка
procedure glgLoad(FN: string);
var
  S : string;
begin
  if GLGUIWork<>nil then
    begin
      assign(F,FN);
      reset(F);
      readln(F, S);
      if S= '__cdmlex file__' then
      readln(F, S);
      if S= '__h&g fast editor__' then
      layerload;
      closefile(F);
    end;
end;

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

procedure GGLMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  i : byte;
begin
  for i:=1 to MofF do
  if (GLForm[i].O<>nil) and (Layer[GlForm[i].P]<>nil)
  and (Layer[GlForm[i].P].Visible) and (GlForm[i].M)
  then GLForm[i].O.MouseMove(Sender,Shift,X,Y);

  for i:=1 to MofB do
  if (GLButton[i].O<>nil) and (Layer[GlButton[i].P]<>nil)
  and (Layer[GlButton[i].P].Visible) and (GlButton[i].M)
  then GLButton[i].O.MouseMove(Sender,Shift,X,Y);

  for i:=1 to MofCH do
  if (GLCheckBox[i].O<>nil) and (Layer[GlCheckBox[i].P]<>nil)
  and (Layer[GlCheckBox[i].P].Visible) and (GlCheckBox[i].M)
  then GLCheckBox[i].O.MouseMove(Sender,Shift,X,Y);

  for i:=1 to MofL do
  if (GLLabel[i].O<>nil) and (Layer[GlLabel[i].P]<>nil)
  and (Layer[GlLabel[i].P].Visible) and (GlLabel[i].M)
  then GLLabel[i].O.MouseMove(Sender,Shift,X,Y);

  for i:=1 to MofP do
  if (GLPanel[i].O<>nil) and (Layer[GlPanel[i].P]<>nil)
  and (Layer[GlLabel[i].P].Visible) and (GlPanel[i].M)
  then GLPanel[i].O.MouseMove(Sender,Shift,X,Y);

  for i:=1 to MofE do
  if (GLEdit[i].O<>nil) and (Layer[GlEdit[i].P]<>nil)
  and (Layer[GlLabel[i].P].Visible) and (GlEdit[i].M)
  then GLEdit[i].O.MouseMove(Sender,Shift,X,Y);
end;

procedure GGLMouseDown(Sender: TObject; Button: TMouseButton;
                               Shift:TShiftState; X, Y: Integer);
var
  i : byte;
begin
  for i:=1 to MofF do
  if (GLForm[i].O<>nil) and (Layer[GlForm[i].P]<>nil)
  and (Layer[GlForm[i].P].Visible) and (GlForm[i].M)
  then GLForm[i].O.MouseDown(Sender,TGLMouseButton(Button),Shift,X,Y);

  for i:=1 to MofB do
  if (GLButton[i].O<>nil) and (Layer[GlButton[i].P]<>nil)
  and (Layer[GlButton[i].P].Visible) and (GlButton[i].M)
  then GLButton[i].O.MouseDown(Sender,TGLMouseButton(Button),Shift,X,Y);

  for i:=1 to MofCH do
  if (GLCheckBox[i].O<>nil) and (Layer[GlCheckBox[i].P]<>nil)
  and (Layer[GlCheckBox[i].P].Visible)  and (GlCheckBox[i].M)
  then GLCheckBox[i].O.MouseDown(Sender,TGLMouseButton(Button),Shift,X,Y);

  for i:=1 to MofL do
  if (GLLabel[i].O<>nil) and (Layer[GlLabel[i].P]<>nil)
  and (Layer[GlLabel[i].P].Visible) and (GlLabel[i].M)
  then GLLabel[i].O.MouseDown(Sender,TGLMouseButton(Button),Shift,X,Y);

  for i:=1 to MofP do
  if (GLPanel[i].O<>nil) and (Layer[GlPanel[i].P]<>nil)
  and (Layer[GlLabel[i].P].Visible) and (GlPanel[i].M)
  then GLPanel[i].O.MouseDown(Sender,TGLMouseButton(Button),Shift,X,Y);

  for i:=1 to MofE do
  if (GLEdit[i].O<>nil) and (Layer[GlEdit[i].P]<>nil)
  and (Layer[GlLabel[i].P].Visible) and (GlEdit[i].M)
  then GLEdit[i].O.MouseDown(Sender,TGLMouseButton(Button),Shift,X,Y);
end;

procedure GGLMouseUp(Sender: TObject; Button: TMouseButton;
                               Shift:TShiftState; X, Y: Integer);
var
  i : byte;
begin
  for i:=1 to MofF do
  if (GLForm[i].O<>nil) and (Layer[GlForm[i].P]<>nil)
  and (Layer[GlForm[i].P].Visible) and (GlForm[i].M)
  then GLForm[i].O.MouseUp(Sender,TGLMouseButton(Button),Shift,X,Y);

  for i:=1 to MofB do
  if (GLButton[i].O<>nil) and (Layer[GlButton[i].P]<>nil)
  and (Layer[GlButton[i].P].Visible) and (GlButton[i].M)
  then GLButton[i].O.MouseUp(Sender,TGLMouseButton(Button),Shift,X,Y);

  for i:=1 to MofCH do
  if (GLCheckBox[i].O<>nil) and (Layer[GlCheckBox[i].P]<>nil)
  and (Layer[GlCheckBox[i].P].Visible) and (GlCheckBox[i].M)
  then GLCheckBox[i].O.MouseUp(Sender,TGLMouseButton(Button),Shift,X,Y);

  for i:=1 to MofL do
  if (GLLabel[i].O<>nil) and (Layer[GlLabel[i].P]<>nil)
  and (Layer[GlLabel[i].P].Visible) and (GlLabel[i].M)
  then GLLabel[i].O.MouseUp(Sender,TGLMouseButton(Button),Shift,X,Y);

  for i:=1 to MofP do
  if (GLPanel[i].O<>nil) and (Layer[GlPanel[i].P]<>nil)
  and (Layer[GlLabel[i].P].Visible) and (GlPanel[i].M)
  then GLPanel[i].O.MouseUp(Sender,TGLMouseButton(Button),Shift,X,Y);

  for i:=1 to MofE do
  if (GLEdit[i].O<>nil) and (Layer[GlEdit[i].P]<>nil)
  and (Layer[GlLabel[i].P].Visible) and (GlEdit[i].M)
  then GLEdit[i].O.MouseUp(Sender,TGLMouseButton(Button),Shift,X,Y);
end;

procedure GGLChange;
var
  i : byte;
begin
  for i:=1 to MofF do
  if (GLForm[i].O<>nil) and (Layer[GlForm[i].P]<>nil)
  and (Layer[GlForm[i].P].Visible) and (GlForm[i].M)
  then GLForm[i].O.DoChanges;

  for i:=1 to MofB do
  if (GLButton[i].O<>nil) and (Layer[GlButton[i].P]<>nil)
  and (Layer[GlButton[i].P].Visible) and (GlButton[i].M)
  then GLButton[i].O.DoChanges;

  for i:=1 to MofCH do
  if (GLCheckBox[i].O<>nil) and (Layer[GlCheckBox[i].P]<>nil)
  and (Layer[GlCheckBox[i].P].Visible) and (GlCheckBox[i].M)
  then GLCheckBox[i].O.DoChanges;

  for i:=1 to MofL do
  if (GLLabel[i].O<>nil) and (Layer[GlLabel[i].P]<>nil)
  and (Layer[GlLabel[i].P].Visible)  and (GlLabel[i].M)
  then GLLabel[i].O.DoChanges;

  for i:=1 to MofP do
  if (GLPanel[i].O<>nil) and (Layer[GlPanel[i].P]<>nil)
  and (Layer[GlLabel[i].P].Visible) and (GlPanel[i].M)
  then GLPanel[i].O.DoChanges;

  for i:=1 to MofE do
  if (GLEdit[i].O<>nil) and (Layer[GlEdit[i].P]<>nil)
  and (Layer[GlLabel[i].P].Visible) and (GlEdit[i].M)
  then GLEdit[i].O.DoChanges;
end;       

end.
