Click here to Skip to main content
15,886,840 members
Articles / Programming Languages / C#

Motion class to animate your controls

Rate me:
Please Sign up or sign in to vote.
4.21/5 (19 votes)
25 Jul 20033 min read 84.9K   4.4K   38  
An article on easily animating controls on a form in C#
  • tween_demo.zip
    • WindowsApplication1.exe
  • tween_source.zip
    • App.ico
    • AssemblyInfo.cs
    • bin
      • Debug
        • WindowsApplication1.exe
        • WindowsApplication1.pdb
      • Release
        • WindowsApplication1.exe
    • Documents and Settings
      • mmoreno.000
        • My Documents
          • Visual Studio Projects
            • WindowsApplication1
              • bin
                • Debug
                • Release
              • obj
                • Debug
                  • temp
                  • TempPE
                • Release
                  • temp
                  • TempPE
    • Form1.cs
    • Form1.resx
    • obj
      • Debug
        • temp
        • TempPE
        • WindowsApplication1.exe
        • WindowsApplication1.exe.incr
        • WindowsApplication1.pdb
        • WindowsApplication1.projdata
      • Release
        • temp
        • TempPE
        • WindowsApplication1.exe
        • WindowsApplication1.Form1.resources
        • WindowsApplication1.projdata
    • tween.cs
    • WindowsApplication1.csproj
    • WindowsApplication1.csproj.user
    • WindowsApplication1.sln
    • WindowsApplication1.suo
    • WindowsApplication1
      • bin
        • Debug
        • Release
      • obj
        • Debug
          • temp
          • TempPE
        • Release
          • temp
          • TempPE
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1 {

	///<summary>
	///This class is implemented so that from any other class it can be called, and by passing the correct parameters
	///from any object, it will animate the object passed. Parameters required are:
	///(sender[do not modify], timer1[do not modify and must exist], X Destination, Y Destination, Animation Type, Duration) or as noted below.
	///From the remote class add this to the object's eventHandler:
	///TweenLibrary.startTweenEvent(sender, timer1, 300, randint(), "easeinoutcubic", 20); :-mm
	///</summary>
	public class TweenLibrary {			
		private  int counter = 0;
		private  int timeStart;
		private  int timeDest;
		private  string animType;
		
		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;

		///<summary>
		///this method kicks off the process
		///</summary>
		public  void startTweenEvent(object _objHolder, int _destXpos, int _destYpos, string _animType, int _timeInterval){
				
				//inits the parameters for the tween process
				counter		= 0;
				timeStart	= counter;
				timeDest	= _timeInterval;
				animType	= _animType;

				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	= (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;
		}

		///<summary>
		///This is the method that gets called every tick interval
		///</summary>
		public  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(tween(0), tween(1));
				counter++;
			}
		}

		///<summary>
		///This method returns a value from the tween formula.
		///</summary>
		private  int	tween(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(animType, t, b, d, c);
		}

		///<summary>
		///this method selects which formula to pick and then returns a number for the tween position of the pictureBox
		///</summary>
		private  int	getFormula(string animType, float t, float b, float d, float c){
			//adjust formula to selected algoritm from combobox
			switch (animType) {
				case "linear":
					// simple linear tweening - no easing 
					return (int)(c*t/d+b);

				case "easeinquad":
					// quadratic (t^2) easing in - accelerating from zero velocity
					return (int)(c*(t/=d)*t + b);
						
				case "easeoutquad":
					// quadratic (t^2) easing out - decelerating to zero velocity
					return (int)(-c*(t=t/d)*(t-2)+b);
						
				case "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 "easeincubic":
					// cubic easing in - accelerating from zero velocity
					return (int)(c*(t/=d)*t*t + b);

				case "easeoutcubic":
					// cubic easing in - accelerating from zero velocity
					return (int)(c*((t=t/d-1)*t*t + 1) + b);
						
				case "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 "easeinquart":
					// quartic easing in - accelerating from zero velocity
					return (int)(c*(t/=d)*t*t*t + b);

				case "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 "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;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions