unit uMapToolForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, GR32_Image, GR32, uArt, uMCFSE, uSEditorForm, StdCtrls,
  ExtCtrls, Menus, GR32_Layers;

type
  TMaptoolForm = class(TForm)
    iID: TImage32;
    btnIdLeft: TSpeedButton;
    btnIdRight: TSpeedButton;
    btnCursor1: TSpeedButton;
    btnCursor3: TSpeedButton;
    btnCursor5: TSpeedButton;
    btnCursor7: TSpeedButton;
    btnGetId: TSpeedButton;
    sbUP: TSpeedButton;
    eUpDown: TEdit;
    sbDOWN: TSpeedButton;
    btnSetId: TSpeedButton;
    eGoX: TLabeledEdit;
    eGoY: TLabeledEdit;
    btnGo: TButton;
    popID: TPopupMenu;
    ID1: TMenuItem;
    ID2: TMenuItem;
    ID3: TMenuItem;
    ID4: TMenuItem;
    ID5: TMenuItem;
    spSetZ: TSpeedButton;
    IdTimer: TTimer;
    spSetRandZ: TSpeedButton;
    eRHMin: TLabeledEdit;
    eRHMax: TLabeledEdit;
    procedure FormShow(Sender: TObject);
    procedure iIDClick(Sender: TObject);
    procedure btnCursor1Click(Sender: TObject);
    procedure btnCursor3Click(Sender: TObject);
    procedure btnCursor5Click(Sender: TObject);
    procedure btnCursor7Click(Sender: TObject);
    procedure btnGetIdClick(Sender: TObject);
    procedure btnSetIdClick(Sender: TObject);
    procedure sbUPClick(Sender: TObject);
    procedure sbDOWNClick(Sender: TObject);
    procedure btnGoClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure iIDMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
    procedure popIDPopup(Sender: TObject);
    procedure ID1Click(Sender: TObject);
    procedure ID2Click(Sender: TObject);
    procedure ID3Click(Sender: TObject);
    procedure ID4Click(Sender: TObject);
    procedure ID5Click(Sender: TObject);
    procedure spSetZClick(Sender: TObject);
    procedure IdTimerTimer(Sender: TObject);
    procedure btnIdRightMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnIdRightMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnIdLeftMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnIdLeftMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure iIDDblClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure spSetRandZClick(Sender: TObject);
  private
    IdNachLinks : Boolean;  
    { Private declarations }
  public
    { Public declarations }
    IdHistory : array[0..5] of Integer;    
    procedure SetCursorWidth( Width : Integer );
    procedure HistoryAdd( Id : Integer );
  end;

var
  MaptoolForm: TMaptoolForm;

implementation

{$R *.dfm}
uses    uStaticToolForm;

procedure TMaptoolForm.FormShow(Sender: TObject);
var     Bitmap : TBitmap32;
begin
        iID.Tag := 0;
        iID.Bitmap.SetSize( 65, 65 );
        iID.Bitmap.Clear( Color32( clWhite ) );
        Bitmap := Art.GetMapTile( 0 );
        iID.Bitmap.Draw( ( 65 - Bitmap.Width ) div 2, ( 65 - Bitmap.Height ) div 2, Bitmap );
end;

procedure TMaptoolForm.iIDClick(Sender: TObject);
begin
        MCFSE.StartId := iID.Tag;
        MCFSE.Show;
end;

procedure TMaptoolForm.btnCursor1Click(Sender: TObject);
begin
        SEditorForm.MapCursorX := 1;
        SEditorForm.MapCursorY := 1;
        StaticToolForm.MakeAllUp( -1 );
end;

procedure TMaptoolForm.btnCursor3Click(Sender: TObject);
begin
        SEditorForm.MapCursorX := 2;
        SEditorForm.MapCursorY := 2;
        StaticToolForm.MakeAllUp( -1 );
end;

