ShareX/ShareX.ScreenCaptureLib/RegionHelpers/ImageEditorScrollBar.cs

155 lines
5.8 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
Copyright (c) 2007-2018 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;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace ShareX.ScreenCaptureLib
{
internal class ImageEditorScrollbar : DrawableObject
{
public Orientation Orientation { get; set; }
public int Thickness { get; set; } = 10;
public int Margin { get; set; } = 15;
public int Padding { get; set; } = 2;
public float Opacity { get; set; }
public Rectangle ThumbRectangle { get; set; }
private RegionCaptureForm form;
public ImageEditorScrollbar(Orientation orientation, RegionCaptureForm form)
{
Orientation = orientation;
this.form = form;
}
public void Update()
{
UpdateOpacity();
if (Visible)
{
Rectangle imageRectangleVisible = form.CanvasRectangle;
imageRectangleVisible.Intersect(form.ClientArea);
int inClientAreaSize, inImageVisibleSize, inImageSize, sideOffsetBase;
float inCanvasCenterOffset;
if (Orientation == Orientation.Horizontal)
{
inClientAreaSize = form.ClientArea.Width;
inImageVisibleSize = imageRectangleVisible.Width;
inImageSize = form.CanvasRectangle.Width;
sideOffsetBase = form.ClientArea.Bottom;
inCanvasCenterOffset = form.CanvasCenterOffset.X;
}
else
{
inClientAreaSize = form.ClientArea.Height;
inImageVisibleSize = imageRectangleVisible.Height;
inImageSize = form.CanvasRectangle.Height;
sideOffsetBase = form.ClientArea.Right;
inCanvasCenterOffset = form.CanvasCenterOffset.Y;
}
2018-04-04 11:01:32 +12:00
int trackLength = inClientAreaSize - Margin * 2 - Padding * 2 - Thickness;
int trackLengthInternal = trackLength - Padding * 2;
int thumbLength = Math.Max(Thickness, (int)Math.Round((float)inImageVisibleSize / inImageSize * trackLengthInternal));
double thumbLimit = (trackLengthInternal - thumbLength) / 2.0f;
int thumbPosition = (int)Math.Round(Margin + trackLength / 2.0f - (thumbLength / 2.0f) -
Math.Min(thumbLimit, Math.Max(-thumbLimit, inCanvasCenterOffset / inImageSize * trackLengthInternal)));
int trackWidth = Padding * 2 + Thickness;
int trackSideOffset = sideOffsetBase - Margin - Thickness - 1 - Padding * 2;
int thumbSideOffset = sideOffsetBase - Margin - Thickness - 1 - Padding;
if (Orientation == Orientation.Horizontal)
{
Rectangle = new Rectangle(Margin, trackSideOffset, trackLength, trackWidth);
ThumbRectangle = new Rectangle(thumbPosition, thumbSideOffset, thumbLength, Thickness);
}
else
{
Rectangle = new Rectangle(trackSideOffset, Margin, trackWidth, trackLength);
ThumbRectangle = new Rectangle(thumbSideOffset, thumbPosition, Thickness, thumbLength);
}
}
}
private void UpdateOpacity()
{
bool isScrollbarNeeded;
if (Orientation == Orientation.Horizontal)
{
isScrollbarNeeded = form.CanvasRectangle.Left < form.ClientArea.Left || form.CanvasRectangle.Right > form.ClientArea.Right;
}
else
2018-04-04 08:27:33 +12:00
{
isScrollbarNeeded = form.CanvasRectangle.Top < form.ClientArea.Top || form.CanvasRectangle.Bottom > form.ClientArea.Bottom;
2018-04-04 08:27:33 +12:00
}
if (!isScrollbarNeeded)
{
Opacity = 0f;
}
else if (form.ShapeManager.IsPanning || IsCursorHover)
2018-04-04 08:27:33 +12:00
{
Opacity = 1f;
2018-04-04 08:27:33 +12:00
}
else
{
2018-04-04 11:01:32 +12:00
Opacity = 0.8f;
2018-04-04 08:27:33 +12:00
}
Visible = Opacity > 0;
2018-04-04 08:27:33 +12:00
}
public override void OnDraw(Graphics g)
{
if (!Visible) return;
using (Brush trackBrush = new SolidBrush(Color.FromArgb((int)(255 * Opacity), 60, 60, 60)))
using (Brush thumbBrush = new SolidBrush(Color.FromArgb((int)(255 * Opacity), 130, 130, 130)))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawCapsule(trackBrush, Rectangle);
g.DrawCapsule(thumbBrush, ThumbRectangle);
g.SmoothingMode = SmoothingMode.None;
}
}
2018-04-04 11:01:32 +12:00
public override void OnMouseDown(Point position)
{
base.OnMouseDown(position);
// Pan here
}
}
}