using System;
using System.Collections.Generic;
using PaintDotNet.Annotation.Enum;
using PaintDotNet.Base.SettingModel;
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 DrawSquareA : DrawObject
{
private const string entryRectangle = "Rect";
///
/// 标注样式信息model
///
public DrawAnalysisModel labelCircleStyleModel;
private Point centerPoint;
public DrawSquareA(ISurfaceBox surfaceBox, int x, int y, int width, int height) : base()
{
this.objectType = DrawClass.Label;
this.drawToolType = DrawToolType.DrawSquareA;
labelCircleStyleModel = surfaceBox.AnalysisStyleModel;
rectangle.X = x;
rectangle.Y = y;
rectangle.Width = width;
rectangle.Height = height;
Initialize();
}
public DrawSquareA(ISurfaceBox surfaceBox, int x, int y, int width, int height, DrawAnalysisModel model) : base()
{
this.objectType = DrawClass.Label;
this.drawToolType = DrawToolType.DrawSquareA;
centerPoint = new Point(x, y);
labelCircleStyleModel = model;
rectangle.X = x - this.labelCircleStyleModel.size / 2;
rectangle.Y = y - this.labelCircleStyleModel.size / 2;
rectangle.Width = this.labelCircleStyleModel.size;
rectangle.Height = this.labelCircleStyleModel.size;
Initialize();
}
///
/// Clone this instance
///
public override DrawObject Clone()
{
DrawSquareA drawRectangle = new DrawSquareA(ISurfaceBox, 0, 0, 1, 0);
drawRectangle.objectType = DrawClass.Other;
drawRectangle.drawToolType = DrawToolType.DrawSquareA;
drawRectangle.ISurfaceBox = this.ISurfaceBox;
drawRectangle.rectangle = this.rectangle;
FillDrawObjectFields(drawRectangle);
return drawRectangle;
}
public override DrawObject Clone(ISurfaceBox surfaceBox)
{
DrawSquareA drawRectangle = new DrawSquareA(surfaceBox, 0, 0, 1, 0);
drawRectangle.objectType = DrawClass.Other;
drawRectangle.drawToolType = DrawToolType.DrawSquareA;
drawRectangle.ISurfaceBox = surfaceBox;
drawRectangle.rectangle = this.rectangle;
drawRectangle.labelCircleStyleModel = this.labelCircleStyleModel;
FillDrawObjectFields(drawRectangle);
return drawRectangle;
}
///
/// Draw rectangle
///
///
public override void Draw(Graphics g)
{
Color color = Color.FromArgb(this.labelCircleStyleModel.lineColor);
Pen pen = new Pen(color, PenWidth);
int penWidth = this.labelCircleStyleModel.lineWidth;
pen.DashStyle = (DashStyle)this.labelCircleStyleModel.lineStyle;
pen.Width = penWidth;
Color fillColor = Color.FromArgb(this.labelCircleStyleModel.fillColor);
SolidBrush fillBrush = new SolidBrush(fillColor);
float x1 = centerPoint.X - this.labelCircleStyleModel.size / 2;
float y1 = centerPoint.Y - this.labelCircleStyleModel.size / 2;
float x2 = centerPoint.X + this.labelCircleStyleModel.size / 2;
float y2 = centerPoint.Y + this.labelCircleStyleModel.size / 2;
if (x2 < x1)
{
float tmp = x2;
x2 = x1;
x1 = tmp;
}
if (y2 < y1)
{
float tmp = y2;
y2 = y1;
y1 = tmp;
}
float radius = (x2 - x1) < (y2 - y1) ? (x2 - x1) : (y2 - y1);
if (labelCircleStyleModel.fillType==1)
{
g.FillRectangle(fillBrush, new RectangleF(x1, y1, radius, radius));
}
g.DrawRectangle(pen, x1, y1, radius, radius);
fillBrush.Dispose();
pen.Dispose();
}
protected void SetRectangle(float x, float y, float width, float height, int handleNumber)
{
this.rectangle.X = x;
this.rectangle.Y = y;
if (handleNumber == 2 || handleNumber == 6)
{
this.rectangle.Width = height;
this.rectangle.Height = height;
}
else
{
this.rectangle.Width = width;
this.rectangle.Height = width;
}
}
///
/// 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 = rectangle.X + rectangle.Width / 2;
yCenter = rectangle.Y + rectangle.Height / 2;
x = rectangle.X;
y = rectangle.Y;
switch (handleNumber)
{
case 1:
x = rectangle.X;
y = rectangle.Y;
break;
case 2:
x = xCenter;
y = rectangle.Y;
break;
case 3:
x = rectangle.Right;
y = rectangle.Y;
break;
case 4:
x = rectangle.Right;
y = yCenter;
break;
case 5:
x = rectangle.Right;
y = rectangle.Bottom;
break;
case 6:
x = xCenter;
y = rectangle.Bottom;
break;
case 7:
x = rectangle.X;
y = rectangle.Bottom;
break;
case 8:
x = 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, handleNumber);
}
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 void Dump()
{
base.Dump();
Trace.WriteLine("rectangle.X = " + rectangle.X.ToString(CultureInfo.InvariantCulture));
Trace.WriteLine("rectangle.Y = " + rectangle.Y.ToString(CultureInfo.InvariantCulture));
Trace.WriteLine("rectangle.Width = " + rectangle.Width.ToString(CultureInfo.InvariantCulture));
Trace.WriteLine("rectangle.Height = " + rectangle.Height.ToString(CultureInfo.InvariantCulture));
}
///
/// Normalize rectangle
///
public override void Normalize()
{
rectangle = DrawRectangle.GetNormalizedRectangle(rectangle);
}
///
/// Save objevt to serialization stream
///
///
///
public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
{
info.AddValue(
String.Format(CultureInfo.InvariantCulture,
"{0}{1}",
entryRectangle, orderNumber),
rectangle);
base.SaveToStream(info, orderNumber);
}
///
/// LOad object from serialization stream
///
///
///
public override void LoadFromStream(SerializationInfo info, int orderNumber)
{
rectangle = (Rectangle)info.GetValue(
String.Format(CultureInfo.InvariantCulture,
"{0}{1}",
entryRectangle, orderNumber),
typeof(Rectangle));
base.LoadFromStream(info, orderNumber);
}
#region Helper Functions
public static Rectangle GetNormalizedRectangle(int x1, int y1, int x2, int y2)
{
if (x2 < x1)
{
int tmp = x2;
x2 = x1;
x1 = tmp;
}
if (y2 < y1)
{
int tmp = y2;
y2 = y1;
y1 = tmp;
}
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
}
public static Rectangle GetNormalizedRectangle(Point p1, Point p2)
{
return GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y);
}
public static Rectangle GetNormalizedRectangle(Rectangle r)
{
return GetNormalizedRectangle(r.X, r.Y, r.X + r.Width, r.Y + r.Height);
}
#endregion
public override RectangleF GetBoundingBox()
{
return rectangle;
}
public override List GetPoints()
{
List points = new List();
points.Add(new PointF(centerPoint.X, centerPoint.Y));
points.Add(new PointF(rectangle.Right, rectangle.Bottom));
return points;
}
public override ParentStyleModel GetStyle()
{
return labelCircleStyleModel;
}
}
}