ZoomTool.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System.Drawing;
  2. using System.Drawing.Drawing2D;
  3. using System.Windows.Forms;
  4. namespace PaintDotNet.Measurement.Tools
  5. {
  6. /// <summary>
  7. /// Allows the user to click on the image to zoom to that location
  8. /// </summary>
  9. public class ZoomTool : Tool
  10. {
  11. private bool moveOffsetMode = false;
  12. private MouseButtons mouseDown;
  13. private Point downPt;
  14. private Point lastPt;
  15. private Rectangle rect = Rectangle.Empty;
  16. private Cursor cursorZoomIn;
  17. private Cursor cursorZoomOut;
  18. private Cursor cursorZoom;
  19. private Cursor cursorZoomPan;
  20. private SelectionRenderer outlineRenderer;
  21. private Selection outline;
  22. public ZoomTool(IDocumentWorkspace documentWorkspace)
  23. : base(documentWorkspace,
  24. PdnResources.GetImageResource("Icons.ZoomToolIcon.png"),
  25. PdnResources.GetString("ZoomTool.Name"),
  26. PdnResources.GetString("ZoomTool.HelpText"),
  27. 'z',
  28. false,
  29. ToolBarConfigItems.None)
  30. {
  31. this.mouseDown = MouseButtons.None;
  32. }
  33. protected override void OnActivate()
  34. {
  35. this.cursorZoom = new Cursor(PdnResources.GetResourceStream("Cursors.ZoomToolCursor.cur"));
  36. this.cursorZoomIn = new Cursor(PdnResources.GetResourceStream("Cursors.ZoomInToolCursor.cur"));
  37. this.cursorZoomOut = new Cursor(PdnResources.GetResourceStream("Cursors.ZoomOutToolCursor.cur"));
  38. this.cursorZoomPan = new Cursor(PdnResources.GetResourceStream("Cursors.ZoomOutToolCursor.cur"));
  39. this.Cursor = this.cursorZoom;
  40. base.OnActivate();
  41. this.outline = new Selection();
  42. this.outlineRenderer = new SelectionRenderer(this.RendererList, this.outline, this.DocumentWorkspace.GetUserControl());
  43. this.outlineRenderer.InvertedTinting = true;
  44. this.outlineRenderer.TintColor = Color.FromArgb(128, 255, 255, 255);
  45. this.outlineRenderer.ResetOutlineWhiteOpacity();
  46. this.RendererList.Add(this.outlineRenderer, true);
  47. }
  48. protected override void OnDeactivate()
  49. {
  50. if (cursorZoom != null)
  51. {
  52. cursorZoom.Dispose();
  53. cursorZoom = null;
  54. }
  55. if (cursorZoomIn != null)
  56. {
  57. cursorZoomIn.Dispose();
  58. cursorZoomIn = null;
  59. }
  60. if (cursorZoomOut != null)
  61. {
  62. cursorZoomOut.Dispose();
  63. cursorZoomOut = null;
  64. }
  65. if (cursorZoomPan != null)
  66. {
  67. cursorZoomPan.Dispose();
  68. cursorZoomPan = null;
  69. }
  70. this.RendererList.Remove(this.outlineRenderer);
  71. this.outlineRenderer.Dispose();
  72. this.outlineRenderer = null;
  73. base.OnDeactivate();
  74. }
  75. protected override void OnMouseDown(MouseEventArgs e)
  76. {
  77. base.OnMouseDown(e);
  78. if (mouseDown != MouseButtons.None)
  79. {
  80. this.moveOffsetMode = true;
  81. }
  82. else
  83. {
  84. switch (e.Button)
  85. {
  86. case MouseButtons.Left:
  87. Cursor = cursorZoomIn;
  88. break;
  89. case MouseButtons.Middle:
  90. Cursor = cursorZoomPan;
  91. break;
  92. case MouseButtons.Right:
  93. Cursor = cursorZoomOut;
  94. break;
  95. }
  96. mouseDown = e.Button;
  97. lastPt = new Point(e.X, e.Y);
  98. downPt = lastPt;
  99. OnMouseMove(e);
  100. }
  101. }
  102. protected override void OnKeyPress(KeyPressEventArgs e)
  103. {
  104. if (!e.Handled)
  105. {
  106. if (this.mouseDown != MouseButtons.None)
  107. {
  108. e.Handled = true;
  109. }
  110. }
  111. base.OnKeyPress(e);
  112. }
  113. protected override void OnMouseMove(MouseEventArgs e)
  114. {
  115. base.OnMouseMove(e);
  116. Point thisPt = new Point(e.X, e.Y);
  117. if (this.moveOffsetMode)
  118. {
  119. Size delta = new Size(thisPt.X - lastPt.X, thisPt.Y - lastPt.Y);
  120. downPt.X += delta.Width;
  121. downPt.Y += delta.Height;
  122. }
  123. if ((e.Button == MouseButtons.Left &&
  124. mouseDown == MouseButtons.Left &&
  125. Utility.Distance(thisPt, downPt) > 10) || // if they've moved the mouse more than 10 pixels since they clicked
  126. !rect.IsEmpty) //don't undraw the rectangle
  127. {
  128. rect = Utility.PointsToRectangle(downPt, thisPt);
  129. rect.Intersect(ActiveLayer.Bounds);
  130. UpdateDrawnRect();
  131. }
  132. else if (e.Button == MouseButtons.Middle && mouseDown == MouseButtons.Middle)
  133. {
  134. PointF lastScrollPosition = DocumentWorkspace.GetDocumentScrollPositionF();
  135. lastScrollPosition.X += thisPt.X - lastPt.X;
  136. lastScrollPosition.Y += thisPt.Y - lastPt.Y;
  137. DocumentWorkspace.SetDocumentScrollPositionF(lastScrollPosition);
  138. Update();
  139. }
  140. else
  141. {
  142. rect = Rectangle.Empty;
  143. }
  144. lastPt = thisPt;
  145. }
  146. protected override void OnMouseUp(MouseEventArgs e)
  147. {
  148. base.OnMouseUp(e);
  149. OnMouseMove(e);
  150. bool resetMouseDown = true;
  151. Cursor = cursorZoom;
  152. if (this.moveOffsetMode)
  153. {
  154. this.moveOffsetMode = false;
  155. resetMouseDown = false;
  156. }
  157. else if (mouseDown == MouseButtons.Left || mouseDown == MouseButtons.Right)
  158. {
  159. Rectangle zoomTo = rect;
  160. rect = Rectangle.Empty;
  161. UpdateDrawnRect();
  162. if (e.Button == MouseButtons.Left)
  163. {
  164. if (Utility.Magnitude(new PointF(zoomTo.Width, zoomTo.Height)) < 10)
  165. {
  166. DocumentWorkspace.ZoomIn();
  167. DocumentWorkspace.RecenterView(new Point(e.X, e.Y));
  168. }
  169. else
  170. {
  171. DocumentWorkspace.ZoomToRectangle(zoomTo);
  172. }
  173. }
  174. else
  175. {
  176. DocumentWorkspace.ZoomOut();
  177. DocumentWorkspace.RecenterView(new Point(e.X, e.Y));
  178. }
  179. this.outline.Reset();
  180. }
  181. if (resetMouseDown)
  182. {
  183. mouseDown = MouseButtons.None;
  184. }
  185. }
  186. private void UpdateDrawnRect()
  187. {
  188. if (!rect.IsEmpty)
  189. {
  190. this.outline.PerformChanging();
  191. this.outline.Reset();
  192. this.outline.SetContinuation(rect, CombineMode.Replace);
  193. this.outlineRenderer.ResetOutlineWhiteOpacity();
  194. this.outline.CommitContinuation();
  195. this.outline.PerformChanged();
  196. Update();
  197. }
  198. }
  199. }
  200. }