unit uStaticToolForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, uMapForm, GR32_Image, rmSpeedBtns,
  rmCaptionButtons, GR32, uArt, ExtCtrls, ComCtrls, Menus, GR32_Layers, uUtilities,
  uHues;

type
  TStaticToolForm = class(TForm)
    btnShowWorldMap: TSpeedButton;
    btnInfo: TSpeedButton;
    btnDelete: TSpeedButton;
    btnAdd: TSpeedButton;
    btnColor: TSpeedButton;
    btnId: TSpeedButton;
    iColor: TImage32;
    iID: TImage32;
    btnIdLeft: TSpeedButton;
    btnIdRight: TSpeedButton;
    btnTile: TSpeedButton;
    cbColor: TCheckBox;
    eTileZ: TEdit;
    gbMove: TGroupBox;
    sbOL: TSpeedButton;
    sbO: TSpeedButton;
    sbOR: TSpeedButton;
    sbR: TSpeedButton;
    sbUR: TSpeedButton;
    sbU: TSpeedButton;
    sbUL: TSpeedButton;
    sbL: TSpeedButton;
    gbNuke: TGroupBox;
    eMove: TEdit;
    sbUP: TSpeedButton;
    eUpDown: TEdit;
    sbDOWN: TSpeedButton;
    cbArea: TCheckBox;
    cbSetColor: TCheckBox;
    cbTestId: TCheckBox;
    cbTestHeight: TCheckBox;
    eDown: TEdit;
    eUp: TEdit;
    btnNuke: TSpeedButton;
    tbMaxZ: TTrackBar;
    btnGetId: TSpeedButton;
    btnGetColor: TSpeedButton;
    popID: TPopupMenu;
    ID1: TMenuItem;
    ID2: TMenuItem;
    ID3: TMenuItem;
    ID4: TMenuItem;
    ID5: TMenuItem;
    cbMoveTestID: TCheckBox;
    IdTimer: TTimer;
    popColor: TPopupMenu;
    Color1: TMenuItem;
    Color2: TMenuItem;
    Color3: TMenuItem;
    Color4: TMenuItem;
    Color5: TMenuItem;
    procedure btnShowWorldMapClick(Sender: TObject);
    procedure iColorDblClick(Sender: TObject);
    procedure iColorClick(Sender: TObject);
    procedure btnInfoClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure btnColorClick(Sender: TObject);
    procedure btnIdClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnTileClick(Sender: TObject);
    procedure sbOLClick(Sender: TObject);
    procedure sbOClick(Sender: TObject);
    procedure sbORClick(Sender: TObject);
    procedure sbRClick(Sender: TObject);
    procedure sbURClick(Sender: TObject);
    procedure sbUClick(Sender: TObject);
    procedure sbULClick(Sender: TObject);
    procedure sbLClick(Sender: TObject);
    procedure sbUPClick(Sender: TObject);
    procedure sbDOWNClick(Sender: TObject);
    procedure iIDClick(Sender: TObject);
    procedure iIDDblClick(Sender: TObject);
    procedure btnNukeClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnGetColorClick(Sender: TObject);
    procedure btnGetIdClick(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 btnIdLeftMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnIdRightMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnIdRightMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnIdLeftMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure IdTimerTimer(Sender: TObject);
    procedure popColorPopup(Sender: TObject);
    procedure Color1Click(Sender: TObject);
    procedure Color2Click(Sender: TObject);
    procedure Color3Click(Sender: TObject);
    procedure Color4Click(Sender: TObject);
    procedure Color5Click(Sender: TObject);
    procedure iColorMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
    procedure tbMaxZChange(Sender: TObject);
  private
    IdNachLinks : Boolean;
    { Private declarations }
  public
    IdHistory, ColorHistory : array[0..5] of Integer;
    procedure MakeAllUp( Ausnahme : Integer );
    procedure HistoryAdd( Id : Integer );
    procedure ColorHistoryAdd( Color : Integer );           
    { Public declarations }
  end;

var
  StaticToolForm: TStaticToolForm;

implementation

{$R *.dfm}

uses    uHFSE, uSEditorForm, uSCFSE, uMapToolForm;

procedure TStaticToolForm.btnShowWorldMapClick(Sender: TObject);
begin
        MapForm.Show;
end;

procedure TStaticToolForm.iColorDblClick(Sender: TObject);
begin
        HFSE.Show;
end;

procedure TStaticToolForm.iColorClick(Sender: TObject);
begin
        HFSE.Show;
end;

procedure TStaticToolForm.MakeAllUp( Ausnahme : Integer );
begin
        if Ausnahme <> LClick_Info_Static then
                btnInfo.Down := False;
        if Ausnahme <> LClick_Delete_Static then
                btnDelete.Down := False;
        if Ausnahme <> LClick_Add_Static then
                btnAdd.Down := False;
        if Ausnahme <> LClick_Color_Static then
                btnColor.Down := False;
        if Ausnahme <> LClick_ID_Static then
                btnId.Down := False;
        if Ausnahme <> LClick_Tile_Static then
                btnTile.Down := False;
        if Ausnahme <> LClick_Move_Static_OL then
                sbOL.Down := False;
        if Ausnahme <> LClick_Move_Static_O then
                sbO.Down := False;
        if Ausnahme <> LClick_Move_Static_OR then
                sbOR.Down := False;
        if Ausnahme <> LClick_Move_Static_R then
                sbR.Down := False;
        if Ausnahme <> LClick_Move_Static_UR then
                sbUR.Down := False;
        if Ausnahme <> LClick_Move_Static_U then
                sbU.Down := False;
        if Ausnahme <> LClick_Move_Static_UL then
                sbUL.Down := False;
        if Ausnahme <> LClick_Move_Static_L then
                sbL.Down := False;
        if Ausnahme <> LClick_Move_Static_UP then
                sbUP.Down := False;
        if Ausnahme <> LClick_Move_Static_DOWN then
                sbDOWN.Down := False;
        if Ausnahme <> LClick_GetColor_Static then
                btnGetColor.Down := False;
        if Ausnahme <> LClick_GetID_Static then
                btnGetID.Down := False;
        if Ausnahme <> LClick_Nuke_Static then
                btnNuke.Down := False;
        if Ausnahme <> -1 then
                MapToolForm.SetCursorWidth( 1 );
end;

procedure TStaticToolForm.btnInfoClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Info_Static;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.btnDeleteClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Delete_Static;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.btnAddClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Add_Static;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.btnColorClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Color_Static;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.btnIdClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_ID_Static;
        MakeAllUp( SEditorForm.LClickType );
end;

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

procedure TStaticToolForm.btnTileClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Tile_Static;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.sbOLClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Move_Static_OL;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.sbOClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Move_Static_O;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.sbORClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Move_Static_OR;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.sbRClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Move_Static_R;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.sbURClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Move_Static_UR;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.sbUClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Move_Static_U;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.sbULClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Move_Static_UL;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.sbLClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Move_Static_L;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.sbUPClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Move_Static_UP;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.sbDOWNClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Move_Static_DOWN;
        MakeAllUp( SEditorForm.LClickType );
end;

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

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

procedure TStaticToolForm.btnNukeClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_Nuke_Static;
        MakeAllUp( SEditorForm.LClickType );        
end;

procedure TStaticToolForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if Key = VK_Delete then begin
                SEditorForm.LClickType := LClick_Delete_Static;
                MakeAllUp( SEditorForm.LClickType );
        end;
end;

procedure TStaticToolForm.btnGetColorClick(Sender: TObject);
begin
        SEditorForm.LClickType := LClick_GetColor_Static;
        MakeAllUp( SEditorForm.LClickType );
end;

procedure TStaticToolForm.btnGetIdClick(Sender: TObject);
begin
begin
        SEditorForm.LClickType := LClick_GetID_Static;
        MakeAllUp( SEditorForm.LClickType );
end;
end;

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

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

procedure TStaticToolForm.ColorHistoryAdd( Color : Integer );
var     I : Integer;
begin
        for I := 4 downto 0 do
                ColorHistory[I+1] := ColorHistory[I];
        
        ColorHistory[0] := Color;
end;

procedure TStaticToolForm.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 TStaticToolForm.ID1Click(Sender: TObject);
var     Bitmap : TBitmap32;
begin
        iID.Tag := IdHistory[1];
        iID.Bitmap.SetSize( 65, 65 );
        iID.Bitmap.Clear( Color32( clWhite ) );
        Bitmap := Art.GetStaticTile( iID.Tag );
        iID.Bitmap.Draw( ( 65 - Bitmap.Width ) div 2, ( 65 - Bitmap.Height ) div 2, Bitmap );
end;

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

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

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

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

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

procedure TStaticToolForm.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 TStaticToolForm.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 TStaticToolForm.btnIdRightMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
        IdTimer.Enabled := False;
end;

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

procedure TStaticToolForm.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 );
        Bitmap := Art.GetStaticTile( Id );
        iID.Bitmap.Clear( Color32( clWhite ) );
        iID.Bitmap.Draw( ( 65 - Bitmap.Width ) div 2, ( 65 - Bitmap.Height ) div 2, Bitmap );
