Understanding VGUI2 Animation

From Valve Developer Community
Revision as of 09:37, 8 April 2011 by Beerdude26 (talk | contribs) (Reorder + how to execute events)
Jump to: navigation, search


VGUI2 has a pretty powerful animation system for animating both HUD elements and VGUI panels. Not that this overview is incomplete: you will find "To do" items at various spots that you may investigate yourself if you so wish.

Architecture Overview

The animation system is defined in AnimationController.cpp, which can be found under vgui2/vgui_controls. It is very extensive, but the main concept is as follows:

  • Add animations through several methods
  • Run these animations each tick, using an interpolator function to calculate the intermediate value.

There are two main ways to add animations, which are script-defined events and dynamic animations, which are done during run-time.


Animation events can be loaded from two sources, both of which are located in the scripts folder:

  • The animation script manifest: hudanimations_manifest.txt. If this isn't found, the latter is loaded:
  • A single script file: HudAnimations.txt.
Note.png Note: Even though it says "Hud" right there in the name, Events can also be defined for VGUI panels.

The manifest expects a KeyValue file, and will load each file in the manifest:

	"file" "scripts/HudAnimations.txt"
	// You can add more here
Tip.png Tip: Neither the hudanimations_manifest.txt nor the HudAnimations.txt file will most likely be present. You can find the latter in the base source engine 2.gcf GCF file.

File Layout

Opening up HudAnimations.txt, you will see many events used in Half-Life 2. Here's the syntax of an event:


Event overview

At the top of the file, you'll see a lovely overview of possible commands and their arguments. Here is an updated overview (there's some new stuff in the code):

  • Animate
    • Arguments: <panel name> <variable> <target value(s)> <interpolator> <start time> <duration>
      • Valid panel names: Any VGUI or HUD panel.
      • Valid variables:
        Note.png Note: Italic values only work for HUD panels and/or weapon selection
        • Unary (1 argument): Xpos, Ypos, Wide, Tall, Blur, Alpha, SelectionAlpha
        • Binary (2 arguments): Position, Size
        • More than two: FgColor, BgColor, TextColor, Ammo2Color
          Tip.png Tip: Color variables can also reference a scheme-defined color instead of an RGBA value, e.g. FgColor "BrightDamagedFg"
        • Unknown: TextScan
        • Any values of the types KeyValues::TYPE_FLOAT and KeyValues::TYPE_COLOR that are accessible when calling RequestInfo on the panel are also modifiable.1
        • Variables defined in code To do: Make a section about this, link it here
      • Valid target values: Any floating-point value. Up to four values are possible. Put multiple values between quotes (e.g. "255 0 0 255" or "512 512")
      • Valid interpolators:
        • Linear
        • Accel - starts moving slow, ends fast
        • Deaccel - starts moving fast, ends slow
        • Spline - very smooth interpolation
        • To do: In AnimationController::ParseScriptFile, the code also supports parsing Pulse and Flicker interpolators. Have a look at what they do.
      • Valid start times: Any floating-point value. Essentially a delay factor.
      • Valid durations: Any floating-point value.
  • RunEvent: starts another event running at the specified time.
    • Arguments: <event name> <start time>
      • Valid event names: Any event defined in the animation files.
  • StopEvent: stops another event that is currently running at the specified time.
    • Arguments: <event name> <start time>
  • StopAnimation: stops all animations referring to the specified variable in the specified panel.
    • Arguments: <panel name> <variable> <start time>
  • StopPanelAnimations: stops all active animations operating on the specified panel.
    • Arguments: <panel name> <start time>
  • SetFont: To do: Look up what this does.
    • Arguments: <panel name> <font parameter> <font name from scheme> <set time>
  • SetTexture: To do: Look up what this does.
    • Arguments: <panel name> <texture ID> <material name> <set time>
  • SetString: To do: Look up what this does.
    • Arguments: <panel name> <string variable name> <value to set> <set time>

Executing Animations

Accessing the controller

First off, you'll need some includes. If you're working in a class that uses the vgui namespace, you can call the animation controller by including vgui_controls/AnimationController.h and calling it like so:


