#pragma once #include #include #include #include #include #include #include #include "WallpaperEngine/Data/Model/DynamicValue.h" #include "WallpaperEngine/Data/Model/PropertyAnimation.h" namespace WallpaperEngine::Scripting { using namespace WallpaperEngine::Data::Model; /** A keyframe curve at a whole frame, wallpaper64.exe's sub_1401A9BC0 */ float sampleAnimationFrame (const std::vector& keys, int frame); /** A keyframe curve at a fractional frame: the two whole frames around it, blended (sub_140171440) */ float evaluateAnimationCurve (const std::vector& keys, float frame, float fps, int frameCount); // one timeline shared by a root animated property and every property naming it as parent (origin driving scale, a // shader's Radius driving Size) class AnimationClock { public: struct Binding { DynamicValue* value; std::shared_ptr data; }; struct FiredEvent { std::string name; float frame; }; AnimationClock (int id, DynamicValue& rootValue, std::shared_ptr definition); [[nodiscard]] int getId () const { return m_id; } [[nodiscard]] DynamicValue& getRootValue () const { return *m_rootValue; } [[nodiscard]] const PropertyAnimation& getDefinition () const { return *m_definition; } [[nodiscard]] float getFrame () const { return m_frame; } [[nodiscard]] bool isPlaying () const { return m_playing; } [[nodiscard]] float getRate () const { return m_rate; } void addBinding (DynamicValue& value, std::shared_ptr data); /** IAnimation.play (sub_1401707F0): a single animation that ran out starts over */ void play (); void pause () { m_playing = false; } void stop (); void setRate (float rate) { m_rate = rate; } void setFrame (float frame); /** Advances the timeline, applies it to every bound value and returns the events it crossed */ std::vector tick (float deltaSeconds); void applyCurrentFrame (); private: void collectEvents (float from, float to, bool includeFrom, std::vector& out) const; void applyBinding (const Binding& binding) const; int m_id; DynamicValue* m_rootValue; std::shared_ptr m_definition; std::vector m_bindings; float m_frame = 0.0f; float m_rate = 1.0f; // only ever flips for mirror mode float m_direction = 1.0f; bool m_playing = true; /** a single animation reached its end (timeline flag 0x40000000, sub_1401A9F60) */ bool m_ended = false; }; class AnimationSystem { public: struct PendingEvent { int clock; std::string name; float frame; }; AnimationSystem (); ~AnimationSystem (); AnimationSystem (const AnimationSystem&) = delete; AnimationSystem& operator= (const AnimationSystem&) = delete; [[nodiscard]] int getId () const { return m_id; } static AnimationSystem* find (int id); // group scopes the parent/children links (one object's transform, or one effect pass's constants) void add (const std::string& group, const std::string& key, DynamicValue& value); /** Drops a not-yet-linked registration, used when a later registration supersedes it */ void remove (const DynamicValue& value); /** Forgets everything registered for one object (its properties, effect constants and animation layers) with the * timelines that were built from it, for an object whose values are about to be freed */ void removeObject (int objectId); void tick (float deltaSeconds); std::vector takeEvents (); AnimationClock* clockOf (const DynamicValue& value); AnimationClock* clock (int id); // getAnimation (name) like 2.8.42's engine slot 15 (sub_14018DB00): the first animated property, in load order, // whose animation or property is name, in objectGroup ("obj") or any object without one AnimationClock* findByName (const std::string& name, const std::optional& objectGroup); private: struct Entry { std::string group; std::string key; DynamicValue* value; }; void link (); int m_id; bool m_linked = false; std::vector m_pending; /** every linked entry, in the order they were added */ std::vector m_entries; std::map> m_groups; std::set m_bound; std::vector> m_clocks; std::map m_byValue; std::vector m_events; }; } // namespace WallpaperEngine::Scripting