AxisCanvas.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Globalization;
  3. using System.Threading;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using System.Windows.Media.Animation;
  8. namespace VirgoUI.Controls.Common
  9. {
  10. public class AxisCanvas : Canvas
  11. {
  12. private Storyboard currentStoryboard;
  13. public bool ShowAxisPoint
  14. {
  15. get; set;
  16. }
  17. public static readonly DependencyProperty AxisLeftProperty;
  18. static AxisCanvas()
  19. {
  20. DefaultStyleKeyProperty.OverrideMetadata(typeof(AxisCanvas), new FrameworkPropertyMetadata(typeof(AxisCanvas)));
  21. AxisLeftProperty = DependencyProperty.Register("AxisLeft", typeof(int), typeof(AxisCanvas), new UIPropertyMetadata(8));
  22. }
  23. public int AxisLeft
  24. {
  25. get
  26. {
  27. return (int)GetValue(AxisLeftProperty);
  28. }
  29. set
  30. {
  31. SetValue(AxisLeftProperty, value);
  32. }
  33. }
  34. protected override void OnRender(DrawingContext dc)
  35. {
  36. base.OnRender(dc);
  37. if (ShowAxisPoint)
  38. {
  39. dc.DrawEllipse(Brushes.Black, new Pen(Brushes.Black, 2), new Point(AxisLeft, Height / 2), 5, 5);
  40. }
  41. // var text = new FormattedText(
  42. //CurrentAngle.ToString(),
  43. //CultureInfo.GetCultureInfo("en-us"),
  44. //FlowDirection.LeftToRight,
  45. //new Typeface("Verdana"),
  46. //14,
  47. //Brushes.Black);
  48. // dc.DrawText(text, new Point(AxisLeft, 60));
  49. }
  50. public double CurrentAngle
  51. {
  52. get
  53. {
  54. if (RenderTransform is RotateTransform)
  55. {
  56. return (RenderTransform as RotateTransform).Angle;
  57. }
  58. return 0;
  59. }
  60. }
  61. public void Rotate(int angle, bool absolute = true, int ms = 0, double accelerationRatio = 0, double decelerationRatio = 0, Action onComplete = null)
  62. {
  63. var oldAngle = 0d;
  64. if (RenderTransform is RotateTransform)
  65. {
  66. oldAngle = (RenderTransform as RotateTransform).Angle;
  67. }
  68. var newAngle = absolute ? angle : oldAngle + angle;
  69. var leftPoint = new Point(AxisLeft, Height / 2);
  70. if (ms <= 0)
  71. {
  72. RenderTransform = new RotateTransform(newAngle, leftPoint.X, leftPoint.Y);
  73. InvalidateVisual();
  74. currentStoryboard = null;
  75. }
  76. else
  77. {
  78. var storyBoard = new Storyboard();
  79. var rotateTransform = new RotateTransform
  80. {
  81. CenterX = leftPoint.X,
  82. CenterY = leftPoint.Y
  83. };
  84. RenderTransform = rotateTransform;
  85. var animation = new DoubleAnimation(oldAngle, newAngle, TimeSpan.FromMilliseconds(ms))
  86. {
  87. AccelerationRatio = accelerationRatio,
  88. DecelerationRatio = decelerationRatio
  89. };
  90. storyBoard.Children.Add(animation);
  91. Storyboard.SetTarget(animation, this);
  92. Storyboard.SetTargetProperty(animation, new PropertyPath("RenderTransform.Angle"));
  93. storyBoard.Completed += (s, e) =>
  94. {
  95. InvalidateVisual();
  96. currentStoryboard = null;
  97. onComplete?.Invoke();
  98. };
  99. currentStoryboard = storyBoard;
  100. storyBoard.Begin(this, true);
  101. }
  102. }
  103. public void Stop()
  104. {
  105. if (currentStoryboard != null)
  106. {
  107. currentStoryboard.Stop(this);
  108. currentStoryboard = null;
  109. }
  110. }
  111. }
  112. }