using MuchInfo.Chart.Data.EnumTypes; using MuchInfo.Chart.Infrastructure.Helpers; using MuchInfo.Chart.WPF.Controls.Utilities; using MuchInfo.Chart.WPF.Helpers; using MuchInfo.Chart.WPF.Primitives.Interfaces; using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; namespace MuchInfo.Chart.WPF.Primitives.Drawing { public class DrawingSelectionArea { #region Fields private Chart _chart; private IDrawingTool _drawingTool; private bool _isMouseLeftDown; private bool _isMove; private Point _parentPosition; private IDrawingTool _preDrawingTool; private Line _selectionLine; #endregion Fields #region Constructors public DrawingSelectionArea(IDrawingTool aTool, Chart theChart) { this._isMouseLeftDown = false; this._drawingTool = aTool; this._chart = theChart; this.SelectionLine = new Line { Stroke = new SolidColorBrush(Colors.Transparent), StrokeThickness = 14.0, IsHitTestVisible = true, StrokeStartLineCap = PenLineCap.Round, StrokeEndLineCap = PenLineCap.Round, Cursor = Cursors.Hand }; } #endregion Constructors #region Properties #region Public Properties public virtual Line SelectionLine { get { return this._selectionLine; } set { var mouseButtonEventHandler = new MouseButtonEventHandler(this.SelectionLine_MouseLeftButtonDown); var mouseEventHandler = new MouseEventHandler(this.SelectionLine_MouseLeave); var mouseEventHandler2 = new MouseEventHandler(this.SelectionLine_MouseOver); var mouseEventHandler3 = new MouseEventHandler(this.SelectionLine_MouseEnter); var mouseEventHandler4 = new MouseEventHandler(this.SelectionLine_LostMouseCapture); var mouseButtonEventHandler2 = new MouseButtonEventHandler(this.SelectionLine_MouseLeftButtonUp); var mouseButtonRightUp = new MouseButtonEventHandler(this.SelectionLine_MouseRightButtonUp); bool flag = this._selectionLine != null; if (flag) { this._selectionLine.MouseLeftButtonDown -= (mouseButtonEventHandler); this._selectionLine.MouseLeave -= (mouseEventHandler); this._selectionLine.MouseMove -= (mouseEventHandler2); this._selectionLine.MouseEnter -= (mouseEventHandler3); this._selectionLine.LostMouseCapture -= (mouseEventHandler4); this._selectionLine.MouseLeftButtonUp -= (mouseButtonEventHandler2); this._selectionLine.MouseRightButtonUp -= mouseButtonRightUp; } this._selectionLine = value; flag = (this._selectionLine != null); if (flag) { this._selectionLine.MouseLeftButtonDown += (mouseButtonEventHandler); this._selectionLine.MouseLeave += (mouseEventHandler); this._selectionLine.MouseMove += (mouseEventHandler2); this._selectionLine.MouseEnter += (mouseEventHandler3); this._selectionLine.LostMouseCapture += (mouseEventHandler4); this._selectionLine.MouseLeftButtonUp += (mouseButtonEventHandler2); //Up弹出Popup,PopupManager里用Down关闭Popup this._selectionLine.MouseRightButtonUp += mouseButtonRightUp; } } } #endregion Public Properties #endregion Properties #region Methods #region Public Methods public List GetControls() { return new List { this.SelectionLine }; } public void SetPosition(double aX1, double aY1, double aX2, double aY2) { this.SelectionLine.X1 = aX1; this.SelectionLine.Y1 = aY1; this.SelectionLine.X2 = aX2; this.SelectionLine.Y2 = aY2; } #endregion Public Methods #region Private Methods private void ShowToolEdit(object sender, EventArgs e) { this._drawingTool.ShowEditor((MouseEventArgs)e); } //private void DrawingSelectionArea_3645(object sender, EventArgs e) //{ // ((DrawingHorizontalLine)this.drawingTool).drawingHorizontalLine_3533((MouseEventArgs)e); //} private void Remove(object sender, EventArgs e) { this._drawingTool.Delete(); } private void DrawingSelectionArea_3648(Point curPos) { bool flag = this._drawingTool != null; if (flag) { var parent = (FrameworkElement)this.SelectionLine.Parent; var height = (float)(parent.ActualHeight - 6.0); var width = (float)parent.ActualWidth; height = Math.Max(0, height); width = Math.Max(0, width); curPos.X = Math.Max(0, curPos.X); curPos.Y = Math.Max(0, curPos.Y); curPos.X = Math.Min(curPos.X, width); curPos.Y = Math.Min(curPos.Y, height); this._drawingTool.MovePlot(this._parentPosition, curPos, new Point(width, height)); } } private void SelectionLine_LostMouseCapture(object sender, MouseEventArgs e) { this._chart.LockPainting = false; this._isMouseLeftDown = false; this._isMove = false; this._chart.Refresh(); } private void SelectionLine_MouseEnter(object sender, MouseEventArgs e) { var flag = this._chart.PointerType != PointerType.Drawing; if (flag) { this.SelectionLine.Stroke = new SolidColorBrush(Color.FromArgb(125, 125, 125, 125)); var flag1 = _chart.PointerType == PointerType.Erase; this.SelectionLine.Cursor = flag1 ? Cursors.Arrow : Cursors.Hand; } } private void SelectionLine_MouseLeave(object sender, MouseEventArgs e) { this.SelectionLine.Stroke = new SolidColorBrush(Colors.Transparent); if (_preDrawingTool == null) return; this._chart.CurrentDrawingTool = _preDrawingTool; this._chart.SetCursorImagePos(true, e); _preDrawingTool = null; } private void SelectionLine_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this._chart.LockPainting = true; this._parentPosition = e.GetPosition((UIElement)this.SelectionLine.Parent); this._isMouseLeftDown = true; this._isMove = false; this.SelectionLine.CaptureMouse(); bool flag = this._drawingTool != null; if (flag) { this._drawingTool.PrepareToMove(); } } private void SelectionLine_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { this._chart.LockPainting = false; bool flag = this._drawingTool != null && !this._isMove; if (flag) { bool flag2 = _chart.PointerType == PointerType.Erase; if (flag2) { this._drawingTool.Delete(); } else { //右键菜单 //this.ShowContextMenu(e); } } else { bool flag2 = this._drawingTool != null; if (flag2) { this.DrawingSelectionArea_3648(e.GetPosition((UIElement)this.SelectionLine.Parent)); flag2 = (this._drawingTool.OwnerSpec != null); if (flag2) { this._drawingTool.OwnerSpec.ToolChanged(this._drawingTool); } this._isMouseLeftDown = false; this._chart.Refresh(); } } this.SelectionLine.ReleaseMouseCapture(); this._isMouseLeftDown = false; this._isMove = false; } private void SelectionLine_MouseOver(object sender, MouseEventArgs e) { bool flag = this._chart != null && _chart.PointerType == PointerType.Erase; if (flag) { this._chart.SetCursorImagePos(true, e); } if (this._chart != null && this._chart.PointerType == PointerType.Drawing) { _preDrawingTool = this._chart.CurrentDrawingTool; this._chart.PointerType = PointerType.Cursor; this._chart.SetCursorImagePos(true, e); } flag = this._isMouseLeftDown; if (flag) { Point position = e.GetPosition((UIElement)this.SelectionLine.Parent); float num = GeometryHelper.DistanceBetweenPoints(this._parentPosition, position); if (num > 8f) { this._isMove = true; } if (_isMove) { this.DrawingSelectionArea_3648(position); } } } /// /// Up弹出Popup,PopupManager里用Down关闭Popup /// /// The source of the event. /// The instance containing the event data. private void SelectionLine_MouseRightButtonUp(object sender, MouseButtonEventArgs e) { this._chart.LockPainting = false; if (this._drawingTool == null) return; ////使用上下文菜单代替Popup,以免以免父控件有右键菜单 //this.ShowContextMenu(e); //修改popup父容器,即每个Chart提供RootPanel,不使用PopupRoot和TrueRootVisual //因为应用程序可能开多个图表窗口 this.ShowPopupMenu(e); e.Handled = true; } private void ShowPopupMenu(MouseEventArgs e) { var popupMenu = new PopupMenu(this._chart.RootPanel) { FontSize = this._drawingTool.GetChartFontSize() }; popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Edit), ImageHelper.GetImage("PropertiesHS.png"), new EventHandler(this.ShowToolEdit)); //bool flag = Conversions.ToBoolean((!Conversions.ToBoolean(this.drawingTool is drawingHorizontalLine && ((drawingHorizontalLine)this.drawingTool).drawingHorizontalLine_3532()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true); //if (flag) //{ // PopupMenu.AddItem("Set Alert", ImageMan.GetImage("SymbolBolt.png"), new EventHandler(this.DrawingSelectionArea_3645)); //} popupMenu.AddSeperator(); popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Remove), ImageHelper.GetImage("DeleteHS.png"), new EventHandler(this.Remove)); popupMenu.Show(e, new Point(-5.0, -5.0)); } private void ShowContextMenu(MouseEventArgs e) { var contextMenu = new ContextMenu(); contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Edit), ImageHelper.GetImage("PropertiesHS.png"), new EventHandler(this.ShowToolEdit))); //bool flag = Conversions.ToBoolean((!Conversions.ToBoolean(this.drawingTool is drawingHorizontalLine && ((drawingHorizontalLine)this.drawingTool).drawingHorizontalLine_3532()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true); //if (flag) //{ // PopupMenu.AddItem("Set Alert", ImageMan.GetImage("SymbolBolt.png"), new EventHandler(this.DrawingSelectionArea_3645)); //} contextMenu.Items.Add(new Separator()); contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Remove), ImageHelper.GetImage("DeleteHS.png"), new EventHandler(this.Remove))); this._selectionLine.ContextMenu = contextMenu; this._selectionLine.ContextMenu.IsOpen = true; } #endregion Private Methods #endregion Methods } }