-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
bcgamegrid.pas
259 lines (217 loc) · 6.29 KB
/
bcgamegrid.pas
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// SPDX-License-Identifier: LGPL-3.0-linking-exception
{
Created by BGRA Controls Team
Dibo, Circular, lainz (007) and contributors.
For detailed information see readme.txt
Site: https://sourceforge.net/p/bgra-controls/
Wiki: http://wiki.lazarus.freepascal.org/BGRAControls
Forum: http://forum.lazarus.freepascal.org/index.php/board,46.0.html
}
{******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
unit BCGameGrid;
{$I bgracontrols.inc}
interface
uses
Classes, SysUtils, {$IFDEF FPC}LResources, LCLProc,{$ENDIF} Types, Forms, Controls, Graphics, Dialogs,
{$IFNDEF FPC}BGRAGraphics, GraphType, FPImage, {$ENDIF}
BCBaseCtrls, BGRABitmap, BGRABitmapTypes;
type
TOnRenderControl = procedure(Sender: TObject; Bitmap: TBGRABitmap;
r: TRect; n, x, y: integer) of object;
TOnClickControl = procedure(Sender: TObject; n, x, y: integer) of object;
{ TBCCustomGrid }
TBCCustomGrid = class(TBCGraphicControl)
private
FBGRA: TBGRABitmap;
FGridWidth: integer;
FGridHeight: integer;
FBlockWidth: integer;
FBlockHeight: integer;
FOnRenderControl: TOnRenderControl;
FOnClickControl: TOnClickControl;
private
procedure SetFBlockHeight(AValue: integer);
procedure SetFBlockWidth(AValue: integer);
procedure SetFGridHeight(AValue: integer);
procedure SetFGridWidth(AValue: integer);
{ Private declarations }
protected
{ Protected declarations }
procedure Click; override;
procedure DrawControl; override;
procedure RenderControl; override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure RenderAndDrawControl;
property GridWidth: integer read FGridWidth write SetFGridWidth;
property GridHeight: integer read FGridHeight write SetFGridHeight;
property BlockWidth: integer read FBlockWidth write SetFBlockWidth;
property BlockHeight: integer read FBlockHeight write SetFBlockHeight;
property OnRenderControl: TOnRenderControl
read FOnRenderControl write FOnRenderControl;
property OnClickControl: TOnClickControl read FOnClickControl write FOnClickControl;
published
{ Published declarations }
end;
TBCGameGrid = class(TBCCustomGrid)
published
property GridWidth;
property GridHeight;
property BlockWidth;
property BlockHeight;
// Support 'n, x, y'
property OnRenderControl;
property OnClickControl;
// 'Classic' events, to be changed...
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
// Ok...
property OnMouseEnter;
property OnMouseLeave;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
end;
{$IFDEF FPC}procedure Register;{$ENDIF}
implementation
{$IFDEF FPC}
procedure Register;
begin
RegisterComponents('BGRA Controls', [TBCGameGrid]);
end;
{$ENDIF}
{ TBCCustomGrid }
procedure TBCCustomGrid.SetFBlockHeight(AValue: integer);
begin
if FBlockHeight = AValue then
Exit;
if AValue < 1 then
FBlockHeight := 1
else
FBlockHeight := AValue;
RenderAndDrawControl;
end;
procedure TBCCustomGrid.SetFBlockWidth(AValue: integer);
begin
if FBlockWidth = AValue then
Exit;
if AValue < 1 then
FBlockWidth := 1
else
FBlockWidth := AValue;
RenderAndDrawControl;
end;
procedure TBCCustomGrid.SetFGridHeight(AValue: integer);
begin
if FGridHeight = AValue then
Exit;
if AValue < 1 then
FGridHeight := 1
else
FGridHeight := AValue;
RenderAndDrawControl;
end;
procedure TBCCustomGrid.SetFGridWidth(AValue: integer);
begin
if FGridWidth = AValue then
Exit;
if AValue < 1 then
FGridWidth := 1
else
FGridWidth := AValue;
RenderAndDrawControl;
end;
procedure TBCCustomGrid.Click;
var
n, x, y: integer;
r: TRect;
var
pos: TPoint;
begin
if (BlockWidth <= 0) or (BlockHeight <= 0) or (GridWidth <= 0) or
(GridHeight <= 0) then
Exit;
pos := ScreenToClient(Mouse.CursorPos);
n := 0;
for y := 0 to GridHeight - 1 do
begin
for x := 0 to GridWidth - 1 do
begin
r.Left := BlockWidth * x;
r.Top := BlockHeight * y;
r.Right := r.Left + BlockWidth;
r.Bottom := r.Top + BlockHeight;
if (pos.x >= r.Left) and (pos.x <= r.Right) and (pos.y >= r.Top) and
(pos.y <= r.Bottom) then
begin
//DebugLn(['TControl.Click ',DbgSName(Self)]);
if Assigned(FOnClickControl) then
FOnClickControl(Self, n, x, y);
if (not (csDesigning in ComponentState)) and (ActionLink <> nil) then
ActionLink.Execute(Self)
end;
Inc(n);
end;
end;
end;
procedure TBCCustomGrid.DrawControl;
begin
if FBGRA <> nil then
FBGRA.Draw(Canvas, 0, 0, False);
end;
procedure TBCCustomGrid.RenderControl;
var
n, x, y: integer;
r: TRect;
begin
if (BlockWidth <= 0) or (BlockHeight <= 0) or (GridWidth <= 0) or
(GridHeight <= 0) then
Exit;
if FBGRA <> nil then
FreeAndNil(FBGRA);
FBGRA := TBGRABitmap.Create(Width, Height);
n := 0;
for y := 0 to GridHeight - 1 do
begin
for x := 0 to GridWidth - 1 do
begin
r.Left := BlockWidth * x;
r.Top := BlockHeight * y;
r.Right := r.Left + BlockWidth;
r.Bottom := r.Top + BlockHeight;
FBGRA.Rectangle(r, BGRA(127, 127, 127, 127), BGRA(255, 255, 255, 127),
dmDrawWithTransparency);
if Assigned(FOnRenderControl) then
FOnRenderControl(Self, FBGRA, r, n, x, y);
Inc(n);
end;
end;
end;
procedure TBCCustomGrid.RenderAndDrawControl;
begin
RenderControl;
Invalidate;
end;
constructor TBCCustomGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
with GetControlClassDefaultSize do
SetInitialBounds(0, 0, CX, CY);
BlockHeight := 30;
BlockWidth := 30;
GridHeight := 5;
GridWidth := 5;
end;
destructor TBCCustomGrid.Destroy;
begin
if FBGRA <> nil then
FreeAndNil(FBGRA);
inherited Destroy;
end;
end.