DrawingPointGrab.xaml.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using MuchInfo.Chart.Data.EnumTypes;
  2. using MuchInfo.Chart.WPF.Primitives.Interfaces;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. namespace MuchInfo.Chart.WPF.Controls.Drawing
  8. {
  9. /// <summary>
  10. /// DrawingPointGrab.xaml 的交互逻辑
  11. /// </summary>
  12. public partial class DrawingPointGrab : UserControl
  13. {
  14. #region Fields
  15. private Chart _chart;
  16. private IDrawingTool _drawingTool;
  17. private bool _flag;
  18. private bool _isMouseEnter;
  19. private int _pointIndex;
  20. #endregion Fields
  21. #region Constructors
  22. //private bool DrawingPointGrab_846;
  23. public DrawingPointGrab()
  24. {
  25. base.MouseEnter -= (new MouseEventHandler(this.theBorder_MouseEnter));
  26. base.MouseEnter += (new MouseEventHandler(this.theBorder_MouseEnter));
  27. base.LostMouseCapture -= (new MouseEventHandler(this.DrawingPointGrab_LostMouseCapture));
  28. base.LostMouseCapture += (new MouseEventHandler(this.DrawingPointGrab_LostMouseCapture));
  29. base.MouseLeave -= (new MouseEventHandler(this.theBorder_MouseLeave));
  30. base.MouseLeave += (new MouseEventHandler(this.theBorder_MouseLeave));
  31. base.MouseLeftButtonDown -= (new MouseButtonEventHandler(this.theBorder_MouseLeftButtonDown));
  32. base.MouseLeftButtonDown += (new MouseButtonEventHandler(this.theBorder_MouseLeftButtonDown));
  33. base.MouseLeftButtonUp -= (new MouseButtonEventHandler(this.theBorder_MouseLeftButtonUp));
  34. base.MouseLeftButtonUp += (new MouseButtonEventHandler(this.theBorder_MouseLeftButtonUp));
  35. base.MouseMove -= (new MouseEventHandler(this.theBorder_MouseMove));
  36. base.MouseMove += (new MouseEventHandler(this.theBorder_MouseMove));
  37. this._isMouseEnter = false;
  38. this._flag = false;
  39. this.InitializeComponent();
  40. }
  41. #endregion Constructors
  42. #region Methods
  43. #region Public Methods
  44. public void Setup(IDrawingTool tool, int pointIndex, Chart chart)
  45. {
  46. this.Cursor = Cursors.Hand;
  47. this._drawingTool = tool;
  48. this._pointIndex = pointIndex;
  49. this._chart = chart;
  50. this.Arrows.Visibility = Visibility.Collapsed;
  51. this.LayoutRoot.BorderBrush = (new SolidColorBrush(Colors.Transparent));
  52. bool isDarkBackground = this._chart.IsDarkBackground;
  53. if (isDarkBackground)
  54. {
  55. this.dot.Stroke = (new SolidColorBrush(Color.FromArgb(150, 255, 255, 255)));
  56. }
  57. else
  58. {
  59. this.dot.Stroke = (new SolidColorBrush(Color.FromArgb(150, 0, 0, 0)));
  60. }
  61. }
  62. #endregion Public Methods
  63. #region Internal Methods
  64. internal void ShowPointGrab()
  65. {
  66. Point point = this._drawingTool[this._pointIndex];
  67. this.ShowPointGrab(point.X, point.Y);
  68. }
  69. #endregion Internal Methods
  70. #region Private Methods
  71. private void UpdateStroke()
  72. {
  73. if (this._isMouseEnter || this._flag)
  74. {
  75. this.Arrows.Visibility = Visibility.Visible;
  76. if (this._chart.IsDarkBackground)
  77. {
  78. this.dot.Stroke = (new SolidColorBrush(Color.FromArgb(255, 255, 255, 255)));
  79. this.L1.Stroke = (new SolidColorBrush(Colors.Yellow));
  80. }
  81. else
  82. {
  83. this.dot.Stroke = (new SolidColorBrush(Color.FromArgb(255, 0, 0, 0)));
  84. this.L1.Stroke = (new SolidColorBrush(Colors.Blue));
  85. }
  86. this.L2.Stroke = this.L1.Stroke;
  87. this.L3.Stroke = this.L1.Stroke;
  88. this.L4.Stroke = this.L1.Stroke;
  89. }
  90. else
  91. {
  92. this.dot.Stroke = this._chart.IsDarkBackground ? new SolidColorBrush(Color.FromArgb(150, 255, 255, 255)) :
  93. new SolidColorBrush(Color.FromArgb(150, 0, 0, 0));
  94. this.Arrows.Visibility = Visibility.Collapsed;
  95. }
  96. }
  97. private FrameworkElement GetPaintAreaCanvas()
  98. {
  99. var parent = this.Parent as FrameworkElement;
  100. if (parent == null) return null;
  101. var grandparent = parent.Parent as FrameworkElement;
  102. if (grandparent == null) return null;
  103. var paintArea = grandparent.Parent as FrameworkElement;
  104. return paintArea;
  105. }
  106. private void UpdatePosition(MouseEventArgs e)
  107. {
  108. var paintArea = GetPaintAreaCanvas();
  109. if (paintArea == null) return;
  110. var position = e.GetPosition(paintArea);
  111. var x = (float)paintArea.ActualWidth;
  112. x = x < 0 ? 0 : x;
  113. var y = (paintArea.ActualHeight - 6.0);
  114. y = y < 0 ? 0 : y;
  115. position.X = position.X < 0 ? 0 : position.X;
  116. position.Y = position.Y < 0 ? 0 : position.Y;
  117. position.X = position.X > x ? x : position.X;
  118. position.Y = position.Y > y ? y : position.Y;
  119. this.ShowPointGrab(position.X, position.Y);
  120. this._drawingTool[this._pointIndex] = position;
  121. //bool flag = this.GetPaintAreaCanvas() != null && this.GetPaintAreaCanvas() is FrameworkElement;
  122. //if (flag)
  123. //{
  124. // Point position = e.GetPosition((UIElement)this.GetPaintAreaCanvas());
  125. // FrameworkElement frameworkElement = (FrameworkElement)this.GetPaintAreaCanvas();
  126. // float num = (float)(frameworkElement.ActualHeight - 6.0);
  127. // flag = (num < 0f);
  128. // if (flag)
  129. // {
  130. // num = 0f;
  131. // }
  132. // float num2 = (float)frameworkElement.ActualWidth;
  133. // flag = (num2 < 0f);
  134. // if (flag)
  135. // {
  136. // num2 = 0f;
  137. // }
  138. // flag = (position.X < 0.0);
  139. // if (flag)
  140. // {
  141. // position.X = (0.0);
  142. // }
  143. // flag = (position.Y < 0.0);
  144. // if (flag)
  145. // {
  146. // position.Y = (0.0);
  147. // }
  148. // flag = (position.X > (double)num2);
  149. // if (flag)
  150. // {
  151. // position.X = ((double)num2);
  152. // }
  153. // flag = (position.Y > (double)num);
  154. // if (flag)
  155. // {
  156. // position.Y = ((double)num);
  157. // }
  158. // this.ShowPointGrab(position.X, position.Y);
  159. // this._drawingTool[this._pointIndex] = position;
  160. //}
  161. }
  162. private void DrawingPointGrab_LostMouseCapture(object sender, MouseEventArgs e)
  163. {
  164. if (this._flag)
  165. {
  166. this._flag = false;
  167. this._chart.LockPainting = false;
  168. this._chart.Refresh();
  169. this.UpdateStroke();
  170. }
  171. }
  172. private void theBorder_MouseEnter(object sender, MouseEventArgs e)
  173. {
  174. bool flag = this._chart.PointerType == PointerType.Drawing;
  175. if (!flag)
  176. {
  177. this._isMouseEnter = true;
  178. this.UpdateStroke();
  179. }
  180. }
  181. private void theBorder_MouseLeave(object sender, MouseEventArgs e)
  182. {
  183. this._isMouseEnter = false;
  184. this.UpdateStroke();
  185. }
  186. private void theBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  187. {
  188. if (this._chart.PointerType != PointerType.Drawing)
  189. {
  190. if (this._chart.PointerType == PointerType.Erase)
  191. {
  192. this._drawingTool.Delete();
  193. }
  194. else
  195. {
  196. this._chart.LockPainting = true;
  197. this.CaptureMouse();
  198. this.UpdatePosition(e);
  199. this._flag = true;
  200. this.UpdateStroke();
  201. }
  202. }
  203. }
  204. private void theBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  205. {
  206. bool flag = this._flag;
  207. if (flag)
  208. {
  209. this.ReleaseMouseCapture();
  210. this.UpdatePosition(e);
  211. flag = (this._drawingTool.OwnerSpec != null);
  212. if (flag)
  213. {
  214. this._drawingTool.OwnerSpec.ToolChanged(this._drawingTool);
  215. }
  216. this._flag = false;
  217. this._chart.LockPainting = false;
  218. this._chart.Refresh();
  219. this.UpdateStroke();
  220. }
  221. }
  222. private void theBorder_MouseMove(object sender, MouseEventArgs e)
  223. {
  224. bool flag = this._chart != null && this._chart.PointerType == PointerType.Erase;
  225. if (flag)
  226. {
  227. this._chart.SetCursorImagePos(true, e);
  228. }
  229. flag = this._flag;
  230. if (flag)
  231. {
  232. this.UpdatePosition(e);
  233. }
  234. }
  235. private void ShowPointGrab(double aX, double aY)
  236. {
  237. this.SetValue(Canvas.LeftProperty, aX - 7.0);
  238. this.SetValue(Canvas.TopProperty, aY - 7.0);
  239. var paintArea = GetPaintAreaCanvas();
  240. if (paintArea == null) return;
  241. if (((aX - 10) > paintArea.ActualWidth) || ((aX + 10) < 0) ||
  242. ((aY - 10) > paintArea.ActualHeight) || ((aY + 10) < 0))
  243. {
  244. this.Visibility = Visibility.Collapsed;
  245. return;
  246. }
  247. this.Visibility = Visibility.Visible;
  248. }
  249. #endregion Private Methods
  250. #endregion Methods
  251. }
  252. }