Difference between revisions of "Understanding VGUI2 Animation"
Beerdude26 (talk | contribs) |
Beerdude26 (talk | contribs) m (typo) |
||
Line 90: | Line 90: | ||
=== Preparing for running animations === | === Preparing for running animations === | ||
− | The method we are interested in is called '''RunAnimationCommand'', and takes arguments similarly to the events defined above. | + | The method we are interested in is called '''RunAnimationCommand''', and takes arguments similarly to the events defined above. |
{{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.}} | {{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.}} |
Revision as of 09:12, 8 April 2011
Contents
Introduction
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.
Events
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.

The manifest expects a KeyValue file, and will load each file in the manifest:
"HudAnimationManifest" { "file" "scripts/HudAnimations.txt" // You can add more here }

File Layout
Opening up HudAnimations.txt, you will see many events used in Half-Life 2. Here's the syntax of an event:
event <EVENTNAME> { <COMMAND> <ARGUMENTS> }
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: 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: 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
- To do: In AnimationController::ParseScriptFile, the code also supports parsing Spline, 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.
- Arguments: <panel name> <variable> <target value(s)> <interpolator> <start time> <duration>
- RunEvent: starts another event running at the specified time.
- Arguments: <event name> <start time>
- Valid event names: Any event defined in the animation files.
- Arguments: <event name> <start time>
- 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>
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!
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:
vgui::GetAnimationController()
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:
g_pClientMode->GetViewportAnimationController()
Preparing for running animations
The method we are interested in is called RunAnimationCommand, and takes arguments similarly to the events defined above.

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 );

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 ) );

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 );
Footnotes
- AnimationController::GetValue(ActiveAnimation_t& anim, Panel *panel, UtlSymId_t var) houses the according code.
To do: Not done yet!