123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- namespace PaintDotNet.Measurement.Tools
- {
- /// <summary>
- /// Allows the user to click on the image to zoom to that location
- /// </summary>
- public class ZoomTool : Tool
- {
- private bool moveOffsetMode = false;
- private MouseButtons mouseDown;
- private Point downPt;
- private Point lastPt;
- private Rectangle rect = Rectangle.Empty;
- private Cursor cursorZoomIn;
- private Cursor cursorZoomOut;
- private Cursor cursorZoom;
- private Cursor cursorZoomPan;
- private SelectionRenderer outlineRenderer;
- private Selection outline;
- public ZoomTool(IDocumentWorkspace documentWorkspace)
- : base(documentWorkspace,
- PdnResources.GetImageResource("Icons.ZoomToolIcon.png"),
- PdnResources.GetString("ZoomTool.Name"),
- PdnResources.GetString("ZoomTool.HelpText"),
- 'z',
- false,
- ToolBarConfigItems.None)
- {
- this.mouseDown = MouseButtons.None;
- }
- protected override void OnActivate()
- {
- this.cursorZoom = new Cursor(PdnResources.GetResourceStream("Cursors.ZoomToolCursor.cur"));
- this.cursorZoomIn = new Cursor(PdnResources.GetResourceStream("Cursors.ZoomInToolCursor.cur"));
- this.cursorZoomOut = new Cursor(PdnResources.GetResourceStream("Cursors.ZoomOutToolCursor.cur"));
- this.cursorZoomPan = new Cursor(PdnResources.GetResourceStream("Cursors.ZoomOutToolCursor.cur"));
- this.Cursor = this.cursorZoom;
- base.OnActivate();
- this.outline = new Selection();
- this.outlineRenderer = new SelectionRenderer(this.RendererList, this.outline, this.DocumentWorkspace.GetUserControl());
- this.outlineRenderer.InvertedTinting = true;
- this.outlineRenderer.TintColor = Color.FromArgb(128, 255, 255, 255);
- this.outlineRenderer.ResetOutlineWhiteOpacity();
- this.RendererList.Add(this.outlineRenderer, true);
- }
- protected override void OnDeactivate()
- {
- if (cursorZoom != null)
- {
- cursorZoom.Dispose();
- cursorZoom = null;
- }
- if (cursorZoomIn != null)
- {
- cursorZoomIn.Dispose();
- cursorZoomIn = null;
- }
- if (cursorZoomOut != null)
- {
- cursorZoomOut.Dispose();
- cursorZoomOut = null;
- }
- if (cursorZoomPan != null)
- {
- cursorZoomPan.Dispose();
- cursorZoomPan = null;
- }
- this.RendererList.Remove(this.outlineRenderer);
- this.outlineRenderer.Dispose();
- this.outlineRenderer = null;
- base.OnDeactivate();
- }
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- if (mouseDown != MouseButtons.None)
- {
- this.moveOffsetMode = true;
- }
- else
- {
- switch (e.Button)
- {
- case MouseButtons.Left:
- Cursor = cursorZoomIn;
- break;
- case MouseButtons.Middle:
- Cursor = cursorZoomPan;
- break;
- case MouseButtons.Right:
- Cursor = cursorZoomOut;
- break;
- }
- mouseDown = e.Button;
- lastPt = new Point(e.X, e.Y);
- downPt = lastPt;
- OnMouseMove(e);
- }
- }
- protected override void OnKeyPress(KeyPressEventArgs e)
- {
- if (!e.Handled)
- {
- if (this.mouseDown != MouseButtons.None)
- {
- e.Handled = true;
- }
- }
- base.OnKeyPress(e);
- }
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
- Point thisPt = new Point(e.X, e.Y);
- if (this.moveOffsetMode)
- {
- Size delta = new Size(thisPt.X - lastPt.X, thisPt.Y - lastPt.Y);
- downPt.X += delta.Width;
- downPt.Y += delta.Height;
- }
- if ((e.Button == MouseButtons.Left &&
- mouseDown == MouseButtons.Left &&
- Utility.Distance(thisPt, downPt) > 10) || // if they've moved the mouse more than 10 pixels since they clicked
- !rect.IsEmpty) //don't undraw the rectangle
- {
- rect = Utility.PointsToRectangle(downPt, thisPt);
- rect.Intersect(ActiveLayer.Bounds);
- UpdateDrawnRect();
- }
- else if (e.Button == MouseButtons.Middle && mouseDown == MouseButtons.Middle)
- {
- PointF lastScrollPosition = DocumentWorkspace.GetDocumentScrollPositionF();
- lastScrollPosition.X += thisPt.X - lastPt.X;
- lastScrollPosition.Y += thisPt.Y - lastPt.Y;
- DocumentWorkspace.SetDocumentScrollPositionF(lastScrollPosition);
- Update();
- }
- else
- {
- rect = Rectangle.Empty;
- }
- lastPt = thisPt;
- }
- protected override void OnMouseUp(MouseEventArgs e)
- {
- base.OnMouseUp(e);
- OnMouseMove(e);
- bool resetMouseDown = true;
- Cursor = cursorZoom;
- if (this.moveOffsetMode)
- {
- this.moveOffsetMode = false;
- resetMouseDown = false;
- }
- else if (mouseDown == MouseButtons.Left || mouseDown == MouseButtons.Right)
- {
- Rectangle zoomTo = rect;
- rect = Rectangle.Empty;
- UpdateDrawnRect();
- if (e.Button == MouseButtons.Left)
- {
- if (Utility.Magnitude(new PointF(zoomTo.Width, zoomTo.Height)) < 10)
- {
- DocumentWorkspace.ZoomIn();
- DocumentWorkspace.RecenterView(new Point(e.X, e.Y));
- }
- else
- {
- DocumentWorkspace.ZoomToRectangle(zoomTo);
- }
- }
- else
- {
- DocumentWorkspace.ZoomOut();
- DocumentWorkspace.RecenterView(new Point(e.X, e.Y));
- }
- this.outline.Reset();
- }
- if (resetMouseDown)
- {
- mouseDown = MouseButtons.None;
- }
- }
- private void UpdateDrawnRect()
- {
- if (!rect.IsEmpty)
- {
- this.outline.PerformChanging();
- this.outline.Reset();
- this.outline.SetContinuation(rect, CombineMode.Replace);
- this.outlineRenderer.ResetOutlineWhiteOpacity();
- this.outline.CommitContinuation();
- this.outline.PerformChanged();
- Update();
- }
- }
- }
- }
|