GR32.Paint.Tool.Pen.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. unit GR32.Paint.Tool.Pen;
  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,
  39. GR32.Paint.Tool.API;
  40. //------------------------------------------------------------------------------
  41. //
  42. // TCustomBitmap32PaintToolPen
  43. //
  44. //------------------------------------------------------------------------------
  45. type
  46. TCustomBitmap32PaintToolPen = class abstract(TCustomBitmap32PaintTool)
  47. strict private
  48. FLastPos: TPoint;
  49. strict protected
  50. procedure DoAction(const Context: IBitmap32PaintToolContext);
  51. strict protected
  52. function GetTransparent: boolean; virtual;
  53. function GetToolFeatures: TBitmap32PaintToolFeatures; override;
  54. procedure BeginAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState); override;
  55. procedure ContinueAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState); override;
  56. procedure EndAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState); override;
  57. function GetCursor(out Cursor: TCursor): boolean; override;
  58. public
  59. end;
  60. //------------------------------------------------------------------------------
  61. //
  62. // TBitmap32PaintToolPen
  63. //
  64. //------------------------------------------------------------------------------
  65. type
  66. TBitmap32PaintToolPen = class(TCustomBitmap32PaintToolPen)
  67. strict private
  68. FTransparent: boolean;
  69. strict protected
  70. function GetTransparent: boolean; override;
  71. function GetCaption: string; override;
  72. public
  73. constructor Create(const APaintHost: IBitmap32PaintHost); override;
  74. property Transparent: boolean read FTransparent write FTransparent;
  75. end;
  76. resourcestring
  77. sBitmap32PaintToolPenCaption = 'Pencil';
  78. //------------------------------------------------------------------------------
  79. //------------------------------------------------------------------------------
  80. //------------------------------------------------------------------------------
  81. implementation
  82. uses
  83. Types;
  84. //------------------------------------------------------------------------------
  85. //
  86. // TCustomBitmap32PaintToolPen
  87. //
  88. //------------------------------------------------------------------------------
  89. function TCustomBitmap32PaintToolPen.GetCursor(out Cursor: TCursor): boolean;
  90. begin
  91. Cursor := crCross;
  92. Result := True;
  93. end;
  94. //------------------------------------------------------------------------------
  95. function TCustomBitmap32PaintToolPen.GetToolFeatures: TBitmap32PaintToolFeatures;
  96. begin
  97. Result := [];
  98. end;
  99. //------------------------------------------------------------------------------
  100. function TCustomBitmap32PaintToolPen.GetTransparent: boolean;
  101. begin
  102. Result := False;
  103. end;
  104. //------------------------------------------------------------------------------
  105. procedure TCustomBitmap32PaintToolPen.DoAction(const Context: IBitmap32PaintToolContext);
  106. begin
  107. // Draw line from new pos to old pos, but don't draw the end pixel as we have
  108. // already drawn it.
  109. (*
  110. ** [] Replace
  111. ** [ssShift] Transparent (blend)
  112. *)
  113. if (GetTransparent) then
  114. Context.Buffer.LineTS(Context.MouseParams.BitmapPos.X, Context.MouseParams.BitmapPos.Y, FLastPos.X, FLastPos.Y, ActiveColor(Context.MouseParams.ShiftState), False)
  115. else
  116. Context.Buffer.LineS(Context.MouseParams.BitmapPos.X, Context.MouseParams.BitmapPos.Y, FLastPos.X, FLastPos.Y, ActiveColor(Context.MouseParams.ShiftState), False);
  117. FLastPos := Context.MouseParams.BitmapPos;
  118. PaintHost.Changed(Caption);
  119. end;
  120. //------------------------------------------------------------------------------
  121. procedure TCustomBitmap32PaintToolPen.BeginAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState);
  122. begin
  123. inherited;
  124. if ([ssLeft, ssRight] * Context.MouseParams.ShiftState <> []) then
  125. begin
  126. if (GetTransparent) then
  127. Context.Buffer.SetPixelTS(Context.MouseParams.BitmapPos.X, Context.MouseParams.BitmapPos.Y, ActiveColor(Context.MouseParams.ShiftState))
  128. else
  129. Context.Buffer.PixelS[Context.MouseParams.BitmapPos.X, Context.MouseParams.BitmapPos.Y] := ActiveColor(Context.MouseParams.ShiftState);
  130. // By default the Pixel setter doesn't call Changed (unless CHANGED_IN_PIXELS is defined)
  131. Context.Buffer.Changed(MakeRect(Context.MouseParams.BitmapPos.X, Context.MouseParams.BitmapPos.Y, Context.MouseParams.BitmapPos.X+1, Context.MouseParams.BitmapPos.Y+1));
  132. FLastPos := Context.MouseParams.BitmapPos;
  133. PaintHost.Changed(Caption);
  134. end else
  135. ToolState := tsAbort;
  136. end;
  137. //------------------------------------------------------------------------------
  138. procedure TCustomBitmap32PaintToolPen.ContinueAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState);
  139. begin
  140. if (FLastPos = Context.MouseParams.BitmapPos) then
  141. Exit;
  142. if ([ssLeft, ssRight] * Context.MouseParams.ShiftState <> []) then
  143. DoAction(Context);
  144. end;
  145. //------------------------------------------------------------------------------
  146. procedure TCustomBitmap32PaintToolPen.EndAction(const Context: IBitmap32PaintToolContext; var ToolState: TBitmap32PaintToolState);
  147. begin
  148. DoAction(Context);
  149. end;
  150. //------------------------------------------------------------------------------
  151. //
  152. // TBitmap32PaintToolPen
  153. //
  154. //------------------------------------------------------------------------------
  155. constructor TBitmap32PaintToolPen.Create(const APaintHost: IBitmap32PaintHost);
  156. begin
  157. inherited;
  158. FTransparent := True;
  159. end;
  160. function TBitmap32PaintToolPen.GetCaption: string;
  161. begin
  162. Result := sBitmap32PaintToolPenCaption;
  163. end;
  164. function TBitmap32PaintToolPen.GetTransparent: boolean;
  165. begin
  166. Result := FTransparent;
  167. end;
  168. //------------------------------------------------------------------------------
  169. end.