{*************************************************************************}
{                                                                         }
{ ItemUnit.pas - Item 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 ItemUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, GR32_Image,dqe2_types,alkatraz,gr32,gr32_layers,
  Spin;

type
  TForm_Items = class(TForm)
    GroupBox1: TGroupBox;
    ListView1: TListView;
    Button1: TButton;
    Button2: TButton;
    GroupBox2: TGroupBox;
    Memo1: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Preview: TImage32;
    Button4: TButton;
    GroupBox4: TGroupBox;
    ComboBox1: TComboBox;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    SpinEdit1: TSpinEdit;
    SpinEdit2: TSpinEdit;
    Label6: TLabel;
    Label7: TLabel;
    Edit1: TEdit;
    CheckBox1: TCheckBox;
    Button3: TButton;
    procedure DeselectItem;
    procedure Button3Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure SpinEdit2Change(Sender: TObject);
    procedure SpinEdit1Change(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure ListView1Edited(Sender: TObject; Item: TListItem; var S: string);
    procedure UpdateList;
    procedure UpdateForm;
    procedure UpdateObject;
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure PreviewClick(Sender: TObject);
    procedure ListView1Change(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure Button2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Localize;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_Items: TForm_Items;
  ItemList:TStringList;
  CurNum:integer;
  CurItem:DQE2_Item=nil;
  can:boolean=true;

implementation

uses MainUnit, EditIconUnit, GameUnit, ObjectUnit;

{$R *.dfm}

var lng:array of string;

procedure TForm_Items.Button4Click(Sender: TObject);
begin
  UpdateObject;close;
end;

procedure TForm_Items.CheckBox1Click(Sender: TObject);
begin
  CurItem.Visible:=(Sender as TCheckBox).Checked;
  if not CurItem.Visible then
  begin
    Button3.Caption:=lng[7];
    DeselectItem;
  end;
  if CurItem.Zone<>nil then
    ComboBox1.ItemIndex:=CurItem.Zone.Index
  else
    ComboBox1.ItemIndex:=-1;
end;

procedure TForm_Items.ComboBox1Change(Sender: TObject);
begin
  CurItem.Zone:=DQE2_Zone(world.Zones[(Sender as TComboBox).ItemIndex]);
end;

procedure TForm_Items.DeselectItem;
var
  a:integer;
begin
  if CurItem=nil then exit;
  for a:=0 to ObjectUnit.PickList.Count-1 do
    if ObjectUnit.PickList[a]=CurItem then
    begin
      ObjectUnit.PickList.Delete(a);
      break;
    end;
  if ObjectUnit.CurObj=CurItem then ObjectUnit.CurObj:=nil;
end;

procedure TForm_Items.FormCreate(Sender: TObject);
var
  a:integer;
begin
  for a:=0 to world.ImageList.Count-1 do
    if AnsiLowerCase(world.ImageList.Strings[a])='items' then
      begin ItemList:=TStringList(world.ImageList.Objects[a]);break;end;
  Preview.SetupBitmap(true,clwhite32);
  SetLength(lng,16);
end;

procedure TForm_Items.Button1Click(Sender: TObject);
var
  s:string;
begin
  s:='';
  Form_EditIcon.edit1.ReadOnly:=false;Form_EditIcon.edit1.Text:='';
  Form_EditIcon.ShowModal;
  if Form_EditIcon.ok then
  begin
    can:=false;
    UpdateObject;
    CurItem:=DQE2_Item.Create(world.Items);
    CurItem.Sprite:=DQE2_Sprite(ItemList.Objects[Form_EditIcon.selnum]);
    CurItem.Name:=Form_EditIcon.Edit1.Text;
    CurItem.RealName:=CurItem.Name;
    ListView1.AddItem(CurItem.Name,CurItem);
    CurItem.Zone:=World.CurrentZone;
    UpdateForm;
    can:=true;
  end;
end;

procedure TForm_Items.UpdateForm;
begin
  if ListView1.Selected=nil then
  begin
    DeselectItem;
    CurItem:=nil;
    GroupBox2.Visible:=false;
    GroupBox4.Visible:=false;
    Button2.Enabled:=false;
  end
  else
  begin
    CurItem:=DQE2_Item(ListView1.Selected.Data);
    SpinEdit1.Value:=CurItem.X;
    SpinEdit2.Value:=CurItem.Y;
    Edit1.Text:=CurItem.RealName;
    if CurItem.Zone<>nil then
      ComboBox1.ItemIndex:=CurItem.Zone.Index
    else
     ComboBox1.ItemIndex:=-1;
    memo1.Text:=CurItem.Description;
    if CurItem.Sprite<>nil then
      preview.Bitmap.Draw(preview.Bitmap.Canvas.ClipRect,CurItem.Sprite.Thumbnail.Canvas.ClipRect,CurItem.Sprite.Thumbnail.Canvas.Handle);
    if CurItem.InBagIndex>=0 then
    begin
      Button3.Caption:=lng[8];
    end
    else
    begin
      Button3.Caption:=lng[7];
    end;
    CheckBox1.Checked:=CurItem.Visible;
    GroupBox2.Visible:=true;
    GroupBox4.Visible:=true;
    Button2.Enabled:=true;
  end;
end;

procedure TForm_Items.UpdateList;
var
  a:integer;
begin
  CurItem:=nil;
  ListView1.Clear;
  for a:=0 to world.Items.Count-1 do
    ListView1.AddItem(dqe2_item(world.Items[a]).name,world.Items[a]);
  ListView1.Selected:=nil;UpdateForm;
end;

procedure TForm_Items.UpdateObject;
begin
  if CurItem=nil then exit;
  CurItem.Description:=memo1.Text;
  CurItem.RealName:=edit1.Text;
end;

procedure TForm_Items.PreviewClick(Sender: TObject);
begin
  form_editicon.selnum:=0;
  Form_EditIcon.edit1.ReadOnly:=true;
  Form_EditIcon.edit1.Text:=CurItem.Name;
  form_editicon.ShowModal;
  if Form_EditIcon.ok then
  begin
    CurItem.Sprite:=DQE2_Sprite(ItemList.Objects[Form_EditIcon.selnum]);
    preview.Bitmap.Draw(preview.Bitmap.Canvas.ClipRect,CurItem.Sprite.Thumbnail.Canvas.ClipRect,CurItem.Sprite.Thumbnail.Canvas.Handle);
  end;
end;

procedure TForm_Items.SpinEdit1Change(Sender: TObject);
begin
  CurItem.X:=(Sender as TSpinEdit).Value;
end;

procedure TForm_Items.SpinEdit2Change(Sender: TObject);
begin
  CurItem.Y:=(Sender as TSpinEdit).Value;
end;

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

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

procedure TForm_Items.Localize;
begin
  LoadStringsFromIni(MainUnit.lang_ini,Name,lng);
  Caption:=lng[0];
  GroupBox1.Caption:=lng[1];
  Button1.Caption:=lng[2];
  Button2.Caption:=lng[3];
  GroupBox4.Caption:=lng[4];
  Label3.Caption:=lng[5];
  CheckBox1.Caption:=lng[6];
  Button3.Caption:=lng[7];
  GroupBox2.Caption:=lng[9];
  Label7.Caption:=lng[10];
  Label1.Caption:=lng[11];
  Label2.Caption:=lng[12];
  Label6.Caption:=lng[13];
end;

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

procedure TForm_Items.Button3Click(Sender: TObject);
var
  b:TButton;
begin
  b:=(sender as TButton);
  if CurItem.InBagIndex=-1 then
  begin
    CurItem.PutInBag;
    CheckBox1.Checked:=true;
    b.Caption:=lng[8];
    DeselectItem;
  end
  else
  begin
    CurItem.Drop;
    b.Caption:=lng[7];
  end;
  ComboBox1.ItemIndex:=CurItem.Zone.Index;
end;

procedure TForm_Items.FormShow(Sender: TObject);
var
  a:integer;
begin
  ComboBox1.Items.Clear;
  for a:=0 to World.Zones.Count-1 do
    ComboBox1.AddItem(dqe2_Zone(World.Zones[a]).Name,World.Zones[a]);
  UpdateForm;
end;

end.
