123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using PaintDotNet.Annotation.Enum;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Globalization;
- using System.Runtime.Serialization;
- using System.Windows.Forms;
- namespace PaintDotNet.Annotation.FieldView
- {
- /// <summary>
- /// 正方形视场绘制
- /// </summary>
- public class ViewSquare : ViewBase
- {
- public override double Width
- {
- get { return Rectangle.Width; }
- set
- {
- MoveHandleTo(new PointF((float)(Points[0].X + value), (float)(Points[0].Y + value)), 3);
- }
- }
- #region Constructor
- public ViewSquare()
- {
- objectType = DrawClass.View;
- drawToolType = DrawToolType.ViewSquare;
- Initialize();
- }
- public ViewSquare(int x, int y) : this()
- {
- for (int i = 0; i < 4; i++) AddPoint(x, y);
- }
- public ViewSquare(List<PointF> points) : this()
- {
- Points = points;
- }
- #endregion
- #region Handles
- public override int HandleCount => Points.Count;
- public override PointF GetHandle(int handleIndex)
- {
- return Points[handleIndex - 1];
- }
- public override Cursor GetHandleCursor(int handleNumber)
- {
- switch (handleNumber)
- {
- case 1:
- return Cursors.SizeNWSE;
- case 2:
- return Cursors.SizeNESW;
- case 3:
- return Cursors.SizeNWSE;
- case 4:
- return Cursors.SizeNESW;
- default:
- return Cursors.Default;
- }
- }
- public override void MoveHandleTo(Point point, int handleNumber)
- {
- MoveHandleTo((PointF)point, handleNumber);
- }
- public override void MoveHandleTo(PointF point, int handleNumber)
- {
- MoveCorner(point, handleNumber - 1);
- OnPropertyChanged();
- }
- private void MoveCorner(PointF point, int i)
- {
- var p0 = Points[(i + 2) % 4];
- var lenX = point.X - p0.X;
- var lenY = point.Y - p0.Y;
- var len = Math.Max(Math.Abs(lenX), Math.Abs(lenY));
- lenX = lenX > 0 ? len : -len;
- lenY = lenY > 0 ? len : -len;
- Points[i] = new PointF(p0.X + lenX, p0.Y + lenY);
- if (i % 2 == 0)
- {
- Points[(i + 1) % 4] = new PointF(p0.X + lenX, p0.Y);
- Points[(i + 3) % 4] = new PointF(p0.X, p0.Y + lenY);
- }
- else
- {
- Points[(i + 3) % 4] = new PointF(p0.X + lenX, p0.Y);
- Points[(i + 1) % 4] = new PointF(p0.X, p0.Y + lenY);
- }
- }
- #endregion
- #region Clone
- public override DrawObject Clone()
- {
- return Clone(ISurfaceBox);
- }
- /// <summary>
- /// Clone this instance
- /// </summary>
- public override DrawObject Clone(ISurfaceBox surfaceBox)
- {
- ViewSquare drawRectangle = new ViewSquare(Points);
- drawRectangle.ISurfaceBox = surfaceBox;
- drawRectangle.combineMode = this.combineMode;
- drawRectangle.Color = Color;
- drawRectangle.ID = ID;
- FillDrawObjectFields(drawRectangle);
- return drawRectangle;
- }
- #endregion
- }
- }
|