RectangleSelectTool.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using PaintDotNet.Measurement.Enum;
  2. using PaintDotNet.Measurement.ObjInfo;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8. namespace PaintDotNet.Measurement.Tools
  9. {
  10. public class RectangleSelectTool : SelectionTool
  11. {
  12. protected override List<Point> TrimShapePath(System.Collections.Generic.List<Point> tracePoints)
  13. {
  14. List<Point> array = new List<Point>();
  15. if (tracePoints.Count > 0)
  16. {
  17. array.Add(tracePoints[0]);
  18. if (tracePoints.Count > 1)
  19. {
  20. array.Add(tracePoints[tracePoints.Count - 1]);
  21. }
  22. }
  23. return array;
  24. }
  25. protected override List<PointF> CreateShape(List<Point> tracePoints)
  26. {
  27. Point a = tracePoints[0];
  28. Point b = tracePoints[tracePoints.Count - 1];
  29. Rectangle rect;
  30. SelectionDrawModeInfo sdmInfo = AppEnvironment.SelectionDrawModeInfo();
  31. switch (sdmInfo.DrawMode)
  32. {
  33. case SelectionDrawMode.Normal:
  34. if ((ModifierKeys & Keys.Shift) != 0)
  35. {
  36. rect = Utility.PointsToConstrainedRectangle(a, b);
  37. }
  38. else
  39. {
  40. rect = Utility.PointsToRectangle(a, b);
  41. }
  42. break;
  43. case SelectionDrawMode.FixedRatio:
  44. try
  45. {
  46. int drawnWidth = b.X - a.X;
  47. int drawnHeight = b.Y - a.Y;
  48. double drawnWidthScale = (double)drawnWidth / (double)sdmInfo.Width;
  49. double drawnWidthSign = Math.Sign(drawnWidthScale);
  50. double drawnHeightScale = (double)drawnHeight / (double)sdmInfo.Height;
  51. double drawnHeightSign = Math.Sign(drawnHeightScale);
  52. double aspect = (double)sdmInfo.Width / (double)sdmInfo.Height;
  53. if (drawnWidthScale < drawnHeightScale)
  54. {
  55. rect = Utility.PointsToRectangle(
  56. new Point(a.X, a.Y),
  57. new Point(a.X + drawnWidth, a.Y + (int)(drawnHeightSign * Math.Abs((double)drawnWidth / aspect))));
  58. }
  59. else
  60. {
  61. rect = Utility.PointsToRectangle(
  62. new Point(a.X, a.Y),
  63. new Point(a.X + (int)(drawnWidthSign * Math.Abs((double)drawnHeight * aspect)), a.Y + drawnHeight));
  64. }
  65. }
  66. catch (ArithmeticException)
  67. {
  68. rect = new Rectangle(a.X, a.Y, 0, 0);
  69. }
  70. break;
  71. case SelectionDrawMode.FixedSize:
  72. double pxWidth = Document.ConvertMeasurement(sdmInfo.Width, sdmInfo.Units, this.Document.DpuUnit, this.Document.DpuX, MeasurementUnit.Pixel);
  73. double pxHeight = Document.ConvertMeasurement(sdmInfo.Height, sdmInfo.Units, this.Document.DpuUnit, this.Document.DpuY, MeasurementUnit.Pixel);
  74. rect = new Rectangle(b.X, b.Y, (int)pxWidth, (int)pxHeight);
  75. break;
  76. default:
  77. throw new InvalidEnumArgumentException();
  78. }
  79. rect.Intersect(DocumentWorkspace.GetDocument().Bounds);
  80. List<PointF> shape;
  81. if (rect.Width > 0 && rect.Height > 0)
  82. {
  83. shape = new List<PointF>(5);
  84. shape.Add(new PointF(rect.Left, rect.Top));
  85. shape.Add(new PointF(rect.Right, rect.Top));
  86. shape.Add(new PointF(rect.Right, rect.Bottom));
  87. shape.Add(new PointF(rect.Left, rect.Bottom));
  88. shape.Add(shape[0]);
  89. }
  90. else
  91. {
  92. shape = new List<PointF>(0);
  93. }
  94. return shape;
  95. }
  96. protected override void OnActivate()
  97. {
  98. SetCursors(
  99. "Cursors.RectangleSelectToolCursor.cur",
  100. "Cursors.RectangleSelectToolCursorMinus.cur",
  101. "Cursors.RectangleSelectToolCursorPlus.cur",
  102. "Cursors.RectangleSelectToolCursorMouseDown.cur");
  103. base.OnActivate();
  104. }
  105. protected override void OnDeactivate()
  106. {
  107. base.OnDeactivate();
  108. }
  109. public RectangleSelectTool(IDocumentWorkspace documentWorkspace)
  110. : base(documentWorkspace,
  111. PdnResources.GetImageResource("Icons.RectangleSelectToolIcon.png"),
  112. PdnResources.GetString("RectangleSelectTool.Name"),
  113. PdnResources.GetString("RectangleSelectTool.HelpText"),
  114. 's',
  115. ToolBarConfigItems.SelectionDrawMode)
  116. {
  117. }
  118. }
  119. }