ShareX/ShareX.ScreenCaptureLib/Shapes/BaseShape.cs

368 lines
12 KiB
C#
Raw Normal View History

2015-07-17 03:21:02 +12:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2017-01-11 21:39:40 +13:00
Copyright (c) 2007-2017 ShareX Team
2015-07-17 03:21:02 +12:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System;
2015-07-17 03:21:02 +12:00
using System.Drawing;
using System.Drawing.Drawing2D;
2016-08-06 01:35:44 +12:00
using System.Windows.Forms;
2015-07-17 03:21:02 +12:00
namespace ShareX.ScreenCaptureLib
{
public abstract class BaseShape : IDisposable
2015-07-17 03:21:02 +12:00
{
2016-05-24 04:18:09 +12:00
protected const int MinimumSize = 3;
2016-09-02 19:09:24 +12:00
public abstract ShapeCategory ShapeCategory { get; }
public abstract ShapeType ShapeType { get; }
private Rectangle rectangle;
public Rectangle Rectangle
{
get
{
return rectangle;
}
set
{
rectangle = value;
startPosition = rectangle.Location;
endPosition = new Point(rectangle.X + rectangle.Width - 1, rectangle.Y + rectangle.Height - 1);
}
}
public Rectangle RectangleInsideCanvas => Rectangle.Intersect(Rectangle, Manager.Form.CanvasRectangle);
2017-11-15 01:05:12 +13:00
public bool IsInsideCanvas => !RectangleInsideCanvas.IsEmpty;
private Point startPosition;
public Point StartPosition
{
get
{
return startPosition;
}
2017-11-16 22:40:56 +13:00
private set
{
startPosition = value;
rectangle = CaptureHelpers.CreateRectangle(startPosition, endPosition);
}
}
private Point endPosition;
public Point EndPosition
{
get
{
return endPosition;
}
2017-11-16 22:40:56 +13:00
private set
{
endPosition = value;
rectangle = CaptureHelpers.CreateRectangle(startPosition, endPosition);
}
}
2015-07-17 03:21:02 +12:00
public virtual bool IsValidShape => !Rectangle.IsEmpty && Rectangle.Width >= MinimumSize && Rectangle.Height >= MinimumSize;
internal ShapeManager Manager { get; set; }
2017-10-24 10:40:37 +13:00
protected InputManager InputManager => Manager.InputManager;
2017-11-07 05:01:02 +13:00
protected RegionCaptureOptions Options => Manager.Options;
protected AnnotationOptions AnnotationOptions => Manager.Options.AnnotationOptions;
2016-08-24 01:03:48 +12:00
private Point tempNodePos, tempStartPos, tempEndPos;
2016-08-06 01:35:44 +12:00
2016-05-29 22:10:48 +12:00
public virtual bool Intersects(Point position)
{
return Rectangle.Contains(position);
}
public virtual void ShowNodes()
{
Manager.NodesVisible = true;
}
public void Remove()
{
Manager.DeleteShape(this);
}
public void AddShapePath(GraphicsPath gp, int sizeOffset = 0)
2016-05-11 10:17:51 +12:00
{
Rectangle rect = Rectangle;
if (sizeOffset != 0)
{
rect = rect.SizeOffset(sizeOffset);
}
OnShapePathRequested(gp, rect);
}
public virtual void Move(int x, int y)
{
Rectangle = Rectangle.LocationOffset(x, y);
}
2017-10-18 03:44:46 +13:00
public void Move(Point offset)
{
2017-10-18 03:44:46 +13:00
Move(offset.X, offset.Y);
}
public virtual void Resize(int x, int y, bool fromBottomRight)
{
if (fromBottomRight)
{
2017-11-16 19:36:21 +13:00
Rectangle = Rectangle.SizeOffset(x, y);
}
else
{
2017-11-16 19:36:21 +13:00
Rectangle = Rectangle.LocationOffset(x, y).SizeOffset(-x, -y);
}
}
2016-08-24 01:03:48 +12:00
public virtual void OnCreating()
{
2017-11-07 05:01:02 +13:00
Point pos = InputManager.ClientMousePosition;
2016-08-24 01:03:48 +12:00
2016-09-02 19:09:24 +12:00
if (Options.IsFixedSize && ShapeCategory == ShapeCategory.Region)
2016-08-24 01:03:48 +12:00
{
Manager.IsMoving = true;
Rectangle = new Rectangle(new Point(pos.X - Options.FixedSize.Width / 2, pos.Y - Options.FixedSize.Height / 2), Options.FixedSize);
}
else
{
Manager.IsCreating = true;
2017-11-16 19:36:21 +13:00
Rectangle = new Rectangle(pos.X, pos.Y, 1, 1);
2016-08-24 01:03:48 +12:00
}
}
public virtual void OnCreated() { }
public virtual void OnMoving() { }
public virtual void OnMoved() { }
public virtual void OnResizing() { }
public virtual void OnResized() { }
public virtual void OnUpdate()
{
if (Manager.IsCreating)
{
2017-11-07 05:01:02 +13:00
Point pos = InputManager.ClientMousePosition;
if (Manager.IsCornerMoving && !Manager.IsPanning)
{
StartPosition = StartPosition.Add(InputManager.MouseVelocity);
}
else if (Manager.IsProportionalResizing)
{
float degree, startDegree;
if (ShapeType == ShapeType.DrawingLine || ShapeType == ShapeType.DrawingArrow)
{
degree = 45;
startDegree = 0;
}
else
{
degree = 90;
startDegree = 45;
}
pos = CaptureHelpers.SnapPositionToDegree(StartPosition, pos, degree, startDegree);
}
else if (Manager.IsSnapResizing)
{
pos = Manager.SnapPosition(StartPosition, pos);
}
EndPosition = pos;
}
else if (Manager.IsMoving && !Manager.IsPanning)
{
Move(InputManager.MouseVelocity);
}
}
public virtual void OnShapePathRequested(GraphicsPath gp, Rectangle rect)
{
gp.AddRectangle(rect);
}
public virtual void OnConfigLoad()
{
}
public virtual void OnConfigSave()
{
}
public virtual void OnDoubleClicked()
{
}
2016-08-06 01:35:44 +12:00
public virtual void OnNodeVisible()
{
2016-08-25 01:29:50 +12:00
for (int i = 0; i < 8; i++)
2016-08-06 01:35:44 +12:00
{
2016-08-25 01:29:50 +12:00
ResizeNode node = Manager.ResizeNodes[i];
2016-08-06 01:35:44 +12:00
node.Visible = true;
}
}
public virtual void OnNodeUpdate()
{
for (int i = 0; i < 8; i++)
{
2016-08-17 20:35:06 +12:00
ResizeNode node = Manager.ResizeNodes[i];
if (node.IsDragging)
2016-08-06 01:35:44 +12:00
{
Manager.IsResizing = true;
if (!InputManager.IsBeforeMouseDown(MouseButtons.Left))
{
tempNodePos = node.Position;
tempStartPos = Rectangle.Location;
tempEndPos = new Point(Rectangle.X + Rectangle.Width - 1, Rectangle.Y + Rectangle.Height - 1);
2017-11-15 00:21:36 +13:00
OnResizing();
2016-08-06 01:35:44 +12:00
}
if (Manager.IsCornerMoving || Manager.IsPanning)
{
tempStartPos.Offset(InputManager.MouseVelocity);
tempEndPos.Offset(InputManager.MouseVelocity);
tempNodePos.Offset(InputManager.MouseVelocity);
}
2017-11-07 05:01:02 +13:00
Point pos = InputManager.ClientMousePosition;
Point startPos = tempStartPos;
Point endPos = tempEndPos;
2016-08-06 01:35:44 +12:00
NodePosition nodePosition = (NodePosition)i;
int x = pos.X - tempNodePos.X;
2016-08-06 01:35:44 +12:00
switch (nodePosition)
{
case NodePosition.TopLeft:
case NodePosition.Left:
case NodePosition.BottomLeft:
startPos.X += x;
2016-08-06 01:35:44 +12:00
break;
case NodePosition.TopRight:
case NodePosition.Right:
case NodePosition.BottomRight:
endPos.X += x;
2016-08-06 01:35:44 +12:00
break;
}
int y = pos.Y - tempNodePos.Y;
2016-08-06 01:35:44 +12:00
switch (nodePosition)
{
case NodePosition.TopLeft:
case NodePosition.Top:
case NodePosition.TopRight:
startPos.Y += y;
2016-08-06 01:35:44 +12:00
break;
case NodePosition.BottomLeft:
case NodePosition.Bottom:
case NodePosition.BottomRight:
endPos.Y += y;
2016-08-06 01:35:44 +12:00
break;
}
StartPosition = startPos;
EndPosition = endPos;
2016-08-06 01:35:44 +12:00
}
}
}
public virtual void OnNodePositionUpdate()
{
int xStart = Rectangle.X;
int xMid = Rectangle.X + Rectangle.Width / 2;
int xEnd = Rectangle.X + Rectangle.Width - 1;
int yStart = Rectangle.Y;
int yMid = Rectangle.Y + Rectangle.Height / 2;
int yEnd = Rectangle.Y + Rectangle.Height - 1;
2016-08-17 20:35:06 +12:00
Manager.ResizeNodes[(int)NodePosition.TopLeft].Position = new Point(xStart, yStart);
Manager.ResizeNodes[(int)NodePosition.Top].Position = new Point(xMid, yStart);
Manager.ResizeNodes[(int)NodePosition.TopRight].Position = new Point(xEnd, yStart);
Manager.ResizeNodes[(int)NodePosition.Right].Position = new Point(xEnd, yMid);
Manager.ResizeNodes[(int)NodePosition.BottomRight].Position = new Point(xEnd, yEnd);
Manager.ResizeNodes[(int)NodePosition.Bottom].Position = new Point(xMid, yEnd);
Manager.ResizeNodes[(int)NodePosition.BottomLeft].Position = new Point(xStart, yEnd);
Manager.ResizeNodes[(int)NodePosition.Left].Position = new Point(xStart, yMid);
for (int i = 0; i < 8; i++)
{
Manager.ResizeNodes[i].Visible = true;
}
if (Manager.ResizeNodes[(int)NodePosition.Right].Rectangle.IntersectsWith(Manager.ResizeNodes[(int)NodePosition.BottomRight].Rectangle))
{
Manager.ResizeNodes[(int)NodePosition.Left].Visible =
Manager.ResizeNodes[(int)NodePosition.Right].Visible = false;
}
if (Manager.ResizeNodes[(int)NodePosition.Bottom].Rectangle.IntersectsWith(Manager.ResizeNodes[(int)NodePosition.BottomRight].Rectangle))
{
Manager.ResizeNodes[(int)NodePosition.Top].Visible =
Manager.ResizeNodes[(int)NodePosition.Bottom].Visible = false;
}
if (Manager.ResizeNodes[(int)NodePosition.TopRight].Rectangle.IntersectsWith(Manager.ResizeNodes[(int)NodePosition.BottomRight].Rectangle))
{
Manager.ResizeNodes[(int)NodePosition.TopLeft].Visible =
Manager.ResizeNodes[(int)NodePosition.Top].Visible =
Manager.ResizeNodes[(int)NodePosition.TopRight].Visible = false;
}
if (Manager.ResizeNodes[(int)NodePosition.BottomLeft].Rectangle.IntersectsWith(Manager.ResizeNodes[(int)NodePosition.BottomRight].Rectangle))
{
Manager.ResizeNodes[(int)NodePosition.TopLeft].Visible =
Manager.ResizeNodes[(int)NodePosition.Left].Visible =
Manager.ResizeNodes[(int)NodePosition.BottomLeft].Visible = false;
}
2016-08-06 01:35:44 +12:00
}
public virtual void Dispose()
{
}
2015-07-17 03:21:02 +12:00
}
}