Refactor ScrollbarManager class

This commit is contained in:
Jaex 2017-12-10 12:45:39 +03:00
parent eb030318e0
commit fa6b20a8fd

View file

@ -37,36 +37,34 @@ internal class ScrollbarManager
public bool IsHorizontalScrollbarVisible { get; private set; } public bool IsHorizontalScrollbarVisible { get; private set; }
public bool IsVerticalScrollbarVisible { get; private set; } public bool IsVerticalScrollbarVisible { get; private set; }
private bool shouldDrawHorizontalScrollbar = true;
private bool shouldDrawVerticalScrollbar = true;
private bool shouldDrawHorizontalScrollbarBefore = true;
private bool shouldDrawVerticalScrollbarBefore = true;
private Stopwatch horizontalScrollbarChangeTime;
private Stopwatch verticalScrollbarChangeTime;
public int Thickness { get; set; } = 10; public int Thickness { get; set; } = 10;
public int Margin { get; set; } = 15; public int Margin { get; set; } = 15;
public int Padding { get; set; } = 2; public int Padding { get; set; } = 2;
// timings in milliseconds // Timings in milliseconds
public int FadeInTime { get; set; } = 150; public int FadeInTime { get; set; } = 150;
public int FadeOutDelay { get; set; } = 500; public int FadeOutDelay { get; set; } = 500;
public int FadeOutTime { get; set; } = 150; public int FadeOutTime { get; set; } = 150;
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 HorizontalScrollbarOpacityLast = 255;
private int HorizontalScrollbarOpacityCurrent = 255; private int HorizontalScrollbarOpacityCurrent = 255;
private int VerticalScrollbarOpacityLast = 255; private int VerticalScrollbarOpacityLast = 255;
private int VerticalScrollbarOpacityCurrent = 255; private int VerticalScrollbarOpacityCurrent = 255;
private RegionCaptureForm form;
private Rectangle horizontalTrackRectangle, horizontalThumbRectangle, verticalTrackRectangle, verticalThumbRectangle;
public ScrollbarManager(RegionCaptureForm regionCaptureForm) public ScrollbarManager(RegionCaptureForm regionCaptureForm)
{ {
form = regionCaptureForm; form = regionCaptureForm;
horizontalScrollbarChangeTime = Stopwatch.StartNew(); horizontalScrollbarChangeTime = Stopwatch.StartNew();
verticalScrollbarChangeTime = Stopwatch.StartNew(); verticalScrollbarChangeTime = Stopwatch.StartNew();
if (form.ClientArea.Contains(form.CanvasRectangle)) if (form.ClientArea.Contains(form.CanvasRectangle))
{ {
HorizontalScrollbarOpacityLast = 0; HorizontalScrollbarOpacityLast = 0;
@ -76,35 +74,30 @@ public ScrollbarManager(RegionCaptureForm regionCaptureForm)
} }
} }
int opacityGain(Stopwatch changeTime)
{
return (int)Math.Min(255.0f, 255.0f * (float)changeTime.ElapsedMilliseconds / Math.Max(0, (float)FadeInTime));
}
int opacityLoss(Stopwatch changeTime)
{
int deltaTime = Math.Max(0, (int)changeTime.ElapsedMilliseconds - FadeOutDelay);
return (int)Math.Min(255.0f, 255.0f * (float)deltaTime / Math.Max(0, (float)FadeOutTime));
}
public void Update() public void Update()
{ {
Rectangle imageRectangleVisible = form.CanvasRectangle; Rectangle imageRectangleVisible = form.CanvasRectangle;
imageRectangleVisible.Intersect(form.ClientArea); imageRectangleVisible.Intersect(form.ClientArea);
shouldDrawHorizontalScrollbar = form.ShapeManager.IsPanning && (form.CanvasRectangle.Left < form.ClientArea.Left || form.CanvasRectangle.Right > form.ClientArea.Right); shouldDrawHorizontalScrollbar = form.ShapeManager.IsPanning &&
(form.CanvasRectangle.Left < form.ClientArea.Left || form.CanvasRectangle.Right > form.ClientArea.Right);
if (shouldDrawHorizontalScrollbar ^ shouldDrawHorizontalScrollbarBefore) if (shouldDrawHorizontalScrollbar != shouldDrawHorizontalScrollbarBefore)
{ {
horizontalScrollbarChangeTime = Stopwatch.StartNew(); horizontalScrollbarChangeTime = Stopwatch.StartNew();
HorizontalScrollbarOpacityLast = HorizontalScrollbarOpacityCurrent; HorizontalScrollbarOpacityLast = HorizontalScrollbarOpacityCurrent;
} }
shouldDrawHorizontalScrollbarBefore = shouldDrawHorizontalScrollbar; shouldDrawHorizontalScrollbarBefore = shouldDrawHorizontalScrollbar;
HorizontalScrollbarOpacityCurrent = shouldDrawHorizontalScrollbar if (shouldDrawHorizontalScrollbar)
? HorizontalScrollbarOpacityLast + opacityGain(horizontalScrollbarChangeTime) {
: HorizontalScrollbarOpacityLast - opacityLoss(horizontalScrollbarChangeTime); HorizontalScrollbarOpacityCurrent = HorizontalScrollbarOpacityLast + OpacityGain(horizontalScrollbarChangeTime);
HorizontalScrollbarOpacityCurrent = Math.Min(255, Math.Max(0, HorizontalScrollbarOpacityCurrent)); }
else
{
HorizontalScrollbarOpacityCurrent = HorizontalScrollbarOpacityLast - OpacityLoss(horizontalScrollbarChangeTime);
}
HorizontalScrollbarOpacityCurrent = HorizontalScrollbarOpacityCurrent.Between(0, 255);
IsHorizontalScrollbarVisible = HorizontalScrollbarOpacityCurrent > 0; IsHorizontalScrollbarVisible = HorizontalScrollbarOpacityCurrent > 0;
@ -124,19 +117,25 @@ public void Update()
new Size(horizontalThumbLength, Thickness)); new Size(horizontalThumbLength, Thickness));
} }
shouldDrawVerticalScrollbar = form.ShapeManager.IsPanning && (form.CanvasRectangle.Top < form.ClientArea.Top || form.CanvasRectangle.Bottom > form.ClientArea.Bottom); shouldDrawVerticalScrollbar = form.ShapeManager.IsPanning &&
(form.CanvasRectangle.Top < form.ClientArea.Top || form.CanvasRectangle.Bottom > form.ClientArea.Bottom);
if (shouldDrawVerticalScrollbar ^ shouldDrawVerticalScrollbarBefore) if (shouldDrawVerticalScrollbar != shouldDrawVerticalScrollbarBefore)
{ {
verticalScrollbarChangeTime = Stopwatch.StartNew(); verticalScrollbarChangeTime = Stopwatch.StartNew();
VerticalScrollbarOpacityLast = VerticalScrollbarOpacityCurrent; VerticalScrollbarOpacityLast = VerticalScrollbarOpacityCurrent;
} }
shouldDrawVerticalScrollbarBefore = shouldDrawVerticalScrollbar; shouldDrawVerticalScrollbarBefore = shouldDrawVerticalScrollbar;
VerticalScrollbarOpacityCurrent = shouldDrawVerticalScrollbar if (shouldDrawVerticalScrollbar)
? VerticalScrollbarOpacityLast + opacityGain(verticalScrollbarChangeTime) {
: VerticalScrollbarOpacityLast - opacityLoss(verticalScrollbarChangeTime); VerticalScrollbarOpacityCurrent = VerticalScrollbarOpacityLast + OpacityGain(verticalScrollbarChangeTime);
VerticalScrollbarOpacityCurrent = Math.Min(255, Math.Max(0, VerticalScrollbarOpacityCurrent)); }
else
{
VerticalScrollbarOpacityCurrent = VerticalScrollbarOpacityLast - OpacityLoss(verticalScrollbarChangeTime);
}
VerticalScrollbarOpacityCurrent = VerticalScrollbarOpacityCurrent.Between(0, 255);
IsVerticalScrollbarVisible = VerticalScrollbarOpacityCurrent > 0; IsVerticalScrollbarVisible = VerticalScrollbarOpacityCurrent > 0;
if (IsVerticalScrollbarVisible) if (IsVerticalScrollbarVisible)
@ -185,5 +184,16 @@ public void Draw(Graphics g)
} }
} }
} }
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));
}
} }
} }