GR32.Paint.Tool.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. unit GR32.Paint.Tool;
  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 Paint tools for Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Anders Melander, [email protected]
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2008-2025
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * ***** END LICENSE BLOCK ***** *)
  31. interface
  32. {$INCLUDE GR32.inc}
  33. uses
  34. Classes,
  35. Controls,
  36. GR32,
  37. GR32.Paint.Host.API,
  38. GR32.Paint.Tool.API;
  39. //------------------------------------------------------------------------------
  40. //
  41. // TCustomBitmap32PaintTool
  42. //
  43. //------------------------------------------------------------------------------
  44. // IBitmap32PaintTool reference implementation
  45. //------------------------------------------------------------------------------
  46. type
  47. TCustomBitmap32PaintTool = class(TInterfacedObject, IBitmap32PaintExtension, IBitmap32PaintTool)
  48. strict private
  49. FPaintHost: IBitmap32PaintHost;
  50. FSettingValues: ISettingValues;
  51. FActive: boolean;
  52. strict protected
  53. // IBitmap32PaintExtension,
  54. function GetIsVisible: boolean; virtual;
  55. function GetIsEnabled: boolean; virtual;
  56. function GetCaption: string; virtual;
  57. function GetHint: string; virtual;
  58. function GetDescription: string; virtual;
  59. function GetAttribution: string; virtual;
  60. procedure Clear; virtual;
  61. procedure Reset; virtual;
  62. property IsVisible: boolean read GetIsVisible;
  63. property IsEnabled: boolean read GetIsEnabled;
  64. property Caption: string read GetCaption;
  65. property Hint: string read GetHint;
  66. property Description: string read GetDescription;
  67. property Attribution: string read GetAttribution;
  68. strict protected
  69. // IBitmap32PaintTool
  70. procedure Activate(var Continue: boolean); virtual;
  71. procedure Deactivate; virtual;
  72. procedure BeginTool(var Continue: boolean); virtual;
  73. procedure EndTool; virtual;
  74. procedure MouseDown(const Context: IBitmap32PaintToolContext; Button: TMouseButton); virtual;
  75. procedure MouseMove(const Context: IBitmap32PaintToolContext); virtual;
  76. procedure MouseUp(const Context: IBitmap32PaintToolContext; Button: TMouseButton); virtual;
  77. procedure MouseEnter; virtual;
  78. procedure MouseLeave; virtual;
  79. procedure KeyDown(var Key: Word; Shift: TShiftState); virtual;
  80. procedure KeyUp(var Key: Word; Shift: TShiftState); virtual;
  81. procedure BeginAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState); virtual;
  82. procedure ContinueAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState); virtual;
  83. procedure EndAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState); virtual;
  84. procedure CallbackAction(Buffer: TBitmap32; Data: pointer; var Result: boolean); virtual;
  85. function GetCursor(out Cursor: TCursor): boolean; virtual;
  86. function GetSnapMouse: boolean; virtual;
  87. property SnapMouse: boolean read GetSnapMouse;
  88. function GetToolFeatures: TBitmap32PaintToolFeatures; virtual;
  89. strict protected
  90. procedure SnapToSquare(const StartPos: TPoint; var Pos: TPoint);
  91. public
  92. constructor Create(const APaintHost: IBitmap32PaintHost); virtual;
  93. destructor Destroy; override;
  94. function ActiveColor(Shift: TShiftState; Invert: boolean = False): TColor32; overload;
  95. function ActiveColor(PrimaryColor, SecondaryColor: TColor32; Shift: TShiftState; Invert: boolean = False): TColor32; overload; virtual;
  96. property PaintHost: IBitmap32PaintHost read FPaintHost;
  97. property Active: boolean read FActive;
  98. end;
  99. TBitmap32PaintToolClass = class of TCustomBitmap32PaintTool;
  100. //------------------------------------------------------------------------------
  101. //------------------------------------------------------------------------------
  102. //------------------------------------------------------------------------------
  103. implementation
  104. uses
  105. Math, SysUtils;
  106. //------------------------------------------------------------------------------
  107. //
  108. // TCustomBitmap32PaintTool
  109. //
  110. //------------------------------------------------------------------------------
  111. constructor TCustomBitmap32PaintTool.Create(const APaintHost: IBitmap32PaintHost);
  112. begin
  113. inherited Create;
  114. FPaintHost := APaintHost;
  115. FSettingValues := FPaintHost.GetToolSettings('');
  116. end;
  117. destructor TCustomBitmap32PaintTool.Destroy;
  118. begin
  119. FPaintHost := nil;
  120. inherited Destroy;
  121. end;
  122. //------------------------------------------------------------------------------
  123. procedure TCustomBitmap32PaintTool.BeginTool(var Continue: boolean);
  124. begin
  125. Continue := True;
  126. FActive := True;
  127. end;
  128. procedure TCustomBitmap32PaintTool.EndTool;
  129. begin
  130. FActive := False;
  131. end;
  132. //------------------------------------------------------------------------------
  133. procedure TCustomBitmap32PaintTool.Activate(var Continue: boolean);
  134. begin
  135. end;
  136. procedure TCustomBitmap32PaintTool.Deactivate;
  137. begin
  138. end;
  139. //------------------------------------------------------------------------------
  140. function TCustomBitmap32PaintTool.ActiveColor(PrimaryColor, SecondaryColor: TColor32; Shift: TShiftState; Invert: boolean): TColor32;
  141. begin
  142. if ((ssRight in Shift) xor Invert) then
  143. Result := SecondaryColor
  144. else
  145. Result := PrimaryColor;
  146. end;
  147. function TCustomBitmap32PaintTool.ActiveColor(Shift: TShiftState; Invert: boolean): TColor32;
  148. begin
  149. Result := ActiveColor(PaintHost.ColorPrimary, PaintHost.ColorSecondary, Shift, Invert);
  150. end;
  151. //------------------------------------------------------------------------------
  152. procedure TCustomBitmap32PaintTool.CallbackAction(Buffer: TBitmap32;
  153. Data: pointer; var Result: boolean);
  154. begin
  155. Result := False;
  156. end;
  157. //------------------------------------------------------------------------------
  158. procedure TCustomBitmap32PaintTool.Clear;
  159. begin
  160. end;
  161. //------------------------------------------------------------------------------
  162. function TCustomBitmap32PaintTool.GetAttribution: string;
  163. begin
  164. Result := '';
  165. end;
  166. //------------------------------------------------------------------------------
  167. function TCustomBitmap32PaintTool.GetCaption: string;
  168. begin
  169. Result := '';
  170. end;
  171. //------------------------------------------------------------------------------
  172. function TCustomBitmap32PaintTool.GetHint: string;
  173. begin
  174. Result := Caption;
  175. end;
  176. //------------------------------------------------------------------------------
  177. function TCustomBitmap32PaintTool.GetIsEnabled: boolean;
  178. begin
  179. Result := True;
  180. end;
  181. //------------------------------------------------------------------------------
  182. function TCustomBitmap32PaintTool.GetIsVisible: boolean;
  183. begin
  184. Result := True;
  185. end;
  186. //------------------------------------------------------------------------------
  187. function TCustomBitmap32PaintTool.GetSnapMouse: boolean;
  188. begin
  189. Result := False;
  190. end;
  191. //------------------------------------------------------------------------------
  192. function TCustomBitmap32PaintTool.GetToolFeatures: TBitmap32PaintToolFeatures;
  193. begin
  194. Result := [];
  195. end;
  196. //------------------------------------------------------------------------------
  197. procedure TCustomBitmap32PaintTool.KeyDown(var Key: Word; Shift: TShiftState);
  198. begin
  199. end;
  200. procedure TCustomBitmap32PaintTool.KeyUp(var Key: Word; Shift: TShiftState);
  201. begin
  202. end;
  203. //------------------------------------------------------------------------------
  204. function TCustomBitmap32PaintTool.GetCursor(out Cursor: TCursor): boolean;
  205. begin
  206. Cursor := crCross;
  207. Result := True;
  208. end;
  209. //------------------------------------------------------------------------------
  210. function TCustomBitmap32PaintTool.GetDescription: string;
  211. begin
  212. Result := '';
  213. end;
  214. //------------------------------------------------------------------------------
  215. procedure TCustomBitmap32PaintTool.MouseDown(const Context: IBitmap32PaintToolContext; Button: TMouseButton);
  216. begin
  217. end;
  218. procedure TCustomBitmap32PaintTool.MouseMove(const Context: IBitmap32PaintToolContext);
  219. begin
  220. end;
  221. procedure TCustomBitmap32PaintTool.MouseUp(const Context: IBitmap32PaintToolContext; Button: TMouseButton);
  222. begin
  223. end;
  224. //------------------------------------------------------------------------------
  225. procedure TCustomBitmap32PaintTool.MouseEnter;
  226. begin
  227. end;
  228. procedure TCustomBitmap32PaintTool.MouseLeave;
  229. begin
  230. end;
  231. //------------------------------------------------------------------------------
  232. procedure TCustomBitmap32PaintTool.BeginAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState);
  233. begin
  234. end;
  235. procedure TCustomBitmap32PaintTool.ContinueAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState);
  236. begin
  237. end;
  238. procedure TCustomBitmap32PaintTool.EndAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState);
  239. begin
  240. end;
  241. //------------------------------------------------------------------------------
  242. procedure TCustomBitmap32PaintTool.Reset;
  243. begin
  244. end;
  245. //------------------------------------------------------------------------------
  246. procedure TCustomBitmap32PaintTool.SnapToSquare(const StartPos: TPoint; var Pos: TPoint);
  247. function Sign(Value: integer): integer; inline;
  248. begin
  249. if (Value < 0) then
  250. Result := -1
  251. else
  252. Result := 1;
  253. end;
  254. var
  255. dx, dy: integer;
  256. begin
  257. dx := Pos.X-StartPos.X;
  258. dy := Pos.Y-StartPos.Y;
  259. if (Abs(dx) > Abs(dy)) then
  260. Pos.Y := StartPos.Y+Sign(dy)*Abs(dx)
  261. else
  262. if (Abs(dx) < Abs(dy)) then
  263. Pos.X := StartPos.X+Sign(dx)*Abs(dy);
  264. end;
  265. //------------------------------------------------------------------------------
  266. end.