{*************************************************************************}
{                                                                         }
{ DialogUnit.pas - Dialogue 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 DialogUnit;

interface

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

type
  TForm_Dialogs = class(TForm)
    GroupBox1: TGroupBox;
    Button1: TButton;
    Button2: TButton;
    GroupBox2: TGroupBox;
    Button3: TButton;
    Button9: TButton;
    SynMemo1: TSynMemo;
    ListBox4: TListBox;
    Label2: TLabel;
    ListView1: TListView;
    GroupBox3: TGroupBox;
    ListBox5: TListBox;
    Button6: TButton;
    Button8: TButton;
    Button7: TButton;
    GroupBox4: TGroupBox;
    Button4: TButton;
    Button10: TButton;
    Button5: TButton;
    ListBox1: TListBox;
    Label1: TLabel;
    Edit1: TEdit;
    Button11: TButton;
    procedure FormCreate(Sender: TObject);
    procedure OnDialogMsg(Sender:DQE2_Scriptable;var Ignore:boolean);
    procedure ListBox4Click(Sender: TObject);
    procedure ShowMsgs;
    procedure Button9Click(Sender: TObject);
    procedure ListView1Edited(Sender: TObject; Item: TListItem; var S: string);
    procedure UpdateList;
    procedure UpdateObject;
    procedure UpdateForm;
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ListView1Change(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure Button4Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure OnScriptStart(Sender:DQE2_Scriptable);
    procedure OnScriptStop(Sender:DQE2_Scriptable);
    procedure Localize;
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_Dialogs: TForm_Dialogs;
  CurDialog:DQE2_Dialog=nil;
  can:boolean=true;

implementation

uses EditPhraseUnit, MainUnit, EditAnswerUnit;

{$R *.dfm}

var lng:array of string;

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

procedure TForm_Dialogs.Button11Click(Sender: TObject);
begin
  if world.CurrentDialog<>nil then
    world.CurrentDialog.Stop
  else
    if CurDialog<>nil then
    begin
      Button9.Click;
      CurDialog.Run;
    end;
end;

procedure TForm_Dialogs.Button1Click(Sender: TObject);
var
  s:string;
begin
  s:='';
  if InputQuery(lng[13],lng[14],s) then
  begin
    can:=false;
    UpdateObject;
    CurDialog:=DQE2_Dialog.Create(world.Dialogs);
    CurDialog.Name:=s;
    CurDialog.OnStart:=OnScriptStart;
    CurDialog.OnStop:=OnScriptStop;
    CurDialog.PersonName:=CurDialog.Name;
    ListView1.AddItem(CurDialog.Name,CurDialog);
    Form_Main_DQE.LoadDefaultTemplate(2,s);
    CurDialog.Text:=s;
    CurDialog.OnError:=OnDialogMsg;
    UpdateForm;
    can:=true;
  end;
end;

procedure TForm_Dialogs.UpdateForm;
var
  a:integer;
begin
  if ListView1.Selected=nil then
  begin
    GroupBox2.Visible:=false;
    CurDialog:=nil;
    Button2.Enabled:=false;
    if(World.CurrentDialog=nil)then
      Button11.Enabled:=false;
    Button9.Enabled:=false;
  end
  else
  begin
    CurDialog:=DQE2_Dialog(ListView1.Selected.Data);
    Edit1.Text:=CurDialog.PersonName;
    ListBox1.Clear;ListBox5.Clear;GroupBox2.Visible:=false;
    for a:=0 to CurDialog.Questions.Count-1 do
      ListBox1.AddItem(inttostr(a)+'. '+CurDialog.Questions[a],nil);
    for a:=0 to CurDialog.AnswersCount-1 do
      ListBox5.AddItem(inttostr(a)+'. '+CurDialog.AnswersShort[a],nil);
    SynMemo1.Text:=CurDialog.Text;
    Form_Main_DQE.SynUpdateGutter(SynMemo1,CurDialog);
    GroupBox2.Visible:=true;
    if(World.CurrentDialog<>nil)then
      Button11.Caption:=lng[11]
    else
      Button11.Caption:=lng[10];
    Button2.Enabled:=true;
    Button11.Enabled:=true;
    Button9.Enabled:=true;
  end;
end;

procedure TForm_Dialogs.UpdateList;
var
  a:integer;
  d:dqe2_dialog;
begin
  CurDialog:=nil;
  ListView1.Clear;
  for a:=0 to world.Dialogs.Count-1 do
  begin
    d:=dqe2_dialog(world.Dialogs[a]);
    ListView1.AddItem(d.name,d);
    d.OnError:=OnDialogMsg;
    d.OnStart:=OnScriptStart;
    d.OnStop:=OnScriptStop;
  end;
  ListView1.Selected:=nil;UpdateForm;
end;

procedure TForm_Dialogs.UpdateObject;
begin
  if CurDialog=nil then exit;
  CurDialog.PersonName:=Edit1.Text;
  CurDialog.Text:=SynMemo1.Text;
  Form_Main_DQE.SynUpdateScript(SynMemo1,CurDialog);
end;

procedure TForm_Dialogs.Button2Click(Sender: TObject);
begin
  if CurDialog<>nil then
    if MessageDlgRus(lng[15],lng[16]+#13#10+CurDialog.Name,MB_ICONEXCLAMATION or MB_OKCANCEL)=mrok then
    begin
      can:=false;
      World.Stop;
      CurDialog.Free;CurDialog:=nil;
      ListView1.Selected.Data:=nil;
      ListView1.Selected.Delete;
      ListView1.Selected:=nil;
      UpdateForm;
      can:=true;
    end;
end;

procedure TForm_Dialogs.ListBox4Click(Sender: TObject);
begin
  if ListBox4.ItemIndex>=0 then
    Form_Main_DQE.ShowErrorPlace(CurDialog,SynMemo1);
end;

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

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

procedure TForm_Dialogs.Localize;
begin
  LoadStringsFromIni(MainUnit.lang_ini,Name,lng);
  Caption:=lng[0];
  GroupBox1.Caption:=lng[1];
  GroupBox2.Caption:=lng[2];
  Label1.Caption:=lng[3];
  GroupBox4.Caption:=lng[4];
  GroupBox3.Caption:=lng[5];
  Label2.Caption:=lng[6];
  Button4.Caption:=lng[7];Button10.Caption:=lng[8];Button5.Caption:=lng[9];
  Button6.Caption:=lng[7];Button8.Caption:=lng[8];Button7.Caption:=lng[9];
  Button1.Caption:=lng[7];Button2.Caption:=lng[9];
  Button11.Caption:=lng[10];
  Button9.Caption:=lng[12];
end;

procedure TForm_Dialogs.OnDialogMsg(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:ListBox4.Items.Add(lng[18]);
    2:ListBox4.Items.Add(lng[19]);
    1:ListBox4.Items.Add(lng[20]);
    4:ListBox4.Items.Add(Format(lng[21],[world.CurrentDialog.CurrentQuestion]));
    else
      ListBox4.Items.Add(lng[22]);
    end;
  end;
end;

procedure TForm_Dialogs.OnScriptStart(Sender: DQE2_Scriptable);
begin
  if Visible then Button11.Caption:=lng[11];
end;

procedure TForm_Dialogs.OnScriptStop(Sender: DQE2_Scriptable);
begin
  if Visible then Button11.Caption:=lng[10];
end;

procedure TForm_Dialogs.ShowMsgs;
begin
  if CurDialog<>nil then
    Form_Main_DQE.ShowErrors(CurDialog,SynMemo1,ListBox4);
end;

procedure TForm_Dialogs.Button4Click(Sender: TObject);
begin
  Form_EditPhrase.Caption:=lng[23];
  Form_EditPhrase.Memo1.Text:='';
  Form_EditPhrase.ShowModal;
  if Form_EditPhrase.ok then
  begin
    CurDialog.Questions.Add(Form_EditPhrase.Memo1.Text);
    ListBox1.AddItem(inttostr(ListBox1.Count)+'. '+Form_EditPhrase.Memo1.Text,nil);
  end;
end;

procedure TForm_Dialogs.Button10Click(Sender: TObject);
begin
  if ListBox1.ItemIndex=-1 then exit;
  Form_EditPhrase.Caption:=lng[24];
  Form_EditPhrase.Memo1.Text:=CurDialog.Questions[ListBox1.itemIndex];
  Form_EditPhrase.ShowModal;
  if Form_EditPhrase.ok then
  begin
    CurDialog.Questions[ListBox1.itemIndex]:=Form_EditPhrase.Memo1.Text;
    ListBox1.Items.Strings[ListBox1.itemIndex]:=
      inttostr(ListBox1.itemIndex)+'. '+Form_EditPhrase.Memo1.Text;
  end;
end;

procedure TForm_Dialogs.Button5Click(Sender: TObject);
var
  a,n:integer;
begin
  if ListBox1.ItemIndex=-1 then exit;
  if MessageDlgRus(lng[25],lng[26]+#13#10+ListBox1.Items[ListBox1.ItemIndex],MB_ICONEXCLAMATION or MB_OKCANCEL)=mrok then
  begin
    n:=ListBox1.ItemIndex;
    CurDialog.Questions.Delete(n);
    ListBox1.Clear;
    for a:=0 to CurDialog.Questions.Count-1 do
      ListBox1.AddItem(inttostr(a)+'. '+CurDialog.Questions[a],nil);
  end;
end;

procedure TForm_Dialogs.Button6Click(Sender: TObject);
begin
  Form_EditAnswer.Caption:=lng[27];
  Form_EditAnswer.Edit1.Text:='';
  Form_EditAnswer.Memo1.Text:='';
  Form_EditAnswer.ShowModal;
  if EditAnswerUnit.ok then
  begin
    ListBox5.Items.Add(inttostr(ListBox5.Count)+'. '+Form_EditAnswer.Edit1.Text);
    if Form_EditAnswer.CheckBox1.Checked then
      CurDialog.AddAnswer(Form_EditAnswer.Edit1.Text,Form_EditAnswer.Memo1.Text)
    else
      CurDialog.AddAnswer(Form_EditAnswer.Edit1.Text,Form_EditAnswer.Edit1.Text)
  end;
end;

procedure TForm_Dialogs.Button8Click(Sender: TObject);
begin
  if ListBox5.ItemIndex<0 then exit;
  Form_EditAnswer.Caption:=lng[28];
  Form_EditAnswer.Edit1.Text:=CurDialog.AnswersShort[ListBox5.ItemIndex];
  Form_EditAnswer.Memo1.Text:=CurDialog.AnswersLong[ListBox5.ItemIndex];
  Form_EditAnswer.CheckBox1.Checked:=
  (CurDialog.AnswersShort[ListBox5.ItemIndex]<>CurDialog.AnswersLong[ListBox5.ItemIndex]);
  Form_EditAnswer.ShowModal;
  if EditAnswerUnit.ok then
  begin
    CurDialog.AnswersShort[ListBox5.ItemIndex]:=Form_EditAnswer.Edit1.Text;
    if Form_EditAnswer.CheckBox1.Checked then
      CurDialog.AnswersLong[ListBox5.ItemIndex]:=Form_EditAnswer.Memo1.Text
    else
      CurDialog.AnswersLong[ListBox5.ItemIndex]:=Form_EditAnswer.Edit1.Text;
    ListBox5.Items[ListBox5.ItemIndex]:=(inttostr(ListBox5.ItemIndex)+'. '+Form_EditAnswer.Edit1.Text);
  end;
end;

procedure TForm_Dialogs.Button9Click(Sender: TObject);
begin
  if CurDialog<>nil then
  begin
    if World.State<>wsStop then
      if World.CurrentDialog=CurDialog then
        if MessageDlgRus(lng[32],lng[31],MB_OKCANCEL or MB_ICONWARNING)<>mrok then
          exit
        else
          World.Stop;
    CurDialog.Text:=SynMemo1.Text;
    if not CurDialog.Compile then
      ShowMsgs
    else
      begin ListBox4.Clear;ListBox4.Items.Add(lng[17]);end;
  end;
end;

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

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

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

procedure TForm_Dialogs.Button7Click(Sender: TObject);
var
  a,n:integer;
begin
  n:=ListBox5.ItemIndex;
  if n=-1 then exit;
  if MessageDlgRus(lng[29],lng[30]+#13#10+CurDialog.AnswersShort[n],MB_ICONEXCLAMATION or MB_OKCANCEL)=mrok then
  begin
    CurDialog.DeleteAnswer(n);
    ListBox5.Clear;
    for a:=0 to CurDialog.AnswersCount-1 do
      ListBox5.AddItem(inttostr(a)+'. '+CurDialog.AnswersShort[a],nil);
  end;
end;

end.
