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
///
/// Initializes a new instance of the class.
///
public DrawingText()
{
this._text = LanguageManager.FindResource(LanguageConst.DrawingTool_Edit_DefaultText);
this._fontSize = 12;
this._isMouseDown = false;
}
#endregion Constructors
#region Properties
#region Public Properties
///
/// Gets or sets the size of the font.
///
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();
}
}
}
///
/// Gets or sets a value indicating whether this instance is remove click.
///
public bool IsRemoveClick
{
get;
set;
}
///
/// 获取和设置the label
///
public TextBlock Label
{
get;
set;
}
///
/// 获取和设置the label border
///
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);
}
}
}
///
/// 获取和设置the popup
///
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);
}
}
}
///
/// Gets the name of the root.
///
public override string RootName
{
get
{
return "DrawText";
}
}
///
/// 获取和设置the text
///
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
///
/// 获取和设置the scope rect
///
internal Rect ScopeRect
{
get;
set;
}
#endregion Internal Properties
#endregion Properties
#region Methods
#region Public Methods
///
/// Abbreviations this instance.
///
/// System.String.
public override string Abbreviation()
{
return MenuDescription();
}
///
/// Additionals the editors.
///
/// List{ControlPair}.
public override List AdditionalEditors()
{
return new List();
}
///
/// Determines whether this instance [can plot on all symbols].
///
/// true if this instance [can plot on all symbols]; otherwise, false.
public override bool CanPlotOnAllSymbols()
{
return false;
}
///
/// Determines whether this instance [can save by percent].
///
/// true if this instance [can save by percent]; otherwise, false.
public override bool CanSaveByPercent()
{
return true;
}
///
/// Clones this instance.
///
/// IDrawingTool.
public override IDrawingTool Clone()
{
return new DrawingText
{
mColor = Colors.White
};
}
///
/// Descriptions this instance.
///
/// System.String.
public override string Description()
{
return this.MenuDescription();
}
///
/// Icons this instance.
///
/// ImageSource.
public override ImageSource Icon()
{
ImageSource result = null;
try
{
result = ImageHelper.GetImage("text.png");
}
catch (Exception expr_3A)
{
throw expr_3A;
}
return result;
}
///
/// Menus the description.
///
/// System.String.
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
///
/// Adds from info.
///
/// The info.
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);
}
}
}
///
/// Adds to info.
///
/// The info.
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();
}
///
/// Colors the changed.
///
protected override void ColorChanged()
{
bool flag = this.Label != null;
if (flag)
{
this.Label.Foreground = (new SolidColorBrush(this.mColor));
}
}
///
/// Gets the drawing plots.
///
/// The owner.
/// A scale.
/// A rect.
/// List{FrameworkElement}.
protected override List GetDrawingPlots(Chart owner, ScaleLayer aScale, Rect aRect)
{
var list = new List();
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;
}
///
/// Moves the drawing to new locations.
///
/// A rect.
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;
}
}
}
///
/// Shows the drag handle.
///
protected override bool ShowDragHandle()
{
//画Text,不需要draghandle, true:左上角有个隐藏的DrawingSelectionArea
//drawingpointbase.cs - GetPlots用到
return false;
}
#endregion Protected Methods
#region Private Methods
///
/// Handles the LostMouseCapture event of the LabelBoder control.
///
/// The source of the event.
/// The instance containing the event data.
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;
}
///
/// Handles the MouseEnter event of the LabelBoder control.
///
/// The source of the event.
/// The instance containing the event data.
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));
}
}
///
/// Handles the MouseLeave event of the LabelBoder control.
///
/// The source of the event.
/// The instance containing the event data.
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;
}
}
///
/// Handles the MouseLeftButtonDown event of the LabelBoder control.
///
/// The source of the event.
/// The instance containing the event data.
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();
}
///
/// Handles the MouseLeftButtonUp event of the LabelBoder control.
///
/// The source of the event.
/// The instance containing the event data.
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;
}
///
/// Handles the MouseMove event of the LabelBoder control.
///
/// The source of the event.
/// The instance containing the event data.
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);
}
}
}
///
/// Handles the MouseRightButtonUp event of the LabelBoder control.
///
/// The source of the event.
/// The instance containing the event data.
private void LabelBoder_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
this.ShowPopupMenu(e);
e.Handled = true;
}
///
/// Moves the plot.
///
/// The cur pos.
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));
}
///
/// Handles the Closed event of the Popup control.
///
/// The source of the event.
/// The instance containing the event data.
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;
}
///
/// Removes the text.
///
/// The sender.
/// The instance containing the event data.
private void RemoveText(object sender, EventArgs e)
{
this.Delete();
this.IsRemoveClick = true;
}
///
/// Shows the editor.
///
/// The sender.
/// The instance containing the event data.
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();
}
}
///
/// Shows the popup menu.
///
/// The instance containing the event data.
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
}
}