AnimationSystem.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #include <glm/vec4.hpp>
  3. #include <map>
  4. #include <memory>
  5. #include <optional>
  6. #include <set>
  7. #include <string>
  8. #include <vector>
  9. #include "WallpaperEngine/Data/Model/DynamicValue.h"
  10. #include "WallpaperEngine/Data/Model/PropertyAnimation.h"
  11. namespace WallpaperEngine::Scripting {
  12. using namespace WallpaperEngine::Data::Model;
  13. /** A keyframe curve at a whole frame, wallpaper64.exe's sub_1401A9BC0 */
  14. float sampleAnimationFrame (const std::vector<AnimationKeyframe>& keys, int frame);
  15. /** A keyframe curve at a fractional frame: the two whole frames around it, blended (sub_140171440) */
  16. float evaluateAnimationCurve (const std::vector<AnimationKeyframe>& keys, float frame, float fps, int frameCount);
  17. // one timeline shared by a root animated property and every property naming it as parent (origin driving scale, a
  18. // shader's Radius driving Size)
  19. class AnimationClock {
  20. public:
  21. struct Binding {
  22. DynamicValue* value;
  23. std::shared_ptr<const PropertyAnimation> data;
  24. };
  25. struct FiredEvent {
  26. std::string name;
  27. float frame;
  28. };
  29. AnimationClock (int id, DynamicValue& rootValue, std::shared_ptr<const PropertyAnimation> definition);
  30. [[nodiscard]] int getId () const { return m_id; }
  31. [[nodiscard]] DynamicValue& getRootValue () const { return *m_rootValue; }
  32. [[nodiscard]] const PropertyAnimation& getDefinition () const { return *m_definition; }
  33. [[nodiscard]] float getFrame () const { return m_frame; }
  34. [[nodiscard]] bool isPlaying () const { return m_playing; }
  35. [[nodiscard]] float getRate () const { return m_rate; }
  36. void addBinding (DynamicValue& value, std::shared_ptr<const PropertyAnimation> data);
  37. /** IAnimation.play (sub_1401707F0): a single animation that ran out starts over */
  38. void play ();
  39. void pause () { m_playing = false; }
  40. void stop ();
  41. void setRate (float rate) { m_rate = rate; }
  42. void setFrame (float frame);
  43. /** Advances the timeline, applies it to every bound value and returns the events it crossed */
  44. std::vector<FiredEvent> tick (float deltaSeconds);
  45. void applyCurrentFrame ();
  46. private:
  47. void collectEvents (float from, float to, bool includeFrom, std::vector<FiredEvent>& out) const;
  48. void applyBinding (const Binding& binding) const;
  49. int m_id;
  50. DynamicValue* m_rootValue;
  51. std::shared_ptr<const PropertyAnimation> m_definition;
  52. std::vector<Binding> m_bindings;
  53. float m_frame = 0.0f;
  54. float m_rate = 1.0f;
  55. // only ever flips for mirror mode
  56. float m_direction = 1.0f;
  57. bool m_playing = true;
  58. /** a single animation reached its end (timeline flag 0x40000000, sub_1401A9F60) */
  59. bool m_ended = false;
  60. };
  61. class AnimationSystem {
  62. public:
  63. struct PendingEvent {
  64. int clock;
  65. std::string name;
  66. float frame;
  67. };
  68. AnimationSystem ();
  69. ~AnimationSystem ();
  70. AnimationSystem (const AnimationSystem&) = delete;
  71. AnimationSystem& operator= (const AnimationSystem&) = delete;
  72. [[nodiscard]] int getId () const { return m_id; }
  73. static AnimationSystem* find (int id);
  74. // group scopes the parent/children links (one object's transform, or one effect pass's constants)
  75. void add (const std::string& group, const std::string& key, DynamicValue& value);
  76. /** Drops a not-yet-linked registration, used when a later registration supersedes it */
  77. void remove (const DynamicValue& value);
  78. /** Forgets everything registered for one object (its properties, effect constants and animation layers) with the
  79. * timelines that were built from it, for an object whose values are about to be freed */
  80. void removeObject (int objectId);
  81. void tick (float deltaSeconds);
  82. std::vector<PendingEvent> takeEvents ();
  83. AnimationClock* clockOf (const DynamicValue& value);
  84. AnimationClock* clock (int id);
  85. // getAnimation (name) like 2.8.42's engine slot 15 (sub_14018DB00): the first animated property, in load order,
  86. // whose animation or property is name, in objectGroup ("obj<id>") or any object without one
  87. AnimationClock* findByName (const std::string& name, const std::optional<std::string>& objectGroup);
  88. private:
  89. struct Entry {
  90. std::string group;
  91. std::string key;
  92. DynamicValue* value;
  93. };
  94. void link ();
  95. int m_id;
  96. bool m_linked = false;
  97. std::vector<Entry> m_pending;
  98. /** every linked entry, in the order they were added */
  99. std::vector<Entry> m_entries;
  100. std::map<std::string, std::map<std::string, DynamicValue*>> m_groups;
  101. std::set<const DynamicValue*> m_bound;
  102. std::vector<std::unique_ptr<AnimationClock>> m_clocks;
  103. std::map<const DynamicValue*, AnimationClock*> m_byValue;
  104. std::vector<PendingEvent> m_events;
  105. };
  106. } // namespace WallpaperEngine::Scripting