AnimationSystem.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #include <glm/vec4.hpp>
  3. #include <map>
  4. #include <memory>
  5. #include <set>
  6. #include <string>
  7. #include <vector>
  8. #include "WallpaperEngine/Data/Model/DynamicValue.h"
  9. #include "WallpaperEngine/Data/Model/PropertyAnimation.h"
  10. namespace WallpaperEngine::Scripting {
  11. using namespace WallpaperEngine::Data::Model;
  12. /** A keyframe curve at a whole frame, wallpaper64.exe's sub_1401A9BC0 */
  13. float sampleAnimationFrame (const std::vector<AnimationKeyframe>& keys, int frame);
  14. /** A keyframe curve at a fractional frame: the two whole frames around it, blended (sub_140171440) */
  15. float evaluateAnimationCurve (const std::vector<AnimationKeyframe>& keys, float frame, float fps, int frameCount);
  16. /**
  17. * One playback timeline shared by a root animated property and every property that lists it as
  18. * its parent (e.g. an origin animation driving the scale one, or a shader's Radius driving Size).
  19. */
  20. class AnimationClock {
  21. public:
  22. struct Binding {
  23. DynamicValue* value;
  24. std::shared_ptr<const PropertyAnimation> data;
  25. };
  26. struct FiredEvent {
  27. std::string name;
  28. float frame;
  29. };
  30. AnimationClock (int id, DynamicValue& rootValue, std::shared_ptr<const PropertyAnimation> definition);
  31. [[nodiscard]] int getId () const { return m_id; }
  32. [[nodiscard]] DynamicValue& getRootValue () const { return *m_rootValue; }
  33. [[nodiscard]] const PropertyAnimation& getDefinition () const { return *m_definition; }
  34. [[nodiscard]] float getFrame () const { return m_frame; }
  35. [[nodiscard]] bool isPlaying () const { return m_playing; }
  36. [[nodiscard]] float getRate () const { return m_rate; }
  37. void addBinding (DynamicValue& value, std::shared_ptr<const PropertyAnimation> data);
  38. void play () { m_playing = true; }
  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. };
  59. class AnimationSystem {
  60. public:
  61. struct PendingEvent {
  62. int clock;
  63. std::string name;
  64. float frame;
  65. };
  66. AnimationSystem ();
  67. ~AnimationSystem ();
  68. AnimationSystem (const AnimationSystem&) = delete;
  69. AnimationSystem& operator= (const AnimationSystem&) = delete;
  70. [[nodiscard]] int getId () const { return m_id; }
  71. static AnimationSystem* find (int id);
  72. /**
  73. * Registers an animated property. `group` scopes the parent/children links between siblings
  74. * (one object's transform properties, or one effect pass's constants).
  75. */
  76. void add (const std::string& group, const std::string& key, DynamicValue& value);
  77. /** Drops a not-yet-linked registration, used when a later registration supersedes it */
  78. void remove (const DynamicValue& value);
  79. void tick (float deltaSeconds);
  80. std::vector<PendingEvent> takeEvents ();
  81. AnimationClock* clockOf (const DynamicValue& value);
  82. AnimationClock* clock (int id);
  83. private:
  84. struct Entry {
  85. std::string group;
  86. std::string key;
  87. DynamicValue* value;
  88. };
  89. void link ();
  90. int m_id;
  91. bool m_linked = false;
  92. std::vector<Entry> m_pending;
  93. std::map<std::string, std::map<std::string, DynamicValue*>> m_groups;
  94. std::set<const DynamicValue*> m_bound;
  95. std::vector<std::unique_ptr<AnimationClock>> m_clocks;
  96. std::map<const DynamicValue*, AnimationClock*> m_byValue;
  97. std::vector<PendingEvent> m_events;
  98. };
  99. } // namespace WallpaperEngine::Scripting