CCActionProgressTimer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /****************************************************************************
  2. Copyright (C) 2010 Lam Pham
  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. #include "2d/CCActionProgressTimer.h"
  24. #include "2d/CCProgressTimer.h"
  25. NS_CC_BEGIN
  26. #define kProgressTimerCast ProgressTimer*
  27. // implementation of ProgressTo
  28. ProgressTo* ProgressTo::create(float duration, float percent)
  29. {
  30. ProgressTo *progressTo = new (std::nothrow) ProgressTo();
  31. if (progressTo && progressTo->initWithDuration(duration, percent))
  32. {
  33. progressTo->autorelease();
  34. return progressTo;
  35. }
  36. delete progressTo;
  37. return nullptr;
  38. }
  39. bool ProgressTo::initWithDuration(float duration, float percent)
  40. {
  41. if (ActionInterval::initWithDuration(duration))
  42. {
  43. _to = percent;
  44. return true;
  45. }
  46. return false;
  47. }
  48. ProgressTo* ProgressTo::clone() const
  49. {
  50. // no copy constructor
  51. return ProgressTo::create(_duration, _to);
  52. }
  53. ProgressTo* ProgressTo::reverse() const
  54. {
  55. CCASSERT(false, "reverse() not supported in ProgressTo");
  56. return nullptr;
  57. }
  58. void ProgressTo::startWithTarget(Node *target)
  59. {
  60. ActionInterval::startWithTarget(target);
  61. _from = ((kProgressTimerCast)(target))->getPercentage();
  62. }
  63. void ProgressTo::update(float time)
  64. {
  65. ((kProgressTimerCast)(_target))->setPercentage(_from + (_to - _from) * time);
  66. }
  67. // implementation of ProgressFromTo
  68. ProgressFromTo* ProgressFromTo::create(float duration, float fromPercentage, float toPercentage)
  69. {
  70. ProgressFromTo *progressFromTo = new (std::nothrow) ProgressFromTo();
  71. if (progressFromTo && progressFromTo->initWithDuration(duration, fromPercentage, toPercentage)) {
  72. progressFromTo->autorelease();
  73. return progressFromTo;
  74. }
  75. delete progressFromTo;
  76. return nullptr;
  77. }
  78. bool ProgressFromTo::initWithDuration(float duration, float fromPercentage, float toPercentage)
  79. {
  80. if (ActionInterval::initWithDuration(duration))
  81. {
  82. _to = toPercentage;
  83. _from = fromPercentage;
  84. return true;
  85. }
  86. return false;
  87. }
  88. ProgressFromTo* ProgressFromTo::clone() const
  89. {
  90. // no copy constructor
  91. return ProgressFromTo::create(_duration, _from, _to);
  92. }
  93. ProgressFromTo* ProgressFromTo::reverse() const
  94. {
  95. return ProgressFromTo::create(_duration, _to, _from);
  96. }
  97. void ProgressFromTo::startWithTarget(Node *target)
  98. {
  99. ActionInterval::startWithTarget(target);
  100. }
  101. void ProgressFromTo::update(float time)
  102. {
  103. ((kProgressTimerCast)(_target))->setPercentage(_from + (_to - _from) * time);
  104. }
  105. NS_CC_END