CustomDrawingExampleControl.cs 8.7 KB

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