using Bro.Common.Interface;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
using System.Windows.Media;
using static Bro.Common.Helper.EnumHelper;
using Bro.Common.Helper;
namespace Bro.Common.ImageCanvas.Shapes
{
public abstract class ShapeElementBase : DrawingVisual, IShapeElement
{
public event PropertyChangedEventHandler PropertyChanged;
public int Index { get; set; }
public string ID { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; }
#region Draw
public bool IsShowing { get; set; } = false;
public void Draw()
{
if (!IsShowing)
return;
if (Pen == null)
{
SetNormalPen();
}
try
{
using (DrawingContext dc = RenderOpen())
{
DrawShape(dc);
}
}
catch (Exception ex)
{
}
}
protected abstract void DrawShape(DrawingContext dc);
public abstract void Draw(System.Drawing.Graphics g);
#endregion
#region 画笔相关
#region 绘图画笔
private System.Windows.Media.Pen pen = null;
public System.Windows.Media.Pen Pen
{
get => pen;
set => SetAndDraw(ref pen, value);
}
public virtual void SetNormalPen()
{
Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.Black), 3);
}
public virtual void SetHoverPen()
{
Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.BlueViolet), 10);
}
public virtual void SetInSidePen()
{
Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.YellowGreen), 6);
}
public virtual void SetSelectedPen()
{
Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.Pink), 10);
}
protected virtual void SetMeasureDoneOKPen()
{
Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.Green), 6);
}
protected virtual void SetMeasureDoneNGPen()
{
Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.Red), 6);
}
protected virtual void SetMeasuringPen()
{
Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.Yellow), 3);
}
#endregion
#region 文字画笔
///
/// 字体大小
///
public virtual float FontSize { get; set; } = 30;
///
/// 字体和基元的距离
///
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 System.Windows.Media.FontFamily("宋体"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
#endregion
#endregion
#region 鼠标位置判断
protected abstract bool IsMouseInSide(System.Windows.Point point);
protected abstract bool IsMouseHover(System.Windows.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;
}
}
}
}
///
/// 是否是运行模式
///
[Browsable(false)]
public RunMode RunMode { get; set; } = RunMode.SetMode;
public bool IsEnabled { get; set; } = true;
public virtual void CheckMouseState(System.Windows.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 virtual void SetAndDraw(ref T field, T value)
{
if (value != null)
{
if (!value.Equals(field))
{
field = value;
Draw();
}
}
}
public virtual void OnMouseDown(System.Drawing.Point point)
{
}
public virtual void OnMouseUp(System.Drawing.Point point)
{
}
public virtual void OnMouseMove(System.Drawing.Point point)
{
}
public virtual void OnMouseDoubleClick(System.Drawing.Point point)
{
}
public virtual bool IsIntersect(Rectangle rect)
{
return false;
}
#endregion
#region ICloneable
public virtual object Clone()
{
return this.DeepSerializeClone();
}
#endregion
}
}