2016-05-11 10:17:51 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2019-01-02 20:43:52 +13:00
|
|
|
|
Copyright (c) 2007-2019 ShareX Team
|
2016-05-11 10:17:51 +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)
|
|
|
|
|
|
2017-11-17 02:00:11 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2016-05-11 10:17:51 +12:00
|
|
|
|
using System.Drawing;
|
2017-11-14 22:12:49 +13:00
|
|
|
|
using System.Drawing.Drawing2D;
|
2018-08-03 23:01:12 +12:00
|
|
|
|
using System.Threading.Tasks;
|
2016-05-11 10:17:51 +12:00
|
|
|
|
|
|
|
|
|
namespace ShareX.ScreenCaptureLib
|
|
|
|
|
{
|
|
|
|
|
public abstract class BaseEffectShape : BaseShape
|
|
|
|
|
{
|
2016-09-02 19:09:24 +12:00
|
|
|
|
public override ShapeCategory ShapeCategory { get; } = ShapeCategory.Effect;
|
|
|
|
|
|
2017-11-17 02:00:11 +13:00
|
|
|
|
public abstract string OverlayText { get; }
|
|
|
|
|
|
2017-11-18 22:33:51 +13:00
|
|
|
|
private bool drawCache, isEffectCaching, isCachePending, isDisposePending;
|
2017-11-14 22:12:49 +13:00
|
|
|
|
private Image cachedEffect;
|
|
|
|
|
|
|
|
|
|
public abstract void ApplyEffect(Bitmap bmp);
|
|
|
|
|
|
2017-11-18 04:50:14 +13:00
|
|
|
|
public override void OnUpdate()
|
|
|
|
|
{
|
|
|
|
|
base.OnUpdate();
|
|
|
|
|
|
2017-11-18 22:33:51 +13:00
|
|
|
|
if (isCachePending)
|
2017-11-18 04:50:14 +13:00
|
|
|
|
{
|
|
|
|
|
CacheEffect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 22:12:49 +13:00
|
|
|
|
public virtual void OnDraw(Graphics g)
|
|
|
|
|
{
|
2017-11-18 20:04:13 +13:00
|
|
|
|
if (drawCache && isEffectCaching)
|
2017-11-18 02:35:59 +13:00
|
|
|
|
{
|
|
|
|
|
OnDrawOverlay(g, "Processing...");
|
|
|
|
|
}
|
2017-11-18 20:04:13 +13:00
|
|
|
|
else if (drawCache && cachedEffect != null)
|
2017-11-14 22:12:49 +13:00
|
|
|
|
{
|
|
|
|
|
g.InterpolationMode = InterpolationMode.NearestNeighbor;
|
2017-11-15 01:27:32 +13:00
|
|
|
|
g.DrawImage(cachedEffect, RectangleInsideCanvas);
|
2017-11-14 22:12:49 +13:00
|
|
|
|
g.InterpolationMode = InterpolationMode.Bilinear;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OnDrawOverlay(g);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-17 02:00:11 +13:00
|
|
|
|
public virtual void OnDrawOverlay(Graphics g)
|
2017-11-18 02:35:59 +13:00
|
|
|
|
{
|
|
|
|
|
OnDrawOverlay(g, OverlayText);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnDrawOverlay(Graphics g, string overlayText)
|
2017-11-17 02:00:11 +13:00
|
|
|
|
{
|
|
|
|
|
using (Brush brush = new SolidBrush(Color.FromArgb(150, Color.Black)))
|
|
|
|
|
{
|
|
|
|
|
g.FillRectangle(brush, Rectangle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.DrawCornerLines(Rectangle.Offset(1), Pens.White, 25);
|
|
|
|
|
|
|
|
|
|
using (Font font = new Font("Verdana", 12))
|
|
|
|
|
{
|
2017-11-18 02:35:59 +13:00
|
|
|
|
Size textSize = g.MeasureString(overlayText, font).ToSize();
|
2017-11-17 02:00:11 +13:00
|
|
|
|
|
|
|
|
|
if (Rectangle.Width > textSize.Width && Rectangle.Height > textSize.Height)
|
|
|
|
|
{
|
|
|
|
|
using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
|
|
|
|
|
{
|
2017-11-18 02:35:59 +13:00
|
|
|
|
g.DrawString(overlayText, font, Brushes.White, Rectangle, sf);
|
2017-11-17 02:00:11 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-11 10:17:51 +12:00
|
|
|
|
|
2016-05-22 00:26:49 +12:00
|
|
|
|
public virtual void OnDrawFinal(Graphics g, Bitmap bmp)
|
2016-05-11 10:17:51 +12:00
|
|
|
|
{
|
2017-11-17 02:00:11 +13:00
|
|
|
|
Rectangle rect = Rectangle.Intersect(new Rectangle(0, 0, bmp.Width, bmp.Height), Rectangle);
|
|
|
|
|
|
|
|
|
|
if (!rect.IsEmpty)
|
|
|
|
|
{
|
|
|
|
|
using (Bitmap croppedImage = ImageHelpers.CropBitmap(bmp, rect))
|
|
|
|
|
{
|
|
|
|
|
ApplyEffect(croppedImage);
|
|
|
|
|
|
|
|
|
|
g.DrawImage(croppedImage, rect);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-11 10:17:51 +12:00
|
|
|
|
}
|
2017-11-14 22:12:49 +13:00
|
|
|
|
|
|
|
|
|
public override void OnCreated()
|
|
|
|
|
{
|
2017-11-21 07:25:18 +13:00
|
|
|
|
base.OnCreated();
|
2017-11-14 22:12:49 +13:00
|
|
|
|
CacheEffect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnMoving()
|
|
|
|
|
{
|
2017-11-18 20:04:13 +13:00
|
|
|
|
StopDrawCache();
|
2017-11-14 22:12:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnMoved()
|
|
|
|
|
{
|
|
|
|
|
CacheEffect();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 23:55:05 +13:00
|
|
|
|
public override void OnResizing()
|
|
|
|
|
{
|
2017-11-18 20:04:13 +13:00
|
|
|
|
StopDrawCache();
|
2017-11-14 23:55:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnResized()
|
|
|
|
|
{
|
|
|
|
|
CacheEffect();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 22:12:49 +13:00
|
|
|
|
private void CacheEffect()
|
|
|
|
|
{
|
2017-11-18 02:35:59 +13:00
|
|
|
|
if (!isEffectCaching)
|
2017-11-15 01:27:32 +13:00
|
|
|
|
{
|
2017-11-18 22:33:51 +13:00
|
|
|
|
isCachePending = false;
|
2017-11-18 20:04:13 +13:00
|
|
|
|
drawCache = true;
|
2017-11-18 04:50:14 +13:00
|
|
|
|
|
|
|
|
|
ClearCache();
|
2017-11-18 02:35:59 +13:00
|
|
|
|
|
2017-11-18 03:57:15 +13:00
|
|
|
|
if (IsInsideCanvas)
|
2017-11-18 02:35:59 +13:00
|
|
|
|
{
|
2017-11-18 03:57:15 +13:00
|
|
|
|
isEffectCaching = true;
|
2017-11-18 02:35:59 +13:00
|
|
|
|
|
2017-11-18 03:57:15 +13:00
|
|
|
|
cachedEffect = Manager.CropImage(RectangleInsideCanvas);
|
2017-11-18 02:35:59 +13:00
|
|
|
|
|
2018-08-03 23:01:12 +12:00
|
|
|
|
Task.Run(() =>
|
2017-11-18 02:35:59 +13:00
|
|
|
|
{
|
|
|
|
|
ApplyEffect((Bitmap)cachedEffect);
|
|
|
|
|
|
2017-11-18 03:57:15 +13:00
|
|
|
|
isEffectCaching = false;
|
2017-11-18 22:33:51 +13:00
|
|
|
|
|
|
|
|
|
if (isDisposePending)
|
|
|
|
|
{
|
|
|
|
|
Dispose();
|
|
|
|
|
}
|
2017-11-18 03:57:15 +13:00
|
|
|
|
});
|
|
|
|
|
}
|
2017-11-15 01:27:32 +13:00
|
|
|
|
}
|
2017-11-18 04:50:14 +13:00
|
|
|
|
else
|
|
|
|
|
{
|
2017-11-18 22:33:51 +13:00
|
|
|
|
isCachePending = true;
|
2017-11-18 04:50:14 +13:00
|
|
|
|
}
|
2017-11-14 22:12:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-18 20:04:13 +13:00
|
|
|
|
private void StopDrawCache()
|
|
|
|
|
{
|
|
|
|
|
drawCache = false;
|
2017-11-18 22:33:51 +13:00
|
|
|
|
isCachePending = false;
|
2017-11-18 20:04:13 +13:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-18 04:50:14 +13:00
|
|
|
|
private void ClearCache()
|
2017-11-14 22:12:49 +13:00
|
|
|
|
{
|
2017-11-18 20:04:13 +13:00
|
|
|
|
if (!isEffectCaching && cachedEffect != null)
|
2017-11-14 22:12:49 +13:00
|
|
|
|
{
|
|
|
|
|
cachedEffect.Dispose();
|
|
|
|
|
cachedEffect = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-18 02:35:59 +13:00
|
|
|
|
|
|
|
|
|
public override void Dispose()
|
|
|
|
|
{
|
2017-11-18 22:33:51 +13:00
|
|
|
|
if (isEffectCaching)
|
|
|
|
|
{
|
|
|
|
|
isDisposePending = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ClearCache();
|
|
|
|
|
}
|
2017-11-18 02:35:59 +13:00
|
|
|
|
}
|
2016-05-11 10:17:51 +12:00
|
|
|
|
}
|
|
|
|
|
}
|