MoveToolBase.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. using PaintDotNet.Measurement.HistoryFunctions;
  2. using PaintDotNet.Measurement.HistoryMementos;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Runtime.Serialization;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. namespace PaintDotNet.Measurement.Tools
  12. {
  13. public abstract class MoveToolBase : Tool
  14. {
  15. protected Cursor moveToolCursor;
  16. protected bool dontDrop = false; // so that OnSelectionChanging() can tell who is raising the event ... don't drop the pixels if WE caused the event
  17. protected float angleDelta;
  18. protected MoveNubRenderer[] moveNubs;
  19. protected RotateNubRenderer rotateNub;
  20. protected bool tracking;
  21. protected Context context;
  22. protected bool hostShouldShowAngle;
  23. protected float hostAngle;
  24. protected List<HistoryMemento> currentHistoryMementos = new List<HistoryMemento>();
  25. protected bool deactivateOnLayerChange = true;
  26. protected bool enableOutline = true;
  27. public override bool DeactivateOnLayerChange
  28. {
  29. get
  30. {
  31. return this.deactivateOnLayerChange;
  32. }
  33. }
  34. protected enum Mode
  35. {
  36. Translate,
  37. Scale,
  38. Rotate
  39. }
  40. // Corresponds to array positions in this.moveNubs for easy mapping between the two
  41. protected enum Edge
  42. {
  43. TopLeft = 0,
  44. Top = 1,
  45. TopRight = 2,
  46. Right = 3,
  47. BottomRight = 4,
  48. Bottom = 5,
  49. BottomLeft = 6,
  50. Left = 7,
  51. None = 99
  52. }
  53. [Serializable]
  54. protected class Context
  55. : ICloneable,
  56. ISerializable,
  57. IDisposable
  58. {
  59. public bool lifted;
  60. public Guid seriesGuid;
  61. public Matrix baseTransform; // a copy of the selection's interim transform at the time of mouse-down
  62. public Matrix liftTransform; // a copy of the selection's interim transform at the time of lifting
  63. public Matrix deltaTransform; // the transformations made since lifting
  64. public RectangleF liftedBounds;
  65. public RectangleF startBounds;
  66. public float startAngle;
  67. public PdnGraphicsPath startPath;
  68. public Mode currentMode;
  69. public Edge startEdge;
  70. public Point startMouseXY;
  71. public Point offset;
  72. private float[] GetMatrixElements(Matrix m)
  73. {
  74. if (m == null)
  75. {
  76. return null;
  77. }
  78. else
  79. {
  80. return m.Elements;
  81. }
  82. }
  83. public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
  84. {
  85. info.AddValue("lifted", this.lifted);
  86. info.AddValue("seriesGuid", this.seriesGuid);
  87. info.AddValue("baseTransform", GetMatrixElements(this.baseTransform));
  88. info.AddValue("deltaTransform", GetMatrixElements(this.deltaTransform));
  89. info.AddValue("liftTransform", GetMatrixElements(this.liftTransform));
  90. info.AddValue("liftedBounds", this.liftedBounds);
  91. info.AddValue("startBounds", this.startBounds);
  92. info.AddValue("startAngle", this.startAngle);
  93. info.AddValue("startPath", this.startPath);
  94. info.AddValue("currentMode", this.currentMode);
  95. info.AddValue("startEdge", this.startEdge);
  96. info.AddValue("startMouseXY", this.startMouseXY);
  97. info.AddValue("offset", this.offset);
  98. }
  99. private Matrix ReadMatrix(SerializationInfo info, StreamingContext context, string name)
  100. {
  101. Matrix m;
  102. float[] e = (float[])info.GetValue(name, typeof(float[]));
  103. if (e == null)
  104. {
  105. m = null;
  106. }
  107. else
  108. {
  109. m = new Matrix(e[0], e[1], e[2], e[3], e[4], e[5]);
  110. }
  111. return m;
  112. }
  113. public Context(SerializationInfo info, StreamingContext context)
  114. {
  115. this.lifted = (bool)info.GetValue("lifted", typeof(bool));
  116. this.seriesGuid = (Guid)info.GetValue("seriesGuid", typeof(Guid));
  117. this.baseTransform = ReadMatrix(info, context, "baseTransform");
  118. this.deltaTransform = ReadMatrix(info, context, "deltaTransform");
  119. this.liftTransform = ReadMatrix(info, context, "liftTransform");
  120. this.liftedBounds = (RectangleF)info.GetValue("liftedBounds", typeof(RectangleF));
  121. this.startBounds = (RectangleF)info.GetValue("startBounds", typeof(RectangleF));
  122. this.startAngle = (float)info.GetValue("startAngle", typeof(float));
  123. this.startPath = (PdnGraphicsPath)info.GetValue("startPath", typeof(PdnGraphicsPath));
  124. this.currentMode = (Mode)info.GetValue("currentMode", typeof(Mode));
  125. this.startEdge = (Edge)info.GetValue("startEdge", typeof(Edge));
  126. this.startMouseXY = (Point)info.GetValue("startMouseXY", typeof(Point));
  127. this.offset = (Point)info.GetValue("offset", typeof(Point));
  128. }
  129. public Context()
  130. {
  131. }
  132. public Context(Context cloneMe)
  133. {
  134. this.lifted = cloneMe.lifted;
  135. this.seriesGuid = cloneMe.seriesGuid;
  136. if (cloneMe.baseTransform != null)
  137. {
  138. this.baseTransform = cloneMe.baseTransform.Clone();
  139. }
  140. if (cloneMe.deltaTransform != null)
  141. {
  142. this.deltaTransform = cloneMe.deltaTransform.Clone();
  143. }
  144. if (cloneMe.liftTransform != null)
  145. {
  146. this.liftTransform = cloneMe.liftTransform.Clone();
  147. }
  148. this.liftedBounds = cloneMe.liftedBounds;
  149. this.startBounds = cloneMe.startBounds;
  150. this.startAngle = cloneMe.startAngle;
  151. if (cloneMe.startPath != null)
  152. {
  153. this.startPath = cloneMe.startPath.Clone();
  154. }
  155. this.currentMode = cloneMe.currentMode;
  156. this.startEdge = cloneMe.startEdge;
  157. this.startMouseXY = cloneMe.startMouseXY;
  158. this.offset = cloneMe.offset;
  159. }
  160. ~Context()
  161. {
  162. Dispose(false);
  163. }
  164. public void Dispose()
  165. {
  166. Dispose(true);
  167. GC.SuppressFinalize(this);
  168. }
  169. protected virtual void Dispose(bool disposing)
  170. {
  171. if (disposing)
  172. {
  173. if (this.baseTransform != null)
  174. {
  175. this.baseTransform.Dispose();
  176. this.baseTransform = null;
  177. }
  178. if (this.deltaTransform != null)
  179. {
  180. this.deltaTransform.Dispose();
  181. this.deltaTransform = null;
  182. }
  183. if (this.liftTransform != null)
  184. {
  185. this.liftTransform.Dispose();
  186. this.liftTransform = null;
  187. }
  188. if (this.startPath != null)
  189. {
  190. this.startPath.Dispose();
  191. this.startPath = null;
  192. }
  193. }
  194. }
  195. public virtual object Clone()
  196. {
  197. return new Context(this);
  198. }
  199. }
  200. protected class CompoundToolHistoryMemento
  201. : ToolHistoryMemento
  202. {
  203. private CompoundHistoryMemento compoundHistoryMemento;
  204. public CompoundHistoryMemento CompoundHistoryMemento
  205. {
  206. get
  207. {
  208. return this.compoundHistoryMemento;
  209. }
  210. }
  211. protected override HistoryMemento OnToolUndo()
  212. {
  213. CompoundHistoryMemento chm = (CompoundHistoryMemento)this.compoundHistoryMemento.PerformUndo();
  214. CompoundToolHistoryMemento cthm = new CompoundToolHistoryMemento(chm, DocumentWorkspace, this.Name, this.Image);
  215. return cthm;
  216. }
  217. public CompoundToolHistoryMemento(CompoundHistoryMemento chm, IDocumentWorkspace documentWorkspace, string name, ImageResource image)
  218. : base(documentWorkspace, name, image)
  219. {
  220. this.compoundHistoryMemento = chm;
  221. }
  222. }
  223. public bool HostShouldShowAngle
  224. {
  225. get
  226. {
  227. return this.hostShouldShowAngle;
  228. }
  229. }
  230. public float HostAngle
  231. {
  232. get
  233. {
  234. return this.hostAngle;
  235. }
  236. }
  237. protected void DestroyNubs()
  238. {
  239. if (this.moveNubs != null)
  240. {
  241. for (int i = 0; i < this.moveNubs.Length; ++i)
  242. {
  243. this.RendererList.Remove(this.moveNubs[i]);
  244. this.moveNubs[i].Dispose();
  245. this.moveNubs[i] = null;
  246. }
  247. this.moveNubs = null;
  248. }
  249. if (this.rotateNub != null)
  250. {
  251. this.RendererList.Remove(this.rotateNub);
  252. this.rotateNub.Dispose();
  253. this.rotateNub = null;
  254. }
  255. }
  256. protected PointF GetEdgeVector(Edge edge)
  257. {
  258. PointF u;
  259. switch (edge)
  260. {
  261. case Edge.TopLeft:
  262. u = new PointF(-1, -1);
  263. break;
  264. case Edge.Top:
  265. u = new PointF(0, -1);
  266. break;
  267. case Edge.TopRight:
  268. u = new PointF(1, -1);
  269. break;
  270. case Edge.Left:
  271. u = new PointF(-1, 0);
  272. break;
  273. case Edge.Right:
  274. u = new PointF(1, 0);
  275. break;
  276. case Edge.BottomLeft:
  277. u = new PointF(-1, 1);
  278. break;
  279. case Edge.BottomRight:
  280. u = new PointF(1, 1);
  281. break;
  282. case Edge.Bottom:
  283. u = new PointF(0, 1);
  284. break;
  285. default:
  286. throw new InvalidEnumArgumentException();
  287. }
  288. return u;
  289. }
  290. protected void DetermineMoveMode(MouseEventArgs e, out Mode mode, out Edge edge)
  291. {
  292. mode = Mode.Translate;
  293. edge = Edge.None;
  294. if (e.Button == MouseButtons.Right)
  295. {
  296. mode = Mode.Rotate;
  297. }
  298. else
  299. {
  300. float minDistance = float.MaxValue;
  301. Point mousePt = new Point(e.X, e.Y);
  302. for (int i = 0; i < this.moveNubs.Length; ++i)
  303. {
  304. MoveNubRenderer nub = this.moveNubs[i];
  305. if (nub.IsPointTouching(mousePt, true))
  306. {
  307. float distance = Utility.Distance((PointF)mousePt, nub.Location);
  308. if (distance < minDistance)
  309. {
  310. minDistance = distance;
  311. mode = Mode.Scale;
  312. edge = (Edge)i;
  313. }
  314. }
  315. }
  316. }
  317. return;
  318. }
  319. protected override void OnPulse()
  320. {
  321. if (this.moveNubs != null)
  322. {
  323. for (int i = 0; i < this.moveNubs.Length; ++i)
  324. {
  325. // Oscillate between 25% and 100% alpha over a period of 2 seconds
  326. // Alpha value of 100% is sustained for a large duration of this period
  327. const int period = 10000 * 2000; // 10000 ticks per ms, 2000ms per period
  328. long tick = (DateTime.Now.Ticks % period) + (i * (period / this.moveNubs.Length));
  329. double sin = Math.Sin(((double)tick / (double)period) * (2.0 * Math.PI));
  330. // sin is [-1, +1]
  331. sin = Math.Min(0.5, sin);
  332. // sin is [-1, +0.5]
  333. sin += 1.0;
  334. // sin is [0, 1.5]
  335. sin /= 2.0;
  336. // sin is [0, 0.75]
  337. sin += 0.25;
  338. // sin is [0.25, 1]
  339. int newAlpha = (int)(sin * 255.0);
  340. int clampedAlpha = Utility.Clamp(newAlpha, 0, 255);
  341. this.moveNubs[i].Alpha = clampedAlpha;
  342. }
  343. }
  344. base.OnPulse();
  345. }
  346. protected void PositionNubs(Mode currentMode)
  347. {
  348. if (this.moveNubs == null)
  349. {
  350. this.moveNubs = new MoveNubRenderer[8];
  351. for (int i = 0; i < this.moveNubs.Length; ++i)
  352. {
  353. this.moveNubs[i] = new MoveNubRenderer(this.RendererList);
  354. this.RendererList.Add(this.moveNubs[i], false);
  355. }
  356. RectangleF bounds = Selection.GetBoundsF(false);
  357. this.moveNubs[(int)Edge.TopLeft].Location = new PointF(bounds.Left, bounds.Top);
  358. this.moveNubs[(int)Edge.TopLeft].Shape = MoveNubShape.Circle;
  359. this.moveNubs[(int)Edge.Top].Location = new PointF((bounds.Left + bounds.Right) / 2.0f, bounds.Top);
  360. this.moveNubs[(int)Edge.TopRight].Location = new PointF(bounds.Right, bounds.Top);
  361. this.moveNubs[(int)Edge.TopRight].Shape = MoveNubShape.Circle;
  362. this.moveNubs[(int)Edge.Left].Location = new PointF(bounds.Left, (bounds.Top + bounds.Bottom) / 2.0f);
  363. this.moveNubs[(int)Edge.Right].Location = new PointF(bounds.Right, (bounds.Top + bounds.Bottom) / 2.0f);
  364. this.moveNubs[(int)Edge.BottomLeft].Location = new PointF(bounds.Left, bounds.Bottom);
  365. this.moveNubs[(int)Edge.BottomLeft].Shape = MoveNubShape.Circle;
  366. this.moveNubs[(int)Edge.Bottom].Location = new PointF((bounds.Left + bounds.Right) / 2.0f, bounds.Bottom);
  367. this.moveNubs[(int)Edge.BottomRight].Location = new PointF(bounds.Right, bounds.Bottom);
  368. this.moveNubs[(int)Edge.BottomRight].Shape = MoveNubShape.Circle;
  369. }
  370. if (this.rotateNub == null)
  371. {
  372. this.rotateNub = new RotateNubRenderer(this.RendererList);
  373. rotateNub.Visible = false;
  374. this.RendererList.Add(this.rotateNub, false);
  375. }
  376. if (Selection.IsEmpty)
  377. {
  378. foreach (SurfaceBoxRenderer nub in this.moveNubs)
  379. {
  380. nub.Visible = false;
  381. }
  382. this.rotateNub.Visible = false;
  383. }
  384. else
  385. {
  386. foreach (MoveNubRenderer nub in this.moveNubs)
  387. {
  388. nub.Visible = !tracking || currentMode == Mode.Scale;
  389. nub.Transform = Selection.GetInterimTransformReadOnly();
  390. }
  391. }
  392. }
  393. protected void HideNubs()
  394. {
  395. if (this.moveNubs != null)
  396. {
  397. foreach (SurfaceBoxRenderer sbr in this.moveNubs)
  398. {
  399. sbr.Visible = false;
  400. }
  401. }
  402. if (this.rotateNub != null)
  403. {
  404. this.rotateNub.Visible = false;
  405. }
  406. }
  407. protected Edge FlipEdgeVertically(Edge flipMe)
  408. {
  409. Edge flippedEdge;
  410. switch (flipMe)
  411. {
  412. default:
  413. throw new InvalidEnumArgumentException();
  414. case Edge.Bottom:
  415. flippedEdge = Edge.Top;
  416. break;
  417. case Edge.BottomLeft:
  418. flippedEdge = Edge.TopLeft;
  419. break;
  420. case Edge.BottomRight:
  421. flippedEdge = Edge.TopRight;
  422. break;
  423. case Edge.Left:
  424. flippedEdge = Edge.Left;
  425. break;
  426. case Edge.None:
  427. flippedEdge = Edge.None;
  428. break;
  429. case Edge.Right:
  430. flippedEdge = Edge.Right;
  431. break;
  432. case Edge.Top:
  433. flippedEdge = Edge.Bottom;
  434. break;
  435. case Edge.TopLeft:
  436. flippedEdge = Edge.BottomLeft;
  437. break;
  438. case Edge.TopRight:
  439. flippedEdge = Edge.BottomRight;
  440. break;
  441. }
  442. return flippedEdge;
  443. }
  444. // Constrains the given width and height to the aspect ratio of this.liftedBounds
  445. protected void ConstrainScaling(RectangleF liftedBounds, float startWidth, float startHeight,
  446. float newWidth, float newHeight, out float newXScale, out float newYScale)
  447. {
  448. float hRatio = newWidth / (float)liftedBounds.Width;
  449. float vRatio = newHeight / (float)liftedBounds.Height;
  450. float bestScale = Math.Min(hRatio, vRatio);
  451. float bestWidth = (float)liftedBounds.Width * bestScale;
  452. float bestHeight = (float)liftedBounds.Height * bestScale;
  453. newXScale = bestWidth / startWidth;
  454. newYScale = bestHeight / startHeight;
  455. }
  456. // Constrains to nearest 15 degree angle
  457. protected float ConstrainAngle(float angle)
  458. {
  459. while (angle < 0)
  460. {
  461. angle += 360.0f;
  462. }
  463. int iangle = (int)angle;
  464. int lowerBound = (iangle / 15) * 15;
  465. int upperBound = lowerBound + 15;
  466. float lowerDiff = Math.Abs(angle - (float)lowerBound);
  467. float upperDiff = Math.Abs(angle - (float)upperBound);
  468. float newAngle;
  469. if (lowerDiff < upperDiff)
  470. {
  471. newAngle = (float)lowerBound;
  472. }
  473. else
  474. {
  475. newAngle = (float)upperBound;
  476. }
  477. if (newAngle > 180.0f)
  478. {
  479. newAngle -= 360.0f;
  480. }
  481. return newAngle;
  482. }
  483. protected override void OnKeyPress(Keys key)
  484. {
  485. if (!tracking)
  486. {
  487. int dx = 0;
  488. int dy = 0;
  489. if ((key & Keys.KeyCode) == Keys.Left)
  490. {
  491. dx = -1;
  492. }
  493. else if ((key & Keys.KeyCode) == Keys.Right)
  494. {
  495. dx = +1;
  496. }
  497. else if ((key & Keys.KeyCode) == Keys.Up)
  498. {
  499. dy = -1;
  500. }
  501. else if ((key & Keys.KeyCode) == Keys.Down)
  502. {
  503. dy = +1;
  504. }
  505. if ((key & Keys.Control) != Keys.None)
  506. {
  507. dx *= 10;
  508. dy *= 10;
  509. }
  510. // Simulate moving the selection
  511. if (dx != 0 || dy != 0)
  512. {
  513. Point pos = Cursor.Position;
  514. Point docPos = new Point(-70000, -70000);
  515. Point newDocPos = new Point(docPos.X + dx, docPos.Y + dy);
  516. OnMouseDown(new MouseEventArgs(MouseButtons.Left, 0, docPos.X, docPos.Y, 0));
  517. OnMouseMove(new MouseEventArgs(MouseButtons.Left, 0, newDocPos.X, newDocPos.Y, 0));
  518. OnMouseUp(new MouseEventArgs(MouseButtons.Left, 0, newDocPos.X, newDocPos.Y, 0));
  519. }
  520. }
  521. else
  522. {
  523. base.OnKeyPress(key);
  524. }
  525. }
  526. protected abstract void OnLift(MouseEventArgs e);
  527. protected abstract void Drop();
  528. protected abstract void PreRender();
  529. protected abstract void Render(Point newOffset, bool useNewOffset);
  530. protected abstract void PushContextHistoryMemento();
  531. protected void Lift(MouseEventArgs e)
  532. {
  533. this.PushContextHistoryMemento();
  534. this.context.seriesGuid = Guid.NewGuid();
  535. DetermineMoveMode(e, out this.context.currentMode, out this.context.startEdge);
  536. // lift!
  537. this.context.startBounds = this.context.liftedBounds;
  538. this.context.liftedBounds = Selection.GetBoundsF(false);
  539. this.context.startMouseXY = new Point(e.X, e.Y);
  540. this.context.offset = new Point(0, 0);
  541. this.context.startAngle = 0.0f;
  542. this.context.lifted = true;
  543. this.context.liftTransform = Selection.GetCumulativeTransformCopy();
  544. OnLift(e);
  545. PositionNubs(this.context.currentMode);
  546. }
  547. protected override void OnMouseDown(MouseEventArgs e)
  548. {
  549. base.OnMouseDown(e);
  550. if (tracking)
  551. {
  552. return;
  553. }
  554. bool determinedMoveMode = false;
  555. Mode newMode = Mode.Translate;
  556. Edge newEdge = Edge.None;
  557. if (Selection.IsEmpty)
  558. {
  559. SelectionHistoryMemento shm = new SelectionHistoryMemento(
  560. SelectAllFunction.StaticName,
  561. PdnResources.GetImageResource("Icons.MenuEditSelectAllIcon.png"),
  562. DocumentWorkspace);
  563. DocumentWorkspace.GetSelection().PerformChanging();
  564. DocumentWorkspace.GetSelection().Reset();
  565. DocumentWorkspace.GetSelection().SetContinuation(Document.Bounds, CombineMode.Replace);
  566. DocumentWorkspace.GetSelection().CommitContinuation();
  567. DocumentWorkspace.GetSelection().PerformChanged();
  568. if (e.Button == MouseButtons.Right)
  569. {
  570. newMode = Mode.Rotate;
  571. }
  572. else
  573. {
  574. newMode = Mode.Translate;
  575. }
  576. newEdge = Edge.None;
  577. determinedMoveMode = true;
  578. }
  579. DocumentWorkspace.SetEnableSelectionOutline(this.enableOutline);
  580. if (!context.lifted)
  581. {
  582. Lift(e);
  583. }
  584. PushContextHistoryMemento();
  585. if (!determinedMoveMode)
  586. {
  587. DetermineMoveMode(e, out newMode, out newEdge);
  588. determinedMoveMode = true;
  589. }
  590. if (this.context.deltaTransform != null)
  591. {
  592. this.context.deltaTransform.Dispose();
  593. this.context.deltaTransform = null;
  594. }
  595. this.context.deltaTransform = new Matrix();
  596. this.context.deltaTransform.Reset();
  597. if (newMode == Mode.Translate ||
  598. newMode == Mode.Scale ||
  599. newMode != this.context.currentMode ||
  600. newMode == Mode.Rotate)
  601. {
  602. this.context.startBounds = Selection.GetBoundsF();
  603. this.context.startMouseXY = new Point(e.X, e.Y);
  604. this.context.offset = new Point(0, 0);
  605. if (this.context.baseTransform != null)
  606. {
  607. this.context.baseTransform.Dispose();
  608. this.context.baseTransform = null;
  609. }
  610. this.context.baseTransform = Selection.GetInterimTransformCopy();
  611. }
  612. this.context.startEdge = newEdge;
  613. this.context.currentMode = newMode;
  614. PositionNubs(this.context.currentMode);
  615. tracking = true;
  616. this.rotateNub.Visible = (this.context.currentMode == Mode.Rotate);
  617. if (this.context.startPath != null)
  618. {
  619. this.context.startPath.Dispose();
  620. this.context.startPath = null;
  621. }
  622. this.context.startPath = Selection.CreatePath();
  623. this.context.startAngle = Utility.GetAngleOfTransform(Selection.GetInterimTransformReadOnly());
  624. SelectionHistoryMemento sha1 = new SelectionHistoryMemento(this.Name, this.Image, this.DocumentWorkspace);
  625. this.currentHistoryMementos.Add(sha1);
  626. OnMouseMove(e);
  627. if (this.enableOutline)
  628. {
  629. DocumentWorkspace.ResetOutlineWhiteOpacity();
  630. }
  631. }
  632. protected override void OnMouseMove(MouseEventArgs e)
  633. {
  634. base.OnMouseMove(e);
  635. StringBuilder sbLogger = new StringBuilder();
  636. try
  637. {
  638. OnMouseMoveImpl(e, sbLogger);
  639. }
  640. catch (Exception ex)
  641. {
  642. throw new ApplicationException("Tracing data: " + sbLogger.ToString(), ex);
  643. }
  644. }
  645. private void OnMouseMoveImpl(MouseEventArgs e, StringBuilder sbLogger)
  646. {
  647. if (!this.tracking)
  648. {
  649. sbLogger.Append("1 ");
  650. Cursor cursor = this.moveToolCursor;
  651. for (int i = 0; i < this.moveNubs.Length; ++i)
  652. {
  653. sbLogger.Append("2 ");
  654. MoveNubRenderer nub = this.moveNubs[i];
  655. sbLogger.Append("3 ");
  656. if (nub.Visible && nub.IsPointTouching(new Point(e.X, e.Y), true))
  657. {
  658. sbLogger.Append("4 ");
  659. cursor = this.handCursor;
  660. break;
  661. }
  662. }
  663. this.Cursor = cursor;
  664. sbLogger.Append("5 ");
  665. }
  666. else
  667. {
  668. sbLogger.Append("6 ");
  669. if (this.context.currentMode != Mode.Translate)
  670. {
  671. sbLogger.Append("7 ");
  672. this.Cursor = this.handCursorMouseDown;
  673. }
  674. sbLogger.Append("8 ");
  675. Point newMouseXY = new Point(e.X, e.Y);
  676. Point newOffset = new Point(newMouseXY.X - context.startMouseXY.X, newMouseXY.Y - context.startMouseXY.Y);
  677. PreRender();
  678. this.dontDrop = true;
  679. sbLogger.Append("9 ");
  680. Selection.PerformChanging();
  681. using (Matrix translateMatrix = new Matrix())
  682. {
  683. RectangleF rect;
  684. translateMatrix.Reset();
  685. if (this.context.baseTransform != null)
  686. {
  687. Selection.SetInterimTransform(this.context.baseTransform);
  688. }
  689. Matrix interim = Selection.GetInterimTransformCopy();
  690. switch (this.context.currentMode)
  691. {
  692. case Mode.Translate:
  693. translateMatrix.Translate((float)newOffset.X, (float)newOffset.Y, MatrixOrder.Append);
  694. break;
  695. case Mode.Rotate:
  696. rect = this.context.liftedBounds;
  697. PointF center = new PointF(rect.X + (rect.Width / 2.0f), rect.Y + (rect.Height / 2.0f));
  698. center = Utility.TransformOnePoint(interim, center);
  699. double theta1 = Math.Atan2(context.startMouseXY.Y - center.Y, context.startMouseXY.X - center.X);
  700. double theta2 = Math.Atan2(e.Y - center.Y, e.X - center.X);
  701. double thetaDelta = theta2 - theta1;
  702. this.angleDelta = (float)(thetaDelta * (180.0f / Math.PI));
  703. float angle = this.context.startAngle + this.angleDelta;
  704. if ((ModifierKeys & Keys.Shift) != 0)
  705. {
  706. angle = ConstrainAngle(angle);
  707. angleDelta = angle - this.context.startAngle;
  708. }
  709. translateMatrix.RotateAt(angleDelta, center, MatrixOrder.Append);
  710. this.rotateNub.Location = center;
  711. this.rotateNub.Angle = this.context.startAngle + angleDelta;
  712. break;
  713. case Mode.Scale:
  714. PointF xyAxes = GetEdgeVector(this.context.startEdge);
  715. PointF xAxis = new PointF(xyAxes.X, 0);
  716. PointF yAxis = new PointF(0, xyAxes.Y);
  717. PointF edgeX = Utility.TransformOneVector(interim, xAxis);
  718. PointF edgeY = Utility.TransformOneVector(interim, yAxis);
  719. PointF edgeXN = Utility.NormalizeVector2(edgeX);
  720. PointF edgeYN = Utility.NormalizeVector2(edgeY);
  721. PointF xu;
  722. float xulen;
  723. PointF xv;
  724. Utility.GetProjection((PointF)newOffset, edgeXN, out xu, out xulen, out xv);
  725. PointF yu;
  726. float yulen;
  727. PointF yv;
  728. Utility.GetProjection((PointF)newOffset, edgeYN, out yu, out yulen, out yv);
  729. PdnGraphicsPath startPath2 = this.context.startPath.Clone();
  730. RectangleF sp2Bounds = startPath2.GetBounds();
  731. PointF sp2BoundsCenter = new PointF((sp2Bounds.Left + sp2Bounds.Right) / 2.0f,
  732. (sp2Bounds.Top + sp2Bounds.Bottom) / 2.0f);
  733. float tAngle = Utility.GetAngleOfTransform(interim);
  734. bool isFlipped = Utility.IsTransformFlipped(interim);
  735. using (Matrix spm = new Matrix())
  736. {
  737. spm.Reset();
  738. spm.RotateAt(-tAngle, sp2BoundsCenter, MatrixOrder.Append);
  739. translateMatrix.RotateAt(-tAngle, sp2BoundsCenter, MatrixOrder.Append);
  740. startPath2.Transform(spm);
  741. }
  742. RectangleF spBounds2 = startPath2.GetBounds();
  743. startPath2.Dispose();
  744. startPath2 = null;
  745. float xTranslate;
  746. float yTranslate;
  747. bool allowConstrain;
  748. Edge theEdge = this.context.startEdge;
  749. // If the transform is flipped, then GetTransformAngle will return 180 degrees
  750. // even though no rotation has actually taken place. Thus we have to scratch
  751. // our head and go "hmm, let's make some adjustments to this." Otherwise stretching
  752. // the top and bottom nubs goes in the wrong direction.
  753. if (isFlipped)
  754. {
  755. theEdge = FlipEdgeVertically(theEdge);
  756. }
  757. switch (theEdge)
  758. {
  759. default:
  760. throw new InvalidEnumArgumentException();
  761. case Edge.TopLeft:
  762. allowConstrain = true;
  763. xTranslate = -spBounds2.X - spBounds2.Width;
  764. yTranslate = -spBounds2.Y - spBounds2.Height;
  765. break;
  766. case Edge.Top:
  767. allowConstrain = false;
  768. xTranslate = 0;
  769. yTranslate = -spBounds2.Y - spBounds2.Height;
  770. break;
  771. case Edge.TopRight:
  772. allowConstrain = true;
  773. xTranslate = -spBounds2.X;
  774. yTranslate = -spBounds2.Y - spBounds2.Height;
  775. break;
  776. case Edge.Left:
  777. allowConstrain = false;
  778. xTranslate = -spBounds2.X - spBounds2.Width;
  779. yTranslate = 0;
  780. break;
  781. case Edge.Right:
  782. allowConstrain = false;
  783. xTranslate = -spBounds2.X;
  784. yTranslate = 0;
  785. break;
  786. case Edge.BottomLeft:
  787. allowConstrain = true;
  788. xTranslate = -spBounds2.X - spBounds2.Width;
  789. yTranslate = -spBounds2.Y;
  790. break;
  791. case Edge.Bottom:
  792. allowConstrain = false;
  793. xTranslate = 0;
  794. yTranslate = -spBounds2.Y;
  795. break;
  796. case Edge.BottomRight:
  797. allowConstrain = true;
  798. xTranslate = -spBounds2.X;
  799. yTranslate = -spBounds2.Y;
  800. break;
  801. }
  802. translateMatrix.Translate(xTranslate, yTranslate, MatrixOrder.Append);
  803. float newWidth = spBounds2.Width + xulen;
  804. float newHeight = spBounds2.Height + yulen;
  805. float xScale = newWidth / spBounds2.Width;
  806. float yScale = newHeight / spBounds2.Height;
  807. if (allowConstrain && (this.ModifierKeys & Keys.Shift) != 0)
  808. {
  809. ConstrainScaling(this.context.liftedBounds, spBounds2.Width, spBounds2.Height,
  810. newWidth, newHeight, out xScale, out yScale);
  811. }
  812. translateMatrix.Scale(xScale, yScale, MatrixOrder.Append);
  813. translateMatrix.Translate(-xTranslate, -yTranslate, MatrixOrder.Append);
  814. translateMatrix.RotateAt(+tAngle, sp2BoundsCenter, MatrixOrder.Append);
  815. break;
  816. default:
  817. throw new InvalidEnumArgumentException();
  818. }
  819. this.context.deltaTransform.Reset();
  820. this.context.deltaTransform.Multiply(this.context.liftTransform, MatrixOrder.Append);
  821. this.context.deltaTransform.Multiply(translateMatrix, MatrixOrder.Append);
  822. translateMatrix.Multiply(this.context.baseTransform, MatrixOrder.Prepend);
  823. Selection.SetInterimTransform(translateMatrix);
  824. interim.Dispose();
  825. interim = null;
  826. }
  827. // advertise our angle of rotation to any host (i.e. mainform) that might want to use that information
  828. this.hostShouldShowAngle = this.rotateNub.Visible;
  829. this.hostAngle = -this.rotateNub.Angle;
  830. Selection.PerformChanged();
  831. dontDrop = false;
  832. Render(newOffset, true);
  833. Update();
  834. sbLogger.Append("a ");
  835. this.context.offset = newOffset;
  836. sbLogger.Append("b ");
  837. if (this.enableOutline)
  838. {
  839. DocumentWorkspace.ResetOutlineWhiteOpacity();
  840. }
  841. sbLogger.Append("c ");
  842. }
  843. sbLogger.Append("d ");
  844. }
  845. protected override void OnMouseUp(MouseEventArgs e)
  846. {
  847. DocumentWorkspace.SetEnableSelectionOutline(true);
  848. base.OnMouseUp(e);
  849. }
  850. public MoveToolBase(IDocumentWorkspace documentWorkspace, ImageResource toolBarImage, string name,
  851. string helpText, char hotKey, bool skipIfActiveOnHotKey, ToolBarConfigItems toolBarConfigItems)
  852. : base(documentWorkspace, toolBarImage, name, helpText, hotKey, skipIfActiveOnHotKey, toolBarConfigItems)
  853. {
  854. }
  855. }
  856. }