using PaintDotNet.Annotation.Enum;
using PaintDotNet.Base.SettingModel;
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;
using static PaintDotNet.Base.SettingModel.LabelStyleModel;
namespace PaintDotNet.Annotation.Label
{
///
/// 标注->多边形->矩形
///
public class DrawRectangle : DrawObject
{
///
/// 标注样式信息model
///
public PolygonRectangle labelRectStyleModel;
public DrawRectangle(ISurfaceBox surfaceBox, int x, int y, int width, int height) : base()
{
this.objectType = DrawClass.Label;
this.drawToolType = DrawToolType.DrawRectangle;
this.labelRectStyleModel = surfaceBox.GetLabelStyleModel().polygonRectangle;
this.rectangle.X = x;
this.rectangle.Y = y;
this.rectangle.Width = width;
this.rectangle.Height = height;
Initialize();
}
public DrawRectangle(ISurfaceBox surfaceBox, List points, ParentStyleModel parentStyleModel, Object content) : base()
{
this.objectType = DrawClass.Label;
this.drawToolType = DrawToolType.DrawRectangle;
this.ISurfaceBox = surfaceBox;
labelRectStyleModel = (PolygonRectangle)parentStyleModel;
rectangle.X = points[0].X;
rectangle.Y = points[0].Y;
rectangle.Width = points[1].X - points[0].X;
rectangle.Height = points[1].Y - points[0].Y;
}
///
/// Clone this instance
///
public override DrawObject Clone()
{
DrawRectangle drawRectangle = new DrawRectangle(ISurfaceBox, 0, 0, 1, 0);
drawRectangle.objectType = DrawClass.Label;
drawRectangle.drawToolType = DrawToolType.DrawRectangle;
drawRectangle.ISurfaceBox = this.ISurfaceBox;
drawRectangle.rectangle = this.rectangle;
FillDrawObjectFields(drawRectangle);
return drawRectangle;
}
public override DrawObject Clone(ISurfaceBox surfaceBox)
{
DrawRectangle drawRectangle = new DrawRectangle(surfaceBox, 0, 0, 1, 0);
drawRectangle.objectType = DrawClass.Label;
drawRectangle.drawToolType = DrawToolType.DrawRectangle;
drawRectangle.ISurfaceBox = surfaceBox;
drawRectangle.rectangle = this.rectangle;
drawRectangle.labelRectStyleModel = this.labelRectStyleModel;
FillDrawObjectFields(drawRectangle);
return drawRectangle;
}
///
/// Draw rectangle
///
///
public override void Draw(Graphics g)
{
Color color = Color.FromArgb(this.labelRectStyleModel.lineColor);
Pen pen = new Pen(color, PenWidth);
int penWidth = this.labelRectStyleModel.lineWidth;
pen.DashStyle = (DashStyle)this.labelRectStyleModel.lineStyle;
pen.Width = penWidth;
Color fillColor = Color.FromArgb(this.labelRectStyleModel.fillColor);
SolidBrush fillBrush = new SolidBrush(fillColor);
g.FillRectangle(fillBrush, DrawRectangle.GetNormalizedRectangle(Rectangle));
g.DrawRectangle(pen, DrawRectangle.GetNormalizedRectangle(Rectangle));
fillBrush.Dispose();
pen.Dispose();
}
protected void SetRectangle(float x, float y, float width, float height)
{
this.rectangle.X = x;
this.rectangle.Y = y;
this.rectangle.Width = width;
this.rectangle.Height = height;
}
///
/// Get number of handles
///
public override int HandleCount
{
get
{
return 8;
}
}
///
/// Get handle pointscroll by 1-based number
///
///
///
public override PointF GetHandle(int handleNumber)
{
float x, y, xCenter, yCenter;
xCenter = this.rectangle.X + this.rectangle.Width / 2;
yCenter = this.rectangle.Y + this.rectangle.Height / 2;
x = this.rectangle.X;
y = this.rectangle.Y;
switch (handleNumber)
{
case 1:
x = this.rectangle.X;
y = this.rectangle.Y;
break;
case 2:
x = xCenter;
y = this.rectangle.Y;
break;
case 3:
x = this.rectangle.Right;
y = this.rectangle.Y;
break;
case 4:
x = this.rectangle.Right;
y = yCenter;
break;
case 5:
x = this.rectangle.Right;
y = this.rectangle.Bottom;
break;
case 6:
x = xCenter;
y = this.rectangle.Bottom;
break;
case 7:
x = this.rectangle.X;
y = this.rectangle.Bottom;
break;
case 8:
x = this.rectangle.X;
y = yCenter;
break;
}
return new PointF(x, y);
}
///
/// Hit test.
/// Return value: -1 - no hit
/// 0 - hit anywhere
/// > 1 - handle number
///
///
///
public override int HitTest(Point point)
{
if (Selected)
{
for (int i = 1; i <= HandleCount; i++)
{
if (GetHandleRectangle(i).Contains(point))
return i;
}
}
if (PointInObject(point))
return 0;
return -1;
}
protected override bool PointInObject(Point point)
{
return rectangle.Contains(point);
}
///
/// Get cursor for the handle
///
///
///
public override Cursor GetHandleCursor(int handleNumber)
{
switch (handleNumber)
{
case 1:
return Cursors.SizeNWSE;
case 2:
return Cursors.SizeNS;
case 3:
return Cursors.SizeNESW;
case 4:
return Cursors.SizeWE;
case 5:
return Cursors.SizeNWSE;
case 6:
return Cursors.SizeNS;
case 7:
return Cursors.SizeNESW;
case 8:
return Cursors.SizeWE;
default:
return Cursors.Default;
}
}
///
/// Move handle to new pointscroll (resizing)
///
///
///
public override void MoveHandleTo(Point point, int handleNumber)
{
float left = Rectangle.Left;
float top = Rectangle.Top;
float right = Rectangle.Right;
float bottom = Rectangle.Bottom;
switch (handleNumber)
{
case 1:
left = point.X;
top = point.Y;
break;
case 2:
top = point.Y;
break;
case 3:
right = point.X;
top = point.Y;
break;
case 4:
right = point.X;
break;
case 5:
right = point.X;
bottom = point.Y;
break;
case 6:
bottom = point.Y;
break;
case 7:
left = point.X;
bottom = point.Y;
break;
case 8:
left = point.X;
break;
}
SetRectangle(left, top, right - left, bottom - top);
}
public override bool IntersectsWith(Rectangle rectangle)
{
return Rectangle.IntersectsWith(rectangle);
}
///
/// Move object
///
///
///
public override void Move(int deltaX, int deltaY)
{
int x = ISurfaceBox.UnscaleScalar(deltaX);
int y = ISurfaceBox.UnscaleScalar(deltaY);
rectangle.X += x;
rectangle.Y += y;
}
public override RectangleF GetBoundingBox()
{
return rectangle;
}
public override List GetPoints()
{
List points = new List();
points.Add(new PointF(rectangle.X, rectangle.Y));
points.Add(new PointF(rectangle.Right, rectangle.Bottom));
return points;
}
public override ParentStyleModel GetStyle()
{
return labelRectStyleModel;
}
}
}