PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/tools/MoveToolBase.cs

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