CCActionProgressTimer.cpp 3.6 KB

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