MainUnit.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. unit MainUnit;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Polygons Example
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Alex A. Denisov
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2010
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. * Christian-W. Budde
  32. *
  33. * ***** END LICENSE BLOCK ***** *)
  34. interface
  35. {$I GR32.inc}
  36. uses
  37. {$IFNDEF FPC} Windows, {$ELSE} LCLIntf, LResources, LCLType, Buttons, {$ENDIF}
  38. SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls,
  39. GR32, GR32_Image, GR32_Layers, GR32_Polygons, GR32_Paths, GR32_Brushes;
  40. type
  41. TFormPolygons = class(TForm)
  42. BitmapList: TBitmap32List;
  43. BtnNewLine: TButton;
  44. CbxPattern: TCheckBox;
  45. CbxThickOutline: TCheckBox;
  46. FillAlpha: TTrackBar;
  47. Image: TImage32;
  48. LblFillOpacity: TLabel;
  49. LblLineOpacity: TLabel;
  50. LabelMiterLimit: TLabel;
  51. LabelOutlineThickness: TLabel;
  52. LineAlpha: TTrackBar;
  53. LineThickness: TTrackBar;
  54. MemoHint: TMemo;
  55. MiterLimit: TTrackBar;
  56. PanelControl: TPanel;
  57. RgpFillMode: TRadioGroup;
  58. RgpJointMode: TRadioGroup;
  59. procedure FormCreate(Sender: TObject);
  60. procedure FormDestroy(Sender: TObject);
  61. procedure ImageMouseDown(Sender: TObject; Button: TMouseButton;
  62. Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
  63. procedure BtnNewLineClick(Sender: TObject);
  64. procedure FillAlphaChange(Sender: TObject);
  65. procedure FillModeChange(Sender: TObject);
  66. procedure ImageResize(Sender: TObject);
  67. procedure JointModeChange(Sender: TObject);
  68. procedure LineAlphaChange(Sender: TObject);
  69. procedure MiterLimitChange(Sender: TObject);
  70. procedure ParamsChanged(Sender: TObject);
  71. procedure PatternFillingChange(Sender: TObject);
  72. procedure ThicknessChanged(Sender: TObject);
  73. procedure ThickOutlineChange(Sender: TObject);
  74. private
  75. FCanvas: TCanvas32;
  76. FFiller: TBitmapPolygonFiller;
  77. FPoints: array of array of TPoint;
  78. FSolid: TSolidBrush;
  79. FStroke: TStrokeBrush;
  80. procedure Draw;
  81. end;
  82. var
  83. FormPolygons: TFormPolygons;
  84. implementation
  85. {$R *.dfm}
  86. uses
  87. {$IFDEF Darwin}
  88. MacOSAll,
  89. {$ENDIF}
  90. Math,
  91. Types,
  92. GR32.ImageFormats.JPG;
  93. { TFormPolygons }
  94. procedure TFormPolygons.FormCreate(Sender: TObject);
  95. begin
  96. // Load the textures (note size 256x256 is implicity expected!)
  97. BitmapList.Bitmap[0].LoadFromResourceName(HInstance, 'Delphi', RT_RCDATA);
  98. BitmapList.Bitmap[1].LoadFromResourceName(HInstance, 'TextureB', RT_RCDATA);
  99. Image.SetupBitmap;
  100. SetLength(FPoints, 1);
  101. SetLength(FPoints[0], 0);
  102. FCanvas := TCanvas32.Create(Image.Bitmap);
  103. FCanvas.Brushes.Add(TSolidBrush);
  104. FSolid := TSolidBrush(FCanvas.Brushes[0]);
  105. FSolid.FillColor := SetAlpha(clGreen32, FillAlpha.Position);
  106. FCanvas.Brushes.Add(TStrokeBrush);
  107. FStroke := TStrokeBrush(FCanvas.Brushes[1]);
  108. FStroke.FillColor := SetAlpha(clBlack32, LineAlpha.Position);
  109. FStroke.StrokeWidth := 1;
  110. FStroke.Visible := False;
  111. ThickOutlineChange(Self);
  112. {$ifndef FPC}
  113. LineAlpha.PositionToolTip := ptTop;
  114. LineThickness.PositionToolTip := ptTop;
  115. {$endif}
  116. end;
  117. procedure TFormPolygons.FormDestroy(Sender: TObject);
  118. begin
  119. FCanvas.Free;
  120. if Assigned(FFiller) then
  121. FFiller.Free;
  122. end;
  123. procedure TFormPolygons.Draw;
  124. var
  125. Index, PointIndex: Integer;
  126. begin
  127. Image.Bitmap.BeginUpdate;
  128. try
  129. Image.Bitmap.Clear(clWhite32);
  130. Image.Bitmap.Draw(50, 50, BitmapList.Bitmap[0]);
  131. for Index := 0 to Length(FPoints) - 1 do
  132. begin
  133. if Length(FPoints[Index]) = 0 then
  134. Continue;
  135. FCanvas.MoveTo(FPoints[Index, 0].X, FPoints[Index, 0].Y);
  136. for PointIndex := 1 to Length(FPoints[Index]) - 1 do
  137. FCanvas.LineTo(FPoints[Index, PointIndex].X, FPoints[Index, PointIndex].Y);
  138. FCanvas.EndPath(True);
  139. end;
  140. finally
  141. Image.Bitmap.EndUpdate;
  142. end;
  143. Image.Bitmap.Changed;
  144. Image.Refresh; // force repaint
  145. end;
  146. procedure TFormPolygons.ImageMouseDown(Sender: TObject; Button: TMouseButton;
  147. Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
  148. var
  149. Index: Integer;
  150. PointIndex: Integer;
  151. begin
  152. if Button = mbLeft then
  153. begin
  154. Index := Length(FPoints) - 1;
  155. PointIndex := Length(FPoints[Index]);
  156. SetLength(FPoints[Index], PointIndex + 1);
  157. FPoints[Index, PointIndex].X := X;
  158. FPoints[Index, PointIndex].Y := Y;
  159. end
  160. else
  161. begin
  162. SetLength(FPoints, 1);
  163. SetLength(FPoints[0], 0);
  164. end;
  165. Draw;
  166. end;
  167. procedure TFormPolygons.ImageResize(Sender: TObject);
  168. begin
  169. Image.SetupBitmap;
  170. Draw;
  171. end;
  172. procedure TFormPolygons.LineAlphaChange(Sender: TObject);
  173. begin
  174. FStroke.FillColor := SetAlpha(clBlack32, LineAlpha.Position);
  175. Draw;
  176. end;
  177. procedure TFormPolygons.MiterLimitChange(Sender: TObject);
  178. begin
  179. FStroke.MiterLimit := MiterLimit.Position * 0.01;
  180. LabelMiterLimit.Caption := Format(LabelMiterLimit.Hint, [FStroke.MiterLimit]);
  181. Draw;
  182. end;
  183. procedure TFormPolygons.ParamsChanged(Sender: TObject);
  184. begin
  185. Draw;
  186. end;
  187. procedure TFormPolygons.FillAlphaChange(Sender: TObject);
  188. begin
  189. FSolid.FillColor := SetAlpha(clGreen32, FillAlpha.Position);
  190. BitmapList.Bitmap[1].MasterAlpha := FillAlpha.Position;
  191. Draw;
  192. end;
  193. procedure TFormPolygons.FillModeChange(Sender: TObject);
  194. begin
  195. FStroke.FillMode := TPolyFillMode(RgpFillMode.ItemIndex);
  196. FSolid.FillMode := TPolyFillMode(RgpFillMode.ItemIndex);
  197. Draw;
  198. end;
  199. procedure TFormPolygons.PatternFillingChange(Sender: TObject);
  200. begin
  201. if CbxPattern.Checked then
  202. begin
  203. BitmapList.Bitmap[1].MasterAlpha := FillAlpha.Position;
  204. BitmapList.Bitmap[1].DrawMode := dmBlend;
  205. FFiller := TBitmapPolygonFiller.Create;
  206. FFiller.Pattern := BitmapList.Bitmap[1];
  207. FCanvas.Renderer.Filler := FFiller;
  208. FSolid.Filler := FFiller;
  209. end
  210. else
  211. begin
  212. FCanvas.Renderer.Filler := nil;
  213. FSolid.Filler := nil;
  214. FreeAndNil(FFiller);
  215. end;
  216. Draw;
  217. end;
  218. procedure TFormPolygons.JointModeChange(Sender: TObject);
  219. begin
  220. FStroke.JoinStyle := TJoinStyle(RgpJointMode.ItemIndex);
  221. MiterLimit.Enabled := CbxThickOutline.Checked and
  222. (FStroke.JoinStyle in [jsMiter, jsRoundEx]);
  223. Draw;
  224. end;
  225. procedure TFormPolygons.BtnNewLineClick(Sender: TObject);
  226. begin
  227. SetLength(FPoints, Length(FPoints) + 1);
  228. end;
  229. procedure TFormPolygons.ThicknessChanged(Sender: TObject);
  230. begin
  231. FStroke.StrokeWidth := LineThickness.Position * 0.1;
  232. LabelOutlineThickness.Caption := Format(LabelOutlineThickness.Hint, [FStroke.StrokeWidth]);
  233. Draw;
  234. end;
  235. procedure TFormPolygons.ThickOutlineChange(Sender: TObject);
  236. begin
  237. FStroke.Visible := CbxThickOutline.Checked;
  238. LineThickness.Enabled := CbxThickOutline.Checked;
  239. RgpJointMode.Enabled := CbxThickOutline.Checked;
  240. MiterLimit.Enabled := CbxThickOutline.Checked and (FStroke.JoinStyle = jsMiter);
  241. Draw;
  242. end;
  243. end.