RectangleTool.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 System;
  10. using System.Collections.Generic;
  11. using System.Drawing;
  12. using System.Drawing.Drawing2D;
  13. using System.Windows.Forms;
  14. namespace PaintDotNet.Measurement.Tools
  15. {
  16. public class RectangleTool : ShapeTool
  17. {
  18. private ImageResource rectangleToolIcon;
  19. private string statusTextFormat = PdnResources.GetString("RectangleTool.StatusText.Format");
  20. private Cursor rectangleToolCursor;
  21. protected override List<PointF> TrimShapePath(List<PointF> points)
  22. {
  23. List<PointF> array = new List<PointF>();
  24. if (points.Count > 0)
  25. {
  26. array.Add(points[0]);
  27. if (points.Count > 1)
  28. {
  29. array.Add(points[points.Count - 1]);
  30. }
  31. }
  32. return array;
  33. }
  34. public override PixelOffsetMode GetPixelOffsetMode()
  35. {
  36. if (AppEnvironment.PenInfo().Width == 1.0f)
  37. {
  38. return PixelOffsetMode.None;
  39. }
  40. return base.GetPixelOffsetMode();
  41. }
  42. protected override PdnGraphicsPath CreateShapePath(PointF[] points)
  43. {
  44. PointF a = points[0];
  45. PointF b = points[points.Length - 1];
  46. RectangleF rect;
  47. if ((ModifierKeys & Keys.Shift) != 0)
  48. {
  49. rect = Utility.PointsToConstrainedRectangle(a, b);
  50. }
  51. else
  52. {
  53. rect = Utility.PointsToRectangle(a, b);
  54. }
  55. PdnGraphicsPath path = new PdnGraphicsPath();
  56. path.AddRectangle(rect);
  57. path.CloseFigure();
  58. path.Reverse();
  59. MeasurementUnit units = AppWorkspace.GetUnits();
  60. double widthPhysical = Math.Abs(Document.PixelToPhysicalX(rect.Width, units));
  61. double heightPhysical = Math.Abs(Document.PixelToPhysicalY(rect.Height, units));
  62. double areaPhysical = widthPhysical * heightPhysical;
  63. string numberFormat;
  64. string unitsAbbreviation;
  65. if (units != MeasurementUnit.Pixel)
  66. {
  67. string unitsAbbreviationName = "MeasurementUnit." + units.ToString() + ".Abbreviation";
  68. unitsAbbreviation = PdnResources.GetString(unitsAbbreviationName);
  69. numberFormat = "F2";
  70. }
  71. else
  72. {
  73. unitsAbbreviation = string.Empty;
  74. numberFormat = "F0";
  75. }
  76. string unitsString = PdnResources.GetString("MeasurementUnit." + units.ToString() + ".Plural");
  77. string statusText = string.Format(
  78. this.statusTextFormat,
  79. widthPhysical.ToString(numberFormat),
  80. unitsAbbreviation,
  81. heightPhysical.ToString(numberFormat),
  82. unitsAbbreviation,
  83. areaPhysical.ToString(numberFormat),
  84. unitsString);
  85. this.SetStatus(this.rectangleToolIcon, statusText);
  86. return path;
  87. }
  88. protected override void OnActivate()
  89. {
  90. rectangleToolCursor = new Cursor(PdnResources.GetResourceStream("Cursors.RectangleToolCursor.cur"));
  91. this.rectangleToolIcon = this.Image;
  92. this.Cursor = rectangleToolCursor;
  93. base.OnActivate();
  94. }
  95. protected override void OnDeactivate()
  96. {
  97. if (this.rectangleToolCursor != null)
  98. {
  99. this.rectangleToolCursor.Dispose();
  100. this.rectangleToolCursor = null;
  101. }
  102. base.OnDeactivate();
  103. }
  104. public RectangleTool(IDocumentWorkspace documentWorkspace)
  105. : base(documentWorkspace,
  106. PdnResources.GetImageResource("Icons.RectangleToolIcon.png"),
  107. PdnResources.GetString("RectangleTool.Name"),
  108. PdnResources.GetString("RectangleTool.HelpText"))
  109. {
  110. }
  111. }
  112. }