Skip to content

ui::AnimatedVisualSource

Header: winui/render/AnimatedVisualSource.hpp.

The content an AnimatedVisualPlayer plays: an animation with a fixed size and length whose render_frame() draws any point of it through Renderer.

Implement it: draw your animation in render_frame() and hand it to an AnimatedVisualPlayer.

A source has a fixed natural size and total duration, and render_frame() draws the frame at a progress point. AnimatedVisualPlayer and AnimatedIcon hold a non-owning const AnimatedVisualSource*, so the caller keeps the source alive at least as long as any player or icon points at it. render_frame() is const and depends only on progress, so one instance can serve many players at once.

Example:

class Pulse final : public ui::AnimatedVisualSource
{
public:
    [[nodiscard]] ui::Size natural_size() const override { return {24.0f, 24.0f}; }
    [[nodiscard]] ui::Duration duration() const override { return ui::Duration{900.0}; }
    void render_frame(ui::Renderer& r, const ui::Rect& bounds, double progress) const override
    {
        const float radius = std::min(bounds.width, bounds.height) * 0.5f * static_cast<float>(progress);
        if (radius > 0.0f)
        {
            r.fill_ellipse(bounds.center(), radius, radius, ui::Color::from_rgba(0, 120, 215));
        }
    }
};

static const Pulse pulse; // must outlive the player
window.set_content(ui::AnimatedVisualPlayer{.source = &pulse, .width = 96, .height = 96});

~AnimatedVisualSource

virtual ~AnimatedVisualSource() = default

natural_size

virtual Size natural_size() const = 0

The unstretched size this source draws at, in DIPs. AnimatedVisualPlayer fits it into its bounds per its Stretch with fit_stretch(), as Image does for a bitmap.

duration

virtual Duration duration() const = 0

How long one full [0,1] progress sweep takes, in milliseconds like every other duration in the library (Theme::anim_normal, ToolTipControl::delay()). A source made from an animation file reads it from the file. A source with no length, such as a pose that never moves, can return 0, which AnimatedVisualPlayerControl::duration() handles.

render_frame

virtual void render_frame(Renderer& renderer, const Rect& bounds, double progress) const = 0

Draws one frame at normalized progress (0 = animation start, 1 = animation end) into bounds. It is called from render(), so what it draws may depend only on progress and the source's own fixed state, and it changes nothing, like every Element::render().

available

virtual bool available() const

Whether this source can be shown at all. A source that depends on something the running system lacks returns false, and AnimatedVisualPlayer shows its fallback content instead.