Code cleanup (removed floats and casts) and comments.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2343 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-12-02 13:26:03 +00:00
commit e21420e2aa

View file

@ -37,46 +37,81 @@ namespace Greenshot.Helpers {
protected double frames; protected double frames;
protected double currentFrame = 0; protected double currentFrame = 0;
/// <summary>
/// The amount of frames
/// </summary>
public double Frames { public double Frames {
get { return frames; } get { return frames; }
} }
/// <summary>
/// Current frame number
/// </summary>
public double CurrentFrame { public double CurrentFrame {
get { return currentFrame; } get { return currentFrame; }
} }
/// <summary>
/// First animation value
/// </summary>
public T First { public T First {
get { return first; } get { return first; }
} }
/// <summary>
/// Last animation value
/// </summary>
public T Last { public T Last {
get { return last; } get { return last; }
} }
public void ChangeDestination(T last) { /// <summary>
ChangeDestination(last, frames); /// This restarts the current animation and changes the last frame
/// </summary>
/// <param name="newDestination"></param>
public void ChangeDestination(T newDestination) {
ChangeDestination(newDestination, frames);
} }
/// <summary>
/// This restarts the current animation and changes the last frame
/// </summary>
/// <param name="newDestination"></param>
/// <param name="frames"></param>
public void ChangeDestination(T newDestination, double frames) {
this.first = current;
this.currentFrame = 0;
this.frames = frames;
this.last = newDestination;
}
/// <summary>
/// Queue the destination, it will be used after the current animation is finished
/// </summary>
/// <param name="queuedDestination"></param>
public void QueueDestination(T queuedDestination) { public void QueueDestination(T queuedDestination) {
queue.Enqueue(queuedDestination); queue.Enqueue(queuedDestination);
} }
public void ChangeDestination(T last, double frames) { /// <summary>
this.first = current; /// The EasingType to use for the animation
this.currentFrame = 0; /// </summary>
this.frames = frames;
this.last = last;
}
public EasingType EasingType { public EasingType EasingType {
get; get;
set; set;
} }
/// <summary>
/// The EasingMode to use for the animation
/// </summary>
public EasingMode EasingMode { public EasingMode EasingMode {
get; get;
set; set;
} }
/// <summary>
/// Get the easing value, which is from 0-1 and depends on the frame
/// </summary>
protected double EasingValue { protected double EasingValue {
get { get {
switch (EasingMode) { switch (EasingMode) {
@ -91,6 +126,14 @@ namespace Greenshot.Helpers {
} }
} }
/// <summary>
/// Constructor
/// </summary>
/// <param name="first"></param>
/// <param name="last"></param>
/// <param name="frames"></param>
/// <param name="easingType"></param>
/// <param name="easingMode"></param>
public AnimatorBase(T first, T last, int frames, EasingType easingType, EasingMode easingMode) { public AnimatorBase(T first, T last, int frames, EasingType easingType, EasingMode easingMode) {
this.first = first; this.first = first;
this.last = last; this.last = last;
@ -99,18 +142,19 @@ namespace Greenshot.Helpers {
this.EasingType = easingType; this.EasingType = easingType;
this.EasingMode = easingMode; this.EasingMode = easingMode;
} }
public virtual void Reset() {
currentFrame = 0;
current = first;
}
/// <summary>
/// Get the current (previous) frame object
/// </summary>
public virtual T Current { public virtual T Current {
get { get {
return current; return current;
} }
} }
/// <summary>
/// Returns if there are any frame left, and if this is the case than the frame is increaded.
/// </summary>
public virtual bool NextFrame { public virtual bool NextFrame {
get { get {
if (currentFrame < frames) { if (currentFrame < frames) {
@ -127,6 +171,9 @@ namespace Greenshot.Helpers {
} }
} }
/// <summary>
/// Are there more frames to animate?
/// </summary>
public virtual bool hasNext { public virtual bool hasNext {
get { get {
if (currentFrame < frames) { if (currentFrame < frames) {
@ -136,6 +183,10 @@ namespace Greenshot.Helpers {
} }
} }
/// <summary>
/// Get the next animation frame value object
/// </summary>
/// <returns></returns>
public abstract T Next(); public abstract T Next();
} }
@ -156,6 +207,10 @@ namespace Greenshot.Helpers {
: base(first, last, frames, easingType, easingMode) { : base(first, last, frames, easingType, easingMode) {
} }
/// <summary>
/// Calculate the next frame object
/// </summary>
/// <returns>Rectangle</returns>
public override Rectangle Next() { public override Rectangle Next() {
if (NextFrame) { if (NextFrame) {
double easingValue = EasingValue; double easingValue = EasingValue;
@ -188,6 +243,11 @@ namespace Greenshot.Helpers {
public PointAnimator(Point first, Point last, int frames, EasingType easingType, EasingMode easingMode) public PointAnimator(Point first, Point last, int frames, EasingType easingType, EasingMode easingMode)
: base(first, last, frames, easingType, easingMode) { : base(first, last, frames, easingType, easingMode) {
} }
/// <summary>
/// Calculate the next frame value
/// </summary>
/// <returns>Point</returns>
public override Point Next() { public override Point Next() {
if (NextFrame) { if (NextFrame) {
double easingValue = EasingValue; double easingValue = EasingValue;
@ -217,6 +277,10 @@ namespace Greenshot.Helpers {
: base(first, last, frames, easingType, easingMode) { : base(first, last, frames, easingType, easingMode) {
} }
/// <summary>
/// Calculate the next frame values
/// </summary>
/// <returns>Size</returns>
public override Size Next() { public override Size Next() {
if (NextFrame) { if (NextFrame) {
double easingValue = EasingValue; double easingValue = EasingValue;
@ -236,9 +300,10 @@ namespace Greenshot.Helpers {
public static class Easing { public static class Easing {
// Adapted from http://www.robertpenner.com/easing/penner_chapter7_tweening.pdf // Adapted from http://www.robertpenner.com/easing/penner_chapter7_tweening.pdf
public static float Ease(double linearStep, float acceleration, EasingType type) { public static double Ease(double linearStep, double acceleration, EasingType type) {
double easedStep = acceleration > 0 ? EaseIn(linearStep, type) : acceleration < 0 ? EaseOut(linearStep, type) : (float)linearStep; double easedStep = acceleration > 0 ? EaseIn(linearStep, type) : acceleration < 0 ? EaseOut(linearStep, type) : (double)linearStep;
return MathHelper.Lerp(linearStep, easedStep, Math.Abs(acceleration)); // Lerp:
return ((easedStep - linearStep) * Math.Abs(acceleration) + linearStep);
} }
public static double EaseIn(double linearStep, EasingType type) { public static double EaseIn(double linearStep, EasingType type) {
@ -246,7 +311,7 @@ namespace Greenshot.Helpers {
case EasingType.Step: case EasingType.Step:
return linearStep < 0.5 ? 0 : 1; return linearStep < 0.5 ? 0 : 1;
case EasingType.Linear: case EasingType.Linear:
return (double)linearStep; return linearStep;
case EasingType.Sine: case EasingType.Sine:
return Sine.EaseIn(linearStep); return Sine.EaseIn(linearStep);
case EasingType.Quadratic: case EasingType.Quadratic:
@ -266,7 +331,7 @@ namespace Greenshot.Helpers {
case EasingType.Step: case EasingType.Step:
return linearStep < 0.5 ? 0 : 1; return linearStep < 0.5 ? 0 : 1;
case EasingType.Linear: case EasingType.Linear:
return (double)linearStep; return linearStep;
case EasingType.Sine: case EasingType.Sine:
return Sine.EaseOut(linearStep); return Sine.EaseOut(linearStep);
case EasingType.Quadratic: case EasingType.Quadratic:
@ -290,7 +355,7 @@ namespace Greenshot.Helpers {
case EasingType.Step: case EasingType.Step:
return linearStep < 0.5 ? 0 : 1; return linearStep < 0.5 ? 0 : 1;
case EasingType.Linear: case EasingType.Linear:
return (float)linearStep; return linearStep;
case EasingType.Sine: case EasingType.Sine:
return Sine.EaseInOut(linearStep); return Sine.EaseInOut(linearStep);
case EasingType.Quadratic: case EasingType.Quadratic:
@ -307,34 +372,37 @@ namespace Greenshot.Helpers {
static class Sine { static class Sine {
public static double EaseIn(double s) { public static double EaseIn(double s) {
return (double)Math.Sin(s * MathHelper.HalfPi - MathHelper.HalfPi) + 1; return Math.Sin(s * (Math.PI / 2) - (Math.PI / 2)) + 1;
} }
public static double EaseOut(double s) { public static double EaseOut(double s) {
return (double)Math.Sin(s * MathHelper.HalfPi); return Math.Sin(s * (Math.PI / 2));
} }
public static double EaseInOut(double s) { public static double EaseInOut(double s) {
return (double)(Math.Sin(s * MathHelper.Pi - MathHelper.HalfPi) + 1) / 2; return Math.Sin(s * Math.PI - (Math.PI / 2) + 1) / 2;
} }
} }
static class Power { static class Power {
public static double EaseIn(double s, int power) { public static double EaseIn(double s, int power) {
return (double)Math.Pow(s, power); return Math.Pow(s, power);
} }
public static double EaseOut(double s, int power) { public static double EaseOut(double s, int power) {
var sign = power % 2 == 0 ? -1 : 1; var sign = power % 2 == 0 ? -1 : 1;
return (double)(sign * (Math.Pow(s - 1, power) + sign)); return sign * (Math.Pow(s - 1, power) + sign);
} }
public static double EaseInOut(double s, int power) { public static double EaseInOut(double s, int power) {
s *= 2; s *= 2;
if (s < 1) if (s < 1)
return EaseIn(s, power) / 2; return EaseIn(s, power) / 2;
var sign = power % 2 == 0 ? -1 : 1; var sign = power % 2 == 0 ? -1 : 1;
return (float)(sign / 2.0 * (Math.Pow(s - 2, power) + sign * 2)); return (sign / 2.0 * (Math.Pow(s - 2, power) + sign * 2));
} }
} }
} }
/// <summary>
/// This defines the way the animation works
/// </summary>
public enum EasingType { public enum EasingType {
Step, Step,
Linear, Linear,
@ -350,13 +418,4 @@ namespace Greenshot.Helpers {
EaseOut, EaseOut,
EaseInOut EaseInOut
} }
public static class MathHelper {
public const float Pi = (float)Math.PI;
public const float HalfPi = (float)(Math.PI / 2);
public static float Lerp(double from, double to, double step) {
return (float)((to - from) * step + from);
}
}
} }