mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-04 12:14:45 +13:00
Improved screen tearing test speed and added mouse wheel animation speed control
This commit is contained in:
parent
88a6af57ef
commit
1c297dc908
1 changed files with 36 additions and 38 deletions
|
@ -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);
|
{
|
||||||
|
g.FillRectangle(Brushes.Black, x, 0, rectangleSize, screenRectangle.Height);
|
||||||
lastElapsed = stopwatch.Elapsed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private TextureBrush CreateVerticalLineBrush(int size, int height, Color color1, Color color2)
|
TimeSpan elapsed = animationTime.Elapsed - lastElapsed;
|
||||||
{
|
|
||||||
using (Bitmap bmp = new Bitmap(size * 2, height))
|
|
||||||
using (Graphics g = Graphics.FromImage(bmp))
|
|
||||||
{
|
|
||||||
g.SmoothingMode = SmoothingMode.HighSpeed;
|
|
||||||
|
|
||||||
using (Brush brush1 = new SolidBrush(color1))
|
currentPosition += (float)(elapsed.TotalSeconds * animationSpeed);
|
||||||
{
|
|
||||||
g.FillRectangle(brush1, 0, 0, size, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
using (Brush brush2 = new SolidBrush(color2))
|
lastElapsed = animationTime.Elapsed;
|
||||||
{
|
|
||||||
g.FillRectangle(brush2, size, 0, size, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new TextureBrush(bmp, WrapMode.Tile);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue