BrushPreviewRenderer.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. namespace PaintDotNet.Measurement
  5. {
  6. /// <summary>
  7. /// Renders a preview of the brush
  8. /// </summary>
  9. internal class BrushPreviewRenderer
  10. : SurfaceBoxRenderer
  11. {
  12. private float brushSize;
  13. public float BrushSize
  14. {
  15. get
  16. {
  17. return this.brushSize;
  18. }
  19. set
  20. {
  21. RectangleF rect1 = GetInvalidateBrushRect();
  22. this.brushSize = value;
  23. RectangleF rect2 = GetInvalidateBrushRect();
  24. Invalidate(RectangleF.Union(rect1, rect2));
  25. }
  26. }
  27. private PointF brushLocation = new PointF(-500.0f, -500.0f);
  28. public PointF BrushLocation
  29. {
  30. get
  31. {
  32. return this.brushLocation;
  33. }
  34. set
  35. {
  36. RectangleF rect1 = GetInvalidateBrushRect();
  37. this.brushLocation = value;
  38. RectangleF rect2 = GetInvalidateBrushRect();
  39. Invalidate(RectangleF.Union(rect1, rect2));
  40. }
  41. }
  42. private int brushAlpha = 255;
  43. public int BrushAlpha
  44. {
  45. get
  46. {
  47. return this.brushAlpha;
  48. }
  49. set
  50. {
  51. this.brushAlpha = value;
  52. InvalidateBrushLocation();
  53. }
  54. }
  55. protected override void OnVisibleChanged()
  56. {
  57. InvalidateBrushLocation();
  58. }
  59. private RectangleF GetInvalidateBrushRect()
  60. {
  61. float ratio = (float)this.OwnerList.ScaleFactor.Ratio;
  62. PointF location = this.BrushLocation;
  63. RectangleF rectF = Utility.RectangleFromCenter(location, this.brushSize);
  64. rectF.Inflate(Math.Max(4.0f, 4.0f / ratio), Math.Max(4.0f, 4.0f / ratio));
  65. return rectF;
  66. }
  67. private void InvalidateBrushLocation()
  68. {
  69. RectangleF rectF = GetInvalidateBrushRect();
  70. Invalidate(rectF);
  71. }
  72. public override void Render(Surface dst, System.Drawing.Point offset)
  73. {
  74. using (RenderArgs ra = new RenderArgs(dst))
  75. {
  76. ra.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  77. PointF ptF = this.BrushLocation;
  78. if (this.BrushSize == 0.5f)
  79. {
  80. ptF.X += 0.5f;
  81. ptF.Y += 0.5f;
  82. }
  83. ptF.X *= (float)OwnerList.ScaleFactor.Ratio;
  84. ptF.Y *= (float)OwnerList.ScaleFactor.Ratio;
  85. ra.Graphics.TranslateTransform(-offset.X, -offset.Y, MatrixOrder.Append);
  86. RectangleF brushRect = Utility.RectangleFromCenter(ptF, this.BrushSize * (float)OwnerList.ScaleFactor.Ratio);
  87. using (Pen white = new Pen(Color.FromArgb(this.brushAlpha, Color.White), -1.0f),
  88. black = new Pen(Color.FromArgb(this.brushAlpha, Color.Black), -1.0f))
  89. {
  90. brushRect.Inflate(-2, -2);
  91. ra.Graphics.DrawEllipse(white, brushRect);
  92. brushRect.Inflate(1, 1);
  93. ra.Graphics.DrawEllipse(black, brushRect);
  94. brushRect.Inflate(1, 1);
  95. ra.Graphics.DrawEllipse(white, brushRect);
  96. }
  97. }
  98. }
  99. public BrushPreviewRenderer(SurfaceBoxRendererList ownerList)
  100. : base(ownerList)
  101. {
  102. }
  103. }
  104. }