-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUBoardFrame.pas
More file actions
168 lines (128 loc) · 3.84 KB
/
UBoardFrame.pas
File metadata and controls
168 lines (128 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
unit UBoardFrame;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UDoubleBufferedFrame, Vcl.ExtCtrls, UMinesweeperDataController,
System.ImageList, Vcl.ImgList;
type
TBoardFrame = class(TDoubleBufferedFrame)
Timer1: TTimer;
ImageList_Fields: TImageList;
procedure Timer1Timer(Sender: TObject);
procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure PaintBox2MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
fController: TMineSweeperDataController;
fFocusedX: Integer;
fFocusedY: Integer;
function GetFieldSize(): Integer;
protected
procedure CanvasPaint(aCanvas: TCanvas); override;
public
{ Public declarations }
function GetPreferredWidth(): Integer;
function GetPreferredHeight(): Integer;
property Controller: TMineSweeperDataController read fController write fController;
end;
var
BoardFrame: TBoardFrame;
implementation
uses
System.Math;
{$R *.dfm}
procedure TBoardFrame.CanvasPaint(aCanvas: TCanvas);
begin
inherited;
aCanvas.Brush.Color := clBlack;
aCanvas.FillRect(aCanvas.ClipRect);
if not Assigned(Controller) then
Exit();
for var y := 0 to Controller.GetHeight() - 1 do
for var x := 0 to Controller.GetWidth() - 1 do
begin
var canvasY := y * GetFieldSize();
var canvasX := x * GetFieldSize();
var field := Controller.GetFieldAt(x, y);
var imageIndex := 0;
if not field.IsRevealed then
imageIndex := IfThen(field.IsFlagged, 1, 0)
else
begin
if field.HasMine then
begin
if field.IsExplosionSource then
imageIndex := 12
else if field.IsFlagged then
imageIndex := 13
else
imageIndex := 11;
end
else
imageIndex := field.GetNumberOfMinesAround() + 2;
end;
ImageList_Fields.Draw(aCanvas, canvasX, canvasY, imageIndex);
end;
aCanvas.Brush.Color := clWhite;
aCanvas.FrameRect(TRect.Create(
fFocusedX*GetFieldSize(),
fFocusedY*GetFieldSize(),
(fFocusedX+1)*GetFieldSize(),
(fFocusedY+1)*GetFieldSize()
));
end;
function TBoardFrame.GetFieldSize: Integer;
begin
result := ImageList_Fields.Width;
end;
function TBoardFrame.GetPreferredHeight: Integer;
begin
result := GetFieldSize() * Controller.GetHeight();
end;
function TBoardFrame.GetPreferredWidth: Integer;
begin
result := GetFieldSize() * Controller.GetWidth();
end;
procedure TBoardFrame.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
fFocusedX := X div GetFieldSize();
fFocusedY := Y div GetFieldSize();
end;
procedure TBoardFrame.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
inherited;
var fieldX := X div GetFieldSize();
var fieldY := Y div GetFieldSize();
if not Controller.IsInBounds(fieldX, fieldY) then
Exit();
var field := Controller.GetFieldAt(fieldX, fieldY);
if Button = mbLeft then
field.OnLeftClicked();
end;
procedure TBoardFrame.PaintBox2MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
inherited;
var fieldX := X div GetFieldSize();
var fieldY := Y div GetFieldSize();
if not Controller.IsInBounds(fieldX, fieldY) then
Exit();
var field := Controller.GetFieldAt(fieldX, fieldY);
if Button = mbLeft then
field.OnLeftClicking();
if Button = mbRight then
field.OnRightClicked();
end;
procedure TBoardFrame.Timer1Timer(Sender: TObject);
begin
inherited;
Redraw();
end;
end.