Understanding VGUI2 Animation

From Valve Developer Community
Jump to: navigation, search
English (en)русский (ru)
... Icon-Important.png

Introduction

VGUI2 has a pretty powerful animation system for animating both HUD elements and VGUI panels. Here is a relatively complete overview of what you can do with it.

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.
Note.pngNote: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:

"HudAnimationManifest"
{
	"file" "scripts/HudAnimations.txt"
	// You can add more here
}
Tip.pngTip: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 <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.pngNote: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.pngTip: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
      • 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
        • Pulse - uses a sine pulse to interpolate. Expects a frequency value.
        • Flicker - uses the PRNG to determine whether to be at original value or at the final value, which causes a flicker effect. Expects a noise amount value.
          Tip.pngTip: Both Pulse and Flicker take an extra value, so be sure to put one in the scripts.
      • 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:
    Blank image.pngTodo: Look up what this does.
    • Arguments: <panel name> <font parameter> <font name from scheme> <set time>
  • SetTexture:
    Blank image.pngTodo: Look up what this does.
    • Arguments: <panel name> <texture ID> <material name> <set time>
  • SetString:
    Blank image.pngTodo: 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:

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

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)

Defining animation variables in code

Defining a variable that can be used in the event files is easy, and uses the following macro:

 #define CPanelAnimationVar( type, name, scriptname, defaultvalue )\
 	CPanelAnimationVarAliasType( type, name, scriptname, defaultvalue, #type )

Looking through code references, this appears to work fine for: 2

  • Colors: CPanelAnimationVar( Color, m_cChangableColor, "Changable_Color", "0 0 0 0" );
  • Fonts: CPanelAnimationVar( vgui::HFont, m_hChangableFont, "FontOverride", "DefaultSmall" );
  • Floats: CPanelAnimationVar( float, m_fChangableFloat, "FloatAnimation", "0" );
  • Integers: CPanelAnimationVar( int, m_iChangableInt, "IntOverride", "0" );
  • Booleans: CPanelAnimationVar( bool, m_bChangableBool, "BoolOverride", "0" );

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.pngNote: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.pngNote: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.pngNote: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 );


Footnotes

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

2. You may also have noticed that CPanelAnimationVarAliasType is also a define, and takes an extra parameter: a type alias.

 // Only the first line
 #define CPanelAnimationVarAliasType( type, name, scriptname, defaultvalue, typealias ) \

This type alias is a string, and is used when converting the string-based default values into proper ones, and can also be used for custom processing. This processing is done in Panel::InitPropertyConverters(). This might be a good place to start if you want to extend this functionality to other variable types.