CCActionTween.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /****************************************************************************
  2. Copyright (c) 2009 lhunath (Maarten Billemont)
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CCACTIONTWEEN_H__
  23. #define __CCACTIONTWEEN_H__
  24. #include "2d/CCActionInterval.h"
  25. NS_CC_BEGIN
  26. /**
  27. * @addtogroup actions
  28. * @{
  29. */
  30. /**
  31. @brief The delegate class for ActionTween.
  32. @details If you want to use ActionTween on a node.
  33. You should implement the node follow these steps:
  34. 1. The node should be inherit from ActionTweenDelegate.
  35. 2. Override the virtual method updateTweenAction in the node.
  36. Then once you running ActionTween on the node, the method updateTweenAction will be invoked.
  37. */
  38. class CC_DLL ActionTweenDelegate
  39. {
  40. public:
  41. /**
  42. * @js NA
  43. * @lua NA
  44. */
  45. virtual ~ActionTweenDelegate() {}
  46. /**
  47. @brief The callback function when ActionTween is running.
  48. @param value The new value of the specified key.
  49. @param key The key of property which should be updated.
  50. */
  51. virtual void updateTweenAction(float value, const std::string& key) = 0;
  52. };
  53. /** ActionTween
  54. ActionTween is an action that lets you update any property of an object.
  55. For example, if you want to modify the "width" property of a target from 200 to 300 in 2 seconds, then:
  56. @code
  57. auto modifyWidth = ActionTween::create(2, "width", 200, 300);
  58. target->runAction(modifyWidth);
  59. @endcode
  60. Another example: ScaleTo action could be rewritten using PropertyAction:
  61. @code
  62. // scaleA and scaleB are equivalents
  63. auto scaleA = ScaleTo::create(2, 3); // (duration, to)
  64. auto scaleB = ActionTween::create(2, "scale", 1, 3); // (duration, key, from, to)
  65. @endcode
  66. @since v0.99.2
  67. */
  68. class CC_DLL ActionTween : public ActionInterval
  69. {
  70. public:
  71. /**
  72. * @brief Create and initializes the action with the property name (key), and the from and to parameters.
  73. * @param duration The duration of the ActionTween. It's a value in seconds.
  74. * @param key The key of property which should be updated.
  75. * @param from The value of the specified property when the action begin.
  76. * @param to The value of the specified property when the action end.
  77. * @return If the creation success, return a pointer of ActionTween; otherwise, return nil.
  78. */
  79. static ActionTween* create(float duration, const std::string& key, float from, float to);
  80. // Overrides
  81. void startWithTarget(Node *target) override;
  82. void update(float dt) override;
  83. ActionTween* reverse() const override;
  84. ActionTween *clone() const override;
  85. CC_CONSTRUCTOR_ACCESS:
  86. /**
  87. * @brief Initializes the action with the property name (key), and the from and to parameters.
  88. * @param duration The duration of the ActionTween. It's a value in seconds.
  89. * @param key The key of property which should be updated.
  90. * @param from The value of the specified property when the action begin.
  91. * @param to The value of the specified property when the action end.
  92. * @return If the initialization success, return true; otherwise, return false.
  93. */
  94. bool initWithDuration(float duration, const std::string& key, float from, float to);
  95. protected:
  96. std::string _key;
  97. float _from, _to;
  98. float _delta;
  99. };
  100. // end of actions group
  101. /// @}
  102. NS_CC_END
  103. #endif /* __CCACTIONTWEEN_H__ */