DrawingSelectionArea.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using MuchInfo.Chart.Data.EnumTypes;
  2. using MuchInfo.Chart.Infrastructure.Helpers;
  3. using MuchInfo.Chart.WPF.Controls.Utilities;
  4. using MuchInfo.Chart.WPF.Helpers;
  5. using MuchInfo.Chart.WPF.Primitives.Interfaces;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Shapes;
  13. namespace MuchInfo.Chart.WPF.Primitives.Drawing
  14. {
  15. public class DrawingSelectionArea
  16. {
  17. #region Fields
  18. private Chart _chart;
  19. private IDrawingTool _drawingTool;
  20. private bool _isMouseLeftDown;
  21. private bool _isMove;
  22. private Point _parentPosition;
  23. private IDrawingTool _preDrawingTool;
  24. private Line _selectionLine;
  25. #endregion Fields
  26. #region Constructors
  27. public DrawingSelectionArea(IDrawingTool aTool, Chart theChart)
  28. {
  29. this._isMouseLeftDown = false;
  30. this._drawingTool = aTool;
  31. this._chart = theChart;
  32. this.SelectionLine = new Line
  33. {
  34. Stroke = new SolidColorBrush(Colors.Transparent),
  35. StrokeThickness = 14.0,
  36. IsHitTestVisible = true,
  37. StrokeStartLineCap = PenLineCap.Round,
  38. StrokeEndLineCap = PenLineCap.Round,
  39. Cursor = Cursors.Hand
  40. };
  41. }
  42. #endregion Constructors
  43. #region Properties
  44. #region Public Properties
  45. public virtual Line SelectionLine
  46. {
  47. get
  48. {
  49. return this._selectionLine;
  50. }
  51. set
  52. {
  53. var mouseButtonEventHandler = new MouseButtonEventHandler(this.SelectionLine_MouseLeftButtonDown);
  54. var mouseEventHandler = new MouseEventHandler(this.SelectionLine_MouseLeave);
  55. var mouseEventHandler2 = new MouseEventHandler(this.SelectionLine_MouseOver);
  56. var mouseEventHandler3 = new MouseEventHandler(this.SelectionLine_MouseEnter);
  57. var mouseEventHandler4 = new MouseEventHandler(this.SelectionLine_LostMouseCapture);
  58. var mouseButtonEventHandler2 = new MouseButtonEventHandler(this.SelectionLine_MouseLeftButtonUp);
  59. var mouseButtonRightUp = new MouseButtonEventHandler(this.SelectionLine_MouseRightButtonUp);
  60. bool flag = this._selectionLine != null;
  61. if (flag)
  62. {
  63. this._selectionLine.MouseLeftButtonDown -= (mouseButtonEventHandler);
  64. this._selectionLine.MouseLeave -= (mouseEventHandler);
  65. this._selectionLine.MouseMove -= (mouseEventHandler2);
  66. this._selectionLine.MouseEnter -= (mouseEventHandler3);
  67. this._selectionLine.LostMouseCapture -= (mouseEventHandler4);
  68. this._selectionLine.MouseLeftButtonUp -= (mouseButtonEventHandler2);
  69. this._selectionLine.MouseRightButtonUp -= mouseButtonRightUp;
  70. }
  71. this._selectionLine = value;
  72. flag = (this._selectionLine != null);
  73. if (flag)
  74. {
  75. this._selectionLine.MouseLeftButtonDown += (mouseButtonEventHandler);
  76. this._selectionLine.MouseLeave += (mouseEventHandler);
  77. this._selectionLine.MouseMove += (mouseEventHandler2);
  78. this._selectionLine.MouseEnter += (mouseEventHandler3);
  79. this._selectionLine.LostMouseCapture += (mouseEventHandler4);
  80. this._selectionLine.MouseLeftButtonUp += (mouseButtonEventHandler2);
  81. //Up弹出Popup,PopupManager里用Down关闭Popup
  82. this._selectionLine.MouseRightButtonUp += mouseButtonRightUp;
  83. }
  84. }
  85. }
  86. #endregion Public Properties
  87. #endregion Properties
  88. #region Methods
  89. #region Public Methods
  90. public List<FrameworkElement> GetControls()
  91. {
  92. return new List<FrameworkElement> { this.SelectionLine };
  93. }
  94. public void SetPosition(double aX1, double aY1, double aX2, double aY2)
  95. {
  96. this.SelectionLine.X1 = aX1;
  97. this.SelectionLine.Y1 = aY1;
  98. this.SelectionLine.X2 = aX2;
  99. this.SelectionLine.Y2 = aY2;
  100. }
  101. #endregion Public Methods
  102. #region Private Methods
  103. private void ShowToolEdit(object sender, EventArgs e)
  104. {
  105. this._drawingTool.ShowEditor((MouseEventArgs)e);
  106. }
  107. //private void DrawingSelectionArea_3645(object sender, EventArgs e)
  108. //{
  109. // ((DrawingHorizontalLine)this.drawingTool).drawingHorizontalLine_3533((MouseEventArgs)e);
  110. //}
  111. private void Remove(object sender, EventArgs e)
  112. {
  113. this._drawingTool.Delete();
  114. }
  115. private void DrawingSelectionArea_3648(Point curPos)
  116. {
  117. bool flag = this._drawingTool != null;
  118. if (flag)
  119. {
  120. var parent = (FrameworkElement)this.SelectionLine.Parent;
  121. var height = (float)(parent.ActualHeight - 6.0);
  122. var width = (float)parent.ActualWidth;
  123. height = Math.Max(0, height);
  124. width = Math.Max(0, width);
  125. curPos.X = Math.Max(0, curPos.X);
  126. curPos.Y = Math.Max(0, curPos.Y);
  127. curPos.X = Math.Min(curPos.X, width);
  128. curPos.Y = Math.Min(curPos.Y, height);
  129. this._drawingTool.MovePlot(this._parentPosition, curPos, new Point(width, height));
  130. }
  131. }
  132. private void SelectionLine_LostMouseCapture(object sender, MouseEventArgs e)
  133. {
  134. this._chart.LockPainting = false;
  135. this._isMouseLeftDown = false;
  136. this._isMove = false;
  137. this._chart.Refresh();
  138. }
  139. private void SelectionLine_MouseEnter(object sender, MouseEventArgs e)
  140. {
  141. var flag = this._chart.PointerType != PointerType.Drawing;
  142. if (flag)
  143. {
  144. this.SelectionLine.Stroke = new SolidColorBrush(Color.FromArgb(125, 125, 125, 125));
  145. var flag1 = _chart.PointerType == PointerType.Erase;
  146. this.SelectionLine.Cursor = flag1 ? Cursors.Arrow : Cursors.Hand;
  147. }
  148. }
  149. private void SelectionLine_MouseLeave(object sender, MouseEventArgs e)
  150. {
  151. this.SelectionLine.Stroke = new SolidColorBrush(Colors.Transparent);
  152. if (_preDrawingTool == null) return;
  153. this._chart.CurrentDrawingTool = _preDrawingTool;
  154. this._chart.SetCursorImagePos(true, e);
  155. _preDrawingTool = null;
  156. }
  157. private void SelectionLine_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  158. {
  159. this._chart.LockPainting = true;
  160. this._parentPosition = e.GetPosition((UIElement)this.SelectionLine.Parent);
  161. this._isMouseLeftDown = true;
  162. this._isMove = false;
  163. this.SelectionLine.CaptureMouse();
  164. bool flag = this._drawingTool != null;
  165. if (flag)
  166. {
  167. this._drawingTool.PrepareToMove();
  168. }
  169. }
  170. private void SelectionLine_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  171. {
  172. this._chart.LockPainting = false;
  173. bool flag = this._drawingTool != null && !this._isMove;
  174. if (flag)
  175. {
  176. bool flag2 = _chart.PointerType == PointerType.Erase;
  177. if (flag2)
  178. {
  179. this._drawingTool.Delete();
  180. }
  181. else
  182. {
  183. //右键菜单
  184. //this.ShowContextMenu(e);
  185. }
  186. }
  187. else
  188. {
  189. bool flag2 = this._drawingTool != null;
  190. if (flag2)
  191. {
  192. this.DrawingSelectionArea_3648(e.GetPosition((UIElement)this.SelectionLine.Parent));
  193. flag2 = (this._drawingTool.OwnerSpec != null);
  194. if (flag2)
  195. {
  196. this._drawingTool.OwnerSpec.ToolChanged(this._drawingTool);
  197. }
  198. this._isMouseLeftDown = false;
  199. this._chart.Refresh();
  200. }
  201. }
  202. this.SelectionLine.ReleaseMouseCapture();
  203. this._isMouseLeftDown = false;
  204. this._isMove = false;
  205. }
  206. private void SelectionLine_MouseOver(object sender, MouseEventArgs e)
  207. {
  208. bool flag = this._chart != null && _chart.PointerType == PointerType.Erase;
  209. if (flag)
  210. {
  211. this._chart.SetCursorImagePos(true, e);
  212. }
  213. if (this._chart != null && this._chart.PointerType == PointerType.Drawing)
  214. {
  215. _preDrawingTool = this._chart.CurrentDrawingTool;
  216. this._chart.PointerType = PointerType.Cursor;
  217. this._chart.SetCursorImagePos(true, e);
  218. }
  219. flag = this._isMouseLeftDown;
  220. if (flag)
  221. {
  222. Point position = e.GetPosition((UIElement)this.SelectionLine.Parent);
  223. float num = GeometryHelper.DistanceBetweenPoints(this._parentPosition, position);
  224. if (num > 8f)
  225. {
  226. this._isMove = true;
  227. }
  228. if (_isMove)
  229. {
  230. this.DrawingSelectionArea_3648(position);
  231. }
  232. }
  233. }
  234. /// <summary>
  235. /// Up弹出Popup,PopupManager里用Down关闭Popup
  236. /// </summary>
  237. /// <param name="sender">The source of the event.</param>
  238. /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
  239. private void SelectionLine_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  240. {
  241. this._chart.LockPainting = false;
  242. if (this._drawingTool == null) return;
  243. ////使用上下文菜单代替Popup,以免以免父控件有右键菜单
  244. //this.ShowContextMenu(e);
  245. //修改popup父容器,即每个Chart提供RootPanel,不使用PopupRoot和TrueRootVisual
  246. //因为应用程序可能开多个图表窗口
  247. this.ShowPopupMenu(e);
  248. e.Handled = true;
  249. }
  250. private void ShowPopupMenu(MouseEventArgs e)
  251. {
  252. var popupMenu = new PopupMenu(this._chart.RootPanel) { FontSize = this._drawingTool.GetChartFontSize() };
  253. popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Edit), ImageHelper.GetImage("PropertiesHS.png"), new EventHandler(this.ShowToolEdit));
  254. //bool flag = Conversions.ToBoolean((!Conversions.ToBoolean(this.drawingTool is drawingHorizontalLine && ((drawingHorizontalLine)this.drawingTool).drawingHorizontalLine_3532()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true);
  255. //if (flag)
  256. //{
  257. // PopupMenu.AddItem("Set Alert", ImageMan.GetImage("SymbolBolt.png"), new EventHandler(this.DrawingSelectionArea_3645));
  258. //}
  259. popupMenu.AddSeperator();
  260. popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Remove), ImageHelper.GetImage("DeleteHS.png"), new EventHandler(this.Remove));
  261. popupMenu.Show(e, new Point(-5.0, -5.0));
  262. }
  263. private void ShowContextMenu(MouseEventArgs e)
  264. {
  265. var contextMenu = new ContextMenu();
  266. contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Edit), ImageHelper.GetImage("PropertiesHS.png"), new EventHandler(this.ShowToolEdit)));
  267. //bool flag = Conversions.ToBoolean((!Conversions.ToBoolean(this.drawingTool is drawingHorizontalLine && ((drawingHorizontalLine)this.drawingTool).drawingHorizontalLine_3532()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true);
  268. //if (flag)
  269. //{
  270. // PopupMenu.AddItem("Set Alert", ImageMan.GetImage("SymbolBolt.png"), new EventHandler(this.DrawingSelectionArea_3645));
  271. //}
  272. contextMenu.Items.Add(new Separator());
  273. contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Remove), ImageHelper.GetImage("DeleteHS.png"), new EventHandler(this.Remove)));
  274. this._selectionLine.ContextMenu = contextMenu;
  275. this._selectionLine.ContextMenu.IsOpen = true;
  276. }
  277. #endregion Private Methods
  278. #endregion Methods
  279. }
  280. }