{*************************************************************************}
{                                                                         }
{ ScriptUnit.pas - Script Editor window                                   }
{                                                                         }
{ This file is a part of the project:                                     }
{   Dizzy Quest Editor - the quest editor for Dizzy-like games            }
{                                                                         }
{ © Parfenov Alexey Andreevich, 2007                                      }
{                                                                         }
{ e-mail: AlkatrazStudio@yandex.ru                                        }
{                                                                         }
{ This program is free software; you can redistribute it and/or modify    }
{ it under the terms of the GNU General Public License as published by    }
{ the Free Software Foundation; either version 2 of the License, or       }
{ (at your option) any later version.                                     }
{                                                                         }
{ This program is distributed in the hope that it will be useful,         }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of          }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            }
{ GNU General Public License for more details.                            }
{                                                                         }
{ You may read GNU General Public License at:                             }
{   http://www.gnu.org/copyleft/gpl.html                                  }
{                                                                         }
{*************************************************************************}
unit ScriptUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, SynEditHighlighter, SynHighlighterPas, SynEdit,
  SynMemo, StdCtrls,dqe2_types,alkatraz, ComCtrls,mmsystem,SynCompletionProposal,
  SynEditKeyCmds;

type
  TForm_Scripts = class(TForm)
    Button1: TButton;
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    SynMemo1: TSynMemo;
    ListBox2: TListBox;
    GroupBox2: TGroupBox;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    ListView1: TListView;
    Splitter1: TSplitter;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    procedure ListView1Edited(Sender: TObject; Item: TListItem; var S: string);
    procedure ListBox2Click(Sender: TObject);
    procedure ShowMsgs;
    procedure OnScriptMsg(Sender:DQE2_Scriptable;var Ignore:boolean);
    procedure FormCreate(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure ListView1Change(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure UpdateList;
    procedure UpdateForm;
    procedure UpdateObject;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Localize;
    procedure FormShow(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure SynMemo1PlaceBookmark(Sender: TObject; var Mark: TSynEditMark);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_Scripts: TForm_Scripts;
  CurScript:DQE2_Script=nil;
  can:boolean=true;
  completition:TSynCompletionProposal;

implementation

uses MainUnit, GameUnit, ZoneUnit;

{$R *.dfm}

var lng:array of string;

procedure TForm_Scripts.Button1Click(Sender: TObject);
begin
  UpdateObject;close;
end;

procedure TForm_Scripts.Button2Click(Sender: TObject);
var
  s:string;
begin
  if InputQuery(lng[11],lng[12],s) then
  begin
    can:=false;
    UpdateObject;
    CurScript:=DQE2_Script.Create(world.Scripts);
    CurScript.Name:=s;
    //
    Form_Main_DQE.LoadDefaultTemplate(1,s);
    CurScript.Text:=s;
    CurScript.OnError:=OnScriptMsg;
    //
    ListView1.AddItem(CurScript.Name,CurScript);
    ListView1.Selected:=ListView1.Items.Item[ListView1.Items.count];
    UpdateForm;
    can:=true;
  end;
end;

procedure TForm_Scripts.UpdateForm;
begin
  if ListView1.Selected<>nil then
  begin
    CurScript:=DQE2_Script(ListView1.Selected.Data);
    GroupBox1.Enabled:=true;
    SynMemo1.text:=CurScript.Text;
    Form_Main_DQE.SynUpdateGutter(SynMemo1,CurScript);
    ListBox2.Clear;
    Button3.Enabled:=true;
    Button4.Enabled:=true;
    Button5.Enabled:=true;
  end
  else
  begin
    Button3.Enabled:=false;
    Button4.Enabled:=false;
    Button5.Enabled:=false;
    CurScript:=nil;
    GroupBox1.Enabled:=false;
    SynMemo1.Clear;
    ListBox2.Clear;
  end;
end;

procedure TForm_Scripts.UpdateList;
var
  a:integer;
  d:dqe2_Script;
begin
  CurScript:=nil;
  ListView1.Clear;
  for a:=0 to world.Scripts.Count-1 do
  begin
    d:=dqe2_Script(world.Scripts[a]);
    ListView1.AddItem(d.name,d);
    d.OnError:=OnScriptMsg;
  end;
  ListView1.Selected:=nil;UpdateForm;
end;

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

procedure TForm_Scripts.Button3Click(Sender: TObject);
begin
  if CurScript<>nil then
    if MessageDlgRus(lng[9],lng[10]+#13#10+CurScript.Name,MB_OKCANCEL or MB_ICONWARNING)=mrok then
    begin
      can:=false;
      World.Stop;
      CurScript.Free;CurScript:=nil;
      ListView1.Selected.Data:=nil;
      ListView1.Selected.Delete;
      ListView1.Selected:=nil;
      UpdateForm;
      can:=true;
      Form_Zones.UpdateForm;
    end;
end;

procedure TForm_Scripts.Button4Click(Sender: TObject);
var
  a:integer;
begin
  if CurScript<>nil then
  begin
    if World.State<>wsStop then
      for a:=0 to World.RunningScripts.Count-1 do
        if World.RunningScripts[a]=CurScript then
          if MessageDlgRus(lng[22],lng[21],MB_OKCANCEL or MB_ICONWARNING)<>mrok then
            exit
          else
          begin
            World.Stop;
            break;
          end;
    CurScript.Text:=SynMemo1.Text;
    CurScript.Compile;
    if not CurScript.LastError.IsSyntaxError then
      begin ListBox2.Clear;ListBox2.Items.Add(lng[13]);end;
  end;
  if World.State<>wsStop then World.Stop;
end;

procedure TForm_Scripts.Button5Click(Sender: TObject);
begin
  if(CurScript<>nil)then
  begin
    Button4.Click;
    CurScript.Run;
  end;
end;

procedure TForm_Scripts.Button6Click(Sender: TObject);
begin
  if CurScript<>nil then CurScript.Step;
end;

procedure TForm_Scripts.Button7Click(Sender: TObject);
begin
  if CurScript<>nil then CurScript.OnUse(nil);
end;

procedure TForm_Scripts.Button8Click(Sender: TObject);
begin
  if CurScript<>nil then CurScript.Stop;
end;

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

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

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

procedure TForm_Scripts.ListBox2Click(Sender: TObject);
begin
  if ListBox2.ItemIndex>=0 then
    Form_Main_DQE.ShowErrorPlace(CurScript,SynMemo1);
end;

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

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

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

procedure TForm_Scripts.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:ListBox2.Items.Add(lng[14]);
      1:ListBox2.Items.Add(lng[15]);
      2:ListBox2.Items.Add(lng[16]);
      3:ListBox2.Items.Add(lng[19]);
      else
        ListBox2.Items.Add(lng[20]);
    end;
  end;
end;

procedure TForm_Scripts.ShowMsgs;
begin
  if CurScript<>nil then
    Form_Main_DQE.ShowErrors(CurScript,SynMemo1,ListBox2);
end;

procedure TForm_Scripts.SynMemo1PlaceBookmark(Sender: TObject;
  var Mark: TSynEditMark);
begin
  //
end;

end.
