unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, Menus, ScrollBoxEx, StdCtrls, ImgList;
type
TMainForm =
class(TForm)
MainMenu1: TMainMenu;
Datei1: TMenuItem;
Neu1: TMenuItem;
ffnen1: TMenuItem;
Speichern1: TMenuItem;
Speicherunter1: TMenuItem;
LevelScroller: TScrollBoxEx;
Grid: TPaintBox;
MenuPanel: TPanel;
_xypos: TLabel;
Label1: TLabel;
ObjectList: TImageList;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure GridMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure LevelScrollerVScroll(Sender: TObject; Position: Integer);
procedure LevelScrollerHScroll(Sender: TObject; Position: Integer);
procedure FormActivate(Sender: TObject);
procedure DrawGrid;
function CheckX(X: Integer): Integer;
function CheckY(Y: Integer): Integer;
procedure DrawImage(X, Y, ImageIndex: Integer);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
type ObjectRec =
Record
X, Y: Integer;
Filled: Boolean;
ImageIndex: Integer;
end;
var
MainForm: TMainForm;
xPos, yPos: Integer;
ObjectArray:
Array of ObjectRec;
implementation
{$R *.dfm}
procedure TMainForm.DrawGrid;
var
x, y, i, pixX, pixY: Integer;
Clear: TRect;
begin
//GRID
Grid.Refresh;
i := 0;
Clear.Left := 0;
Clear.Top := 0;
Clear.Right := Grid.Width;
Clear.Bottom := Grid.Height;
Grid.Canvas.Rectangle(Clear);
Grid.Canvas.Brush.Color := clAppWorkSpace;
Grid.Canvas.FillRect(Clear);
for x := 0
to Grid.Width
do
begin
Grid.Canvas.MoveTo(i, 0);
Grid.Canvas.LineTo(i, Grid.Height);
inc(i, 20);
end;
i := 0;
for y := 0
to Grid.Height
do
begin
Grid.Canvas.MoveTo(0, i);
Grid.Canvas.LineTo(Grid.Width, i);
inc(i, 20);
end;
//GRID
//ADD IMAGES
for i := 0
to (Grid.Width
div 20 * Grid.Height
div 20)
do
begin
if ObjectArray[i].Filled = True
then
DrawImage(ObjectArray[i].X, ObjectArray[i].Y, ObjectArray[i].ImageIndex);
end;
//ADD IMAGES
// HIGHLIGHT
Grid.Canvas.Pen.Color := clYellow;
pixX := xPos * 20;
pixY := yPos * 20;
Grid.Canvas.MoveTo(pixX, pixY);
Grid.Canvas.LineTo(pixX, pixY+20);
Grid.Canvas.LineTo(pixX+20, pixY+20);
Grid.Canvas.LineTo(pixX+20, pixY);
Grid.Canvas.LineTo(pixX, pixY);
Grid.Canvas.Pen.Color := clBlack;
//HIGHLIGHT
end;
procedure TMainForm.FormActivate(Sender: TObject);
begin
DrawGrid;
end;
procedure TMainForm.LevelScrollerHScroll(Sender: TObject; Position: Integer);
begin
DrawGrid;
end;
procedure TMainForm.LevelScrollerVScroll(Sender: TObject; Position: Integer);
begin
DrawGrid;
end;
function TMainForm.CheckX(X: Integer): Integer;
var a, b, i, counter: Integer;
begin
a := 0;
b := 20;
Counter := 0;
for i := 0
to Grid.Width
do
begin
if (X > a)
and (X < b)
then
Result := Counter;
inc(Counter, 1);
inc(a, 20);
inc(b, 20);
end;
end;
function TMainForm.CheckY(Y: Integer): Integer;
var a, b, i, Counter: Integer;
begin
a := 0;
b := 20;
Counter := 0;
for i := 0
to Grid.Height
do
begin
if (Y > a)
and (Y < b)
then
Result := Counter;
inc(Counter, 1);
inc(a, 20);
inc(b, 20);
end;
end;
procedure TMainForm.GridMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
_xypos.Caption := '
X: ' + IntToStr(CheckX(X)) + '
Y: ' + IntToStr(CheckY(Y));
xPos := CheckX(X);
yPos := CheckY(Y);
DrawGrid;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
ObjectList.Draw(Grid.Canvas, xPos*20, yPos*20, 0);
ObjectArray[xPos*yPos].Filled := True;
ObjectArray[xPos*yPos].X := xPos;
ObjectArray[xPos*yPos].Y := yPos;
ObjectArray[xPos*yPos].ImageIndex := 0;
end;
procedure TMainForm.FormCreate(Sender: TObject);
var
i: Integer;
begin
SetLength(ObjectArray, (Grid.Width
div 20 * Grid.Height
div 20));
for i := 0
to (Grid.Width
div 20 * Grid.Height
div 20)
do
begin
ObjectArray[i].X := 0;
ObjectArray[i].Y := 0;
ObjectArray[i].Filled := False;
ObjectArray[i].ImageIndex := 0;
end;
end;
procedure TMainForm.DrawImage(X, Y, ImageIndex: Integer);
begin
ObjectList.Draw(Grid.Canvas, X*20, Y*20, ImageIndex);
end;
end.