Easter egg class refactor

This commit is contained in:
Jaex 2018-12-28 03:17:09 +03:00
parent 5aefb5cf4d
commit bf8b5ab4db
2 changed files with 30 additions and 29 deletions

View file

@ -33,22 +33,18 @@ namespace ShareX
{
public class EasterEggAboutAnimation : IDisposable
{
private const int w = 200;
private const int h = w;
private const int mX = w / 2;
private const int mY = h / 2;
private const int minStep = 3;
private const int maxStep = 35;
private const int speed = 1;
public Canvas Canvas { get; private set; }
public bool IsPaused { get; set; }
public Size Size { get; set; } = new Size(200, 200);
public int Step { get; set; } = 10;
public int Direction { get; set; } = speed;
public Color Color { get; set; } = new HSB(0d, 1d, 0.9d);
public int MinStep { get; set; } = 3;
public int MaxStep { get; set; } = 35;
public int Speed { get; set; } = 1;
public Color Color { get; set; } = new HSB(0.0, 1.0, 0.9);
public int ClickCount { get; private set; }
private EasterEggBounce easterEggBounce;
private int direction;
public EasterEggAboutAnimation(Canvas canvas, Form form)
{
@ -61,6 +57,7 @@ public EasterEggAboutAnimation(Canvas canvas, Form form)
public void Start()
{
direction = Speed;
Canvas.Start(50);
}
@ -88,20 +85,23 @@ private void Canvas_Draw(Graphics g)
{
g.SetHighQuality();
int halfWidth = Size.Width / 2;
int halfHeight = Size.Height / 2;
using (Matrix m = new Matrix())
{
m.RotateAt(45, new PointF(mX, mY));
m.RotateAt(45, new PointF(halfWidth, halfHeight));
g.Transform = m;
}
using (Pen pen = new Pen(Color, 2))
{
for (int i = 0; i <= mX; i += Step)
for (int i = 0; i <= halfWidth; i += Step)
{
g.DrawLine(pen, i, mY, mX, mY - i); // Left top
g.DrawLine(pen, mX, i, mX + i, mY); // Right top
g.DrawLine(pen, w - i, mY, mX, mY + i); // Right bottom
g.DrawLine(pen, mX, h - i, mX - i, mY); // Left bottom
g.DrawLine(pen, i, halfHeight, halfWidth, halfHeight - i); // Left top
g.DrawLine(pen, halfWidth, i, halfWidth + i, halfHeight); // Right top
g.DrawLine(pen, Size.Width - i, halfHeight, halfWidth, halfHeight + i); // Right bottom
g.DrawLine(pen, halfWidth, Size.Height - i, halfWidth - i, halfHeight); // Left bottom
/*
g.DrawLine(pen, i, mY, mX, mY - i); // Left top
@ -123,16 +123,16 @@ private void Canvas_Draw(Graphics g)
if (!IsPaused)
{
if (Step + speed > maxStep)
if (Step + Speed > MaxStep)
{
Direction = -speed;
direction = -Speed;
}
else if (Step - speed < minStep)
else if (Step - Speed < MinStep)
{
Direction = speed;
direction = Speed;
}
Step += Direction;
Step += direction;
HSB hsb = Color;

View file

@ -34,12 +34,12 @@ public class EasterEggBounce : IDisposable
{
public Form Form { get; private set; }
public bool IsWorking { get; private set; }
public Rectangle BounceRectangle { get; set; }
public int Speed { get; set; } = 20;
public bool ApplyGravity { get; set; } = true;
public int GravityPower { get; set; } = 3;
public int BouncePower { get; set; } = 50;
private Rectangle screenRectangle;
private Timer timer;
private Point velocity;
@ -50,6 +50,8 @@ public EasterEggBounce(Form form)
timer = new Timer();
timer.Interval = 20;
timer.Tick += bounceTimer_Tick;
BounceRectangle = CaptureHelpers.GetScreenWorkingArea();
}
public void Start()
@ -58,7 +60,6 @@ public void Start()
{
IsWorking = true;
screenRectangle = CaptureHelpers.GetScreenWorkingArea();
velocity = new Point(MathHelpers.RandomPick(-Speed, Speed), ApplyGravity ? GravityPower : MathHelpers.RandomPick(-Speed, Speed));
timer.Start();
}
@ -82,11 +83,11 @@ private void bounceTimer_Tick(object sender, EventArgs e)
if (Form != null && !Form.IsDisposed)
{
int x = Form.Left + velocity.X;
int windowRight = screenRectangle.X + screenRectangle.Width - Form.Width - 1;
int windowRight = BounceRectangle.X + BounceRectangle.Width - Form.Width - 1;
if (x <= screenRectangle.X)
if (x <= BounceRectangle.X)
{
x = screenRectangle.X;
x = BounceRectangle.X;
velocity.X = Speed;
}
else if (x >= windowRight)
@ -96,7 +97,7 @@ private void bounceTimer_Tick(object sender, EventArgs e)
}
int y = Form.Top + velocity.Y;
int windowBottom = screenRectangle.Y + screenRectangle.Height - Form.Height - 1;
int windowBottom = BounceRectangle.Y + BounceRectangle.Height - Form.Height - 1;
if (ApplyGravity)
{
@ -112,9 +113,9 @@ private void bounceTimer_Tick(object sender, EventArgs e)
}
else
{
if (y <= screenRectangle.Y)
if (y <= BounceRectangle.Y)
{
y = screenRectangle.Y;
y = BounceRectangle.Y;
velocity.Y = Speed;
}
else if (y >= windowBottom)