ShareX/ShareX/EasterEggBounce.cs

142 lines
4.1 KiB
C#
Raw Normal View History

2018-12-24 13:23:13 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2018-12-24 13:23:13 +13:00
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.Windows.Forms;
namespace ShareX
{
public class EasterEggBounce : IDisposable
{
public Form Form { get; private set; }
public bool IsWorking { get; private set; }
2018-12-28 13:17:09 +13:00
public Rectangle BounceRectangle { get; set; }
2018-12-24 13:23:13 +13:00
public int Speed { get; set; } = 20;
2018-12-25 05:57:32 +13:00
public bool ApplyGravity { get; set; } = true;
2018-12-24 13:23:13 +13:00
public int GravityPower { get; set; } = 3;
public int BouncePower { get; set; } = 50;
2018-12-25 05:57:32 +13:00
private Timer timer;
2018-12-24 13:23:13 +13:00
private Point velocity;
public EasterEggBounce(Form form)
{
Form = form;
2018-12-25 05:57:32 +13:00
timer = new Timer();
timer.Interval = 20;
timer.Tick += bounceTimer_Tick;
2018-12-28 13:17:09 +13:00
BounceRectangle = CaptureHelpers.GetScreenWorkingArea();
2018-12-24 13:23:13 +13:00
}
public void Start()
{
if (!IsWorking)
{
IsWorking = true;
velocity = new Point(RandomFast.Pick(-Speed, Speed), ApplyGravity ? GravityPower : RandomFast.Pick(-Speed, Speed));
2018-12-25 05:57:32 +13:00
timer.Start();
2018-12-24 13:23:13 +13:00
}
}
public void Stop()
{
if (IsWorking)
{
2018-12-25 05:57:32 +13:00
if (timer != null)
2018-12-24 13:23:13 +13:00
{
2018-12-25 05:57:32 +13:00
timer.Stop();
2018-12-24 13:23:13 +13:00
}
IsWorking = false;
}
}
private void bounceTimer_Tick(object sender, EventArgs e)
{
if (Form != null && !Form.IsDisposed)
{
int x = Form.Left + velocity.X;
2018-12-28 13:17:09 +13:00
int windowRight = BounceRectangle.X + BounceRectangle.Width - Form.Width - 1;
2018-12-24 13:23:13 +13:00
2018-12-28 13:17:09 +13:00
if (x <= BounceRectangle.X)
2018-12-24 13:23:13 +13:00
{
2018-12-28 13:17:09 +13:00
x = BounceRectangle.X;
2018-12-24 13:23:13 +13:00
velocity.X = Speed;
}
else if (x >= windowRight)
{
x = windowRight;
velocity.X = -Speed;
}
int y = Form.Top + velocity.Y;
2018-12-28 13:17:09 +13:00
int windowBottom = BounceRectangle.Y + BounceRectangle.Height - Form.Height - 1;
2018-12-24 13:23:13 +13:00
2018-12-25 05:57:32 +13:00
if (ApplyGravity)
2018-12-24 13:23:13 +13:00
{
2018-12-25 05:57:32 +13:00
if (y >= windowBottom)
{
y = windowBottom;
velocity.Y = -BouncePower + RandomFast.Next(-10, 10);
2018-12-25 05:57:32 +13:00
}
else
{
velocity.Y += GravityPower;
}
2018-12-24 13:23:13 +13:00
}
else
{
2018-12-28 13:17:09 +13:00
if (y <= BounceRectangle.Y)
2018-12-25 05:57:32 +13:00
{
2018-12-28 13:17:09 +13:00
y = BounceRectangle.Y;
2018-12-25 05:57:32 +13:00
velocity.Y = Speed;
}
else if (y >= windowBottom)
{
y = windowBottom;
velocity.Y = -Speed;
}
2018-12-24 13:23:13 +13:00
}
Form.Location = new Point(x, y);
}
}
public void Dispose()
{
Stop();
2018-12-25 05:57:32 +13:00
if (timer != null)
2018-12-24 13:23:13 +13:00
{
2018-12-25 05:57:32 +13:00
timer.Dispose();
2018-12-24 13:23:13 +13:00
}
}
}
}