CCActionTween.h 4.5 KB

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