tb_widget_animation.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_WIDGET_ANIMATION_H
  6. #define TB_WIDGET_ANIMATION_H
  7. #include "../tb_widgets_listener.h"
  8. #include "../animation/tb_animation.h"
  9. namespace tb {
  10. /** Don't use 0.0 for opacity animations since that may break focus code.
  11. At the moment a window should appear and start fading in from opacity 0,
  12. it would also attempt setting the focus to it, but if opacity is 0 it will
  13. think focus should not be set in that window and fail. */
  14. #define TB_ALMOST_ZERO_OPACITY 0.001f
  15. /** Base class for widget animations. This animation object will
  16. be deleted automatically if the widget is deleted. */
  17. class TBWidgetAnimationObject : public TBAnimationObject, public TBLinkOf<TBWidgetAnimationObject>
  18. {
  19. public:
  20. // For safe typecasting
  21. TBOBJECT_SUBCLASS(TBWidgetAnimationObject, TBAnimationObject);
  22. TBWidgetAnimationObject(TBWidget *widget);
  23. virtual ~TBWidgetAnimationObject();
  24. public:
  25. TBWidget *m_widget;
  26. };
  27. /** Animate the opacity of the target widget. */
  28. class TBWidgetAnimationOpacity : public TBWidgetAnimationObject
  29. {
  30. public:
  31. // For safe typecasting
  32. TBOBJECT_SUBCLASS(TBWidgetAnimationOpacity, TBWidgetAnimationObject);
  33. TBWidgetAnimationOpacity(TBWidget *widget, float src_opacity, float dst_opacity, bool die);
  34. virtual void OnAnimationStart();
  35. virtual void OnAnimationUpdate(float progress);
  36. virtual void OnAnimationStop(bool aborted);
  37. private:
  38. float m_src_opacity;
  39. float m_dst_opacity;
  40. bool m_die;
  41. };
  42. /** Animate the rectangle of the target widget. */
  43. class TBWidgetAnimationRect : public TBWidgetAnimationObject
  44. {
  45. public:
  46. // For safe typecasting
  47. TBOBJECT_SUBCLASS(TBWidgetAnimationRect, TBWidgetAnimationObject);
  48. enum MODE {
  49. /** Animate from source to dest. */
  50. MODE_SRC_TO_DST,
  51. /** Animate from current + delta to current. */
  52. MODE_DELTA_IN,
  53. /** Animate from current to current + delta. */
  54. MODE_DELTA_OUT
  55. };
  56. /** Animate the widget between the given source and dest rectangle. */
  57. TBWidgetAnimationRect(TBWidget *widget, const TBRect &src_rect, const TBRect &dst_rect);
  58. /** Animate the widget between rectangles based on the current widget
  59. rectangle and a delta. The reference rectangle will be taken from
  60. the target widget on the first OnAnimationUpdate. */
  61. TBWidgetAnimationRect(TBWidget *widget, const TBRect &delta_rect, MODE mode);
  62. virtual void OnAnimationStart();
  63. virtual void OnAnimationUpdate(float progress);
  64. virtual void OnAnimationStop(bool aborted);
  65. private:
  66. TBRect m_src_rect;
  67. TBRect m_dst_rect;
  68. TBRect m_delta_rect;
  69. MODE m_mode;
  70. };
  71. class TBWidgetsAnimationManager : public TBWidgetListener
  72. {
  73. public:
  74. /** Init the widgets animation manager. */
  75. static void Init();
  76. /** Shutdown the widgets animation manager. */
  77. static void Shutdown();
  78. /** Abort all animations that are running for the given widget. */
  79. static void AbortAnimations(TBWidget *widget);
  80. /** Abort all animations matching the given type that are running for the given widget.
  81. This example will abort all opacity animations:
  82. AbortAnimations(widget, TBTypedObject::GetTypeId<TBWidgetAnimationOpacity>()) */
  83. static void AbortAnimations(TBWidget *widget, TB_TYPE_ID type_id);
  84. private:
  85. // == TBWidgetListener ==================
  86. virtual void OnWidgetDelete(TBWidget *widget);
  87. virtual bool OnWidgetDying(TBWidget *widget);
  88. virtual void OnWidgetAdded(TBWidget *parent, TBWidget *child);
  89. virtual void OnWidgetRemove(TBWidget *parent, TBWidget *child);
  90. };
  91. }; // namespace tb
  92. #endif // TB_WIDGET_ANIMATION_H