unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, LCLType;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
PaintBox1: TPaintBox;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure PaintBox1MouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PaintBox1Paint(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
FieldBitmap, AnimationBitmap: TBitmap;
AnimationFrame: Integer;
procedure FieldInit(Cols, Rows: Integer);
procedure StartCaptureAnimation;
procedure StopCaptureAnimation;
procedure ClearAnimationBitmap;
public
end;
var
Form1: TForm1;
BoxSize: Integer = 24;
FieldRow: integer = 0;
FieldCol: integer = 0;
Button1Clicked: Boolean = False;
Button2Clicked: Boolean = False;
FieldWidth, FieldHeight: Integer;
ThereWillBeBlood: Boolean = False;
CellXCenter, CellYCenter: Integer;
implementation
{$R *.lfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Button1Clicked := True;
FieldRow := 5;
FieldCol := 5;
FieldInit(FieldCol, FieldRow);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Button2Clicked := True;
FieldRow := 20;
FieldCol := 20;
FieldInit(FieldCol, FieldRow);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FieldBitmap := TBitmap.Create;
AnimationBitmap := TBitmap.Create;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
FieldBitmap.Destroy;
AnimationBitmap.Destroy;
end;
procedure TForm1.PaintBox1MouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
CellX, CellY: Integer;
begin
Label1.Caption := X.ToString + ', ' + Y.ToString;
CellX := X div BoxSize;
CellY := Y div BoxSize;
Label2.Caption := CellX.ToString + ', ' + CellY.ToString;
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
PaintBox1.Canvas.Draw(0,0, FieldBitmap);
if ThereWillBeBlood then
PaintBox1.Canvas.Draw(0, 0, AnimationBitmap);
end;
procedure TForm1.FieldInit(Cols, Rows: Integer);
var
x, y: Integer;
r: TRect;
begin
FieldWidth := Cols * BoxSize;
FieldHeight := Rows * BoxSize;
PaintBox1.Width := FieldWidth;
PaintBox1.Height := FieldHeight;
FieldBitmap.Clear;
AnimationBitmap.Clear;
FieldBitmap.SetSize(FieldWidth, FieldHeight);
FieldBitmap.Canvas.Brush.Color := clNone;
FieldBitmap.Canvas.FillRect(0, 0, FieldWidth, FieldHeight);
AnimationBitmap.SetSize(FieldWidth, FieldHeight);
AnimationBitmap.Transparent := True;
AnimationBitmap.Canvas.Brush.Color := clNone;
AnimationBitmap.Canvas.FillRect(0, 0, FieldWidth, FieldHeight);
for x := 0 to Cols - 1 do
begin
for y := 0 to Rows - 1 do
begin
r := rect(x * BoxSize, y * BoxSize, x * BoxSize + BoxSize, y * BoxSize + BoxSize);
FieldBitmap.Canvas.Frame3d(r, 2, bvRaised);
end;
end;
PaintBox1.Refresh;
end;
procedure TForm1.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
CellX, CellY: Integer;
begin
CellX := (X div BoxSize) * Boxsize;
CellY := (Y div BoxSize) * Boxsize;
CellXCenter := CellX + (Boxsize div 2);
CellYCenter := CellY + (Boxsize div 2);
case Button of
mbLeft: begin
FieldBitmap.Canvas.Brush.Color := RGBToColor(225, 25, 25);
FieldBitmap.Canvas.Brush.Style := bsSolid;
FieldBitmap.Canvas.Pen.Color := RGBToColor(225, 25, 25);
FieldBitmap.Canvas.Pen.Style := psSolid;
FieldBitmap.Canvas.Ellipse(CellX + 2, CellY + 2, CellX + BoxSize - 2, CellY + BoxSize - 2);
end;
mbRight: begin
FieldBitmap.Canvas.Brush.Color := RGBToColor(25, 225, 25);
FieldBitmap.Canvas.Brush.Style := bsSolid;
FieldBitmap.Canvas.Pen.Color := RGBToColor(25, 225, 25);
FieldBitmap.Canvas.Pen.Style := psSolid;
FieldBitmap.Canvas.Ellipse(CellX + 2, CellY + 2, CellX + BoxSize - 2, CellY + BoxSize - 2);
end;
mbMiddle: begin
ThereWillBeBlood := True;
StartCaptureAnimation;
end;
end;
PaintBox1.Repaint;
end;
procedure TForm1.StartCaptureAnimation;
begin
AnimationFrame := 0;
Timer1.Interval := 50; // slower
//Timer1.Interval := 15; // faster
Timer1.Enabled := True;
end;
procedure TForm1.ClearAnimationBitmap;
// -----------------------------------
// clearing AnimationBitmap.Canvas
// -----------------------------------
begin
// -- method 1 -- works strange
//AnimationBitmap.TransparentColor := clFuchsia;
//AnimationBitmap.Canvas.Brush.Color := clFuchsia;
//AnimationBitmap.Canvas.FillRect(0, 0, FieldWidth, FieldHeight);
// -- method 2 - works not perfect
//AnimationBitmap.Clear;
//AnimationBitmap.SetSize(FieldWidth, FieldHeight);
// -- method 3 - works not perfect
AnimationBitmap.Canvas.Brush.Color := clNone;
//AnimationBitmap.Canvas.Brush.Color := clFuchsia; // clFuchsia instead of clNone - for clarity
AnimationBitmap.Canvas.FillRect(0, 0, FieldWidth, FieldHeight);
end;
procedure TForm1.StopCaptureAnimation;
begin
Timer1.Enabled := False;
ClearAnimationBitmap;
PaintBox1.Invalidate;
ThereWillBeBlood := False;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
i, x, y, size: Integer;
begin
AnimationFrame := AnimationFrame + 1;
ClearAnimationBitmap;
AnimationBitmap.Canvas.Brush.Color := RGBToColor(150, 0, 0);
AnimationBitmap.Canvas.Pen.Style := psClear;
AnimationBitmap.Canvas.Brush.Style := bsSolid;
AnimationBitmap.Canvas.Ellipse(CellXCenter - AnimationFrame * 4, CellYCenter - AnimationFrame * 4,
CellXCenter + AnimationFrame * 4, CellYCenter + AnimationFrame * 4);
Randomize;
for i := 0 to 10 do
begin
x := CellXCenter + Random(AnimationFrame * 15) - (AnimationFrame * 7);
y := CellYCenter + Random(AnimationFrame * 15) - (AnimationFrame * 7);
size := Random(8) + 3;
AnimationBitmap.Canvas.Ellipse(x - size, y - size, x + size, y + size);
end;
PaintBox1.Invalidate;
if AnimationFrame > 10 then
StopCaptureAnimation;
end;
end.