MoveSelectionTool.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using PaintDotNet.Measurement.HistoryMementos;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace PaintDotNet.Measurement.Tools
  7. {
  8. public class MoveSelectionTool : MoveToolBase
  9. {
  10. public static string StaticName
  11. {
  12. get
  13. {
  14. return PdnResources.GetString("MoveSelectionTool.Name");
  15. }
  16. }
  17. private class ContextHistoryMemento
  18. : ToolHistoryMemento
  19. {
  20. [Serializable]
  21. private class OurHistoryMementoData
  22. : HistoryMementoData
  23. {
  24. public Context context;
  25. public OurHistoryMementoData(Context context)
  26. {
  27. this.context = (Context)context.Clone();
  28. }
  29. }
  30. protected override HistoryMemento OnToolUndo()
  31. {
  32. MoveSelectionTool moveSelectionTool = DocumentWorkspace.GetTool() as MoveSelectionTool;
  33. if (moveSelectionTool == null)
  34. {
  35. throw new InvalidOperationException("Current Tool is not the MoveSelectionTool");
  36. }
  37. ContextHistoryMemento cha = new ContextHistoryMemento(DocumentWorkspace, moveSelectionTool.context, this.Name, this.Image);
  38. OurHistoryMementoData ohad = (OurHistoryMementoData)this.Data;
  39. Context newContext = ohad.context;
  40. moveSelectionTool.context.Dispose();
  41. moveSelectionTool.context = newContext;
  42. moveSelectionTool.DestroyNubs();
  43. if (moveSelectionTool.context.lifted)
  44. {
  45. moveSelectionTool.PositionNubs(moveSelectionTool.context.currentMode);
  46. }
  47. return cha;
  48. }
  49. public ContextHistoryMemento(IDocumentWorkspace documentWorkspace, Context context, string name, ImageResource image)
  50. : base(documentWorkspace, name, image)
  51. {
  52. this.Data = new OurHistoryMementoData(context);
  53. }
  54. }
  55. protected override void OnActivate()
  56. {
  57. DocumentWorkspace.SetEnableSelectionTinting(true);
  58. this.moveToolCursor = new Cursor(PdnResources.GetResourceStream("Cursors.MoveSelectionToolCursor.cur"));
  59. this.Cursor = this.moveToolCursor;
  60. this.context.offset = new Point(0, 0);
  61. this.context.liftedBounds = Selection.GetBoundsF();
  62. this.tracking = false;
  63. PositionNubs(this.context.currentMode);
  64. base.OnActivate();
  65. }
  66. protected override void OnDeactivate()
  67. {
  68. DocumentWorkspace.SetEnableSelectionTinting(false);
  69. if (this.moveToolCursor != null)
  70. {
  71. this.moveToolCursor.Dispose();
  72. this.moveToolCursor = null;
  73. }
  74. if (context.lifted)
  75. {
  76. Drop();
  77. }
  78. this.tracking = false;
  79. DestroyNubs();
  80. base.OnDeactivate();
  81. }
  82. protected override void Drop()
  83. {
  84. ContextHistoryMemento cha = new ContextHistoryMemento(this.DocumentWorkspace, this.context, this.Name, this.Image);
  85. this.currentHistoryMementos.Add(cha);
  86. SelectionHistoryMemento sha = new SelectionHistoryMemento(this.Name, this.Image, this.DocumentWorkspace);
  87. this.currentHistoryMementos.Add(sha);
  88. this.context.Dispose();
  89. this.context = new Context();
  90. this.FlushHistoryMementos(PdnResources.GetString("MoveSelectionTool.HistoryMemento.DropSelection"));
  91. }
  92. protected override void OnSelectionChanging()
  93. {
  94. base.OnSelectionChanging();
  95. if (!dontDrop)
  96. {
  97. if (context.lifted)
  98. {
  99. Drop();
  100. }
  101. if (tracking)
  102. {
  103. tracking = false;
  104. }
  105. }
  106. }
  107. protected override void OnSelectionChanged()
  108. {
  109. if (!this.context.lifted)
  110. {
  111. DestroyNubs();
  112. PositionNubs(this.context.currentMode);
  113. }
  114. base.OnSelectionChanged();
  115. }
  116. protected override void OnLift(MouseEventArgs e)
  117. {
  118. // do nothing
  119. }
  120. protected override void PushContextHistoryMemento()
  121. {
  122. ContextHistoryMemento cha = new ContextHistoryMemento(this.DocumentWorkspace, this.context, null, null);
  123. this.currentHistoryMementos.Add(cha);
  124. }
  125. protected override void Render(Point newOffset, bool useNewOffset)
  126. {
  127. PositionNubs(this.context.currentMode);
  128. }
  129. protected override void PreRender()
  130. {
  131. // do nothing
  132. }
  133. protected override void OnMouseUp(MouseEventArgs e)
  134. {
  135. base.OnMouseUp(e);
  136. if (!tracking)
  137. {
  138. return;
  139. }
  140. OnMouseMove(e);
  141. this.rotateNub.Visible = false;
  142. tracking = false;
  143. PositionNubs(this.context.currentMode);
  144. string resourceName;
  145. switch (this.context.currentMode)
  146. {
  147. default:
  148. throw new InvalidEnumArgumentException();
  149. case Mode.Rotate:
  150. resourceName = "MoveSelectionTool.HistoryMemento.Rotate";
  151. break;
  152. case Mode.Scale:
  153. resourceName = "MoveSelectionTool.HistoryMemento.Scale";
  154. break;
  155. case Mode.Translate:
  156. resourceName = "MoveSelectionTool.HistoryMemento.Translate";
  157. break;
  158. }
  159. this.context.startAngle += this.angleDelta;
  160. string actionName = PdnResources.GetString(resourceName);
  161. FlushHistoryMementos(actionName);
  162. }
  163. private void FlushHistoryMementos(string name)
  164. {
  165. if (this.currentHistoryMementos.Count > 0)
  166. {
  167. CompoundHistoryMemento cha = new CompoundHistoryMemento(null, null,
  168. this.currentHistoryMementos.ToArray());
  169. string haName;
  170. if (name == null)
  171. {
  172. haName = this.Name;
  173. }
  174. else
  175. {
  176. haName = name;
  177. }
  178. ImageResource image = this.Image;
  179. CompoundToolHistoryMemento ctha = new CompoundToolHistoryMemento(cha, DocumentWorkspace, haName, image);
  180. ctha.SeriesGuid = context.seriesGuid;
  181. //HistoryStack.PushNewMemento(ctha);
  182. this.currentHistoryMementos.Clear();
  183. }
  184. }
  185. public MoveSelectionTool(IDocumentWorkspace documentWorkspace)
  186. : base(documentWorkspace,
  187. PdnResources.GetImageResource("Icons.MoveSelectionToolIcon.png"),
  188. MoveSelectionTool.StaticName,
  189. PdnResources.GetString("MoveSelectionTool.HelpText"), // "Click and drag to move a selected region",
  190. 'm',
  191. false,
  192. ToolBarConfigItems.None)
  193. {
  194. this.context = new Context();
  195. }
  196. protected override void Dispose(bool disposing)
  197. {
  198. base.Dispose(disposing);
  199. if (disposing)
  200. {
  201. DestroyNubs();
  202. if (this.context != null)
  203. {
  204. this.context.Dispose();
  205. this.context = null;
  206. }
  207. }
  208. }
  209. protected override void OnExecutingHistoryMemento(ExecutingHistoryMementoEventArgs e)
  210. {
  211. this.dontDrop = true;
  212. if (e.MayAlterSuspendTool)
  213. {
  214. e.SuspendTool = false;
  215. }
  216. }
  217. protected override void OnExecutedHistoryMemento(ExecutedHistoryMementoEventArgs e)
  218. {
  219. if (this.context.lifted)
  220. {
  221. Render(context.offset, true);
  222. }
  223. else
  224. {
  225. DestroyNubs();
  226. PositionNubs(this.context.currentMode);
  227. }
  228. this.dontDrop = false;
  229. }
  230. }
  231. }