GR32.Paint.Tool.API.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. unit GR32.Paint.Tool.API;
  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. //------------------------------------------------------------------------------
  38. //
  39. // IBitmap32PaintExtension
  40. //
  41. //------------------------------------------------------------------------------
  42. // Base interface for bitmap tools (tools, actions, filters, color pickers, etc)
  43. //------------------------------------------------------------------------------
  44. type
  45. IBitmap32PaintExtension = interface
  46. ['{17AF7811-406F-4C58-906C-C3E6E3FC67D2}']
  47. function GetIsVisible: boolean;
  48. function GetIsEnabled: boolean;
  49. function GetCaption: string;
  50. function GetHint: string;
  51. function GetDescription: string;
  52. function GetAttribution: string;
  53. // Clear instructs the extension to delete all allocated resources.
  54. procedure Clear;
  55. // Reset instructs the extension to reset its setting to their default values.
  56. procedure Reset;
  57. property IsVisible: boolean read GetIsVisible;
  58. property IsEnabled: boolean read GetIsEnabled;
  59. property Caption: string read GetCaption;
  60. property Hint: string read GetHint;
  61. property Description: string read GetDescription;
  62. property Attribution: string read GetAttribution;
  63. end;
  64. //------------------------------------------------------------------------------
  65. type
  66. // TBitmap32PaintToolFeatures:
  67. TBitmap32PaintToolFeature = (
  68. betfMouseCapture // Tool captures the mouse during operation.
  69. // If this flag is not set then the framework will reset
  70. // mouse capture to the image control during operation.
  71. );
  72. TBitmap32PaintToolFeatures = set of TBitmap32PaintToolFeature;
  73. // TBitmap32PaintToolState:
  74. //
  75. // - tsContinue Tool is performing the requested operation
  76. //
  77. // - tsAbort Tool has rejected the requested operation
  78. //
  79. // - tsComplete Tool has completed the requested operation
  80. //
  81. TBitmap32PaintToolState = (tsContinue, tsAbort, tsComplete);
  82. //------------------------------------------------------------------------------
  83. //
  84. // Paint tool mouse event parameters
  85. //
  86. //------------------------------------------------------------------------------
  87. type
  88. TBitmap32PaintToolMouseParams = record
  89. MouseMessageTime: Cardinal; // Timestamp
  90. ScreenPos: TPoint; // Screen coordinates
  91. ViewPortPos: TPoint; // Viewport coordinates
  92. BitmapPos: TPoint; // Bitmap coordinates, rounded down
  93. BitmapPosSnap: TPoint; // Bitmap coordinates, snapped to nearest.
  94. BitmapPosFloat: TFloatPoint; // Fractional bitmap coordinates
  95. ShiftState: TShiftState; // Mouse/keyboard shift state
  96. end;
  97. PBitmap32PaintToolMouseParams = ^TBitmap32PaintToolMouseParams;
  98. type
  99. IBitmap32PaintTool = interface;
  100. IBitmap32PaintToolContext = interface
  101. ['{BE0ECC3B-F27B-4689-A7A8-55958EA4047C}']
  102. function GetPaintTool: IBitmap32PaintTool;
  103. property PaintTool: IBitmap32PaintTool read GetPaintTool;
  104. function GetBuffer: TBitmap32;
  105. property Buffer: TBitmap32 read GetBuffer;
  106. function GetMouseParams: PBitmap32PaintToolMouseParams;
  107. // MouseParams is a pointer to avoid copy-on-read overhead.
  108. // The value is static and is valid for the lifetime of the context object.
  109. property MouseParams: PBitmap32PaintToolMouseParams read GetMouseParams;
  110. procedure Update(const ViewPortPos: TPoint; SnapMouse: boolean);
  111. end;
  112. //------------------------------------------------------------------------------
  113. //
  114. // IBitmap32PaintTool
  115. //
  116. //------------------------------------------------------------------------------
  117. // A mouse-operated drawing tool. E.g. Pen, Brush, Line, Circle, Selection, etc.
  118. //------------------------------------------------------------------------------
  119. IBitmap32PaintTool = interface(IBitmap32PaintExtension)
  120. ['{712D1D8A-5C4B-43C7-A16A-8D8BBA9A6818}']
  121. /// <summary>Activate is called when the tool is selected.</summary>
  122. /// <comments>Set Continue to False to cancel the activation.</comments>
  123. procedure Activate(var Continue: boolean);
  124. /// <summary>Deactivate is called when tool is deselected.</summary>
  125. procedure Deactivate;
  126. /// <summary>BeginTool is called before BeginAction, just before IBitmap32PaintHost.BeginUpdate is called.</summary>
  127. /// <comments>Set Continue to False to abort the operation.</comments>
  128. procedure BeginTool(var Continue: boolean);
  129. /// <summary>EndTool is called after EndAction, just before IBitmap32PaintHost.EndUpdate is called.</summary>
  130. /// <comments>EndTool is called even if the operation is aborted prematurely.</comments>
  131. procedure EndTool;
  132. // BeginAction, ContinueAction and EndAction is called when the tool has
  133. // been selected and the user presses, moves and releases the mouse.
  134. // Set State to tsAbort to cancel the operation.
  135. procedure BeginAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState);
  136. procedure ContinueAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState);
  137. procedure EndAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState);
  138. procedure CallbackAction(Buffer: TBitmap32; Data: pointer; var Result: boolean);
  139. // MouseDown, MouseMove and MouseUp is called when the tool has been selected
  140. // and the user presses, moves and releases the mouse.
  141. procedure MouseDown(const Context: IBitmap32PaintToolContext; Button: TMouseButton);
  142. procedure MouseMove(const Context: IBitmap32PaintToolContext);
  143. procedure MouseUp(const Context: IBitmap32PaintToolContext; Button: TMouseButton);
  144. procedure MouseEnter;
  145. procedure MouseLeave;
  146. // TODO : Not yet implemented in controller layer
  147. procedure KeyDown(var Key: Word; Shift: TShiftState);
  148. procedure KeyUp(var Key: Word; Shift: TShiftState);
  149. /// <summary>GetCursor returns the cursor used by the tool.</summary>
  150. /// <comments>The tool should use IBitmap32PaintHost.RegisterCursor to register custom cursors.</comments>
  151. function GetCursor(out Cursor: TCursor): boolean;
  152. // SnapMouse specifies if bitmap coordinates should always be rounded (SnapMouse=True) or
  153. // truncated (SnapMouse=False) when converting mouse position from viewport to bitmap
  154. // coordinates.
  155. // If SnapMouse=True then the coordinates specified in Context.BitmapPos passed to BeginAction,
  156. // ContinueAction and EndAction will be snapped coordinates. I.e. the same value as Context.BitmapPosSnap.
  157. // Tools that operate on pixels usually need SnapMouse=False while tools that operate
  158. // on areas usually need SnapMouse=True.
  159. function GetSnapMouse: boolean;
  160. property SnapMouse: boolean read GetSnapMouse;
  161. function GetToolFeatures: TBitmap32PaintToolFeatures;
  162. property ToolFeatures: TBitmap32PaintToolFeatures read GetToolFeatures;
  163. end;
  164. //------------------------------------------------------------------------------
  165. //
  166. // Global settings
  167. //
  168. //------------------------------------------------------------------------------
  169. var
  170. // Snap to: rect, 45deg, etc.
  171. Bitmap32PaintToolKeyStateSnap: TShiftState = [ssShift]; // PhotoShop: [ssShift]
  172. // StartPos is center of: rect, circle, etc.
  173. Bitmap32PaintToolKeyStateCenter: TShiftState = [ssAlt]; // PhotoShop: [ssAlt]
  174. // Generic modifier (action depends on tool)
  175. Bitmap32PaintToolKeyStateAlternate: TShiftState = [ssCtrl]; // Must be different from the two above
  176. //------------------------------------------------------------------------------
  177. //------------------------------------------------------------------------------
  178. //------------------------------------------------------------------------------
  179. implementation
  180. end.