123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- using PaintDotNet.Measurement.HistoryMementos;
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- namespace PaintDotNet.Measurement.Tools
- {
- public class MoveSelectionTool : MoveToolBase
- {
- public static string StaticName
- {
- get
- {
- return PdnResources.GetString("MoveSelectionTool.Name");
- }
- }
- private class ContextHistoryMemento
- : ToolHistoryMemento
- {
- [Serializable]
- private class OurHistoryMementoData
- : HistoryMementoData
- {
- public Context context;
- public OurHistoryMementoData(Context context)
- {
- this.context = (Context)context.Clone();
- }
- }
- protected override HistoryMemento OnToolUndo()
- {
- MoveSelectionTool moveSelectionTool = DocumentWorkspace.GetTool() as MoveSelectionTool;
- if (moveSelectionTool == null)
- {
- throw new InvalidOperationException("Current Tool is not the MoveSelectionTool");
- }
- ContextHistoryMemento cha = new ContextHistoryMemento(DocumentWorkspace, moveSelectionTool.context, this.Name, this.Image);
- OurHistoryMementoData ohad = (OurHistoryMementoData)this.Data;
- Context newContext = ohad.context;
- moveSelectionTool.context.Dispose();
- moveSelectionTool.context = newContext;
- moveSelectionTool.DestroyNubs();
- if (moveSelectionTool.context.lifted)
- {
- moveSelectionTool.PositionNubs(moveSelectionTool.context.currentMode);
- }
- return cha;
- }
- public ContextHistoryMemento(IDocumentWorkspace documentWorkspace, Context context, string name, ImageResource image)
- : base(documentWorkspace, name, image)
- {
- this.Data = new OurHistoryMementoData(context);
- }
- }
- protected override void OnActivate()
- {
- DocumentWorkspace.SetEnableSelectionTinting(true);
- this.moveToolCursor = new Cursor(PdnResources.GetResourceStream("Cursors.MoveSelectionToolCursor.cur"));
- this.Cursor = this.moveToolCursor;
- this.context.offset = new Point(0, 0);
- this.context.liftedBounds = Selection.GetBoundsF();
- this.tracking = false;
- PositionNubs(this.context.currentMode);
- base.OnActivate();
- }
- protected override void OnDeactivate()
- {
- DocumentWorkspace.SetEnableSelectionTinting(false);
- if (this.moveToolCursor != null)
- {
- this.moveToolCursor.Dispose();
- this.moveToolCursor = null;
- }
- if (context.lifted)
- {
- Drop();
- }
- this.tracking = false;
- DestroyNubs();
- base.OnDeactivate();
- }
- protected override void Drop()
- {
- ContextHistoryMemento cha = new ContextHistoryMemento(this.DocumentWorkspace, this.context, this.Name, this.Image);
- this.currentHistoryMementos.Add(cha);
- SelectionHistoryMemento sha = new SelectionHistoryMemento(this.Name, this.Image, this.DocumentWorkspace);
- this.currentHistoryMementos.Add(sha);
- this.context.Dispose();
- this.context = new Context();
- this.FlushHistoryMementos(PdnResources.GetString("MoveSelectionTool.HistoryMemento.DropSelection"));
- }
- protected override void OnSelectionChanging()
- {
- base.OnSelectionChanging();
- if (!dontDrop)
- {
- if (context.lifted)
- {
- Drop();
- }
- if (tracking)
- {
- tracking = false;
- }
- }
- }
- protected override void OnSelectionChanged()
- {
- if (!this.context.lifted)
- {
- DestroyNubs();
- PositionNubs(this.context.currentMode);
- }
- base.OnSelectionChanged();
- }
- protected override void OnLift(MouseEventArgs e)
- {
- // do nothing
- }
- protected override void PushContextHistoryMemento()
- {
- ContextHistoryMemento cha = new ContextHistoryMemento(this.DocumentWorkspace, this.context, null, null);
- this.currentHistoryMementos.Add(cha);
- }
- protected override void Render(Point newOffset, bool useNewOffset)
- {
- PositionNubs(this.context.currentMode);
- }
- protected override void PreRender()
- {
- // do nothing
- }
- protected override void OnMouseUp(MouseEventArgs e)
- {
- base.OnMouseUp(e);
- if (!tracking)
- {
- return;
- }
- OnMouseMove(e);
- this.rotateNub.Visible = false;
- tracking = false;
- PositionNubs(this.context.currentMode);
- string resourceName;
- switch (this.context.currentMode)
- {
- default:
- throw new InvalidEnumArgumentException();
- case Mode.Rotate:
- resourceName = "MoveSelectionTool.HistoryMemento.Rotate";
- break;
- case Mode.Scale:
- resourceName = "MoveSelectionTool.HistoryMemento.Scale";
- break;
- case Mode.Translate:
- resourceName = "MoveSelectionTool.HistoryMemento.Translate";
- break;
- }
- this.context.startAngle += this.angleDelta;
- string actionName = PdnResources.GetString(resourceName);
- FlushHistoryMementos(actionName);
- }
- private void FlushHistoryMementos(string name)
- {
- if (this.currentHistoryMementos.Count > 0)
- {
- CompoundHistoryMemento cha = new CompoundHistoryMemento(null, null,
- this.currentHistoryMementos.ToArray());
- string haName;
- if (name == null)
- {
- haName = this.Name;
- }
- else
- {
- haName = name;
- }
- ImageResource image = this.Image;
- CompoundToolHistoryMemento ctha = new CompoundToolHistoryMemento(cha, DocumentWorkspace, haName, image);
- ctha.SeriesGuid = context.seriesGuid;
- //HistoryStack.PushNewMemento(ctha);
- this.currentHistoryMementos.Clear();
- }
- }
- public MoveSelectionTool(IDocumentWorkspace documentWorkspace)
- : base(documentWorkspace,
- PdnResources.GetImageResource("Icons.MoveSelectionToolIcon.png"),
- MoveSelectionTool.StaticName,
- PdnResources.GetString("MoveSelectionTool.HelpText"), // "Click and drag to move a selected region",
- 'm',
- false,
- ToolBarConfigItems.None)
- {
- this.context = new Context();
- }
- protected override void Dispose(bool disposing)
- {
- base.Dispose(disposing);
- if (disposing)
- {
- DestroyNubs();
- if (this.context != null)
- {
- this.context.Dispose();
- this.context = null;
- }
- }
- }
- protected override void OnExecutingHistoryMemento(ExecutingHistoryMementoEventArgs e)
- {
- this.dontDrop = true;
- if (e.MayAlterSuspendTool)
- {
- e.SuspendTool = false;
- }
- }
- protected override void OnExecutedHistoryMemento(ExecutedHistoryMementoEventArgs e)
- {
- if (this.context.lifted)
- {
- Render(context.offset, true);
- }
- else
- {
- DestroyNubs();
- PositionNubs(this.context.currentMode);
- }
- this.dontDrop = false;
- }
- }
- }
|