using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Mesoft.Animation
{
public class MeSlide
{
private int counter = 0;
private int timeStart;
private int timeDest;
private AnimationType Animation;
private float t;
private float d;
private float b;
private float c;
private int[] Arr_startPos = new int[]{0,0};
private int[] Arr_destPos = new int[]{0,0};
private System.Windows.Forms.Timer objTimer;
private System.Windows.Forms.Control objHolder;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Timer timer1;
public void StartSlideEvent(object _objHolder, int _destXpos, int _destYpos, AnimationType ObjAnimationType, int _timeInterval)
{
//inits the parameters for the tween process
counter = 0;
timeStart = counter;
timeDest = _timeInterval;
Animation = ObjAnimationType;
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer1.Interval = 1;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//Manages the object passed in to be tweened.
//I create a new instance of a control and then force the object to convert to
//a control. Doing it this way, the method accepts ANY control,
//rather than hard-coding "Button" or some other specific control.
objHolder = new System.Windows.Forms.Control();
objHolder = (System.Windows.Forms.Control) _objHolder;
objTimer = this.timer1;
//initializes the object's position in the pos Arrays
Arr_startPos[0] = objHolder.Location.X;
Arr_startPos[1] = objHolder.Location.Y;
Arr_destPos[0] = _destXpos;
Arr_destPos[1] = _destYpos;
//resets the timer and finally starts it
objTimer.Stop();
objTimer.Enabled = false;
objTimer.Enabled = true;
}
///
///This is the method that gets called every tick interval
///
private void timer1_Tick(object sender, System.EventArgs e)
{
if(objHolder.Location.X == Arr_destPos[0] && objHolder.Location.Y == Arr_destPos[1])
{
objTimer.Stop();
objTimer.Enabled = false;
}
else
{
objHolder.Location = new System.Drawing.Point(Slide(0), Slide(1));
counter++;
}
}
///
///This method returns a value from the tween formula.
///
private int Slide(int prop)
{
t = (float)counter - timeStart;
b = (float)Arr_startPos[prop];
c = (float)Arr_destPos[prop] - Arr_startPos[prop];
d = (float)timeDest - timeStart;
return getFormula(Animation, t, b, d, c);
}
public AnimationType AnimationTypeStlye
{
get;
set;
}
///
///this method selects which formula to pick and then returns a number for the tween position of the pictureBox
///
private int getFormula(AnimationType ObjAnimationType, float t, float b, float d, float c)
{
//adjust formula to selected algoritm from combobox
switch (ObjAnimationType)
{
case AnimationType.Linear:
// simple linear tweening - no easing
return (int)(c*t/d+b);
case AnimationType.EaseinQuad:
// quadratic (t^2) easing in - accelerating from zero velocity
return (int)(c*(t/=d)*t + b);
case AnimationType.EaseoutQuad:
// quadratic (t^2) easing out - decelerating to zero velocity
return (int)(-c*(t=t/d)*(t-2)+b);
case AnimationType.EaseinoutQuad:
// quadratic easing in/out - acceleration until halfway, then deceleration
if ((t/=d/2)<1) return (int)(c/2*t*t+b); else return (int)(-c/2*((--t)*(t-2)-1)+b);
case AnimationType.EaseinCubic:
// cubic easing in - accelerating from zero velocity
return (int)(c*(t/=d)*t*t + b);
case AnimationType.EaseoutCubic:
// cubic easing in - accelerating from zero velocity
return (int)(c*((t=t/d-1)*t*t + 1) + b);
case AnimationType.EaseinoutCubic:
// cubic easing in - accelerating from zero velocity
if ((t/=d/2) < 1)return (int)(c/2*t*t*t+b);else return (int)(c/2*((t-=2)*t*t + 2)+b);
case AnimationType.EaseinQuart:
// quartic easing in - accelerating from zero velocity
return (int)(c*(t/=d)*t*t*t + b);
case AnimationType.EaseinExpo:
// exponential (2^t) easing in - accelerating from zero velocity
if (t==0) return (int)b; else return (int)(c*Math.Pow(2,(10*(t/d-1)))+b);
case AnimationType.EaseoutExpo:
// exponential (2^t) easing out - decelerating to zero velocity
if (t==d) return (int)(b+c); else return (int)(c * (-Math.Pow(2,-10*t/d)+1)+b);
default:
return 0;
}
}
}
public enum AnimationType
{
Linear = 0,
EaseinQuad = 1,
EaseoutQuad = 2,
EaseinoutQuad = 3,
EaseinCubic = 4,
EaseoutCubic = 5,
EaseinoutCubic = 6,
EaseinQuart = 7,
EaseinExpo = 8,
EaseoutExpo = 9
}
}
/*--------------Implement in form-----------------------------*/
Mesoft.Animation.MeSlide s = new Mesoft.Animation.MeSlide();
s.StartSlideEvent(this, 200, 0, Mesoft.Animation.AnimationType.EaseinCubic, 100);
No comments:
Post a Comment