procedure TMaptoolForm.btnCursor5Click(Sender: TObject);
begin
        SEditorForm.MapCursorX := 3;
        SEditorForm.MapCursorY := 3;
        StaticToolForm.MakeAllUp( -1 );
end;

procedure TMaptoolForm.btnCursor7Click(Sender: TObject);
begin
        SEditorForm.MapCursorX := 4;
        SEditorForm.MapCursorY := 4;
        StaticToolForm.MakeAllUp( -1 );
end;

procedure TMapToolForm.SetCursorWidth( Width : Integer );
begin
        if Width = 1 then
                btnCursor1.Down := True
        else
                btnCursor1.Down := False;
        if Width = 2 then
                btnCursor3.Down := True
        else
                btnCursor3.Down := False;
        if Width = 3 then
                btnCursor5.Down := True
        else
                btnCursor5.Down := False;
        if Width = 4 then
                btnCursor7.Down := True
        else
                btnCursor7.Down := False;

        if SEditorForm.LClickType <> LClick_GetID_Map then
                btnGetId.Down := False;
        if SEditorForm.LClickType <> LClick_SetID_Map then
                btnSetId.Down := False;
        if SEditorForm.LClickType <> LClick_MoveUp_Map then
                sbUP.Down := False;
        if SEditorForm.LClickType <> LClick_MoveDown_Map then
                sbDown.Down := False;

        SEditorForm.MapCursorX := Width;
        SEditorForm.MapCursorY := Width;
end;

procedure TMaptoolForm.btnGetIdClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_GetID_Map;
        StaticToolForm.MakeAllUp( -1 );
end;

procedure TMaptoolForm.btnSetIdClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_SetId_Map;
        StaticToolForm.MakeAllUp( -1 );
end;

procedure TMaptoolForm.sbUPClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_MoveUp_Map;
        StaticToolForm.MakeAllUp( -1 );
end;

procedure TMaptoolForm.sbDOWNClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_MoveDown_Map;
        StaticToolForm.MakeAllUp( -1 );
end;

procedure TMaptoolForm.btnGoClick(Sender: TObject);
begin
        SEditorForm.CenterX := StrToInt( eGoX.Text );
        SEditorForm.CenterY := StrToInt( eGoY.Text );
        SEditorForm.ObjectListChanged := True;
end;

procedure TMapToolForm.HistoryAdd( Id : Integer );
var     I : Integer;
begin
        for I := 4 downto 0 do
                IdHistory[I+1] := IdHistory[I];
        
        IdHistory[0] := Id;
end;

procedure TMaptoolForm.FormCreate(Sender: TObject);
var     I : Integer;
begin
        for I := 0 to 5 do
                IdHistory[I] := 0;
end;

procedure TMaptoolForm.iIDMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
begin
        if Button = mbRight then
                popID.Popup( X+MaptoolForm.Left, Y+MaptoolForm.Top );
end;

procedure TMaptoolForm.popIDPopup(Sender: TObject);
begin
        ID1.Caption := Format( '0x%x', [IdHistory[1]] );
        ID2.Caption := Format( '0x%x', [IdHistory[2]] );
        ID3.Caption := Format( '0x%x', [IdHistory[3]] );
        ID4.Caption := Format( '0x%x', [IdHistory[4]] );
        ID5.Caption := Format( '0x%x', [IdHistory[5]] );
end;

procedure TMaptoolForm.ID1Click(Sender: TObject);
var     Bitmap : TBitmap32;
begin
        iID.Tag := IdHistory[1];
        iID.Bitmap.SetSize( 65, 65 );
        iID.Bitmap.Clear( Color32( clWhite ) );
        Bitmap := Art.GetMapTile( iID.Tag );
        iID.Bitmap.Draw( ( 65 - Bitmap.Width ) div 2, ( 65 - Bitmap.Height ) div 2, Bitmap );
end;

