unit uMCFSE;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, GR32, uUtilities, uArt, ExtCtrls;

type
  TMCFSE = class(TForm)
    dgStatic: TDrawGrid;
    procedure dgStaticSelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure dgStaticDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure FormShow(Sender: TObject);
    procedure dgStaticDblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    SRow, SCol : Integer;
    { Private declarations }
  public
    StartId : Integer;
    { Public declarations }
  end;

var
  MCFSE: TMCFSE;

implementation

uses uMapToolForm;
{$R *.dfm}

procedure TMCFSE.dgStaticSelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
        SRow := ARow;
        SCol := ACol;
end;

procedure TMCFSE.dgStaticDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var     Bitmap : TBitmap32;
        Id : Integer;
        x, y, H, B : Integer;
        BgColor, OrigColor : TColor;
begin
        Id := ARow*12 + ACol;
        if Id > $3FFF then exit;

        if ( gdSelected in state ) then
                BgColor := clBlue
        else
                BgColor := clWhite;

        Bitmap := Art.GetMapTile( Id );

        for x := 0 to 43 do
                for y := 0 to 43 do begin
                        H := y;
                        B := x;

                        if ( H < 0 ) or ( B < 0 ) or ( H >= Bitmap.Height ) or ( B >= Bitmap.Width ) then begin
                                dgStatic.Canvas.Pixels[ Rect.Left+x, Rect.Top+y ] := BgColor;
                        end
                        else begin
                                OrigColor := WinColor( Bitmap.Pixel[B,H] );
                                if ((H < 21) and (B < 21-H)) or ((H > 22) and (B < H-22)) or ((H < 21) and (B > 22+H)) or ((H > 22) and (B > 65-H))  then
                                        dgStatic.Canvas.Pixels[ Rect.Left+x, Rect.Top+y ] := BgColor
                                else
                                        dgStatic.Canvas.Pixels[ Rect.Left+x, Rect.Top+y ] := OrigColor;
                        end;
                end;
        Bitmap.Free;
end;

procedure TMCFSE.FormShow(Sender: TObject);
begin
        SRow := StartId div 12;
        SCol := StartId mod 12;

        dgStatic.Row := SRow;
        dgStatic.Col := SCol;
end;

procedure TMCFSE.dgStaticDblClick(Sender: TObject);
var     Id : Integer;
        Bitmap : TBitmap32;
begin
        Id := SRow*12 + SCol;
        if not Art.MapExists( Id ) then exit;

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

procedure TMCFSE.FormCreate(Sender: TObject);
begin
        StartID := 0;
end;

end.
