bcgamegrid.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: LGPL-3.0-linking-exception
  2. {
  3. Created by BGRA Controls Team
  4. Dibo, Circular, lainz (007) and contributors.
  5. For detailed information see readme.txt
  6. Site: https://sourceforge.net/p/bgra-controls/
  7. Wiki: http://wiki.lazarus.freepascal.org/BGRAControls
  8. Forum: http://forum.lazarus.freepascal.org/index.php/board,46.0.html
  9. }
  10. {******************************* CONTRIBUTOR(S) ******************************
  11. - Edivando S. Santos Brasil | [email protected]
  12. (Compatibility with delphi VCL 11/2018)
  13. ***************************** END CONTRIBUTOR(S) *****************************}
  14. unit BCGameGrid;
  15. {$I bgracontrols.inc}
  16. interface
  17. uses
  18. Classes, SysUtils, {$IFDEF FPC}LResources, LCLProc,{$ENDIF} Types, Forms, Controls, Graphics, Dialogs,
  19. {$IFNDEF FPC}BGRAGraphics, GraphType, FPImage, {$ENDIF}
  20. BCBaseCtrls, BGRABitmap, BGRABitmapTypes;
  21. type
  22. TOnRenderControl = procedure(Sender: TObject; Bitmap: TBGRABitmap;
  23. r: TRect; n, x, y: integer) of object;
  24. TOnClickControl = procedure(Sender: TObject; n, x, y: integer) of object;
  25. { TBCCustomGrid }
  26. TBCCustomGrid = class(TBCGraphicControl)
  27. private
  28. FBGRA: TBGRABitmap;
  29. FGridWidth: integer;
  30. FGridHeight: integer;
  31. FBlockWidth: integer;
  32. FBlockHeight: integer;
  33. FOnRenderControl: TOnRenderControl;
  34. FOnClickControl: TOnClickControl;
  35. private
  36. procedure SetFBlockHeight(AValue: integer);
  37. procedure SetFBlockWidth(AValue: integer);
  38. procedure SetFGridHeight(AValue: integer);
  39. procedure SetFGridWidth(AValue: integer);
  40. { Private declarations }
  41. protected
  42. { Protected declarations }
  43. procedure Click; override;
  44. procedure DrawControl; override;
  45. procedure RenderControl; override;
  46. public
  47. { Public declarations }
  48. constructor Create(AOwner: TComponent); override;
  49. destructor Destroy; override;
  50. procedure RenderAndDrawControl;
  51. property GridWidth: integer read FGridWidth write SetFGridWidth;
  52. property GridHeight: integer read FGridHeight write SetFGridHeight;
  53. property BlockWidth: integer read FBlockWidth write SetFBlockWidth;
  54. property BlockHeight: integer read FBlockHeight write SetFBlockHeight;
  55. property OnRenderControl: TOnRenderControl
  56. read FOnRenderControl write FOnRenderControl;
  57. property OnClickControl: TOnClickControl read FOnClickControl write FOnClickControl;
  58. published
  59. { Published declarations }
  60. end;
  61. TBCGameGrid = class(TBCCustomGrid)
  62. published
  63. property GridWidth;
  64. property GridHeight;
  65. property BlockWidth;
  66. property BlockHeight;
  67. // Support 'n, x, y'
  68. property OnRenderControl;
  69. property OnClickControl;
  70. // 'Classic' events, to be changed...
  71. property OnMouseDown;
  72. property OnMouseMove;
  73. property OnMouseUp;
  74. // Ok...
  75. property OnMouseEnter;
  76. property OnMouseLeave;
  77. property OnMouseWheel;
  78. property OnMouseWheelDown;
  79. property OnMouseWheelUp;
  80. end;
  81. {$IFDEF FPC}procedure Register;{$ENDIF}
  82. implementation
  83. {$IFDEF FPC}
  84. procedure Register;
  85. begin
  86. RegisterComponents('BGRA Controls', [TBCGameGrid]);
  87. end;
  88. {$ENDIF}
  89. { TBCCustomGrid }
  90. procedure TBCCustomGrid.SetFBlockHeight(AValue: integer);
  91. begin
  92. if FBlockHeight = AValue then
  93. Exit;
  94. if AValue < 1 then
  95. FBlockHeight := 1
  96. else
  97. FBlockHeight := AValue;
  98. RenderAndDrawControl;
  99. end;
  100. procedure TBCCustomGrid.SetFBlockWidth(AValue: integer);
  101. begin
  102. if FBlockWidth = AValue then
  103. Exit;
  104. if AValue < 1 then
  105. FBlockWidth := 1
  106. else
  107. FBlockWidth := AValue;
  108. RenderAndDrawControl;
  109. end;
  110. procedure TBCCustomGrid.SetFGridHeight(AValue: integer);
  111. begin
  112. if FGridHeight = AValue then
  113. Exit;
  114. if AValue < 1 then
  115. FGridHeight := 1
  116. else
  117. FGridHeight := AValue;
  118. RenderAndDrawControl;
  119. end;
  120. procedure TBCCustomGrid.SetFGridWidth(AValue: integer);
  121. begin
  122. if FGridWidth = AValue then
  123. Exit;
  124. if AValue < 1 then
  125. FGridWidth := 1
  126. else
  127. FGridWidth := AValue;
  128. RenderAndDrawControl;
  129. end;
  130. procedure TBCCustomGrid.Click;
  131. var
  132. n, x, y: integer;
  133. r: TRect;
  134. var
  135. pos: TPoint;
  136. begin
  137. if (BlockWidth <= 0) or (BlockHeight <= 0) or (GridWidth <= 0) or
  138. (GridHeight <= 0) then
  139. Exit;
  140. pos := ScreenToClient(Mouse.CursorPos);
  141. n := 0;
  142. for y := 0 to GridHeight - 1 do
  143. begin
  144. for x := 0 to GridWidth - 1 do
  145. begin
  146. r.Left := BlockWidth * x;
  147. r.Top := BlockHeight * y;
  148. r.Right := r.Left + BlockWidth;
  149. r.Bottom := r.Top + BlockHeight;
  150. if (pos.x >= r.Left) and (pos.x <= r.Right) and (pos.y >= r.Top) and
  151. (pos.y <= r.Bottom) then
  152. begin
  153. //DebugLn(['TControl.Click ',DbgSName(Self)]);
  154. if Assigned(FOnClickControl) then
  155. FOnClickControl(Self, n, x, y);
  156. if (not (csDesigning in ComponentState)) and (ActionLink <> nil) then
  157. ActionLink.Execute(Self)
  158. end;
  159. Inc(n);
  160. end;
  161. end;
  162. end;
  163. procedure TBCCustomGrid.DrawControl;
  164. begin
  165. if FBGRA <> nil then
  166. FBGRA.Draw(Canvas, 0, 0, False);
  167. end;
  168. procedure TBCCustomGrid.RenderControl;
  169. var
  170. n, x, y: integer;
  171. r: TRect;
  172. begin
  173. if (BlockWidth <= 0) or (BlockHeight <= 0) or (GridWidth <= 0) or
  174. (GridHeight <= 0) then
  175. Exit;
  176. if FBGRA <> nil then
  177. FreeAndNil(FBGRA);
  178. FBGRA := TBGRABitmap.Create(Width, Height);
  179. n := 0;
  180. for y := 0 to GridHeight - 1 do
  181. begin
  182. for x := 0 to GridWidth - 1 do
  183. begin
  184. r.Left := BlockWidth * x;
  185. r.Top := BlockHeight * y;
  186. r.Right := r.Left + BlockWidth;
  187. r.Bottom := r.Top + BlockHeight;
  188. FBGRA.Rectangle(r, BGRA(127, 127, 127, 127), BGRA(255, 255, 255, 127),
  189. dmDrawWithTransparency);
  190. if Assigned(FOnRenderControl) then
  191. FOnRenderControl(Self, FBGRA, r, n, x, y);
  192. Inc(n);
  193. end;
  194. end;
  195. end;
  196. procedure TBCCustomGrid.RenderAndDrawControl;
  197. begin
  198. RenderControl;
  199. Invalidate;
  200. end;
  201. constructor TBCCustomGrid.Create(AOwner: TComponent);
  202. begin
  203. inherited Create(AOwner);
  204. with GetControlClassDefaultSize do
  205. SetInitialBounds(0, 0, CX, CY);
  206. BlockHeight := 30;
  207. BlockWidth := 30;
  208. GridHeight := 5;
  209. GridWidth := 5;
  210. end;
  211. destructor TBCCustomGrid.Destroy;
  212. begin
  213. if FBGRA <> nil then
  214. FreeAndNil(FBGRA);
  215. inherited Destroy;
  216. end;
  217. end.