ShareX/ShareX.ScreenCaptureLib/Shapes/Drawing/StepDrawingShape.cs

272 lines
9.5 KiB
C#
Raw Normal View History

2016-05-23 07:19:25 +12:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2016-05-23 07:19:25 +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)
2016-11-30 08:47:53 +13:00
using ShareX.HelpersLib;
2016-05-23 07:19:25 +12:00
using System;
using System.Drawing;
2020-01-15 10:25:36 +13:00
using System.Drawing.Drawing2D;
2016-05-23 07:19:25 +12:00
using System.Drawing.Text;
namespace ShareX.ScreenCaptureLib
{
2016-11-30 08:47:53 +13:00
public class StepDrawingShape : EllipseDrawingShape
2016-05-23 07:19:25 +12:00
{
2016-05-24 03:31:22 +12:00
private const int DefaultSize = 30;
2016-05-23 07:19:25 +12:00
public override ShapeType ShapeType { get; } = ShapeType.DrawingStep;
2017-12-08 09:17:36 +13:00
public int FontSize { get; set; }
2021-04-17 20:50:59 +12:00
public int Number { get; set; }
public StepType StepType { get; set; } = StepType.Numbers;
public bool IsTailActive { get; set; }
private PointF tailPosition;
2020-01-15 10:25:36 +13:00
public PointF TailPosition
{
2020-01-15 10:25:36 +13:00
get
{
return tailPosition;
}
private set
{
tailPosition = value;
TailNode.Position = tailPosition;
}
}
2020-01-15 10:25:36 +13:00
public bool TailVisible => !Rectangle.Contains(TailPosition);
internal ResizeNode TailNode => Manager.ResizeNodes[(int)NodePosition.Extra];
protected const float TailWidthMultiplier = 1f;
public StepDrawingShape()
{
2020-01-15 10:25:36 +13:00
Rectangle = new Rectangle(0, 0, DefaultSize, DefaultSize);
}
2016-08-24 01:03:48 +12:00
public override void OnCreating()
{
Manager.IsMoving = true;
PointF pos = Manager.Form.ScaledClientMousePosition;
Rectangle = new RectangleF(new PointF(pos.X - (Rectangle.Width / 2), pos.Y - (Rectangle.Height / 2)), Rectangle.Size);
int tailOffset = 5;
TailPosition = Rectangle.Location.Add(Rectangle.Width + tailOffset, Rectangle.Height + tailOffset);
OnCreated();
2020-01-15 10:25:36 +13:00
}
2020-01-15 10:48:31 +13:00
protected override void UseLightResizeNodes()
{
Manager.ResizeNodes[(int)NodePosition.Extra].Shape = NodeShape.Circle;
}
2020-01-15 10:25:36 +13:00
public override void OnNodeVisible()
{
TailNode.Position = TailPosition;
TailNode.Visible = true;
}
public override void OnNodeUpdate()
{
if (TailNode.IsDragging)
{
IsTailActive = true;
TailPosition = Manager.Form.ScaledClientMousePosition;
2020-01-15 10:25:36 +13:00
}
}
public override void OnNodePositionUpdate()
{
2016-08-24 01:03:48 +12:00
}
public override void OnConfigLoad()
2016-05-24 03:31:22 +12:00
{
BorderColor = AnnotationOptions.StepBorderColor;
BorderSize = AnnotationOptions.StepBorderSize;
FillColor = AnnotationOptions.StepFillColor;
Shadow = AnnotationOptions.Shadow;
2018-01-31 06:14:28 +13:00
ShadowColor = AnnotationOptions.ShadowColor;
ShadowOffset = AnnotationOptions.ShadowOffset;
2017-12-08 09:17:36 +13:00
FontSize = AnnotationOptions.StepFontSize;
2021-04-17 20:50:59 +12:00
StepType = AnnotationOptions.StepType;
2016-05-24 03:31:22 +12:00
}
public override void OnConfigSave()
2016-05-24 03:31:22 +12:00
{
AnnotationOptions.StepBorderColor = BorderColor;
AnnotationOptions.StepBorderSize = BorderSize;
AnnotationOptions.StepFillColor = FillColor;
AnnotationOptions.Shadow = Shadow;
2018-01-31 06:14:28 +13:00
AnnotationOptions.ShadowColor = ShadowColor;
AnnotationOptions.ShadowOffset = ShadowOffset;
2017-12-08 09:17:36 +13:00
AnnotationOptions.StepFontSize = FontSize;
2021-04-17 20:50:59 +12:00
AnnotationOptions.StepType = StepType;
2016-05-24 03:31:22 +12:00
}
2016-05-23 07:19:25 +12:00
public override void OnDraw(Graphics g)
{
DrawNumber(g);
}
2016-05-23 07:19:25 +12:00
private string GetText()
{
2021-04-17 20:50:59 +12:00
switch (StepType)
{
case StepType.LettersUppercase:
return Helpers.NumberToLetters(Number);
case StepType.LettersLowercase:
return Helpers.NumberToLetters(Number).ToLowerInvariant();
case StepType.RomanNumeralsUppercase:
return Helpers.NumberToRomanNumeral(Number);
case StepType.RomanNumeralsLowercase:
return Helpers.NumberToRomanNumeral(Number).ToLowerInvariant();
default:
return Number.ToString();
}
}
protected void DrawNumber(Graphics g)
{
2021-04-17 20:50:59 +12:00
string text = GetText();
2017-12-28 03:48:51 +13:00
2017-12-08 09:21:55 +13:00
using (Font font = new Font(FontFamily.GenericSansSerif, FontSize, FontStyle.Bold))
2016-05-23 07:19:25 +12:00
{
2017-12-28 03:48:51 +13:00
Size textSize = g.MeasureString(text, font).ToSize();
2017-12-08 09:17:36 +13:00
int maxSize = Math.Max(textSize.Width, textSize.Height);
int padding = 3;
2016-05-23 07:19:25 +12:00
PointF center = Rectangle.Center();
2022-05-18 16:14:00 +12:00
Rectangle = new RectangleF(center.X - (maxSize / 2f) - padding, center.Y - (maxSize / 2f) - padding, maxSize + (padding * 2), maxSize + (padding * 2));
2020-01-15 10:25:36 +13:00
if (Shadow)
{
if (IsBorderVisible)
{
DrawEllipse(g, ShadowColor, BorderSize, BorderStyle, Color.Transparent, Rectangle.LocationOffset(ShadowOffset));
2020-01-15 10:25:36 +13:00
}
else if (FillColor.A == 255)
{
DrawEllipse(g, Color.Transparent, 0, BorderStyle, ShadowColor, Rectangle.LocationOffset(ShadowOffset));
2020-01-15 10:25:36 +13:00
}
}
if (IsTailActive && TailVisible)
2020-01-15 10:25:36 +13:00
{
2020-01-15 10:48:31 +13:00
if (Shadow)
{
DrawTail(g, ShadowColor, Rectangle.Offset(BorderSize / 2).LocationOffset(ShadowOffset), TailPosition.Add(ShadowOffset));
2020-01-15 10:48:31 +13:00
}
2020-01-15 10:25:36 +13:00
Color tailColor;
if (IsBorderVisible)
{
tailColor = BorderColor;
}
else
{
tailColor = FillColor;
}
DrawTail(g, tailColor, Rectangle.Offset(BorderSize / 2), TailPosition);
2020-01-15 10:25:36 +13:00
}
DrawEllipse(g, BorderColor, BorderSize, BorderStyle, FillColor, Rectangle);
2016-05-23 07:19:25 +12:00
2017-12-08 09:17:36 +13:00
if (Shadow)
2016-05-23 11:53:10 +12:00
{
2017-12-28 03:48:51 +13:00
DrawNumber(g, text, font, ShadowColor, Rectangle.LocationOffset(ShadowOffset));
2016-05-23 11:53:10 +12:00
}
2017-12-28 03:48:51 +13:00
DrawNumber(g, text, font, BorderColor, Rectangle);
2017-12-08 09:17:36 +13:00
}
}
2016-05-23 11:53:10 +12:00
protected void DrawNumber(Graphics g, string text, Font font, Color textColor, RectangleF rect)
2017-12-08 09:17:36 +13:00
{
using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
using (Brush textBrush = new SolidBrush(textColor))
{
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
rect = rect.LocationOffset(0, 1);
2017-12-28 03:48:51 +13:00
g.DrawString(text, font, textBrush, rect, sf);
2017-12-08 09:17:36 +13:00
g.TextRenderingHint = TextRenderingHint.SystemDefault;
2016-05-23 07:19:25 +12:00
}
}
private void DrawTail(Graphics g, Color tailColor, RectangleF rectangle, PointF tailPosition)
2020-01-15 10:25:36 +13:00
{
2020-01-15 10:48:31 +13:00
using (GraphicsPath gpTail = CreateTailPath(rectangle, tailPosition))
2020-01-15 10:25:36 +13:00
{
2020-01-15 10:48:31 +13:00
if (gpTail != null)
2020-01-15 10:25:36 +13:00
{
2020-01-15 10:48:31 +13:00
g.SmoothingMode = SmoothingMode.HighQuality;
2020-01-15 10:25:36 +13:00
2020-01-15 10:48:31 +13:00
using (Brush brush = new SolidBrush(tailColor))
{
g.FillPath(brush, gpTail);
}
g.SmoothingMode = SmoothingMode.None;
}
2020-01-15 10:25:36 +13:00
}
}
public override void Move(float x, float y)
2020-01-15 10:25:36 +13:00
{
base.Move(x, y);
TailPosition = TailPosition.Add(x, y);
}
public override void Resize(int x, int y, bool fromBottomRight)
{
Move(x, y);
}
2020-01-15 10:25:36 +13:00
protected GraphicsPath CreateTailPath(RectangleF rect, PointF tailPosition)
2020-01-15 10:25:36 +13:00
{
GraphicsPath gpTail = new GraphicsPath();
PointF center = rect.Center();
float rectAverageSize = (rect.Width + rect.Height) / 2;
float tailWidth = TailWidthMultiplier * rectAverageSize;
2020-01-15 10:25:36 +13:00
tailWidth = Math.Min(Math.Min(tailWidth, rect.Width), rect.Height);
float tailOrigin = tailWidth / 2;
float tailLength = MathHelpers.Distance(center, tailPosition);
2020-01-15 10:25:36 +13:00
gpTail.AddLine(0, -tailOrigin, 0, tailOrigin);
gpTail.AddLine(0, tailOrigin, tailLength, 0);
gpTail.CloseFigure();
using (Matrix matrix = new Matrix())
{
matrix.Translate(center.X, center.Y);
float tailDegree = MathHelpers.LookAtDegree(center, tailPosition);
matrix.Rotate(tailDegree);
gpTail.Transform(matrix);
}
return gpTail;
}
2016-05-23 07:19:25 +12:00
}
}