|
Tweeny 4.1.0
A Tweening library for modern C++
|
This document is the manual for Tweeny. It walks you through all the important steps when creating and controlling tweens.
Tweeny uses a fluent builder API to create tweens. The tweeny::from function returns a builder object, not a tween directly. You configure the interpolation using the builder's methods (to(), via(), during()) and then call build() to create the actual tween object.
Most commonly, you'll chain all calls together in a single expression:
Once built, a tween's keyframes, durations, and easing functions are immutable. However, the tween's current state (frame position and value) changes as you navigate it with step(), seek(), or jump().
Tweeny can interpolate single values, multiple values, or values of different types. The types you pass to tweeny::from determine the tween's type signature, which affects all subsequent builder methods and the tween's return values:
Every tween needs at least a starting point and an ending point. tweeny::from specifies the starting values, and you must call to() at least once to specify target values. This requirement is enforced at compile time - if you try to build a tween without calling to(), you'll get a compilation error.
The number and types of arguments to to() must match those passed to from():
Every interpolation segment needs a duration. The during() method specifies how many units (typically frames or milliseconds) the interpolation should take to reach the target values. The duration is always an unsigned 32-bit integer (uint32_t).
Unlike a missing to(), omitting during() is not a compile-time error. The segment’s duration defaults to 0, so both keyframes sit at the same frame: progress() is always 1.0, and peek() / step() / seek() / jump() keep returning the starting** values (never the target). Calling during(0U) is the same. Always call during() with a positive duration for each segment you intend to animate.
For multi-value tweens, you can specify either:
When using per-value durations, the total interpolation length is determined by the longest duration. In the example above, the first value reaches its target at frame 30, the second at frame 60, and the third at frame 90. The interpolation is complete when all values have reached their targets (at frame 90).
Easing functions control how values interpolate between keyframes. They take a progress value (0.0 to 1.0), a start value, and an end value, then return the interpolated value at that progress. For example, a linear easing is simply:
By default, tweens use easing::def (an alias of easing::linear). You can change this with the via() method, which must be called after to(). Tweeny includes 30+ built-in easing functions:
Like during(), you can specify easings per-value or use the same for all values:
See tweeny::easing namespace documentation for all available easings, or visit http://easings.net for visualizations.
When the easing must be chosen at runtime (for example from configuration or user input), use easing::byName(). It returns a callable usable with via():
Names match the identifier names exactly (linear, cubicInOut, bounceOut, …). Unknown names throw std::invalid_argument at the call to byName().
You can provide custom easing functions as any callable matching the signature T(float, T, T):
For heterogeneous tweens, each easing must match its corresponding value type:
You can create complex interpolations by chaining multiple keyframes together. Each call to to() adds a new keyframe, and subsequent calls to during() and via() configure that specific segment:
The resulting tween seamlessly transitions through all keyframes. Navigation methods like step() and seek() work transparently across keyframe boundaries.
Once built, a tween can be navigated in three ways: stepping, seeking, and jumping.
Stepping moves the tween by a relative amount (delta). This is the primary method for frame-by-frame interpolation in game loops:
step() accepts a signed 32-bit integer (int32_t). Positive values move forward, negative values move backward:
Seeking jumps to an absolute frame position. Useful for scrubbing or jumping to specific points:
seek() accepts an unsigned 32-bit integer (uint32_t) representing the absolute frame number. Values are clamped to the valid frame range, from the first keyframe's position to the last keyframe's position.
Jumping moves directly to a keyframe by its index (0-based). This is useful for multi-point interpolations:
All navigation methods (step(), seek(), jump()) return the current interpolated value(s):
Use peek() to query the current value without modifying the tween's state:
Use progress() to query how far the tween has advanced as a normalized float in [0, 1]. Like peek(), it does not mutate the tween: peek() answers “what value?”, progress() answers “how far in time?”.
Tweeny provides an event system for reacting to interpolation lifecycle events. Register callbacks using the tween::on() method with an event type tag.
Available event types:
When several apply to the same call, they fire in this order: specific (step|seek|jump) → update → complete (when complete applies).
Most callbacks receive a reference to the tween and return an event::response:
Keyframe events receive additional data through an event struct:
The return value controls whether a callback stays registered:
Any callable matching the required signature can be used - lambdas, function pointers, functors, etc:
Enjoy using Tweeny!