123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663 |
- using PaintDotNet.Annotation.Enum;
- using PaintDotNet.Base;
- using PaintDotNet.Base.SettingModel;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Globalization;
- using System.Runtime.Serialization;
- using System.Windows.Forms;
- namespace PaintDotNet.Annotation
- {
- public abstract class DrawObject
- {
- /// <summary>
- /// 目标被选择状态
- /// </summary>
- private bool selected;
- /// <summary>
- /// 鼠标按下状态, 静态,所有现存的公用
- /// </summary>
- public static bool mouseStatus = false;
- public static int smallRectangleWidth = MarkPointRect.markPointRectWidth;
- public static int MarkpointStyle = MarkPointRect.MarkpointStyle;
- public static int MarkpointLineColor = MarkPointRect.MarkpointLineColor;
- public static int MarkpointLineWidth = MarkPointRect.MarkpointLineWidth;
- public static int MarkpointAreaColor = MarkPointRect.MarkpointAreaColor;
- public static bool isFollow = MarkPointRect.isFollow;
- /// <summary>
- /// 点集合
- /// </summary>
- public List<PointF> pointArrayList;
- /// <summary>
- /// 颜色
- /// </summary>
- private Color color;
- /// <summary>
- /// 线宽
- /// </summary>
- private int penWidth;
- /// <summary>
- /// 用于撤消-重做
- /// </summary>
- int id;
- /// <summary>
- /// 上次使用的属性值
- /// 也许可以不用??
- /// </summary>
- private static Color lastUsedColor = Color.Black;
- /// <summary>
- /// 上次使用的线宽
- /// 也许可以不用??
- /// </summary>
- private static int lastUsedPenWidth = 1;
- /// <summary>
- /// 用于序列化-颜色
- /// </summary>
- private const string entryColor = "Color";
- /// <summary>
- /// 用于序列化-线宽
- /// </summary>
- private const string entryPenWidth = "PenWidth";
- /// <summary>
- /// 画板
- /// </summary>
- private ISurfaceBox surfaceBox;
- /// <summary>
- /// 主要用于区分是标注、测量、视场、其它
- /// </summary>
- public DrawClass objectType;
- /// <summary>
- /// 用于标注自身是什么工具
- /// </summary>
- public DrawToolType drawToolType;
- /// <summary>
- /// 外接矩形,或者是计算的矩形
- /// </summary>
- protected RectangleF rectangle;
- /// <summary>
- /// 起始点
- /// </summary>
- public PointF startPoint;
- /// <summary>
- /// 物理长度
- /// </summary>
- public double length = 0;
- /// <summary>
- /// 结束点
- /// </summary>
- public PointF endPoint;
- /// <summary>
- /// 判断辅助线方向的点
- /// </summary>
- public Point judgeP;
- /// <summary>
- /// 文本框内容
- /// </summary>
- public string textboxMessage;
- /// <summary>
- /// 目标点状态 0:添加 1:选择 2:删除
- /// </summary>
- public int CompoundRatioSelected;
- /// <summary>
- /// 删除状态 0:不删除 1:删除
- /// </summary>
- public int delete;
- public bool isLocked = false;
- public virtual RectangleF Rectangle
- {
- get
- {
- return rectangle;
- }
- set
- {
- rectangle = value;
- OnPropertyChanged();
- }
- }
- public virtual double Height
- { get; set; }
- public virtual double Width
- { get; set; }
- /// <summary>
- /// 公开出去的自定义事件
- /// 当对象本身属性有所改变的时候
- /// 比如边长,原点等有变化
- /// </summary>
- public event EventHandler PropertyChanged;
- protected virtual void OnPropertyChanged()
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, EventArgs.Empty);
- }
- }
- /// <summary>
- /// 选中状态改变的事件
- /// </summary>
- public event Action SelectedChanged;
- public ISurfaceBox ISurfaceBox
- {
- set
- {
- this.surfaceBox = value;
- }
- get
- {
- return this.surfaceBox;
- }
- }
- public DrawObject()
- {
- id = this.GetHashCode();
- }
- /// <summary>
- /// Selection flag
- /// </summary>
- public bool Selected
- {
- get
- {
- return selected;
- }
- set
- {
- selected = value;
- SelectedChanged?.Invoke();
- }
- }
- /// <summary>
- /// Color
- /// </summary>
- public Color Color
- {
- get
- {
- return color;
- }
- set
- {
- color = value;
- }
- }
- /// <summary>
- /// Pen width
- /// </summary>
- public int PenWidth
- {
- get
- {
- return penWidth;
- }
- set
- {
- penWidth = value;
- }
- }
- /// <summary>
- /// Number of handles
- /// </summary>
- public virtual int HandleCount
- {
- get
- {
- return 0;
- }
- }
- /// <summary>
- /// Object ID
- /// </summary>
- public int ID
- {
- get { return id; }
- set { id = value; }
- }
- /// <summary>
- /// Last used color
- /// </summary>
- public static Color LastUsedColor
- {
- get
- {
- return lastUsedColor;
- }
- set
- {
- lastUsedColor = value;
- }
- }
- /// <summary>
- /// Last used pen width
- /// </summary>
- public static int LastUsedPenWidth
- {
- get
- {
- return lastUsedPenWidth;
- }
- set
- {
- lastUsedPenWidth = value;
- }
- }
- #region 虚函数
- /// <summary>
- /// Clone this instance.
- /// </summary>
- public abstract DrawObject Clone();
- public virtual DrawObject Clone(ISurfaceBox ISurfaceBox) { return null; }
- /// <summary>
- /// Draw object
- /// </summary>
- /// <param name="g"></param>
- public virtual void Draw(Graphics g)
- {
- }
- public abstract RectangleF GetBoundingBox();
- /// <summary>
- /// Get handle pointscroll by 1-based number
- /// </summary>
- /// <param name="handleNumber"></param>
- /// <returns></returns>
- public virtual PointF GetHandle(int handleNumber)
- {
- return new PointF(0, 0);
- }
- /// <summary>
- /// Get handle rectangle by 1-based number
- /// </summary>
- /// <param name="handleNumber"></param>
- /// <returns></returns>
- public virtual Rectangle GetHandleRectangle(int handleNumber)
- {
- PointF point = GetHandle(handleNumber);
- int tempLess;
- int tempPlus;
- if (isFollow)
- {
- if (smallRectangleWidth == 0)
- {
- tempLess = (int)(6 * (this.surfaceBox.ScaleRatio < 1 ? 1 / this.surfaceBox.ScaleRatio : 1));
- tempPlus = (int)(12 * (this.surfaceBox.ScaleRatio < 1 ? 1 / this.surfaceBox.ScaleRatio : 1));
- }
- else
- {
- tempLess = (int)((smallRectangleWidth / 2) * (this.surfaceBox.ScaleRatio < 1 ? 1 / this.surfaceBox.ScaleRatio : 1));
- tempPlus = (int)(smallRectangleWidth * (this.surfaceBox.ScaleRatio < 1 ? 1 / this.surfaceBox.ScaleRatio : 1));
- }
- }
- else
- {
- if (smallRectangleWidth == 0)
- {
- tempLess = (int)(6 * 1);
- tempPlus = (int)(12 * 1);
- }
- else
- {
- tempLess = (int)((smallRectangleWidth / 2) * 1);
- tempPlus = (int)(smallRectangleWidth * 1);
- }
- }
- return new Rectangle((int)(point.X - tempLess), (int)(point.Y - tempLess), tempPlus, tempPlus);
- }
- /// <summary>
- /// Get handle rectangle by 1-based number
- /// </summary>
- /// <param name="handleNumber"></param>
- /// <returns></returns>
- public virtual PointF[] GetHandlePoint(int handleNumber)
- {
- PointF point = GetHandle(handleNumber);
- int tempLess;
- int tempPlus;
- if (isFollow)
- {
- if (smallRectangleWidth == 0)
- {
- tempLess = (int)(6 * (this.surfaceBox.ScaleRatio < 1 ? 1 / this.surfaceBox.ScaleRatio : 1));
- tempPlus = (int)(12 * (this.surfaceBox.ScaleRatio < 1 ? 1 / this.surfaceBox.ScaleRatio : 1));
- }
- else
- {
- tempLess = (int)((smallRectangleWidth / 2) * (this.surfaceBox.ScaleRatio < 1 ? 1 / this.surfaceBox.ScaleRatio : 1));
- tempPlus = (int)(smallRectangleWidth * (this.surfaceBox.ScaleRatio < 1 ? 1 / this.surfaceBox.ScaleRatio : 1));
- }
- }
- else {
- if (smallRectangleWidth == 0)
- {
- tempLess = (int)(6 * 1);
- tempPlus = (int)(12 * 1);
- }
- else
- {
- tempLess = (int)((smallRectangleWidth / 2) * 1);
- tempPlus = (int)(smallRectangleWidth * 1);
- }
- }
- PointF point1 = new PointF(point.X,point.Y - tempLess);
- PointF point2 = new PointF(point.X - tempLess, point.Y + tempLess);
- PointF point3 = new PointF(point.X + tempLess, point.Y + tempLess);
- return new PointF[3] { point1,point2,point3 };
- }
- /// <summary>
- /// Draw tracker for selected object
- /// </summary>
- /// <param name="g"></param>
- public virtual void DrawTracker(Graphics g)
- {
- if (!Selected)
- return;
- SolidBrush brush = new SolidBrush(Color.FromArgb(MarkpointAreaColor));
- Pen pen = new Pen(Color.FromArgb(MarkpointLineColor),MarkpointLineWidth);
- for (int i = 1; i <= HandleCount; i++)
- {
- switch (MarkpointStyle)
- {
- case 0:
- g.FillRectangle(brush, GetHandleRectangle(i));
- g.DrawRectangle(pen, GetHandleRectangle(i));
- break;
- case 1:
- g.FillEllipse(brush, GetHandleRectangle(i));
- g.DrawEllipse(pen, GetHandleRectangle(i));
- break;
- case 2:
- g.FillPolygon(brush, GetHandlePoint(i));
- g.DrawPolygon(pen, GetHandlePoint(i));
- break;
- }
- }
- brush.Dispose();
- pen.Dispose();
- }
- /// <summary>
- /// Hit test.
- /// Return value: -1 - no hit
- /// 0 - hit anywhere
- /// > 1 - handle number
- /// </summary>
- /// <param name="pointscroll"></param>
- /// <returns></returns>
- public virtual int HitTest(Point point)
- {
- return -1;
- }
- /// <summary>
- /// 获取鼠标按下时的坐标
- /// </summary>
- /// <param name="point"></param>
- public virtual void MouseDownPoint(Point point)
- {
- }
- public virtual void MouseUp(bool up)
- {
- }
- /// <summary>
- /// Test whether pointscroll is inside of the object
- /// </summary>
- /// <param name="pointscroll"></param>
- /// <returns></returns>
- protected virtual bool PointInObject(Point point)
- {
- return false;
- }
- /// <summary>
- /// Get cursor for the handle
- /// </summary>
- /// <param name="handleNumber"></param>
- /// <returns></returns>
- public virtual Cursor GetHandleCursor(int handleNumber)
- {
- return Cursors.Default;
- }
- /// <summary>
- /// Test whether object intersects with rectangle
- /// </summary>
- /// <param name="rectangle"></param>
- /// <returns></returns>
- public virtual bool IntersectsWith(Rectangle rectangle)
- {
- return false;
- }
- /// <summary>
- /// Move object
- /// </summary>
- /// <param name="deltaX"></param>
- /// <param name="deltaY"></param>
- public virtual void Move(int deltaX, int deltaY)
- {
- OnPropertyChanged();
- }
- /// <summary>
- /// Move handle to the pointscroll
- /// </summary>
- /// <param name="pointscroll"></param>
- /// <param name="handleNumber"></param>
- public virtual void MoveHandleTo(Point point, int handleNumber)
- {
- OnPropertyChanged();
- }
- public virtual void MoveHandleTo(PointF point, int handleNumber)
- {
- OnPropertyChanged();
- }
- /// <summary>
- /// Dump (for debugging)
- /// </summary>
- public virtual void Dump()
- {
- Trace.WriteLine(this.GetType().Name);
- Trace.WriteLine("Selected = " +
- selected.ToString(CultureInfo.InvariantCulture)
- + " ID = " + id.ToString(CultureInfo.InvariantCulture));
- }
- /// <summary>
- /// Normalize object.
- /// Call this function in the end of object resizing.
- /// </summary>
- public virtual void Normalize()
- {
- this.Rectangle = GetNormalizedRectangleF(this.Rectangle);
- }
- public static RectangleF GetNormalizedRectangleF(RectangleF r)
- {
- return GetNormalizedRectangleF(r.X, r.Y, r.X + r.Width, r.Y + r.Height);
- }
- public static RectangleF GetNormalizedRectangleF(PointF p1, PointF p2)
- {
- return GetNormalizedRectangleF(p1.X, p1.Y, p2.X, p2.Y);
- }
- public static RectangleF GetNormalizedRectangleF(float x1, float y1, float x2, float y2)
- {
- if (x2 < x1)
- {
- float tmp = x2;
- x2 = x1;
- x1 = tmp;
- }
- if (y2 < y1)
- {
- float tmp = y2;
- y2 = y1;
- y1 = tmp;
- }
- return new RectangleF(x1, y1, (x2 - x1), (y2 - y1));
- }
- public static Rectangle GetNormalizedRectangle(RectangleF r)
- {
- return GetNormalizedRectangle(r.X, r.Y, r.X + r.Width, r.Y + r.Height);
- }
- public static Rectangle GetNormalizedRectangle(PointF p1, PointF p2)
- {
- return GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y);
- }
- public static Rectangle GetNormalizedRectangle(float x1, float y1, float x2, float y2)
- {
- if (x2 < x1)
- {
- float tmp = x2;
- x2 = x1;
- x1 = tmp;
- }
- if (y2 < y1)
- {
- float tmp = y2;
- y2 = y1;
- y1 = tmp;
- }
- return new Rectangle((int)x1, (int)y1, (int)(x2 - x1), (int)(y2 - y1));
- }
- /// <summary>
- /// Save object to serialization stream
- /// </summary>
- /// <param name="info"></param>
- /// <param name="orderNumber"></param>
- public virtual void SaveToStream(SerializationInfo info, int orderNumber)
- {
- info.AddValue(
- String.Format(CultureInfo.InvariantCulture,
- "{0}{1}",
- entryColor, orderNumber),
- Color.ToArgb());
- info.AddValue(
- String.Format(CultureInfo.InvariantCulture,
- "{0}{1}",
- entryPenWidth, orderNumber),
- PenWidth);
- }
- /// <summary>
- /// Load object from serialization stream
- /// </summary>
- /// <param name="info"></param>
- /// <param name="orderNumber"></param>
- public virtual void LoadFromStream(SerializationInfo info, int orderNumber)
- {
- int n = info.GetInt32(
- String.Format(CultureInfo.InvariantCulture,
- "{0}{1}",
- entryColor, orderNumber));
- Color = Color.FromArgb(n);
- PenWidth = info.GetInt32(
- String.Format(CultureInfo.InvariantCulture,
- "{0}{1}",
- entryPenWidth, orderNumber));
- id = this.GetHashCode();
- }
- /// <summary>
- /// 用于返回组成标注/测量/视场的所有的点的集合
- /// 暂时没有想到有特殊的数据需要处理,想到再说
- /// </summary>
- /// <returns></returns>
- public virtual List<PointF> GetPoints()
- {
- return null;
- }
- public virtual ParentStyleModel GetStyle()
- {
- return null;
- }
- public virtual string GetContent()
- {
- return "";
- }
- #endregion
- #region 其它方法
- /// <summary>
- /// Initialization
- /// </summary>
- protected void Initialize()
- {
- color = lastUsedColor;
- penWidth = LastUsedPenWidth;
- }
- /// <summary>
- /// Copy fields from this instance to cloned instance drawObject.
- /// Called from Clone functions of derived classes.
- /// </summary>
- protected void FillDrawObjectFields(DrawObject drawObject)
- {
- drawObject.selected = this.selected;
- drawObject.color = this.color;
- drawObject.penWidth = this.penWidth;
- drawObject.ID = this.ID;
- }
- #endregion
- }
- }
|