FlipHorizintalCanvas.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 FurnaceUI.Controls.Common
  9. {
  10. public class FlipHorizintalCanvas : Canvas
  11. {
  12. private Storyboard currentStoryboard;
  13. public bool ShowAxisPoint
  14. {
  15. get { return (bool)GetValue(ShowAxisPointProperty); }
  16. set { SetValue(ShowAxisPointProperty, value); }
  17. }
  18. // Using a DependencyProperty as the backing store for ShowAxisPoint. This enables animation, styling, binding, etc...
  19. public static readonly DependencyProperty ShowAxisPointProperty =
  20. DependencyProperty.Register("ShowAxisPoint", typeof(bool), typeof(FlipHorizintalCanvas), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  21. public static readonly DependencyProperty AxisLeftProperty;
  22. static FlipHorizintalCanvas()
  23. {
  24. DefaultStyleKeyProperty.OverrideMetadata(typeof(FlipHorizintalCanvas), new FrameworkPropertyMetadata(typeof(FlipHorizintalCanvas)));
  25. AxisLeftProperty = DependencyProperty.Register("AxisLeft", typeof(int), typeof(FlipHorizintalCanvas), new UIPropertyMetadata(8));
  26. }
  27. public int AxisLeft
  28. {
  29. get
  30. {
  31. return (int)GetValue(AxisLeftProperty);
  32. }
  33. set
  34. {
  35. SetValue(AxisLeftProperty, value);
  36. }
  37. }
  38. protected override void OnRender(DrawingContext dc)
  39. {
  40. base.OnRender(dc);
  41. if (ShowAxisPoint)
  42. {
  43. dc.DrawEllipse(Brushes.Black, new Pen(Brushes.White, 2), new Point(AxisLeft, Height / 2), 5, 5);
  44. }
  45. // var text = new FormattedText(
  46. //CurrentAngle.ToString(),
  47. //CultureInfo.GetCultureInfo("en-us"),
  48. //FlowDirection.LeftToRight,
  49. //new Typeface("Verdana"),
  50. //14,
  51. //Brushes.Black);
  52. // dc.DrawText(text, new Point(AxisLeft, 60));
  53. }
  54. public double CurrentAngle
  55. {
  56. get
  57. {
  58. if (RenderTransform is RotateTransform)
  59. {
  60. return (RenderTransform as RotateTransform).Angle;
  61. }
  62. return 0;
  63. }
  64. }
  65. public void Rotate(int angle, bool absolute = true, int ms = 0, double accelerationRatio = 0, double decelerationRatio = 0, Action onComplete = null)
  66. {
  67. var storyboard = new Storyboard();
  68. storyboard.Completed += (s, e) => onComplete?.Invoke();
  69. if (Rotate(storyboard, angle, absolute, ms, accelerationRatio, decelerationRatio, onComplete))
  70. {
  71. storyboard.Begin();
  72. }
  73. else
  74. {
  75. onComplete?.Invoke();
  76. }
  77. }
  78. public double oldScale = 0;
  79. public bool Rotate(Storyboard storyboard, int angle, bool absolute = true, int ms = 0, double accelerationRatio = 0, double decelerationRatio = 0, Action onComplete = null)
  80. {
  81. //var oldAngle = 0d;
  82. //if (RenderTransform is RotateTransform)
  83. //{
  84. // oldAngle = (RenderTransform as RotateTransform).Angle;
  85. //}
  86. //var newAngle = absolute ? angle : oldAngle + angle;
  87. //if (newAngle == oldAngle)
  88. //{
  89. // return false;
  90. //}
  91. //var top = Height / 2;
  92. //var leftPoint = new Point(AxisLeft, top);
  93. //if (ms <= 0)
  94. //{
  95. // RenderTransform = new RotateTransform(newAngle, leftPoint.X, leftPoint.Y);
  96. // return false;
  97. // //InvalidateVisual();
  98. //}
  99. //else
  100. //{
  101. // //var rotateTransform = new RotateTransform();
  102. // //rotateTransform.CenterX = leftPoint.X;
  103. // //rotateTransform.CenterY = leftPoint.Y;
  104. // //RenderTransform = rotateTransform;
  105. // //var animation = new DoubleAnimation(oldAngle, newAngle, TimeSpan.FromMilliseconds(ms));
  106. // //animation.AccelerationRatio = accelerationRatio;
  107. // //animation.DecelerationRatio = decelerationRatio;
  108. // //storyboard.Children.Add(animation);
  109. // //Storyboard.SetTarget(animation, this);
  110. // //Storyboard.SetTargetProperty(animation, new PropertyPath("RenderTransform.Angle"));
  111. // //return true;
  112. //}
  113. var newScale = 0.5;
  114. //if (angle >= 180)
  115. //{
  116. // newScale = 1;
  117. //}
  118. if (newScale == oldScale)
  119. {
  120. return false;
  121. }
  122. TranslateTransform translateTransform = new TranslateTransform();
  123. translateTransform.X = angle;
  124. TransformGroup Group = new TransformGroup();
  125. Group.Children.Add(new TranslateTransform());
  126. RenderTransform = Group;
  127. var animation = new DoubleAnimation(oldScale, angle, TimeSpan.FromMilliseconds(ms));
  128. animation.AccelerationRatio = accelerationRatio;
  129. animation.DecelerationRatio = decelerationRatio;
  130. storyboard.Children.Add(animation);
  131. Storyboard.SetTarget(animation, this);
  132. Storyboard.SetTargetProperty(animation, new PropertyPath("RenderTransform.(TransformGroup.Children)[0].(TranslateTransform.X)"));
  133. oldScale = angle;
  134. return true;
  135. }
  136. public void Stop()
  137. {
  138. if (currentStoryboard != null)
  139. {
  140. currentStoryboard.Stop(this);
  141. currentStoryboard = null;
  142. }
  143. }
  144. }
  145. }