using System.ComponentModel;
|
using System.Windows;
|
using System.Windows.Media;
|
using static Bro.Common.Helper.EnumHelper;
|
|
namespace Bro.Common.ImageCanvas.Shapes
|
{
|
public abstract class ShapeElementBase : DrawingVisual, INotifyPropertyChanged
|
{
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
public int Index { get; set; }
|
|
#region Draw
|
public void Draw()
|
{
|
if (Pen == null)
|
{
|
SetNormalPen();
|
}
|
|
//Application.Current.Dispatcher.Invoke(() =>
|
//{
|
using (DrawingContext dc = RenderOpen())
|
{
|
DrawShape(dc);
|
}
|
//});
|
}
|
|
protected abstract void DrawShape(DrawingContext dc);
|
#endregion
|
|
#region 画笔相关
|
#region 绘图画笔
|
private Pen pen = null;
|
public Pen Pen
|
{
|
get => pen;
|
set => SetAndDraw(ref pen, value);
|
}
|
|
public virtual void SetNormalPen()
|
{
|
Pen = new Pen(new SolidColorBrush(Colors.Black), 3);
|
}
|
|
public virtual void SetHoverPen()
|
{
|
Pen = new Pen(new SolidColorBrush(Colors.BlueViolet), 10);
|
}
|
|
public virtual void SetInSidePen()
|
{
|
Pen = new Pen(new SolidColorBrush(Colors.YellowGreen), 6);
|
}
|
|
public virtual void SetSelectedPen()
|
{
|
Pen = new Pen(new SolidColorBrush(Colors.Pink), 10);
|
}
|
|
protected virtual void SetMeasureDoneOKPen()
|
{
|
Pen = new Pen(new SolidColorBrush(Colors.Green), 6);
|
}
|
|
protected virtual void SetMeasureDoneNGPen()
|
{
|
Pen = new Pen(new SolidColorBrush(Colors.Red), 6);
|
}
|
|
protected virtual void SetMeasuringPen()
|
{
|
Pen = new Pen(new SolidColorBrush(Colors.Yellow), 3);
|
}
|
#endregion
|
|
#region 文字画笔
|
/// <summary>
|
/// 字体大小
|
/// </summary>
|
public virtual float FontSize { get; set; } = 30;
|
/// <summary>
|
/// 字体和基元的距离
|
/// </summary>
|
public virtual int FontDistance { get; set; } = 5;
|
|
public virtual bool IsShowRemark { get; set; } = true;
|
|
public string Text { get; set; }
|
|
public virtual Typeface Typeface { get; set; } = new Typeface(new FontFamily("宋体"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
|
#endregion
|
#endregion
|
|
#region 鼠标位置判断
|
protected abstract bool IsMouseInSide(Point point);
|
protected abstract bool IsMouseHover(Point point);
|
#endregion
|
|
#region 状态
|
private ElementState state = ElementState.New;
|
public ElementState State
|
{
|
get
|
{
|
return state;
|
}
|
set
|
{
|
if (state != value)
|
{
|
state = value;
|
|
switch (state)
|
{
|
case ElementState.MouseHover:
|
SetHoverPen();
|
break;
|
case ElementState.MouseInSide:
|
SetInSidePen();
|
break;
|
case ElementState.Selected:
|
SetSelectedPen();
|
break;
|
case ElementState.Normal:
|
SetNormalPen();
|
break;
|
case ElementState.Measuring:
|
SetMeasuringPen();
|
break;
|
case ElementState.MeasureDoneOK:
|
SetMeasureDoneOKPen();
|
break;
|
case ElementState.MeasureDoneNG:
|
SetMeasureDoneNGPen();
|
break;
|
}
|
}
|
}
|
}
|
|
/// <summary>
|
/// 是否是运行模式
|
/// </summary>
|
[Browsable(false)]
|
public RunMode RunMode { get; set; } = RunMode.SetMode;
|
|
public virtual void CheckMouseState(Point point)
|
{
|
if (State != ElementState.Selected)
|
{
|
if (IsMouseInSide(point))
|
{
|
State = ElementState.MouseInSide;
|
}
|
else if (IsMouseHover(point))
|
{
|
State = ElementState.MouseHover;
|
}
|
else
|
{
|
State = ElementState.Normal;
|
}
|
}
|
}
|
#endregion
|
|
#region 变化导致重绘事件
|
protected void SetAndDraw<T>(ref T field, T value)
|
{
|
if (value != null)
|
{
|
if (!value.Equals(field))
|
{
|
field = value;
|
Draw();
|
}
|
}
|
}
|
#endregion
|
}
|
}
|