ShareX/ShareX.ScreenCaptureLib/Shapes/Drawing/TextDrawingShape.cs

185 lines
5.9 KiB
C#
Raw Normal View History

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2022-01-12 05:32:17 +13:00
Copyright (c) 2007-2022 ShareX Team
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.Drawing;
2016-05-23 07:11:34 +12:00
using System.Drawing.Text;
2017-10-30 20:43:21 +13:00
using System.Windows.Forms;
namespace ShareX.ScreenCaptureLib
{
public class TextDrawingShape : RectangleDrawingShape
{
2017-02-16 13:49:13 +13:00
public override ShapeType ShapeType { get; } = ShapeType.DrawingTextBackground;
2016-05-21 03:45:55 +12:00
public string Text { get; set; }
public TextDrawingOptions TextOptions { get; set; }
public virtual bool SupportGradient { get; }
public override void OnConfigLoad()
{
TextOptions = AnnotationOptions.TextOptions.Copy();
BorderColor = AnnotationOptions.TextBorderColor;
BorderSize = AnnotationOptions.TextBorderSize;
FillColor = AnnotationOptions.TextFillColor;
CornerRadius = AnnotationOptions.DrawingCornerRadius;
Shadow = AnnotationOptions.Shadow;
2018-01-31 06:14:28 +13:00
ShadowColor = AnnotationOptions.ShadowColor;
ShadowOffset = AnnotationOptions.ShadowOffset;
}
public override void OnConfigSave()
{
AnnotationOptions.TextOptions = TextOptions;
AnnotationOptions.TextBorderColor = BorderColor;
AnnotationOptions.TextBorderSize = BorderSize;
AnnotationOptions.TextFillColor = FillColor;
AnnotationOptions.DrawingCornerRadius = CornerRadius;
AnnotationOptions.Shadow = Shadow;
2018-01-31 06:14:28 +13:00
AnnotationOptions.ShadowColor = ShadowColor;
AnnotationOptions.ShadowOffset = ShadowOffset;
}
public override void OnDraw(Graphics g)
{
2017-02-16 13:49:13 +13:00
DrawRectangle(g);
DrawText(g);
}
2016-05-21 09:09:31 +12:00
protected void DrawText(Graphics g)
{
2016-11-30 07:48:19 +13:00
if (Shadow)
{
DrawText(g, Text, ShadowColor, TextOptions, Rectangle.LocationOffset(ShadowOffset));
}
DrawText(g, Text, TextOptions, Rectangle);
}
protected void DrawText(Graphics g, string text, TextDrawingOptions options, RectangleF rect)
{
DrawText(g, text, options.Color, options, rect);
}
protected void DrawText(Graphics g, string text, Color textColor, TextDrawingOptions options, RectangleF rect)
{
2016-11-30 07:48:19 +13:00
if (!string.IsNullOrEmpty(text) && rect.Width > 10 && rect.Height > 10)
2016-05-21 09:09:31 +12:00
{
2016-11-30 07:48:19 +13:00
using (Font font = new Font(options.Font, options.Size, options.Style))
using (Brush textBrush = new SolidBrush(textColor))
using (StringFormat sf = new StringFormat { Alignment = options.AlignmentHorizontal, LineAlignment = options.AlignmentVertical })
2016-05-21 09:09:31 +12:00
{
2016-05-23 07:11:34 +12:00
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
2016-11-30 07:48:19 +13:00
g.DrawString(text, font, textBrush, rect, sf);
2016-05-23 07:11:34 +12:00
g.TextRenderingHint = TextRenderingHint.SystemDefault;
2016-05-21 09:09:31 +12:00
}
}
}
public override void OnCreating()
{
PointF pos = Manager.Form.ScaledClientMousePosition;
Rectangle = new RectangleF(pos.X, pos.Y, 1, 1);
2017-10-30 20:43:21 +13:00
if (ShowTextInputBox())
{
2017-10-30 20:43:21 +13:00
OnCreated();
}
else
{
2017-10-30 20:43:21 +13:00
Remove();
}
}
public override void OnCreated()
{
OnCreated(true);
}
protected void OnCreated(bool autoSize)
{
if (autoSize)
{
AutoSize(true);
}
base.OnCreated();
ShowNodes();
}
public override void OnDoubleClicked()
{
ShowTextInputBox();
}
2017-10-30 20:43:21 +13:00
private bool ShowTextInputBox()
{
2017-10-30 20:43:21 +13:00
bool result;
Manager.Form.Pause();
using (TextDrawingInputBox inputBox = new TextDrawingInputBox(Text, TextOptions, SupportGradient, Manager.Options.ColorPickerOptions))
{
result = inputBox.ShowDialog(Manager.Form) == DialogResult.OK;
2016-05-21 03:45:55 +12:00
Text = inputBox.InputText;
OnConfigSave();
}
Manager.Form.Resume();
2017-10-30 20:43:21 +13:00
return result;
}
public void AutoSize(bool center)
{
Size size;
2017-10-30 20:43:21 +13:00
if (!string.IsNullOrEmpty(Text))
{
using (Font font = new Font(TextOptions.Font, TextOptions.Size, TextOptions.Style))
{
size = Helpers.MeasureText(Text, font).Offset(15, 20);
}
}
else
{
2017-10-30 20:43:21 +13:00
size = new Size(100, 60);
}
PointF location;
if (center)
{
location = new PointF(Rectangle.X - (size.Width / 2), Rectangle.Y - (size.Height / 2));
}
else
{
location = Rectangle.Location;
}
Rectangle = new RectangleF(location, size);
}
}
}