using CMLeonOS.Gui;
using CMLeonOS.Gui.UILib;
using System;
using System.Drawing;
namespace CMLeonOS.UILib.Animations
{
///
/// An animation that moves or resizes a window.
///
internal class MovementAnimation : Animation
{
///
/// Initialise the animation.
///
/// The window associated with the animation.
/// The goal of the animation.
internal MovementAnimation(Window window)
{
Window = window;
From = new Rectangle(window.X, window.Y, window.Width, window.Height);
}
///
/// The starting rectangle of the animation.
///
internal Rectangle From;
///
/// The goal rectangle of the animation.
///
internal Rectangle To;
internal override bool Advance()
{
if (From.IsEmpty || To.IsEmpty) throw new Exception("The From or To value of this MovementAnimation is empty.");
Position++;
if (Position == Duration)
{
Window.MoveAndResize(To.X, To.Y, To.Width, To.Height);
if (Window is Control control)
{
control.Render();
}
}
else
{
double t = Easing.Ease(Position / (double)Duration, EasingType, EasingDirection);
Rectangle current = new Rectangle(
(int)Easing.Lerp(From.X, To.X, t),
(int)Easing.Lerp(From.Y, To.Y, t),
(int)Easing.Lerp(From.Width, To.Width, t),
(int)Easing.Lerp(From.Height, To.Height, t)
);
Window.MoveAndResize(current.X, current.Y, current.Width, current.Height);
if (Window is Control control)
{
control.Render();
}
}
return Finished;
}
}
}