Writing custom Animator for TLabelAnimated
Posted on March 28, 2012
One of the powerful features of TLabelAnimated is that you can write your own custom animator for it. The custom animator allows you to completely customize the way TLabelAnimated animates the text within it.
Let’s say your TLabelAnimated is not wide enough to encompass its text (typical scenario). Therefore you would like the text to animate back and forth plus you would like to have the opportunity to pause the animation at any time and also resume it at any time. This is a perfect scenario for writing your own custom animator.
In this article we will create a form with an image in its background, put a TLabelAnimated on it and TCheckBox. We will use TCheckBox to pause the animation at any time.
We will implement the custom animator in a class called MyAnimator. The class needs to implement BeeMobile.TransparentControls.IAnimator interface. When the class is implemented, we simply need to create an object of it and assign it to Animator property of TLabelAnimated.
The code for Form2 looks like this:
public Form2() { InitializeComponent(); MyAnimator ma = new MyAnimator(); tLabelAnimated1.Animator = ma; tCheckBox1.Checked = !ma.StandStill; } private void Form2_Load(object sender, EventArgs e) { tLabelAnimated1.StartAnimation(); } private void tCheckBox1_CheckStateChanged(object sender, EventArgs e) { (tLabelAnimated1.Animator as MyAnimator).StandStill = !tCheckBox1.Checked; }
Now let’s take a look at MyAnimator class. If you let Visual Studio implicitly implement IAnimator interface for you, this what it will create for you:
class MyAnimator : IAnimator { #region IAnimator Members public event AnimationCycleCompletedHandler AnimationCycleCompleted; public System.Drawing.Point ChangeTextLocation(TLabelAnimated tLabelAnimated) { throw new NotImplementedException(); } #endregion }
The ChangeTextLocation will be automatically fired by TLabelAnimated each time a new frame is about to be rendered. By returning a Point object you specify where the text should be positioned within TLabelAnimated. Let’s add these data members. The explanation is in the comments section:
private const int KAnimationStep = 2; // animation step private Point m_CurrentPosition; // current position of the text private SizeF m_StringSize; // size of the string inside TLabelAnimated private bool m_bDirectionLeft; // which direction are we currently animating? private bool m_bStandStill; // should the text stand still ? /// <summary> /// Important parameter /// </summary> /// <remarks> /// Specifies the difference between the width of TLabelAnimated and the text within it. /// </remarks> private float m_fDelta;
The constructor of MyAnimator simply initializes some of these data members:
public MyAnimator() { m_StringSize = SizeF.Empty; m_CurrentPosition = new Point(0, 0); m_bDirectionLeft = true; m_bStandStill = false; }
Finally, the ChangeTextLocation method is where the magic happens:
public Point ChangeTextLocation(TLabelAnimated tLabelAnimated) { if (!StandStill) { if (m_StringSize == SizeF.Empty) { using (Graphics gr = tLabelAnimated.CreateGraphics()) { m_StringSize = gr.MeasureString(tLabelAnimated.Text, tLabelAnimated.Font); m_fDelta = tLabelAnimated.Width - m_StringSize.Width; } } if (m_bDirectionLeft) { if (m_CurrentPosition.X > Math.Min(0, m_fDelta)) m_CurrentPosition.X -= KAnimationStep; else m_bDirectionLeft = false; } else { if (m_CurrentPosition.X < Math.Max(0, m_fDelta)) m_CurrentPosition.X += KAnimationStep; else m_bDirectionLeft = true; } } return m_CurrentPosition; } public bool StandStill { get { return m_bStandStill; } set { m_bStandStill = value; } }
Comments? Please write your feedback to support@beemobile4.net.