PencilTool.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using PaintDotNet.Measurement.HistoryMementos;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6. namespace PaintDotNet.Measurement.Tools
  7. {
  8. public class PencilTool : Tool
  9. {
  10. private bool mouseDown = false;
  11. private ColorBgra pencilColor;
  12. private MouseButtons mouseButton;
  13. private BitmapLayer bitmapLayer;
  14. private RenderArgs renderArgs;
  15. private List<Point> tracePoints;
  16. private List<Rectangle> savedRects;
  17. private PdnRegion clipRegion;
  18. private Point lastPoint;
  19. private Point difference;
  20. private Cursor pencilToolCursor;
  21. private BinaryPixelOp blendOp = new UserBlendOps.NormalBlendOp();
  22. private BinaryPixelOp copyOp = new BinaryPixelOps.AssignFromRhs();
  23. protected override void OnActivate()
  24. {
  25. base.OnActivate();
  26. this.pencilToolCursor = new Cursor(PdnResources.GetResourceStream("Cursors.PencilToolCursor.cur"));
  27. this.Cursor = this.pencilToolCursor;
  28. this.savedRects = new List<Rectangle>();
  29. if (ActiveLayer != null)
  30. {
  31. bitmapLayer = (BitmapLayer)ActiveLayer;
  32. renderArgs = new RenderArgs(bitmapLayer.Surface);
  33. tracePoints = new List<Point>();
  34. }
  35. else
  36. {
  37. bitmapLayer = null;
  38. if (renderArgs != null)
  39. {
  40. renderArgs.Dispose();
  41. renderArgs = null;
  42. }
  43. }
  44. }
  45. protected override void OnDeactivate()
  46. {
  47. base.OnDeactivate();
  48. if (this.pencilToolCursor != null)
  49. {
  50. this.pencilToolCursor.Dispose();
  51. this.pencilToolCursor = null;
  52. }
  53. if (mouseDown)
  54. {
  55. Point lastTracePoint = (Point)tracePoints[tracePoints.Count - 1];
  56. OnMouseUp(new MouseEventArgs(mouseButton, 0, lastTracePoint.X, lastTracePoint.Y, 0));
  57. }
  58. this.savedRects = null;
  59. this.tracePoints = null;
  60. this.bitmapLayer = null;
  61. if (this.renderArgs != null)
  62. {
  63. this.renderArgs.Dispose();
  64. this.renderArgs = null;
  65. }
  66. this.mouseDown = false;
  67. if (clipRegion != null)
  68. {
  69. clipRegion.Dispose();
  70. clipRegion = null;
  71. }
  72. }
  73. // Draws a point, but first intersects it with the selection
  74. private void DrawPoint(RenderArgs ra, Point p, ColorBgra color)
  75. {
  76. if (ra.Surface.Bounds.Contains(p))
  77. {
  78. if (ra.Graphics.IsVisible(p))
  79. {
  80. BinaryPixelOp op = AppEnvironment.AlphaBlending() ? blendOp : copyOp;
  81. ra.Surface[p.X, p.Y] = op.Apply(ra.Surface[p.X, p.Y], color);
  82. }
  83. }
  84. }
  85. private void DrawLines(RenderArgs ra, List<Point> points, int startIndex, int length, ColorBgra color)
  86. {
  87. // Draw a point in the line
  88. if (points.Count == 0)
  89. {
  90. return;
  91. }
  92. else if (points.Count == 1)
  93. {
  94. Point p = (Point)points[0];
  95. if (ra.Surface.Bounds.Contains(p))
  96. {
  97. DrawPoint(ra, p, color);
  98. }
  99. }
  100. else
  101. {
  102. for (int i = startIndex + 1; i < startIndex + length; ++i)
  103. {
  104. Point[] linePoints = Utility.GetLinePoints(points[i - 1], points[i]);
  105. int startPoint = 0;
  106. if (i != 1)
  107. {
  108. startPoint = 1;
  109. }
  110. for (int pi = startPoint; pi < linePoints.Length; ++pi)
  111. {
  112. Point p = linePoints[pi];
  113. DrawPoint(ra, p, color);
  114. }
  115. }
  116. }
  117. }
  118. protected override void OnMouseDown(MouseEventArgs e)
  119. {
  120. base.OnMouseDown(e);
  121. if (mouseDown)
  122. {
  123. return;
  124. }
  125. if (((e.Button & MouseButtons.Left) == MouseButtons.Left) ||
  126. ((e.Button & MouseButtons.Right) == MouseButtons.Right))
  127. {
  128. mouseDown = true;
  129. mouseButton = e.Button;
  130. tracePoints = new List<Point>();
  131. bitmapLayer = (BitmapLayer)ActiveLayer;
  132. renderArgs = new RenderArgs(bitmapLayer.Surface);
  133. if (clipRegion != null)
  134. {
  135. clipRegion.Dispose();
  136. clipRegion = null;
  137. }
  138. clipRegion = Selection.CreateRegion();
  139. renderArgs.Graphics.SetClip(clipRegion.GetRegionReadOnly(), CombineMode.Replace);
  140. OnMouseMove(e);
  141. }
  142. }
  143. protected override void OnMouseMove(MouseEventArgs e)
  144. {
  145. base.OnMouseMove(e);
  146. if (mouseDown && ((e.Button & mouseButton) != MouseButtons.None))
  147. {
  148. Point mouseXY = new Point(e.X, e.Y);
  149. if (lastPoint == Point.Empty)
  150. {
  151. lastPoint = mouseXY;
  152. }
  153. difference = new Point(mouseXY.X - lastPoint.X, mouseXY.Y - lastPoint.Y);
  154. if (tracePoints.Count > 0)
  155. {
  156. Point lastMouseXY = (Point)tracePoints[tracePoints.Count - 1];
  157. if (lastMouseXY == mouseXY)
  158. {
  159. return;
  160. }
  161. }
  162. if ((mouseButton & MouseButtons.Left) == MouseButtons.Left)
  163. {
  164. this.pencilColor = AppEnvironment.PrimaryColor();
  165. }
  166. else // if ((mouseButton & MouseButtons.Right) == MouseButtons.Right)
  167. {
  168. // right mouse button = swap primary/secondary
  169. this.pencilColor = AppEnvironment.SecondaryColor();
  170. }
  171. if (!(tracePoints.Count > 0 && mouseXY == (Point)tracePoints[tracePoints.Count - 1]))
  172. {
  173. tracePoints.Add(mouseXY);
  174. }
  175. if (ActiveLayer is BitmapLayer)
  176. {
  177. Rectangle saveRect;
  178. if (tracePoints.Count == 1)
  179. {
  180. saveRect = Utility.PointsToRectangle(mouseXY, mouseXY);
  181. }
  182. else
  183. {
  184. // >1 points
  185. saveRect = Utility.PointsToRectangle((Point)tracePoints[tracePoints.Count - 1], (Point)tracePoints[tracePoints.Count - 2]);
  186. }
  187. saveRect.Inflate(2, 2);
  188. saveRect.Intersect(ActiveLayer.Bounds);
  189. // drawing outside of the canvas is a no-op, so don't do anything in that case!
  190. // also make sure it's within the clipping bounds
  191. if (saveRect.Width > 0 && saveRect.Height > 0 && renderArgs.Graphics.IsVisible(saveRect))
  192. {
  193. SaveRegion(null, saveRect);
  194. this.savedRects.Add(saveRect);
  195. int startIndex;
  196. int length;
  197. if (tracePoints.Count == 1)
  198. {
  199. startIndex = 0;
  200. length = 1;
  201. }
  202. else
  203. {
  204. startIndex = tracePoints.Count - 2;
  205. length = 2;
  206. }
  207. DrawLines(this.renderArgs, tracePoints, startIndex, length, pencilColor);
  208. bitmapLayer.Invalidate(saveRect);
  209. Update();
  210. }
  211. }
  212. else
  213. {
  214. // will have to do something here if we add other layer types besides BitmapLayer
  215. }
  216. lastPoint = mouseXY;
  217. }
  218. }
  219. protected override void OnMouseUp(MouseEventArgs e)
  220. {
  221. base.OnMouseUp(e);
  222. if (mouseDown)
  223. {
  224. OnMouseMove(e);
  225. mouseDown = false;
  226. if (savedRects.Count > 0)
  227. {
  228. Rectangle[] savedScans = this.savedRects.ToArray();
  229. PdnRegion saveMeRegion = Utility.RectanglesToRegion(savedScans);
  230. HistoryMemento ha = new BitmapHistoryMemento(Name, Image, DocumentWorkspace,
  231. ActiveLayerIndex, saveMeRegion, ScratchSurface);
  232. //HistoryStack.PushNewMemento(ha);
  233. saveMeRegion.Dispose();
  234. this.savedRects.Clear();
  235. ClearSavedMemory();
  236. }
  237. tracePoints = null;
  238. }
  239. }
  240. public PencilTool(IDocumentWorkspace documentWorkspace)
  241. : base(documentWorkspace,
  242. PdnResources.GetImageResource("Icons.PencilToolIcon.png"),
  243. PdnResources.GetString("PencilTool.Name"),
  244. PdnResources.GetString("PencilTool.HelpText"),
  245. 'p',
  246. true,
  247. ToolBarConfigItems.AlphaBlending)
  248. {
  249. // initialize any state information you need
  250. mouseDown = false;
  251. }
  252. }
  253. }