CustomDrawingExampleControl.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  5. using Avalonia;
  6. using Avalonia.Controls;
  7. using Avalonia.Media;
  8. using Avalonia.Input;
  9. using Avalonia.Threading;
  10. using Avalonia.Controls.Shapes;
  11. namespace ControlCatalog.Pages
  12. {
  13. public class CustomDrawingExampleControl : Control
  14. {
  15. private Point _cursorPoint;
  16. public static readonly StyledProperty<double> ScaleProperty = AvaloniaProperty.Register<CustomDrawingExampleControl, double>(nameof(Scale), 1.0d);
  17. public double Scale { get => GetValue(ScaleProperty); set => SetValue(ScaleProperty, value); }
  18. public static readonly StyledProperty<double> RotationProperty = AvaloniaProperty.Register<CustomDrawingExampleControl, double>(nameof(Rotation));
  19. /// <summary>
  20. /// Rotation, measured in Radians!
  21. /// </summary>
  22. public double Rotation
  23. {
  24. get => GetValue(RotationProperty);
  25. set
  26. {
  27. double valueToUse = value % (Math.PI * 2);
  28. SetValue(RotationProperty, valueToUse);
  29. }
  30. }
  31. public static readonly StyledProperty<double> ViewportCenterYProperty = AvaloniaProperty.Register<CustomDrawingExampleControl, double>(nameof(ViewportCenterY), 0.0d);
  32. public double ViewportCenterY { get => GetValue(ViewportCenterYProperty); set => SetValue(ViewportCenterYProperty, value); }
  33. public static readonly StyledProperty<double> ViewportCenterXProperty = AvaloniaProperty.Register<CustomDrawingExampleControl, double>(nameof(ViewportCenterX), 0.0d);
  34. public double ViewportCenterX { get => GetValue(ViewportCenterXProperty); set => SetValue(ViewportCenterXProperty, value); }
  35. private IPen _pen;
  36. private System.Diagnostics.Stopwatch _timeKeeper = System.Diagnostics.Stopwatch.StartNew();
  37. private bool _isPointerCaptured = false;
  38. public CustomDrawingExampleControl()
  39. {
  40. _pen = new Pen(new SolidColorBrush(Colors.Black), lineCap: PenLineCap.Round);
  41. var _arc = new ArcSegment()
  42. {
  43. IsLargeArc = false,
  44. Point = new Point(0, 0),
  45. RotationAngle = 0,
  46. Size = new Size(25, 25),
  47. SweepDirection = SweepDirection.Clockwise,
  48. };
  49. StreamGeometry sg = new StreamGeometry();
  50. using (var cntx = sg.Open())
  51. {
  52. cntx.BeginFigure(new Point(-25.0d, -10.0d), false);
  53. cntx.ArcTo(new Point(25.0d, -10.0d), new Size(10.0d, 10.0d), 0.0d, false, SweepDirection.Clockwise);
  54. cntx.EndFigure(true);
  55. }
  56. _smileGeometry = sg.Clone();
  57. }
  58. private Geometry _smileGeometry;
  59. protected override void OnPointerMoved(PointerEventArgs e)
  60. {
  61. base.OnPointerMoved(e);
  62. Point previousPoint = _cursorPoint;
  63. _cursorPoint = e.GetPosition(this);
  64. if (_isPointerCaptured)
  65. {
  66. Point oldWorldPoint = UIPointToWorldPoint(previousPoint, ViewportCenterX, ViewportCenterY, Scale, Rotation);
  67. Point newWorldPoint = UIPointToWorldPoint(_cursorPoint, ViewportCenterX, ViewportCenterY, Scale, Rotation);
  68. Vector diff = newWorldPoint - oldWorldPoint;
  69. ViewportCenterX -= diff.X;
  70. ViewportCenterY -= diff.Y;
  71. }
  72. }
  73. protected override void OnPointerPressed(PointerPressedEventArgs e)
  74. {
  75. e.Handled = true;
  76. e.Pointer.Capture(this);
  77. _isPointerCaptured = true;
  78. base.OnPointerPressed(e);
  79. }
  80. protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
  81. {
  82. base.OnPointerWheelChanged(e);
  83. var oldScale = Scale;
  84. Scale *= (1.0d + e.Delta.Y / 12.0d);
  85. Point oldWorldPoint = UIPointToWorldPoint(_cursorPoint, ViewportCenterX, ViewportCenterY, oldScale, Rotation);
  86. Point newWorldPoint = UIPointToWorldPoint(_cursorPoint, ViewportCenterX, ViewportCenterY, Scale, Rotation);
  87. Vector diff = newWorldPoint - oldWorldPoint;
  88. ViewportCenterX -= diff.X;
  89. ViewportCenterY -= diff.Y;
  90. }
  91. protected override void OnPointerReleased(PointerReleasedEventArgs e)
  92. {
  93. e.Pointer.Capture(null);
  94. _isPointerCaptured = false;
  95. base.OnPointerReleased(e);
  96. }
  97. public override void Render(DrawingContext context)
  98. {
  99. var localBounds = new Rect(new Size(this.Bounds.Width, this.Bounds.Height));
  100. var clip = context.PushClip(this.Bounds);
  101. context.DrawRectangle(Brushes.White, _pen, localBounds, 1.0d);
  102. var halfMax = Math.Max(this.Bounds.Width / 2.0d, this.Bounds.Height / 2.0d) * Math.Sqrt(2.0d);
  103. var halfMin = Math.Min(this.Bounds.Width / 2.0d, this.Bounds.Height / 2.0d) / 1.3d;
  104. var halfWidth = this.Bounds.Width / 2.0d;
  105. var halfHeight = this.Bounds.Height / 2.0d;
  106. // 0,0 refers to the top-left of the control now. It is not prime time to draw gui stuff because it'll be under the world
  107. var translateModifier = context.PushTransform(Avalonia.Matrix.CreateTranslation(new Avalonia.Vector(halfWidth, halfHeight)));
  108. // now 0,0 refers to the ViewportCenter(X,Y).
  109. var rotationMatrix = Avalonia.Matrix.CreateRotation(Rotation);
  110. var rotationModifier = context.PushTransform(rotationMatrix);
  111. // everything is rotated but not scaled
  112. var scaleModifier = context.PushTransform(Avalonia.Matrix.CreateScale(Scale, -Scale));
  113. var mapPositionModifier = context.PushTransform(Matrix.CreateTranslation(new Vector(-ViewportCenterX, -ViewportCenterY)));
  114. // now everything is rotated and scaled, and at the right position, now we're drawing strictly in world coordinates
  115. context.DrawEllipse(Brushes.White, _pen, new Point(0.0d, 0.0d), 50.0d, 50.0d);
  116. context.DrawLine(_pen, new Point(-25.0d, -5.0d), new Point(-25.0d, 15.0d));
  117. context.DrawLine(_pen, new Point(25.0d, -5.0d), new Point(25.0d, 15.0d));
  118. context.DrawGeometry(null, _pen, _smileGeometry);
  119. Point cursorInWorldPoint = UIPointToWorldPoint(_cursorPoint, ViewportCenterX, ViewportCenterY, Scale, Rotation);
  120. context.DrawEllipse(Brushes.Gray, _pen, cursorInWorldPoint, 20.0d, 20.0d);
  121. for (int i = 0; i < 10; i++)
  122. {
  123. double orbitRadius = i * 100 + 200;
  124. var orbitInput = ((_timeKeeper.Elapsed.TotalMilliseconds + 987654d) / orbitRadius) / 10.0d;
  125. if (i % 3 == 0)
  126. orbitInput *= -1;
  127. Point orbitPosition = new Point(Math.Sin(orbitInput) * orbitRadius, Math.Cos(orbitInput) * orbitRadius);
  128. context.DrawEllipse(Brushes.Gray, _pen, orbitPosition, 20.0d, 20.0d);
  129. }
  130. // end drawing the world
  131. mapPositionModifier.Dispose();
  132. scaleModifier.Dispose();
  133. rotationModifier.Dispose();
  134. translateModifier.Dispose();
  135. // this is prime time to draw gui stuff
  136. context.DrawLine(_pen, _cursorPoint + new Vector(-20, 0), _cursorPoint + new Vector(20, 0));
  137. context.DrawLine(_pen, _cursorPoint + new Vector(0, -20), _cursorPoint + new Vector(0, 20));
  138. clip.Dispose();
  139. // oh and draw again when you can, no rush, right?
  140. Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background);
  141. }
  142. private Point UIPointToWorldPoint(Point inPoint, double viewportCenterX, double viewportCenterY, double scale, double rotation)
  143. {
  144. Point workingPoint = new Point(inPoint.X, -inPoint.Y);
  145. workingPoint += new Vector(-this.Bounds.Width / 2.0d, this.Bounds.Height / 2.0d);
  146. workingPoint /= scale;
  147. workingPoint = Matrix.CreateRotation(rotation).Transform(workingPoint);
  148. workingPoint += new Vector(viewportCenterX, viewportCenterY);
  149. return workingPoint;
  150. }
  151. private Point WorldPointToUIPoint(Point inPoint, double viewportCenterX, double viewportCenterY, double scale, double rotation)
  152. {
  153. Point workingPoint = new Point(inPoint.X, inPoint.Y);
  154. workingPoint -= new Vector(viewportCenterX, viewportCenterY);
  155. // undo rotation
  156. workingPoint = Matrix.CreateRotation(-rotation).Transform(workingPoint);
  157. workingPoint *= scale;
  158. workingPoint -= new Vector(-this.Bounds.Width / 2.0d, this.Bounds.Height / 2.0d);
  159. workingPoint = new Point(workingPoint.X, -workingPoint.Y);
  160. return workingPoint;
  161. }
  162. }
  163. }