| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723 |
- using MuchInfo.Chart.Data;
- using MuchInfo.Chart.Data.EnumTypes;
- using MuchInfo.Chart.Infrastructure.Helpers;
- using MuchInfo.Chart.Infrastructure.Utilities;
- using MuchInfo.Chart.WPF.Controls.Editors;
- 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.Controls.Primitives;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Xml.Linq;
- namespace MuchInfo.Chart.WPF.Primitives.Drawing
- {
- public sealed class DrawingText : Drawing1PointBase
- {
- #region Fields
- private bool _flag;
- private double _fontSize;
- private bool _isMouseDown;
- private Border _labelBoder;
- private Popup _popup;
- private Point _position;
- //鼠标移到上面时记录上次画线工具
- private IDrawingTool _preDrawingTool;
- private string _text;
- #endregion Fields
- #region Constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="DrawingText"/> class.
- /// </summary>
- public DrawingText()
- {
- this._text = LanguageManager.FindResource(LanguageConst.DrawingTool_Edit_DefaultText);
- this._fontSize = 12;
- this._isMouseDown = false;
- }
- #endregion Constructors
- #region Properties
- #region Public Properties
- /// <summary>
- /// Gets or sets the size of the font.
- /// </summary>
- public double FontSize
- {
- get
- {
- return this._fontSize;
- }
- set
- {
- var flag = !this._fontSize.Equals(value);
- if (flag)
- {
- this._fontSize = value;
- this.Label.FontSize = (this._fontSize);
- //this.PropertyChanged();
- }
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether this instance is remove click.
- /// </summary>
- public bool IsRemoveClick
- {
- get;
- set;
- }
- /// <summary>
- /// 获取和设置the label
- /// </summary>
- public TextBlock Label
- {
- get;
- set;
- }
- /// <summary>
- /// 获取和设置the label border
- /// </summary>
- public Border LabelBorder
- {
- get
- {
- return this._labelBoder;
- }
- set
- {
- var mouseMove = new MouseEventHandler(this.LabelBoder_MouseMove);
- var mouseLeftButtonUp = new MouseButtonEventHandler(this.LabelBoder_MouseLeftButtonUp);
- var lostMouseCapture = new MouseEventHandler(this.LabelBoder_LostMouseCapture);
- var mouseButtonEventHandler2 = new MouseButtonEventHandler(this.LabelBoder_MouseLeftButtonDown);
- var mouseLeave = new MouseEventHandler(this.LabelBoder_MouseLeave);
- var mouseEnter = new MouseEventHandler(this.LabelBoder_MouseEnter);
- var mouseRightButtonUp = new MouseButtonEventHandler(this.LabelBoder_MouseRightButtonUp);
- bool flag = this._labelBoder != null;
- if (flag)
- {
- this._labelBoder.MouseMove -= (mouseMove);
- this._labelBoder.MouseLeftButtonUp -= (mouseLeftButtonUp);
- this._labelBoder.LostMouseCapture -= (lostMouseCapture);
- this._labelBoder.MouseLeftButtonDown -= (mouseButtonEventHandler2);
- this._labelBoder.MouseRightButtonUp -= (mouseRightButtonUp);
- this._labelBoder.MouseLeave -= (mouseLeave);
- this._labelBoder.MouseEnter -= (mouseEnter);
- }
- this._labelBoder = value;
- flag = (this._labelBoder != null);
- if (flag)
- {
- this._labelBoder.MouseMove += (mouseMove);
- this._labelBoder.MouseLeftButtonUp += (mouseLeftButtonUp);
- this._labelBoder.LostMouseCapture += (lostMouseCapture);
- this._labelBoder.MouseLeftButtonDown += (mouseButtonEventHandler2);
- this._labelBoder.MouseRightButtonUp += (mouseRightButtonUp);
- this._labelBoder.MouseLeave += (mouseLeave);
- this._labelBoder.MouseEnter += (mouseEnter);
- }
- }
- }
- /// <summary>
- /// 获取和设置the popup
- /// </summary>
- public Popup Popup
- {
- get
- {
- return this._popup;
- }
- set
- {
- var eventHandler = new EventHandler(this.Popup_Closed);
- bool flag = this._popup != null;
- if (flag)
- {
- this._popup.Closed -= (eventHandler);
- }
- this._popup = value;
- flag = (this._popup != null);
- if (flag)
- {
- this._popup.Closed += (eventHandler);
- }
- }
- }
- /// <summary>
- /// Gets the name of the root.
- /// </summary>
- public override string RootName
- {
- get
- {
- return "DrawText";
- }
- }
- /// <summary>
- /// 获取和设置the text
- /// </summary>
- public string Text
- {
- get
- {
- return this._text;
- }
- set
- {
- if (!_text.Equals(value))
- {
- this._text = value;
- this.Label.Text = this._text;
- //this.PropertyChanged();
- }
- }
- }
- #endregion Public Properties
- #region Internal Properties
- /// <summary>
- /// 获取和设置the scope rect
- /// </summary>
- internal Rect ScopeRect
- {
- get;
- set;
- }
- #endregion Internal Properties
- #endregion Properties
- #region Methods
- #region Public Methods
- /// <summary>
- /// Abbreviations this instance.
- /// </summary>
- /// <returns>System.String.</returns>
- public override string Abbreviation()
- {
- return MenuDescription();
- }
- /// <summary>
- /// Additionals the editors.
- /// </summary>
- /// <returns>List{ControlPair}.</returns>
- public override List<ControlPair> AdditionalEditors()
- {
- return new List<ControlPair>();
- }
- /// <summary>
- /// Determines whether this instance [can plot on all symbols].
- /// </summary>
- /// <returns><c>true</c> if this instance [can plot on all symbols]; otherwise, <c>false</c>.</returns>
- public override bool CanPlotOnAllSymbols()
- {
- return false;
- }
- /// <summary>
- /// Determines whether this instance [can save by percent].
- /// </summary>
- /// <returns><c>true</c> if this instance [can save by percent]; otherwise, <c>false</c>.</returns>
- public override bool CanSaveByPercent()
- {
- return true;
- }
- /// <summary>
- /// Clones this instance.
- /// </summary>
- /// <returns>IDrawingTool.</returns>
- public override IDrawingTool Clone()
- {
- return new DrawingText
- {
- mColor = Colors.White
- };
- }
- /// <summary>
- /// Descriptions this instance.
- /// </summary>
- /// <returns>System.String.</returns>
- public override string Description()
- {
- return this.MenuDescription();
- }
- /// <summary>
- /// Icons this instance.
- /// </summary>
- /// <returns>ImageSource.</returns>
- public override ImageSource Icon()
- {
- ImageSource result = null;
- try
- {
- result = ImageHelper.GetImage("text.png");
- }
- catch (Exception expr_3A)
- {
- throw expr_3A;
- }
- return result;
- }
- /// <summary>
- /// Menus the description.
- /// </summary>
- /// <returns>System.String.</returns>
- public override string MenuDescription()
- {
- return LanguageManager.FindResource(LanguageConst.DrawingTool_Edit_Description_Text);
- }
- public override DrawingToolType ToolType()
- {
- return DrawingToolType.Text;
- }
- #endregion Public Methods
- #region Protected Methods
- /// <summary>
- /// Adds from info.
- /// </summary>
- /// <param name="info">The info.</param>
- protected override void AddFromInfo(DrawingToolInfo info)
- {
- base.AddFromInfo(info);
- XElement xElement = XElement.Parse(info.XML);
- bool flag = xElement.Name == "Root";
- if (flag)
- {
- bool flag2 = xElement.Attribute("StringData1") != null;
- if (flag2)
- {
- this._text = xElement.Attribute("StringData1").Value;
- }
- flag2 = (xElement.Attribute("Value1") != null);
- if (flag2)
- {
- this._fontSize = (double)XMLHelp.ParseSingle(xElement.Attribute("Value1").Value);
- }
- }
- }
- /// <summary>
- /// Adds to info.
- /// </summary>
- /// <param name="info">The info.</param>
- protected override void AddToInfo(DrawingToolInfo info)
- {
- base.AddToInfo(info);
- var xElement = new XElement("Root");
- xElement.Add(new XAttribute("StringData1", this._text));
- xElement.Add(new XAttribute("Value1", (float)this._fontSize));
- info.XML = xElement.ToString();
- }
- /// <summary>
- /// Colors the changed.
- /// </summary>
- protected override void ColorChanged()
- {
- bool flag = this.Label != null;
- if (flag)
- {
- this.Label.Foreground = (new SolidColorBrush(this.mColor));
- }
- }
- /// <summary>
- /// Gets the drawing plots.
- /// </summary>
- /// <param name="owner">The owner.</param>
- /// <param name="aScale">A scale.</param>
- /// <param name="aRect">A rect.</param>
- /// <returns>List{FrameworkElement}.</returns>
- protected override List<FrameworkElement> GetDrawingPlots(Chart owner, ScaleLayer aScale, Rect aRect)
- {
- var list = new List<FrameworkElement>();
- this.ScopeRect = aRect;
- bool flag = this.Label == null;
- if (flag)
- {
- this.Label = new TextBlock
- {
- Cursor = Cursors.Hand,
- FontSize = this._fontSize
- };
- }
- this.Label.Foreground = (new SolidColorBrush(this.mColor));
- this.Label.Text = (this._text);
- flag = (this.LabelBorder == null);
- if (flag)
- {
- this.LabelBorder = new Border
- {
- Background = (new SolidColorBrush(Colors.Transparent)),
- Child = this.Label
- };
- LabelBorder.CornerRadius = new CornerRadius(4.0);
- LabelBorder.Padding = new Thickness(2.0);
- }
- this.MoveDrawingToNewLocations(aRect);
- list.Add(this.LabelBorder);
- return list;
- }
- /// <summary>
- /// Moves the drawing to new locations.
- /// </summary>
- /// <param name="aRect">A rect.</param>
- protected override void MoveDrawingToNewLocations(Rect aRect)
- {
- bool flag = this.LabelBorder != null;
- if (flag)
- {
- this.LabelBorder.SetValue(Canvas.LeftProperty, this.mLastX1);
- this.LabelBorder.SetValue(Canvas.TopProperty, this.mLastY1);
- flag = (this.Popup != null);
- if (flag)
- {
- Point point = GeometryHelper.PositionOfControlInApp(this.LabelBorder, this.Chart.RootPanel);
- this.Popup.HorizontalOffset = point.X - 7.9;// + 3.0;
- this.Popup.VerticalOffset = point.Y - 6; //+ 3.0;
- }
- }
- }
- /// <summary>
- /// Shows the drag handle.
- /// </summary>
- protected override bool ShowDragHandle()
- {
- //画Text,不需要draghandle, true:左上角有个隐藏的DrawingSelectionArea
- //drawingpointbase.cs - GetPlots用到
- return false;
- }
- #endregion Protected Methods
- #region Private Methods
- /// <summary>
- /// Handles the LostMouseCapture event of the LabelBoder control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
- private void LabelBoder_LostMouseCapture(object sender, MouseEventArgs e)
- {
- bool flag = this.Chart != null;
- if (flag)
- {
- this.Chart.LockPainting = false;
- this.Chart.Refresh();
- }
- this._flag = false;
- this._isMouseDown = false;
- }
- /// <summary>
- /// Handles the MouseEnter event of the LabelBoder control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
- private void LabelBoder_MouseEnter(object sender, MouseEventArgs e)
- {
- if (this.Chart == null) return;
- if (this.Chart.PointerType == PointerType.Drawing)
- {
- this.Label.Cursor = Cursors.None;
- _preDrawingTool = this.Chart.CurrentDrawingTool;
- this.Chart.PointerType = PointerType.Cursor;
- this.Chart.SetCursorImagePos(true, e);
- }
- else
- {
- this.Label.Cursor = this.Chart.PointerType == PointerType.Erase ? Cursors.Arrow : Cursors.Hand;
- this.LabelBorder.Background = new SolidColorBrush(Color.FromArgb(100, 120, 120, 120));
- }
- }
- /// <summary>
- /// Handles the MouseLeave event of the LabelBoder control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
- private void LabelBoder_MouseLeave(object sender, MouseEventArgs e)
- {
- if (_preDrawingTool == null)
- {
- this.LabelBorder.Background = new SolidColorBrush(Colors.Transparent);
- }
- else
- {
- this.Chart.CurrentDrawingTool = _preDrawingTool;
- this.Chart.SetCursorImagePos(true, e);
- _preDrawingTool = null;
- }
- }
- /// <summary>
- /// Handles the MouseLeftButtonDown event of the LabelBoder control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
- private void LabelBoder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (_preDrawingTool != null) return;
- bool flag = this.Chart != null;
- if (flag)
- {
- this.Chart.LockPainting = true;
- }
- this._position = e.GetPosition((UIElement)this.LabelBorder.Parent);
- this._isMouseDown = true;
- this._flag = false;
- this.LabelBorder.CaptureMouse();
- this.PrepareToMove();
- }
- /// <summary>
- /// Handles the MouseLeftButtonUp event of the LabelBoder control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
- private void LabelBoder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- if (_preDrawingTool != null) return;
- bool flag = this.Chart != null;
- if (flag)
- {
- this.Chart.LockPainting = false;
- }
- flag = !this._flag;
- if (flag)
- {
- bool flag2 = Chart.PointerType == PointerType.Erase;
- if (flag2)
- {
- this.Delete();
- }
- else
- {
- //右键菜单代替,统一画线工具风格
- //this.ShowDrawTextEditor();
- }
- }
- else
- {
- this.MovePlot(e.GetPosition((UIElement)this.LabelBorder.Parent));
- bool flag2 = this.OwnerSpec != null;
- if (flag2)
- {
- this.OwnerSpec.ToolChanged(this);
- }
- this._isMouseDown = false;
- flag2 = (this.Chart != null);
- if (flag2)
- {
- this.Chart.Refresh();
- }
- }
- this.LabelBorder.ReleaseMouseCapture();
- this._isMouseDown = false;
- this._flag = false;
- }
- /// <summary>
- /// Handles the MouseMove event of the LabelBoder control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
- private void LabelBoder_MouseMove(object sender, MouseEventArgs e)
- {
- bool flag = this.Chart != null && Chart.PointerType == PointerType.Erase;
- if (flag)
- {
- this.Chart.SetCursorImagePos(true, e);
- }
- flag = this._isMouseDown;
- if (flag)
- {
- Point position = e.GetPosition((UIElement)this.LabelBorder.Parent);
- float num = GeometryHelper.DistanceBetweenPoints(this._position, position);
- flag = (num > 8f);
- if (flag)
- {
- this._flag = true;
- }
- flag = this._flag;
- if (flag)
- {
- this.MovePlot(position);
- }
- }
- }
- /// <summary>
- /// Handles the MouseRightButtonUp event of the LabelBoder control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
- private void LabelBoder_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
- {
- this.ShowPopupMenu(e);
- e.Handled = true;
- }
- /// <summary>
- /// Moves the plot.
- /// </summary>
- /// <param name="curPos">The cur pos.</param>
- private void MovePlot(Point curPos)
- {
- var parent = (FrameworkElement)this.LabelBorder.Parent;
- if (parent == null) return;
- var height = (float)(parent.ActualHeight - this.LabelBorder.ActualHeight + 10);
- height = Math.Max(0, height);
- var width = (float)parent.ActualWidth;
- 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.MovePlot(this._position, curPos, new Point(width, height));
- }
- /// <summary>
- /// Handles the Closed event of the Popup control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
- private void Popup_Closed(object sender, EventArgs e)
- {
- if (this.IsRemoveClick)
- {
- this.IsRemoveClick = false;
- return;
- }
- this.Popup.Child = null;
- bool flag = this.Chart != null;
- if (string.IsNullOrWhiteSpace(this.Label.Text))
- {
- this.Delete();
- }
- if (flag)
- {
- this.Chart.LockPainting = false;
- this.PropertyChanged(); //this._chart.Refresh();
- }
- this.Popup = null;
- }
- /// <summary>
- /// Removes the text.
- /// </summary>
- /// <param name="sender">The sender.</param>
- /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
- private void RemoveText(object sender, EventArgs e)
- {
- this.Delete();
- this.IsRemoveClick = true;
- }
- /// <summary>
- /// Shows the editor.
- /// </summary>
- /// <param name="sender">The sender.</param>
- /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
- private void ShowEditor(object sender, EventArgs e)
- {
- bool flag = this.Chart != null;
- if (flag)
- {
- this.Popup = PopupManager.CreateOverlay(this.Chart.RootPanel);
- var drawingTextEditor = new DrawingTextEditor(this, this.Popup);
- this.Popup.Child = drawingTextEditor;
- drawingTextEditor.ShowIt();
- this.Popup.IsOpen = true;
- this.Chart.LockPainting = false;
- this.Chart.Refresh();
- }
- }
- /// <summary>
- /// Shows the popup menu.
- /// </summary>
- /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
- private void ShowPopupMenu(MouseEventArgs e)
- {
- var popUpMenu = new PopupMenu(this.Chart.RootPanel) { FontSize = this.GetChartFontSize() };
- popUpMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Edit), ImageHelper.GetImage("PropertiesHS.png"), new EventHandler(this.ShowEditor));
- popUpMenu.AddSeperator();
- popUpMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Remove), ImageHelper.GetImage("DeleteHS.png"), new EventHandler(this.RemoveText));
- popUpMenu.Show(e, new Point(-5.0, -5.0));
- }
- #endregion Private Methods
- #endregion Methods
- #region Other
- //private void ShowDrawTextEditor()
- //{
- // bool flag = this.Chart != null;
- // if (flag)
- // {
- // this.Chart.LockPainting = true;
- // this.Popup = PopupManager.CreateOverlay(this.Chart.RootPanel);
- // var drawingTextEditor = new DrawingTextEditor(this, this.Popup);
- // this.Popup.Child = drawingTextEditor;
- // drawingTextEditor.ShowIt();
- // this.Popup.IsOpen = true;
- // }
- //}
- #endregion Other
- }
- }
|