EllipseTool.cs 4.3 KB

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