ElementAnimation.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #pragma once
  2. #include "../../Include/RmlUi/Core/Header.h"
  3. #include "../../Include/RmlUi/Core/Property.h"
  4. #include "../../Include/RmlUi/Core/Tween.h"
  5. namespace Rml {
  6. struct AnimationKey {
  7. AnimationKey(float time, const Property& property, Tween tween) : time(time), property(property), tween(tween) {}
  8. float time; // Local animation time (Zero means the time when the animation iteration starts)
  9. Property property;
  10. Tween tween; // Tweening between the previous and this key. Ignored for the first animation key.
  11. };
  12. // The origin is tracked for determining its behavior when adding and removing animations.
  13. // User: Animation started by the Element API
  14. // Animation: Animation started by the 'animation' property
  15. // Transition: Animation started by the 'transition' property
  16. enum class ElementAnimationOrigin : uint8_t { User, Animation, Transition };
  17. class ElementAnimation {
  18. private:
  19. PropertyId property_id = PropertyId::Invalid;
  20. float duration = 0; // for a single iteration
  21. int num_iterations = 0; // -1 for infinity
  22. bool alternate_direction = false; // between iterations
  23. Vector<AnimationKey> keys;
  24. double last_update_world_time = 0;
  25. float time_since_iteration_start = 0;
  26. int current_iteration = 0;
  27. bool reverse_direction = false;
  28. bool animation_complete = false;
  29. ElementAnimationOrigin origin = ElementAnimationOrigin::User;
  30. bool InternalAddKey(float time, const Property& property, Element& element, Tween tween);
  31. float GetInterpolationFactorAndKeys(int* out_key0, int* out_key1) const;
  32. public:
  33. ElementAnimation() {}
  34. ElementAnimation(PropertyId property_id, ElementAnimationOrigin origin, const Property& current_value, Element& element, double start_world_time,
  35. float duration, int num_iterations, bool alternate_direction);
  36. bool AddKey(float target_time, const Property& property, Element& element, Tween tween, bool extend_duration);
  37. Property UpdateAndGetProperty(double time, Element& element);
  38. PropertyId GetPropertyId() const { return property_id; }
  39. float GetDuration() const { return duration; }
  40. bool IsComplete() const { return animation_complete; }
  41. bool IsTransition() const { return origin == ElementAnimationOrigin::Transition; }
  42. bool IsInitalized() const { return !keys.empty(); }
  43. float GetInterpolationFactor() const { return GetInterpolationFactorAndKeys(nullptr, nullptr); }
  44. ElementAnimationOrigin GetOrigin() const { return origin; }
  45. };
  46. } // namespace Rml