Polyphase Game Engine
Loading...
Searching...
No Matches
SpriteAnimPlayback.h
Go to the documentation of this file.
1#pragma once
2
3#include "PolyphaseAPI.h"
4#include "AssetRef.h"
6#include "ScriptFunc.h"
7
8#include "glm/glm.hpp"
9
10#include <functional>
11#include <string>
12#include <unordered_map>
13#include <vector>
14
15class Texture;
16
17// One playable animation entry — backed by a SpriteAnimation asset
18// (sourceAsset != null) OR by a runtime-built texture list.
20{
21 std::string name;
23
24 // Used only when sourceAsset is null (script-built).
25 std::vector<TextureRef> frames;
26 float fps = 12.0f;
27 bool loop = true;
28};
29
30// Reusable sprite-animation playback core. Owns the registry, current frame,
31// elapsed time, and the public play/pause/stop API. No knowledge of nodes,
32// signals, or rendering — those concerns live in the host node (SpriteAnimator,
33// AnimatedWidget, AnimatedSprite3D) which calls Tick(dt) and reads the output
34// queries each frame.
35//
36// Frame-changed and animation-end callbacks are passed into Tick rather than
37// stored, so the host can route them to its own EmitSignal / property updates
38// without this struct having to know about Node.
40{
41public:
42 // Property-backed
43 std::vector<SpriteAnimationRef> mAnimations;
44 std::string mDefaultAnimation;
45 bool mAutoPlay = true;
46 bool mLoopOverride = false;
47 float mPlaybackSpeed = 1.0f;
48
49 // Runtime state
50 std::unordered_map<std::string, SpriteAnimPlaybackEntry> mRegistry;
51 std::string mCurrentName;
52 int32_t mCurrentFrame = 0;
53 float mElapsed = 0.0f;
54 bool mPlaying = false;
55 bool mRegistryDirty = true;
56
57 using FrameCallback = std::function<void(int32_t frameIndex)>;
58 using AnimEndCallback = std::function<void(const std::string& animName)>;
59 using AnimStartCallback = std::function<void(const std::string& animName)>;
60
61 // Advances state by deltaTime. Calls onFrameChanged whenever mCurrentFrame
62 // increments, and onAnimationEnd when a non-looping clip finishes.
63 void Tick(float deltaTime,
64 const FrameCallback& onFrameChanged = nullptr,
65 const AnimEndCallback& onAnimationEnd = nullptr);
66
67 void Play();
68 void Pause();
69 void Stop();
70 void PlayAnimation(const std::string& name,
71 const AnimStartCallback& onAnimationStart = nullptr);
72
73 // Jump to a specific frame in the current animation. Clamps to [0..N-1]
74 // when out of range. Resets the inter-frame elapsed timer so playback
75 // (if running) resumes cleanly from the new frame. Does NOT change
76 // mPlaying — caller controls Play/Pause separately. Returns true if the
77 // frame index actually changed (useful for skipping a redundant rebind).
78 bool SetFrame(int32_t frameIndex);
79
80 // Play forward until reaching targetFrame, then optionally pause and call
81 // onFinished. If targetFrame is already the current frame, fires
82 // immediately (without playing a full lap). Cancels any prior AnimateTo.
83 // Implicitly Play()s so it starts even if the animator was paused.
84 // Returns true if a target was set; false if the target couldn't be
85 // resolved (no animation playing, frameCount == 0, etc.).
86 bool AnimateTo(int32_t targetFrame, bool pauseOnFinished, const ScriptFunc& onFinished);
87 bool AnimateToProgress(float progress, bool pauseOnFinished, const ScriptFunc& onFinished);
88 void CancelAnimateTo();
89 bool HasAnimateToTarget() const { return mAnimateTo.active; }
90
91 void RebuildRegistry();
92 void AddAnimationAsset(SpriteAnimation* asset);
93 void AddAnimationByPath(const std::string& assetPath);
94
95 void CreateAnimation(const std::string& name);
96 void CreateAnimation(const std::string& name, const std::vector<Texture*>& frames);
97 void AddImage(const std::string& name, Texture* tex);
98 void AddImage(const std::string& name, const std::string& path);
99 void AddImages(const std::string& name, const std::vector<std::string>& paths);
100
101 void RemoveAnimation(const std::string& name);
102 bool HasAnimation(const std::string& name) const;
103
104 // Output queries — values are tuned for the engine's Quad encoding
105 // (final = (baseUV + offset) * scale). UVRect is the raw (u0,v0,u1,v1)
106 // for material-vec4 use cases.
107 Texture* GetCurrentTexture() const;
108 glm::vec2 GetCurrentUVScale() const;
109 glm::vec2 GetCurrentUVOffset() const;
110 glm::vec4 GetCurrentUVRect() const;
111
112 int32_t GetCurrentFrameCount() const;
113
114 // Smooth playback progress in [0, 1] across the current clip.
115 // 0 = start of frame 0, ~1 = end of last frame. Sub-frame interpolated
116 // via mElapsed so progress bars look smooth at low FPS. Snaps to 1.0
117 // when a non-looping clip has finished. Returns 0 when no clip is playing.
118 float GetProgress() const;
119
120 // AnimateTo state — exposed so derived/host nodes can inspect / clear.
122 {
123 bool active = false;
124 int32_t targetFrame = -1;
125 bool pauseOnFinished = true;
127 };
129
130private:
131 SpriteAnimPlaybackEntry* FindEntry(const std::string& name);
132 const SpriteAnimPlaybackEntry* FindEntry(const std::string& name) const;
133 int32_t EntryFrameCount(const SpriteAnimPlaybackEntry& entry) const;
134 float EntryFps(const SpriteAnimPlaybackEntry& entry) const;
135 bool EntryLoop(const SpriteAnimPlaybackEntry& entry) const;
136
137 Texture* ResolveCurrentTexture() const;
138 bool ResolveCurrentUV(glm::vec2& outUV0, glm::vec2& outUV1) const;
139};
Export macros for Polyphase Engine symbols.
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Definition AssetRef.h:18
Definition ScriptFunc.h:10
Definition SpriteAnimPlayback.h:40
AnimateToState mAnimateTo
Definition SpriteAnimPlayback.h:128
std::vector< SpriteAnimationRef > mAnimations
Definition SpriteAnimPlayback.h:43
std::function< void(int32_t frameIndex)> FrameCallback
Definition SpriteAnimPlayback.h:57
std::string mCurrentName
Definition SpriteAnimPlayback.h:51
std::unordered_map< std::string, SpriteAnimPlaybackEntry > mRegistry
Definition SpriteAnimPlayback.h:50
std::function< void(const std::string &animName)> AnimEndCallback
Definition SpriteAnimPlayback.h:58
std::string mDefaultAnimation
Definition SpriteAnimPlayback.h:44
bool HasAnimateToTarget() const
Definition SpriteAnimPlayback.h:89
std::function< void(const std::string &animName)> AnimStartCallback
Definition SpriteAnimPlayback.h:59
Definition SpriteAnimation.h:31
Definition Texture.h:10
Definition SpriteAnimPlayback.h:20
float fps
Definition SpriteAnimPlayback.h:26
std::string name
Definition SpriteAnimPlayback.h:21
SpriteAnimationRef sourceAsset
Definition SpriteAnimPlayback.h:22
bool loop
Definition SpriteAnimPlayback.h:27
std::vector< TextureRef > frames
Definition SpriteAnimPlayback.h:25
Definition SpriteAnimPlayback.h:122
ScriptFunc onFinished
Definition SpriteAnimPlayback.h:126