end;

procedure TStaticToolForm.popColorPopup(Sender: TObject);
begin
        Color1.Caption := Format( '0x%x', [ColorHistory[1]] );
        Color2.Caption := Format( '0x%x', [ColorHistory[2]] );
        Color3.Caption := Format( '0x%x', [ColorHistory[3]] );
        Color4.Caption := Format( '0x%x', [ColorHistory[4]] );
        Color5.Caption := Format( '0x%x', [ColorHistory[5]] );
end;

procedure TStaticToolForm.Color1Click(Sender: TObject);
begin
        iColor.Tag := ColorHistory[1];
        iColor.Bitmap.SetSize( 44, 20 );
        iColor.Bitmap.Clear( Color15to32( Hues.GetColor( iColor.Tag, 28 ) ) );
end;

procedure TStaticToolForm.Color2Click(Sender: TObject);
begin
        iColor.Tag := ColorHistory[2];
        iColor.Bitmap.SetSize( 44, 20 );
        iColor.Bitmap.Clear( Color15to32( Hues.GetColor( iColor.Tag, 28 ) ) );
end;

procedure TStaticToolForm.Color3Click(Sender: TObject);
begin
        iColor.Tag := ColorHistory[3];
        iColor.Bitmap.SetSize( 44, 20 );
        iColor.Bitmap.Clear( Color15to32( Hues.GetColor( iColor.Tag, 28 ) ) );
end;

procedure TStaticToolForm.Color4Click(Sender: TObject);
begin
        iColor.Tag := ColorHistory[4];
        iColor.Bitmap.SetSize( 44, 20 );
        iColor.Bitmap.Clear( Color15to32( Hues.GetColor( iColor.Tag, 28 ) ) );
end;

procedure TStaticToolForm.Color5Click(Sender: TObject);
begin
        iColor.Tag := ColorHistory[5];
        iColor.Bitmap.SetSize( 44, 20 );
        iColor.Bitmap.Clear( Color15to32( Hues.GetColor( iColor.Tag, 28 ) ) );
end;

procedure TStaticToolForm.iColorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer;
  Layer: TCustomLayer);
begin
        if Button = mbRight then
                popColor.Popup( X+StaticToolForm.Left, Y+StaticToolForm.Top );        
end;

procedure TStaticToolForm.tbMaxZChange(Sender: TObject);
begin
        SEditorForm.MaxZ := tbMaxZ.Position;
end;

end.
