unit EntityUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, SynEdit, SynMemo,alkatraz,DQE2_Types;

type
  TForm_Entities = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    SynMemo1: TSynMemo;
    ListBox1: TListBox;
    ListView1: TListView;
    Button1: TButton;
    Button2: TButton;
    Button4: TButton;
    Button3: TButton;
    procedure FormActivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Localize;
    procedure UpdateList;
    procedure UpdateForm;
    procedure UpdateObject;
    procedure ShowMsgs;
    procedure OnScriptMsg(Sender:DQE2_Scriptable;var Ignore:boolean);
    procedure ListView1Edited(Sender: TObject; Item: TListItem; var S: string);
    procedure ListView1Change(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_Entities: TForm_Entities;
  CurType:DQE2_EntityType=nil;
  can:boolean=true;

implementation

uses MainUnit, ObjectUnit;

{$R *.dfm}

var lng:array of string;

procedure TForm_Entities.Button1Click(Sender: TObject);
var
  s:string;
begin
  if InputQuery(lng[7],lng[8],s) then
  begin
    can:=false;
    UpdateObject;
    CurType:=DQE2_EntityType.Create(world.EntityTypes);
    CurType.Name:=s;
    //
    Form_Main_DQE.LoadDefaultTemplate(3,s);
    CurType.Text:=s;
    CurType.OnError:=OnScriptMsg;
    //
    ListView1.AddItem(CurType.Name,CurType);
    ListView1.Selected:=ListView1.Items.Item[ListView1.Items.count];
    UpdateForm;
    can:=true;
  end;
end;

procedure TForm_Entities.Button2Click(Sender: TObject);
var
  a:integer;
begin
  if CurType<>nil then
    if MessageDlgRus(lng[9],lng[11]+#13#10+CurType.Name+#13#10#13#10+lng[10],MB_OKCANCEL or MB_ICONWARNING)=mrok then
    begin
      can:=false;
      World.Stop;
      PickList.Clear;
      for a:=0 to World.Objects.Count-1 do
        if DQE2_Dummy(World.Objects[a]) is DQE2_Entity then
          if DQE2_Entity(World.Objects[a]).EntityType=CurType then
            DQE2_Entity(World.Objects[a]).DowngradeToObject;
      CurType.Free;CurType:=nil;
      ListView1.Selected.Data:=nil;
      ListView1.Selected.Delete;
      ListView1.Selected:=nil;
      UpdateForm;
      can:=true;
      Form_Objects.GenerateProperties;
    end;
end;

procedure TForm_Entities.Button3Click(Sender: TObject);
begin
  UpdateObject;close;
end;

procedure TForm_Entities.Button4Click(Sender: TObject);
var
  a:integer;
begin
  if CurType<>nil then
  begin
    if World.State<>wsStop then
      for a:=0 to world.CurrentZone.Objects.Count-1 do
        if DQE2_Dummy(world.CurrentZone.Objects[a]) is DQE2_Entity then
          if DQE2_Entity(world.CurrentZone.Objects[a]).EntityType=CurType then
            if MessageDlgRus(lng[20],lng[19],MB_OKCANCEL or MB_ICONWARNING)<>mrok then
              exit
            else
            begin
              World.Stop;
              break;
            end;
    CurType.Text:=SynMemo1.Text;
    CurType.Compile;
    if not CurType.LastError.IsSyntaxError then
      begin ListBox1.Clear;ListBox1.Items.Add(lng[12]);end;
  end;
end;

procedure TForm_Entities.FormActivate(Sender: TObject);
begin
  Form_Main_DQE.synpopup.PopupComponent:=SynMemo1;
end;

procedure TForm_Entities.FormCreate(Sender: TObject);
begin
  SynMemo1.Highlighter:=Form_Main_DQE.SynPas;
  SynMemo1.PopupMenu:=Form_Main_DQE.SynPopup;
  SynMemo1.OnGutterClick:=Form_Main_DQE.OnSynGutterClick;
  SetLength(lng,21);
end;

procedure TForm_Entities.FormShow(Sender: TObject);
begin
  UpdateForm;
end;

procedure TForm_Entities.ListBox1Click(Sender: TObject);
begin
  if ListBox1.ItemIndex>=0 then
    Form_Main_DQE.ShowErrorPlace(CurType,SynMemo1);
end;

procedure TForm_Entities.ListView1Change(Sender: TObject; Item: TListItem;
  Change: TItemChange);
begin
  if Application.Terminated then exit;
  if not can then exit;
  if Item.Data=CurType then exit;
  UpdateObject;
  UpdateForm;
end;

procedure TForm_Entities.ListView1Edited(Sender: TObject; Item: TListItem;
  var S: string);
begin
  CurType.Name:=s;s:=CurType.Name;
end;

procedure TForm_Entities.Localize;
begin
  LoadStringsFromIni(MainUnit.lang_ini,Name,lng);
  Caption:=lng[0];
  GroupBox1.Caption:=lng[1];
  Button1.Caption:=lng[2];
  Button2.Caption:=lng[3];
  GroupBox2.Caption:=lng[4];
  Button3.Caption:=lng[5];
  Button4.Caption:=lng[6];
end;

procedure TForm_Entities.OnScriptMsg(Sender: DQE2_Scriptable;
  var Ignore: boolean);
begin
  if not Ignore then
  begin
    Show;
    ListView1.Selected:=ListView1.Items.Item[Sender.Index];
    UpdateForm;
    ShowMsgs;
    case Sender.LastError.MsgID of
      100:ListBox1.Items.Add(lng[13]);
      1:ListBox1.Items.Add(lng[14]);
      2:ListBox1.Items.Add(lng[15]);
      3:ListBox1.Items.Add(lng[16]);
      4:ListBox1.Items.Add(lng[17]);
      else
        ListBox1.Items.Add(lng[18]);
    end;
  end;
end;

procedure TForm_Entities.ShowMsgs;
begin
  if CurType<>nil then
    Form_Main_DQE.ShowErrors(CurType,SynMemo1,ListBox1);
end;

procedure TForm_Entities.UpdateForm;
begin
  if ListView1.Selected<>nil then
  begin
    CurType:=DQE2_EntityType(ListView1.Selected.Data);
    SynMemo1.text:=CurType.Text;
    Form_Main_DQE.SynUpdateGutter(SynMemo1,CurType);
    ListBox1.Clear;
    Button4.Enabled:=true;
    GroupBox2.Visible:=true;
  end
  else
  begin
    Button4.Enabled:=false;
    CurType:=nil;
    GroupBox2.Visible:=false;
    SynMemo1.Clear;
  end;
  ListBox1.Clear;
end;

procedure TForm_Entities.UpdateList;
var
  a:integer;
  e:DQE2_EntityType;
begin
  CurType:=nil;
  ListView1.Clear;
  for a:=0 to world.EntityTypes.Count-1 do
  begin
    e:=DQE2_EntityType(world.EntityTypes[a]);
    ListView1.AddItem(e.name,e);
    e.OnError:=OnScriptMsg;
  end;
  ListView1.Selected:=nil;UpdateForm;
end;

procedure TForm_Entities.UpdateObject;
begin
  if CurType<>nil then
  begin
    CurType.Text:=SynMemo1.Text;
    Form_Main_DQE.SynUpdateScript(SynMemo1,CurType);
  end;
end;

end.