If you're not using the vgui namespace, you'll need to include both clientmode.h and vgui_controls/AnimationController.h, and call the animation controller like so:


Executing Events

An event can be executed using the StartAnimationSequence method, which has two overloads. One simply takes the event name and looks up panels by itself, starting from the root panel, and the other takes the event name and a pointer to a panel that houses the controls that will be animated.

 vgui::GetAnimationController()->StartAnimationSequence( "ShowCommentary" ); // Just the event name
 vgui::GetAnimationController()->StartAnimationSequence( this, "MapZoomToLarge" ); // Event name + parent panel (example from CS:S)

Executing dynamic animations

Events are handy for static animations, but if you want to load any argument on the fly, you'll need dynamic animations. It's surprisingly simple!

Preparing for running animations

The method we are interested in is called RunAnimationCommand, and takes arguments similarly to the events defined above.

Note.png Note: If you're accessing the controller via g_pClientMode, you'll want to call the UpdateAnimations method manually in the OnThink() method of your panel.

The RunAnimationCommand has two overloads: one for a single value, and another for multiple values. Unfortunately, the multiple-valued one uses the Color class to load any values, which means the values will be truncated to integers AND wrap around very quickly! To mitigate this, let's create a new overload:

Copy this under the other RunAnimationCommand overloads in AnimationController.cpp:

 // Purpose: Runs a custom command from code, not from a script file
 void AnimationController::RunAnimationCommand(vgui::Panel *panel, const char *variable, PublicValue_t targetValue, float startDelaySeconds, float duration, Interpolators_e interpolator, float animParameter /* = 0 */ )
 	// clear any previous animations of this variable
 	UtlSymId_t var = g_ScriptSymbols.AddString(variable);
 	RemoveQueuedAnimationByType(panel, var, UTL_INVAL_SYMBOL);
 	// build a new animation
 	AnimCmdAnimate_t animateCmd;
 	memset(&animateCmd, 0, sizeof(animateCmd));
 	animateCmd.panel = 0;
 	animateCmd.variable = var;
 	animateCmd.target.a = targetValue.a;
 	animateCmd.target.b = targetValue.b;
 	animateCmd.target.c = targetValue.c;
 	animateCmd.target.d = targetValue.d;
 	animateCmd.interpolationFunction = interpolator;
 	animateCmd.interpolationParameter = animParameter;
 	animateCmd.startTime = startDelaySeconds;
 	animateCmd.duration = duration;
 	// start immediately
 	StartCmd_Animate(panel, 0, animateCmd);

And copy this above the prototype of RunAnimationCommand in AnimationController.h:

 struct PublicValue_t
 	float a, b, c, d;
 void RunAnimationCommand(vgui::Panel *panel, const char *variable, PublicValue_t targetValue, float startDelaySeconds, float durationSeconds, Interpolators_e interpolator, float animParameter = 0 );
Note.png Note: I know, there's a perfectly fine Value_t struct that we can also use, but when I made it public, the entire class went haywire. Feel free to find a correct implementation.

Running animations

Now we're in business! You can call animations though the controller very easily. Here's an example as to how you would programmatically change the size of an ImagePanel:

Let's say we have an ImagePanel called "overviewImage ", which is defined in our panel's .res file. First, we get the panel:

 m_pOverviewImage = dynamic_cast<ImagePanel*>( FindChildByName( "overviewImage ", false ) );
Note.png Note: Put this in the class constructor right after you've loaded the Resource file. If you're confused, read this: Loading a Resource File.

You can then resize the ImagePanel as follows:

 AnimationController::PublicValue_t newSize;
 newSize.a = 256;
 newSize.b = 256;
 newSize.c = 0; // Unused
 newSize.d = 0; // Unused

 float totalAnimationTime = 3.0f;
 vgui::GetAnimationController()->RunAnimationCommand( m_pOverviewImage , "size", newSize, 0.0f, totalAnimationTime , vgui::AnimationController::INTERPOLATOR_LINEAR );


  1. AnimationController::GetValue(ActiveAnimation_t& anim, Panel *panel, UtlSymId_t var) houses the according code.

To do: Not done yet!