From 37a6127e5739710cfb660c28775706469b2bcc7b Mon Sep 17 00:00:00 2001 From: Jaex Date: Tue, 3 Apr 2018 23:08:24 +0300 Subject: [PATCH] Added ScrollBar class to support mouse input --- .../RegionHelpers/ImageEditorScrollBar.cs | 178 ++++++++++++++++++ .../RegionHelpers/ScrollbarManager.cs | 160 +--------------- .../ShareX.ScreenCaptureLib.csproj | 1 + 3 files changed, 188 insertions(+), 151 deletions(-) create mode 100644 ShareX.ScreenCaptureLib/RegionHelpers/ImageEditorScrollBar.cs diff --git a/ShareX.ScreenCaptureLib/RegionHelpers/ImageEditorScrollBar.cs b/ShareX.ScreenCaptureLib/RegionHelpers/ImageEditorScrollBar.cs new file mode 100644 index 000000000..a6c0caa28 --- /dev/null +++ b/ShareX.ScreenCaptureLib/RegionHelpers/ImageEditorScrollBar.cs @@ -0,0 +1,178 @@ +#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 . +*/ + +#endregion License Information (GPL v3) + +using ShareX.HelpersLib; +using System; +using System.Diagnostics; +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 Rectangle TrackRectangle { get; set; } + public Rectangle ThumbRectangle { get; set; } + + public int FadeInTime { get; set; } = 150; + public int FadeOutDelay { get; set; } = 500; + public int FadeOutTime { get; set; } = 150; + + private bool shouldDraw = true; + private bool shouldDrawBefore = true; + private Stopwatch changeTime; + private int opacityLast = 255; + private int opacityCurrent = 255; + private RegionCaptureForm form; + + public ImageEditorScrollbar(Orientation orientation, RegionCaptureForm form) + { + Orientation = orientation; + this.form = form; + + changeTime = Stopwatch.StartNew(); + + if (form.ClientArea.Contains(form.CanvasRectangle)) + { + opacityLast = 0; + opacityCurrent = 0; + } + } + + public void Update() + { + Rectangle imageRectangleVisible = form.CanvasRectangle; + imageRectangleVisible.Intersect(form.ClientArea); + + if (Orientation == Orientation.Horizontal) + { + shouldDraw = form.ShapeManager.IsPanning && (form.CanvasRectangle.Left < form.ClientArea.Left || form.CanvasRectangle.Right > form.ClientArea.Right); + + if (shouldDraw != shouldDrawBefore) + { + changeTime = Stopwatch.StartNew(); + opacityLast = opacityCurrent; + } + shouldDrawBefore = shouldDraw; + + if (shouldDraw) + { + opacityCurrent = opacityLast + OpacityGain(changeTime); + } + else + { + opacityCurrent = opacityLast - OpacityLoss(changeTime); + } + opacityCurrent = opacityCurrent.Between(0, 255); + + Visible = opacityCurrent > 0; + + if (Visible) + { + int horizontalTrackLength = form.ClientArea.Width - Margin * 2 - Thickness - Padding * 2; + int horizontalThumbLength = Math.Max(Thickness, (int)Math.Round((float)imageRectangleVisible.Width / form.CanvasRectangle.Width * horizontalTrackLength)); + + TrackRectangle = new Rectangle(new Point(Margin - Padding, form.ClientArea.Bottom - (Thickness + Margin) - Padding), + new Size(horizontalTrackLength + Padding * 2, Thickness + Padding * 2)); + + double limitHorizontal = (horizontalTrackLength - horizontalThumbLength) / 2.0f; + double thumbHorizontalPositionX = Math.Round(TrackRectangle.X + TrackRectangle.Width / 2.0f - (horizontalThumbLength / 2.0f) - + Math.Min(limitHorizontal, Math.Max(-limitHorizontal, form.CanvasCenterOffset.X / form.CanvasRectangle.Width * horizontalTrackLength))); + + ThumbRectangle = new Rectangle(new Point((int)thumbHorizontalPositionX, form.ClientArea.Bottom - (Thickness + Margin)), + new Size(horizontalThumbLength, Thickness)); + } + } + else + { + shouldDraw = form.ShapeManager.IsPanning && (form.CanvasRectangle.Top < form.ClientArea.Top || form.CanvasRectangle.Bottom > form.ClientArea.Bottom); + + if (shouldDraw != shouldDrawBefore) + { + changeTime = Stopwatch.StartNew(); + opacityLast = opacityCurrent; + } + shouldDrawBefore = shouldDraw; + + if (shouldDraw) + { + opacityCurrent = opacityLast + OpacityGain(changeTime); + } + else + { + opacityCurrent = opacityLast - OpacityLoss(changeTime); + } + opacityCurrent = opacityCurrent.Between(0, 255); + + Visible = opacityCurrent > 0; + if (Visible) + { + int verticalTrackLength = form.ClientArea.Height - Margin * 2 - Thickness - Padding * 2; + int verticalThumbLength = Math.Max(Thickness, (int)Math.Round((float)imageRectangleVisible.Height / form.CanvasRectangle.Height * verticalTrackLength)); + + TrackRectangle = new Rectangle(new Point(form.ClientArea.Right - (Thickness + Margin) - Padding, Margin - Padding), + new Size(Thickness + Padding * 2, verticalTrackLength + Padding * 2)); + + double limitVertical = (verticalTrackLength - verticalThumbLength) / 2.0f; + double thumbVerticalPositionY = Math.Round(TrackRectangle.Y + TrackRectangle.Height / 2.0f - (verticalThumbLength / 2.0f) - + Math.Min(limitVertical, Math.Max(-limitVertical, form.CanvasCenterOffset.Y / form.CanvasRectangle.Height * verticalTrackLength))); + + ThumbRectangle = new Rectangle(new Point(form.ClientArea.Right - (Thickness + Margin), (int)thumbVerticalPositionY), + new Size(Thickness, verticalThumbLength)); + } + } + } + + public override void OnDraw(Graphics g) + { + if (!Visible) return; + + using (Brush trackBrush = new SolidBrush(Color.FromArgb(opacityCurrent, 60, 60, 60))) + using (Brush thumbBrush = new SolidBrush(Color.FromArgb(opacityCurrent, 130, 130, 130))) + { + g.SmoothingMode = SmoothingMode.HighQuality; + g.DrawCapsule(trackBrush, TrackRectangle); + g.DrawCapsule(thumbBrush, ThumbRectangle); + g.SmoothingMode = SmoothingMode.None; + } + } + + private int OpacityGain(Stopwatch changeTime) + { + return (int)Math.Min(255.0f, 255.0f * changeTime.ElapsedMilliseconds / Math.Max(0, (float)FadeInTime)); + } + + private int OpacityLoss(Stopwatch changeTime) + { + int deltaTime = Math.Max(0, (int)changeTime.ElapsedMilliseconds - FadeOutDelay); + return (int)Math.Min(255.0f, 255.0f * deltaTime / Math.Max(0, (float)FadeOutTime)); + } + } +} \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/RegionHelpers/ScrollbarManager.cs b/ShareX.ScreenCaptureLib/RegionHelpers/ScrollbarManager.cs index bad174858..5858b3d82 100644 --- a/ShareX.ScreenCaptureLib/RegionHelpers/ScrollbarManager.cs +++ b/ShareX.ScreenCaptureLib/RegionHelpers/ScrollbarManager.cs @@ -23,177 +23,35 @@ You should have received a copy of the GNU General Public License #endregion License Information (GPL v3) -using ShareX.HelpersLib; -using System; -using System.Diagnostics; using System.Drawing; -using System.Drawing.Drawing2D; +using System.Windows.Forms; namespace ShareX.ScreenCaptureLib { internal class ScrollbarManager { - public bool IsVisible => IsHorizontalScrollbarVisible || IsVerticalScrollbarVisible; - public bool IsHorizontalScrollbarVisible { get; private set; } - public bool IsVerticalScrollbarVisible { get; private set; } - - public int Thickness { get; set; } = 10; - public int Margin { get; set; } = 15; - public int Padding { get; set; } = 2; - - // Timings in milliseconds - public int FadeInTime { get; set; } = 150; - public int FadeOutDelay { get; set; } = 500; - public int FadeOutTime { get; set; } = 150; + public bool IsVisible => horizontalScrollbar.Visible || verticalScrollbar.Visible; private RegionCaptureForm form; - private Rectangle horizontalTrackRectangle, horizontalThumbRectangle, verticalTrackRectangle, verticalThumbRectangle; - private bool shouldDrawHorizontalScrollbar = true; - private bool shouldDrawVerticalScrollbar = true; - private bool shouldDrawHorizontalScrollbarBefore = true; - private bool shouldDrawVerticalScrollbarBefore = true; - private Stopwatch horizontalScrollbarChangeTime; - private Stopwatch verticalScrollbarChangeTime; - private int HorizontalScrollbarOpacityLast = 255; - private int HorizontalScrollbarOpacityCurrent = 255; - private int VerticalScrollbarOpacityLast = 255; - private int VerticalScrollbarOpacityCurrent = 255; + private ImageEditorScrollbar horizontalScrollbar, verticalScrollbar; public ScrollbarManager(RegionCaptureForm regionCaptureForm) { form = regionCaptureForm; - horizontalScrollbarChangeTime = Stopwatch.StartNew(); - verticalScrollbarChangeTime = Stopwatch.StartNew(); - - if (form.ClientArea.Contains(form.CanvasRectangle)) - { - HorizontalScrollbarOpacityLast = 0; - HorizontalScrollbarOpacityCurrent = 0; - VerticalScrollbarOpacityLast = 0; - VerticalScrollbarOpacityCurrent = 0; - } + horizontalScrollbar = new ImageEditorScrollbar(Orientation.Horizontal, form); + verticalScrollbar = new ImageEditorScrollbar(Orientation.Vertical, form); } public void Update() { - Rectangle imageRectangleVisible = form.CanvasRectangle; - imageRectangleVisible.Intersect(form.ClientArea); - - shouldDrawHorizontalScrollbar = form.ShapeManager.IsPanning && - (form.CanvasRectangle.Left < form.ClientArea.Left || form.CanvasRectangle.Right > form.ClientArea.Right); - - if (shouldDrawHorizontalScrollbar != shouldDrawHorizontalScrollbarBefore) - { - horizontalScrollbarChangeTime = Stopwatch.StartNew(); - HorizontalScrollbarOpacityLast = HorizontalScrollbarOpacityCurrent; - } - shouldDrawHorizontalScrollbarBefore = shouldDrawHorizontalScrollbar; - - if (shouldDrawHorizontalScrollbar) - { - HorizontalScrollbarOpacityCurrent = HorizontalScrollbarOpacityLast + OpacityGain(horizontalScrollbarChangeTime); - } - else - { - HorizontalScrollbarOpacityCurrent = HorizontalScrollbarOpacityLast - OpacityLoss(horizontalScrollbarChangeTime); - } - HorizontalScrollbarOpacityCurrent = HorizontalScrollbarOpacityCurrent.Between(0, 255); - - IsHorizontalScrollbarVisible = HorizontalScrollbarOpacityCurrent > 0; - - if (IsHorizontalScrollbarVisible) - { - int horizontalTrackLength = form.ClientArea.Width - Margin * 2 - Thickness - Padding * 2; - int horizontalThumbLength = Math.Max(Thickness, (int)Math.Round((float)imageRectangleVisible.Width / form.CanvasRectangle.Width * horizontalTrackLength)); - - horizontalTrackRectangle = new Rectangle(new Point(Margin - Padding, form.ClientArea.Bottom - (Thickness + Margin) - Padding), - new Size(horizontalTrackLength + Padding * 2, Thickness + Padding * 2)); - - double limitHorizontal = (horizontalTrackLength - horizontalThumbLength) / 2.0f; - double thumbHorizontalPositionX = Math.Round(horizontalTrackRectangle.X + horizontalTrackRectangle.Width / 2.0f - (horizontalThumbLength / 2.0f) - - Math.Min(limitHorizontal, Math.Max(-limitHorizontal, form.CanvasCenterOffset.X / form.CanvasRectangle.Width * horizontalTrackLength))); - - horizontalThumbRectangle = new Rectangle(new Point((int)thumbHorizontalPositionX, form.ClientArea.Bottom - (Thickness + Margin)), - new Size(horizontalThumbLength, Thickness)); - } - - shouldDrawVerticalScrollbar = form.ShapeManager.IsPanning && - (form.CanvasRectangle.Top < form.ClientArea.Top || form.CanvasRectangle.Bottom > form.ClientArea.Bottom); - - if (shouldDrawVerticalScrollbar != shouldDrawVerticalScrollbarBefore) - { - verticalScrollbarChangeTime = Stopwatch.StartNew(); - VerticalScrollbarOpacityLast = VerticalScrollbarOpacityCurrent; - } - shouldDrawVerticalScrollbarBefore = shouldDrawVerticalScrollbar; - - if (shouldDrawVerticalScrollbar) - { - VerticalScrollbarOpacityCurrent = VerticalScrollbarOpacityLast + OpacityGain(verticalScrollbarChangeTime); - } - else - { - VerticalScrollbarOpacityCurrent = VerticalScrollbarOpacityLast - OpacityLoss(verticalScrollbarChangeTime); - } - VerticalScrollbarOpacityCurrent = VerticalScrollbarOpacityCurrent.Between(0, 255); - - IsVerticalScrollbarVisible = VerticalScrollbarOpacityCurrent > 0; - if (IsVerticalScrollbarVisible) - { - int verticalTrackLength = form.ClientArea.Height - Margin * 2 - Thickness - Padding * 2; - int verticalThumbLength = Math.Max(Thickness, (int)Math.Round((float)imageRectangleVisible.Height / form.CanvasRectangle.Height * verticalTrackLength)); - - verticalTrackRectangle = new Rectangle(new Point(form.ClientArea.Right - (Thickness + Margin) - Padding, Margin - Padding), - new Size(Thickness + Padding * 2, verticalTrackLength + Padding * 2)); - - double limitVertical = (verticalTrackLength - verticalThumbLength) / 2.0f; - double thumbVerticalPositionY = Math.Round(verticalTrackRectangle.Y + verticalTrackRectangle.Height / 2.0f - (verticalThumbLength / 2.0f) - - Math.Min(limitVertical, Math.Max(-limitVertical, form.CanvasCenterOffset.Y / form.CanvasRectangle.Height * verticalTrackLength))); - - verticalThumbRectangle = new Rectangle(new Point(form.ClientArea.Right - (Thickness + Margin), (int)thumbVerticalPositionY), - new Size(Thickness, verticalThumbLength)); - } + horizontalScrollbar.Update(); + verticalScrollbar.Update(); } public void Draw(Graphics g) { - if (IsVisible) - { - if (IsHorizontalScrollbarVisible) - { - using (Brush trackBrush = new SolidBrush(Color.FromArgb(HorizontalScrollbarOpacityCurrent, 60, 60, 60))) - using (Brush thumbBrush = new SolidBrush(Color.FromArgb(HorizontalScrollbarOpacityCurrent, 130, 130, 130))) - { - g.SmoothingMode = SmoothingMode.HighQuality; - g.DrawCapsule(trackBrush, horizontalTrackRectangle); - g.DrawCapsule(thumbBrush, horizontalThumbRectangle); - g.SmoothingMode = SmoothingMode.None; - } - } - - if (IsVerticalScrollbarVisible) - { - using (Brush trackBrush = new SolidBrush(Color.FromArgb(VerticalScrollbarOpacityCurrent, 60, 60, 60))) - using (Brush thumbBrush = new SolidBrush(Color.FromArgb(VerticalScrollbarOpacityCurrent, 130, 130, 130))) - { - g.SmoothingMode = SmoothingMode.HighQuality; - g.DrawCapsule(trackBrush, verticalTrackRectangle); - g.DrawCapsule(thumbBrush, verticalThumbRectangle); - g.SmoothingMode = SmoothingMode.None; - } - } - } - } - - private int OpacityGain(Stopwatch changeTime) - { - return (int)Math.Min(255.0f, 255.0f * changeTime.ElapsedMilliseconds / Math.Max(0, (float)FadeInTime)); - } - - private int OpacityLoss(Stopwatch changeTime) - { - int deltaTime = Math.Max(0, (int)changeTime.ElapsedMilliseconds - FadeOutDelay); - return (int)Math.Min(255.0f, 255.0f * deltaTime / Math.Max(0, (float)FadeOutTime)); + horizontalScrollbar.OnDraw(g); + verticalScrollbar.OnDraw(g); } } } \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/ShareX.ScreenCaptureLib.csproj b/ShareX.ScreenCaptureLib/ShareX.ScreenCaptureLib.csproj index 977b7d1e6..e44d7f68c 100644 --- a/ShareX.ScreenCaptureLib/ShareX.ScreenCaptureLib.csproj +++ b/ShareX.ScreenCaptureLib/ShareX.ScreenCaptureLib.csproj @@ -126,6 +126,7 @@ StickerPackForm.cs +