{*************************************************************************}
{                                                                         }
{ VarUnit.pas - Global Variables 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 VarUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ValEdit,dqe2_types,alkatraz;

type
  TForm_Vars = class(TForm)
    v: TValueListEditor;
    Button1: TButton;
    Button2: TButton;
    procedure FormDeactivate(Sender: TObject);
    procedure UpdateList;
    procedure vKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure vValidate(Sender: TObject; ACol, ARow: Integer; const KeyName,
      KeyValue: string);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure OnVarNew(VarName:string;VarValue:Variant);
    procedure OnVarChange(VarName:string;VarValue:Variant);
    procedure OnVarDelete(VarName:string;VarValue:Variant);
    procedure Button1Click(Sender: TObject);
    procedure Localize;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_Vars: TForm_Vars;
  can:boolean;

implementation

uses MainUnit;

{$R *.dfm}

var lng:array of string;

procedure TForm_Vars.Button1Click(Sender: TObject);
var
  x:string;
  res:TModalResult;
begin
  repeat
    if InputQuery(lng[3],lng[4],x) then
    begin
      if x='' then
      begin
        res:=MessageDlgRus(lng[5],lng[6],MB_ICONWARNING or MB_OKCANCEL);
        if res=mrOk then continue else exit;
      end;
      if world.VarExists(x) then
      begin
        res:=MessageDlgRus(lng[7],lng[8],MB_ICONWARNING or MB_OKCANCEL);
        if res=mrOk then continue else exit;
      end;
      world.WriteVar(x,'');exit;
    end else break;
  until false;
end;

procedure TForm_Vars.Button2Click(Sender: TObject);
begin
  if v.Strings.Count>0 then
    if v.Row>=0 then
      if MessageDlgRus(lng[9],lng[10]+#13#10+v.Keys[v.Row],MB_OKCANCEL or MB_ICONWARNING)=mrok then
        World.DeleteVar(v.Keys[v.Row]);
end;

procedure TForm_Vars.FormCreate(Sender: TObject);
begin
 world.OnVarNew:=OnVarNew;
 world.OnVarChange:=OnVarChange;
 world.OnVarDelete:=OnVarDelete;
 can:=true;
 SetLength(lng,11);
end;

procedure TForm_Vars.FormDeactivate(Sender: TObject);
begin
  vValidate(v,v.Col,v.Row,v.Keys[v.Row],v.Values[v.Keys[v.Row]]);
end;

procedure TForm_Vars.Localize;
begin
  LoadStringsFromIni(MainUnit.lang_ini,Name,lng);
  Caption:=lng[0];
  Button1.Caption:=lng[1];
  Button2.Caption:=lng[2];
end;

procedure TForm_Vars.OnVarChange(VarName: string; VarValue: Variant);
var
  n:integer;
begin
  if v.FindRow(VarName,n) then
  begin
    if not can then exit;
    v.Values[v.Keys[n]]:=VarValue;
  end;
end;

procedure TForm_Vars.OnVarDelete(VarName: string; VarValue: Variant);
var
  n:integer;
begin
  if v.FindRow(VarName,n) then
    v.Strings.Delete(n);
  if v.Strings.Count=0 then v.Visible:=false;
end;

procedure TForm_Vars.OnVarNew(VarName: string; VarValue: Variant);
var
  n:integer;
begin
  if v.FindRow(VarName,n) then
    v.Values[v.Keys[n]]:=VarValue
  else
    v.InsertRow(VarName,VarValue,true);
  if v.Strings.Count>0 then v.Visible:=true;
end;

procedure TForm_Vars.UpdateList;
var
  a:integer;
begin
  v.Strings.Clear;
  if world.VarCount>0 then
  begin
    v.Visible:=true;
    for a:=0 to world.VarCount-1 do
      v.InsertRow(world.VarNames[a],world.VarValues[a],true);
  end
  else
    v.Visible:=false;
end;

procedure TForm_Vars.vKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=13 then
    vValidate(v,v.Col,v.Row,v.Keys[v.Row],v.Values[v.Keys[v.Row]]);
end;

procedure TForm_Vars.vValidate(Sender: TObject; ACol, ARow: Integer;
  const KeyName, KeyValue: string);
begin
  if Application.Terminated then exit;
  can:=false;
  if Keyname<>'' then
    World.WriteVar(KeyName,KeyValue);
  can:=true;
end;

end.
