Improved screen tearing test speed and added mouse wheel animation speed control

This commit is contained in:
Jaex 2016-06-08 00:55:52 +03:00
parent 88a6af57ef
commit 1c297dc908

View file

@ -39,11 +39,12 @@ namespace ShareX.HelpersLib
public class ScreenTearingTestForm : Form public class ScreenTearingTestForm : Form
{ {
private Rectangle screenRectangle, screenRectangle0Based; private Rectangle screenRectangle, screenRectangle0Based;
private TextureBrush brush; private Timer drawTimer;
private Timer timer; private Stopwatch animationTime;
private Stopwatch stopwatch;
private TimeSpan lastElapsed; private TimeSpan lastElapsed;
private float animationSpeed = 200; private int rectangleSize = 50;
private float animationSpeed = 500, minSpeed = 100, maxSpeed = 2000, speedChange = 50;
private float currentPosition;
public ScreenTearingTestForm() public ScreenTearingTestForm()
{ {
@ -56,6 +57,7 @@ public ScreenTearingTestForm()
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
StartPosition = FormStartPosition.Manual; StartPosition = FormStartPosition.Manual;
Bounds = screenRectangle; Bounds = screenRectangle;
Cursor = Cursors.Hand;
FormBorderStyle = FormBorderStyle.None; FormBorderStyle = FormBorderStyle.None;
Icon = ShareXResources.Icon; Icon = ShareXResources.Icon;
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
@ -65,13 +67,11 @@ public ScreenTearingTestForm()
ResumeLayout(false); ResumeLayout(false);
brush = CreateVerticalLineBrush(50, screenRectangle.Height, Color.Black, Color.White); animationTime = Stopwatch.StartNew();
stopwatch = Stopwatch.StartNew(); drawTimer = new Timer { Interval = 5 };
drawTimer.Tick += timer_Tick;
timer = new Timer { Interval = 10 }; drawTimer.Start();
timer.Tick += timer_Tick;
timer.Start();
} }
protected override void OnShown(EventArgs e) protected override void OnShown(EventArgs e)
@ -91,6 +91,20 @@ protected override void OnKeyUp(KeyEventArgs e)
base.OnKeyUp(e); base.OnKeyUp(e);
} }
protected override void OnMouseWheel(MouseEventArgs e)
{
if (e.Delta > 0)
{
animationSpeed = (animationSpeed + speedChange).Between(minSpeed, maxSpeed);
}
else if (e.Delta < 0)
{
animationSpeed = (animationSpeed - speedChange).Between(minSpeed, maxSpeed);
}
base.OnMouseWheel(e);
}
protected override void OnMouseUp(MouseEventArgs e) protected override void OnMouseUp(MouseEventArgs e)
{ {
Close(); Close();
@ -111,44 +125,28 @@ protected override void OnPaintBackground(PaintEventArgs e)
protected override void OnPaint(PaintEventArgs e) protected override void OnPaint(PaintEventArgs e)
{ {
Graphics g = e.Graphics; Graphics g = e.Graphics;
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.SmoothingMode = SmoothingMode.HighSpeed; g.SmoothingMode = SmoothingMode.HighSpeed;
g.FillRectangle(brush, screenRectangle0Based); g.Clear(Color.White);
TimeSpan elapsed = stopwatch.Elapsed - lastElapsed; int nextPosition = rectangleSize * 2;
int startOffset = (int)(currentPosition % nextPosition);
float x = (float)(elapsed.TotalSeconds * animationSpeed); for (int x = startOffset - rectangleSize; x < screenRectangle.Width; x += nextPosition)
brush.TranslateTransform(x, 0);
lastElapsed = stopwatch.Elapsed;
}
private TextureBrush CreateVerticalLineBrush(int size, int height, Color color1, Color color2)
{
using (Bitmap bmp = new Bitmap(size * 2, height))
using (Graphics g = Graphics.FromImage(bmp))
{ {
g.SmoothingMode = SmoothingMode.HighSpeed; g.FillRectangle(Brushes.Black, x, 0, rectangleSize, screenRectangle.Height);
using (Brush brush1 = new SolidBrush(color1))
{
g.FillRectangle(brush1, 0, 0, size, height);
}
using (Brush brush2 = new SolidBrush(color2))
{
g.FillRectangle(brush2, size, 0, size, height);
}
return new TextureBrush(bmp, WrapMode.Tile);
} }
TimeSpan elapsed = animationTime.Elapsed - lastElapsed;
currentPosition += (float)(elapsed.TotalSeconds * animationSpeed);
lastElapsed = animationTime.Elapsed;
} }
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
if (timer != null) timer.Dispose(); if (drawTimer != null) drawTimer.Dispose();
if (brush != null) brush.Dispose();
base.Dispose(disposing); base.Dispose(disposing);
} }