tb_animation_utils.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_ANIMATION_UTILS_H
  6. #define TB_ANIMATION_UTILS_H
  7. #include "animation/tb_animation.h"
  8. namespace tb {
  9. // TBAnimatedFloat - A animated float value
  10. class TBAnimatedFloat : public TBAnimationObject
  11. {
  12. public:
  13. float src_val;
  14. float dst_val;
  15. float current_progress;
  16. public:
  17. // For safe typecasting
  18. TBOBJECT_SUBCLASS(TBAnimatedFloat, TBAnimationObject);
  19. TBAnimatedFloat( float initial_value,
  20. ANIMATION_CURVE animation_curve = ANIMATION_DEFAULT_CURVE,
  21. double animation_duration = ANIMATION_DEFAULT_DURATION)
  22. : src_val(initial_value), dst_val(initial_value), current_progress(0)
  23. {
  24. TBAnimationObject::animation_curve = animation_curve;
  25. TBAnimationObject::animation_duration = animation_duration;
  26. }
  27. float GetValue() { return src_val + (dst_val - src_val) * current_progress; }
  28. void SetValueAnimated(float value) { src_val = GetValue(); dst_val = value; TBAnimationManager::StartAnimation(this, animation_curve, animation_duration); }
  29. void SetValueImmediately(float value) { TBAnimationManager::AbortAnimation(this, false); src_val = dst_val = value; OnAnimationUpdate(1.0f); }
  30. virtual void OnAnimationStart() { current_progress = 0; }
  31. virtual void OnAnimationUpdate(float progress) { current_progress = progress; }
  32. virtual void OnAnimationStop(bool aborted) {}
  33. };
  34. // TBFloatAnimator - Animates a external float value, which address is given in the constructor.
  35. class TBFloatAnimator : public TBAnimatedFloat
  36. {
  37. public:
  38. float *target_value;
  39. public:
  40. // For safe typecasting
  41. TBOBJECT_SUBCLASS(TBFloatAnimator, TBAnimationObject);
  42. TBFloatAnimator( float *target_value,
  43. ANIMATION_CURVE animation_curve = ANIMATION_DEFAULT_CURVE,
  44. double animation_duration = ANIMATION_DEFAULT_DURATION)
  45. : TBAnimatedFloat(*target_value), target_value(target_value) {}
  46. virtual void OnAnimationStart() { TBAnimatedFloat::OnAnimationStart(); *target_value = GetValue(); }
  47. virtual void OnAnimationUpdate(float progress) { TBAnimatedFloat::OnAnimationUpdate(progress); *target_value = GetValue(); }
  48. };
  49. }; // namespace tb
  50. #endif // TB_ANIMATION_UTILS_H