procedure TMaptoolForm.ID2Click(Sender: TObject);
var     Bitmap : TBitmap32;
begin
        iID.Tag := IdHistory[2];
        iID.Bitmap.SetSize( 65, 65 );
        iID.Bitmap.Clear( Color32( clWhite ) );
        Bitmap := Art.GetMapTile( iID.Tag );
        iID.Bitmap.Draw( ( 65 - Bitmap.Width ) div 2, ( 65 - Bitmap.Height ) div 2, Bitmap );
end;

procedure TMaptoolForm.ID3Click(Sender: TObject);
var     Bitmap : TBitmap32;
begin
        iID.Tag := IdHistory[3];
        iID.Bitmap.SetSize( 65, 65 );
        iID.Bitmap.Clear( Color32( clWhite ) );
        Bitmap := Art.GetMapTile( iID.Tag );
        iID.Bitmap.Draw( ( 65 - Bitmap.Width ) div 2, ( 65 - Bitmap.Height ) div 2, Bitmap );
end;

procedure TMaptoolForm.ID4Click(Sender: TObject);
var     Bitmap : TBitmap32;
begin
        iID.Tag := IdHistory[4];
        iID.Bitmap.SetSize( 65, 65 );
        iID.Bitmap.Clear( Color32( clWhite ) );
        Bitmap := Art.GetMapTile( iID.Tag );
        iID.Bitmap.Draw( ( 65 - Bitmap.Width ) div 2, ( 65 - Bitmap.Height ) div 2, Bitmap );
end;

procedure TMaptoolForm.ID5Click(Sender: TObject);
var     Bitmap : TBitmap32;
begin
        iID.Tag := IdHistory[5];
        iID.Bitmap.SetSize( 65, 65 );
        iID.Bitmap.Clear( Color32( clWhite ) );
        Bitmap := Art.GetMapTile( iID.Tag );
        iID.Bitmap.Draw( ( 65 - Bitmap.Width ) div 2, ( 65 - Bitmap.Height ) div 2, Bitmap );
end;

procedure TMaptoolForm.spSetZClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_SetZ_Map;
        StaticToolForm.MakeAllUp( -1 );
end;

procedure TMaptoolForm.IdTimerTimer(Sender: TObject);
var     Id : Integer;
        Bitmap : TBitmap32;
begin
        Id := iID.Tag;
        if (not IdNachLinks) and (Id < $3FFF) then inc( Id );
        if (IdNachLinks) and (Id > 0) then dec( Id );

        iID.Tag := Id;
        iID.Bitmap.SetSize( 65, 65 );
        iID.Bitmap.Clear( Color32( clWhite ) );
        Bitmap := Art.GetMapTile( Id );
        iID.Bitmap.Draw( ( 65 - Bitmap.Width ) div 2, ( 65 - Bitmap.Height ) div 2, Bitmap );
end;

procedure TMaptoolForm.btnIdRightMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
        if Button = mbLeft then begin
                IdNachLinks := False;
                IdTimer.Enabled := True;
                IdTimerTimer( Sender );
        end;
end;

procedure TMaptoolForm.btnIdRightMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
        IdTimer.Enabled := False;
end;

procedure TMaptoolForm.btnIdLeftMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
        if Button = mbLeft then begin
                IdNachLinks := True;
                IdTimer.Enabled := True;
                IdTimerTimer( Sender );
        end;
end;

procedure TMaptoolForm.btnIdLeftMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
        IdTimer.Enabled := False;
end;

procedure TMaptoolForm.iIDDblClick(Sender: TObject);
begin
        MCFSE.StartId := iID.Tag;
        MCFSE.Show;
end;

procedure TMaptoolForm.Button1Click(Sender: TObject);
begin
begin
        SEditorForm.LClickType := LClick_SetRandomZ_Map;
        StaticToolForm.MakeAllUp( -1 );
end;
end;

procedure TMaptoolForm.spSetRandZClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_SetRandomZ_Map;
        StaticToolForm.MakeAllUp( -1 );
end;

end.
