Added ScrollbarManager

This commit is contained in:
Jaex 2017-12-08 17:21:44 +03:00
parent fbf5f88ce1
commit 6c1047eab8
3 changed files with 141 additions and 63 deletions

View file

@ -101,6 +101,7 @@ public Color CurrentColor
private TextAnimation editorPanTipAnimation;
private Bitmap bmpBackgroundImage;
private Cursor defaultCursor;
private ScrollbarManager scrollbarManager;
public RegionCaptureForm(RegionCaptureMode mode, RegionCaptureOptions options, Image canvas = null)
{
@ -118,14 +119,20 @@ public RegionCaptureForm(RegionCaptureMode mode, RegionCaptureOptions options, I
{
Duration = TimeSpan.FromMilliseconds(200)
};
if (IsEditorMode && Options.ShowEditorPanTip)
if (IsEditorMode)
{
editorPanTipAnimation = new TextAnimation()
scrollbarManager = new ScrollbarManager(this);
if (Options.ShowEditorPanTip)
{
Duration = TimeSpan.FromMilliseconds(5000),
FadeOutDuration = TimeSpan.FromMilliseconds(1000),
Text = Resources.RegionCaptureForm_TipYouCanPanImageByHoldingMouseMiddleButtonAndDragging
};
editorPanTipAnimation = new TextAnimation()
{
Duration = TimeSpan.FromMilliseconds(5000),
FadeOutDuration = TimeSpan.FromMilliseconds(1000),
Text = Resources.RegionCaptureForm_TipYouCanPanImageByHoldingMouseMiddleButtonAndDragging
};
}
}
borderPen = new Pen(Color.Black);
@ -667,6 +674,11 @@ private new void Update()
borderDotPen.DashOffset = (float)timerStart.Elapsed.TotalSeconds * -15;
ShapeManager.Update();
if (scrollbarManager != null)
{
scrollbarManager.Update();
}
}
protected override void OnPaintBackground(PaintEventArgs e)
@ -864,10 +876,10 @@ private void Draw(Graphics g)
DrawTextAnimation(g, ShapeManager.MenuTextAnimation);
}
// Draw scroll bars while panning
if (ShapeManager.IsPanning)
// Draw scroll bars
if (scrollbarManager != null)
{
DrawPanningScrollbars(g);
scrollbarManager.Draw(g);
}
}
@ -1023,60 +1035,6 @@ private void DrawBottomTipAnimation(Graphics g, TextAnimation textAnimation)
DrawTextAnimation(g, textAnimation, textRectangle, padding);
}
private void DrawPanningScrollbars(Graphics g)
{
if (ClientArea.Contains(CanvasRectangle)) return;
int scrollbarThickness = 10;
int trackMargin = 15;
int trackPadding = 2;
Rectangle imageRectangleVisible = CanvasRectangle;
imageRectangleVisible.Intersect(ClientArea);
using (Brush trackBrush = new SolidBrush(Color.FromArgb(255, 60, 60, 60)))
using (Brush thumbBrush = new SolidBrush(Color.FromArgb(255, 130, 130, 130)))
{
if (CanvasRectangle.Left < ClientArea.Left || CanvasRectangle.Right > ClientArea.Right)
{
int trackHorizontalLength = ClientArea.Width - trackMargin * 2 - scrollbarThickness - trackPadding * 2;
int thumbHorizontalLength = Math.Max(scrollbarThickness, (int)Math.Round((float)imageRectangleVisible.Width / CanvasRectangle.Width * trackHorizontalLength));
Rectangle trackHorizontalRegion = new Rectangle(new Point(trackMargin - trackPadding, ClientArea.Bottom - (scrollbarThickness + trackMargin) - trackPadding), new Size(trackHorizontalLength + trackPadding * 2, scrollbarThickness + trackPadding * 2));
double limitHorizontal = (trackHorizontalLength - thumbHorizontalLength) / 2.0f;
double thumbHorizontalPositionX = Math.Round(trackHorizontalRegion.X + trackHorizontalRegion.Width / 2.0f - (thumbHorizontalLength / 2.0f) -
Math.Min(limitHorizontal, Math.Max(-limitHorizontal, CanvasCenterOffset.X / CanvasRectangle.Width * trackHorizontalLength)));
Rectangle thumbHorizontalRegion = new Rectangle(new Point((int)thumbHorizontalPositionX, ClientArea.Bottom - (scrollbarThickness + trackMargin)), new Size(thumbHorizontalLength, scrollbarThickness));
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawCapsule(trackBrush, trackHorizontalRegion);
g.DrawCapsule(thumbBrush, thumbHorizontalRegion);
g.SmoothingMode = SmoothingMode.None;
}
if (CanvasRectangle.Top < ClientArea.Top || CanvasRectangle.Bottom > ClientArea.Bottom)
{
int trackVerticalLength = ClientArea.Height - trackMargin * 2 - scrollbarThickness - trackPadding * 2;
int thumbVerticalLength = Math.Max(scrollbarThickness, (int)Math.Round((float)imageRectangleVisible.Height / CanvasRectangle.Height * trackVerticalLength));
Rectangle trackVerticalRegion = new Rectangle(new Point(ClientArea.Right - (scrollbarThickness + trackMargin) - trackPadding, trackMargin - trackPadding), new Size(scrollbarThickness + trackPadding * 2, trackVerticalLength + trackPadding * 2));
double limitVertical = (trackVerticalLength - thumbVerticalLength) / 2.0f;
double thumbVerticalPositionY = Math.Round(trackVerticalRegion.Y + trackVerticalRegion.Height / 2.0f - (thumbVerticalLength / 2.0f) -
Math.Min(limitVertical, Math.Max(-limitVertical, CanvasCenterOffset.Y / CanvasRectangle.Height * trackVerticalLength)));
Rectangle thumbVerticalRegion = new Rectangle(new Point(ClientArea.Right - (scrollbarThickness + trackMargin), (int)thumbVerticalPositionY), new Size(scrollbarThickness, thumbVerticalLength));
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawCapsule(trackBrush, trackVerticalRegion);
g.DrawCapsule(thumbBrush, thumbVerticalRegion);
g.SmoothingMode = SmoothingMode.None;
}
}
}
private void WriteTips(StringBuilder sb)
{
sb.AppendLine(Resources.RectangleRegion_WriteTips__F1__Hide_tips);

View file

@ -0,0 +1,119 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2017 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;
namespace ShareX.ScreenCaptureLib
{
internal class ScrollbarManager
{
public bool IsVisible => form.ShapeManager.IsPanning && (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;
private RegionCaptureForm form;
private Rectangle horizontalTrackRectangle, horizontalThumbRectangle, verticalTrackRectangle, verticalThumbRectangle;
public ScrollbarManager(RegionCaptureForm regionCaptureForm)
{
form = regionCaptureForm;
}
public void Update()
{
Rectangle imageRectangleVisible = form.CanvasRectangle;
imageRectangleVisible.Intersect(form.ClientArea);
IsHorizontalScrollbarVisible = form.CanvasRectangle.Left < form.ClientArea.Left || form.CanvasRectangle.Right > form.ClientArea.Right;
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));
}
IsVerticalScrollbarVisible = form.CanvasRectangle.Top < form.ClientArea.Top || form.CanvasRectangle.Bottom > form.ClientArea.Bottom;
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));
}
}
public void Draw(Graphics g)
{
if (IsVisible)
{
using (Brush trackBrush = new SolidBrush(Color.FromArgb(255, 60, 60, 60)))
using (Brush thumbBrush = new SolidBrush(Color.FromArgb(255, 130, 130, 130)))
{
if (IsHorizontalScrollbarVisible)
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawCapsule(trackBrush, horizontalTrackRectangle);
g.DrawCapsule(thumbBrush, horizontalThumbRectangle);
g.SmoothingMode = SmoothingMode.None;
}
if (IsVerticalScrollbarVisible)
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawCapsule(trackBrush, verticalTrackRectangle);
g.DrawCapsule(thumbBrush, verticalThumbRectangle);
g.SmoothingMode = SmoothingMode.None;
}
}
}
}
}
}

View file

@ -93,6 +93,7 @@
<Compile Include="Forms\ImageSizeForm.Designer.cs">
<DependentUpon>ImageSizeForm.cs</DependentUpon>
</Compile>
<Compile Include="RegionHelpers\ScrollbarManager.cs" />
<Compile Include="Shapes\AnnotationOptions.cs" />
<Compile Include="Enums.cs" />
<Compile Include="Forms\RegionCaptureSimpleAnnotateForm.cs">