{*************************************************************************}
{                                                                         }
{ EditIconUnit.pas - item's icon selection 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 EditIconUnit;

interface

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

type
  TForm_EditIcon = class(TForm)
    ScrollBar1: TScrollBar;
    ItemSelect: TImage32;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    procedure DrawRed;
    procedure FormShow(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ItemSelectMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer; Layer: TCustomLayer);
    procedure ItemSelectMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
    procedure Localize;
  private
    { Private declarations }
  public
    { Public declarations }
    ok:boolean;
    selnum:integer;
  end;

var
  Form_EditIcon: TForm_EditIcon;
  curNum:integer;

implementation

uses ItemUnit, MainUnit;

{$R *.dfm}

var lng:array of string;

procedure TForm_EditIcon.FormShow(Sender: TObject);
begin
  ok:=false;
end;

procedure TForm_EditIcon.BitBtn2Click(Sender: TObject);
begin
  close;
end;

procedure TForm_EditIcon.BitBtn1Click(Sender: TObject);
begin
  if selnum=-1 then
    MessageDlgRus(lng[4],lng[5],MB_OK or MB_ICONINFORMATION)
  else
  begin
    ok:=true;
    close;
  end
end;

procedure TForm_EditIcon.ScrollBar1Change(Sender: TObject);
var
  a:integer;
  bmp:TBitmap;
begin
  ItemSelect.Bitmap.Clear(clWhite32);
  for a:=0 to ItemUnit.ItemList.Count-1 do
  begin
    bmp:=DQE2_Sprite(ItemUnit.ItemList.Objects[a]).Thumbnail;
    ItemSelect.Bitmap.Draw(rect(a*56+3-ScrollBar1.Position,3,(a+1)*56-4-ScrollBar1.Position,53),bmp.Canvas.ClipRect,bmp.Canvas.Handle);
  end;
  DrawRed;
end;

procedure TForm_EditIcon.FormCreate(Sender: TObject);
var
  m:integer;
begin
  ItemSelect.SetupBitmap(true,clwhite32);
  m:=56*ItemList.Count-400;if m<0 then m:=0;ScrollBar1.Max:=m;
  CurNum:=-1;SelNum:=-1;
  ScrollBar1Change(nil);
  SetLength(lng,6);
end;

procedure TForm_EditIcon.ItemSelectMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
begin
  ItemSelect.Bitmap.FrameRectS(CurNum*56-ScrollBar1.Position,0,(CurNum+1)*56-1-ScrollBar1.Position,56,clwhite32);
  ItemSelect.Bitmap.FrameRectS(CurNum*56-ScrollBar1.Position+2,2,(CurNum+1)*56-3-ScrollBar1.Position,54,clwhite32);
  CurNum:=(x+ScrollBar1.Position) div 56;
  if CurNum>(ItemList.Count-1)then CurNum:=-1
  else
  begin
    ItemSelect.Bitmap.FrameRectS(CurNum*56-ScrollBar1.Position,0,(CurNum+1)*56-1-ScrollBar1.Position,56,clblue32);
    ItemSelect.Bitmap.FrameRectS(CurNum*56-ScrollBar1.Position+2,2,(CurNum+1)*56-3-ScrollBar1.Position,54,clblue32);
  end;
  drawred;
end;

procedure TForm_EditIcon.Localize;
begin
  LoadStringsFromIni(MainUnit.lang_ini,Name,lng);
  Caption:=lng[0];
  Label1.Caption:=lng[1];
  Label2.Caption:=lng[2];
  BitBtn2.Caption:=lng[3];
end;

procedure TForm_EditIcon.ItemSelectMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer;
  Layer: TCustomLayer);
begin
  if CurNum=-1 then exit;
  selNum:=CurNum;
  ScrollBar1Change(nil);
end;

procedure TForm_EditIcon.DrawRed;
begin
  if selnum=-1 then exit;
  ItemSelect.Bitmap.FrameRectS(selNum*56-ScrollBar1.Position,0,(selNum+1)*56-1-ScrollBar1.Position,56,clred32);
  ItemSelect.Bitmap.FrameRectS(selNum*56-ScrollBar1.Position+2,2,(selNum+1)*56-3-ScrollBar1.Position,54,clred32);
end;

end.
