CCProcessBase.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/CCProcessBase.h"
  21. #include "editor-support/cocostudio/CCUtilMath.h"
  22. using namespace cocos2d;
  23. namespace cocostudio {
  24. ProcessBase::ProcessBase(void)
  25. : _processScale(1)
  26. , _isPause(true)
  27. , _isComplete(true)
  28. , _isPlaying(false)
  29. , _currentPercent(0.0f)
  30. , _rawDuration(0)
  31. , _loopType(ANIMATION_LOOP_BACK)
  32. , _tweenEasing(cocos2d::tweenfunc::Linear)
  33. , _animationInternal(1/60.0f)
  34. , _durationTween(0)
  35. , _currentFrame(0)
  36. , _curFrameIndex(0)
  37. , _isLoopBack(false)
  38. {
  39. }
  40. ProcessBase::~ProcessBase(void)
  41. {
  42. }
  43. void ProcessBase::pause()
  44. {
  45. _isPause = true;
  46. _isPlaying = false;
  47. }
  48. void ProcessBase::resume()
  49. {
  50. _isPause = false;
  51. _isPlaying = true;
  52. }
  53. void ProcessBase::stop()
  54. {
  55. _isComplete = true;
  56. _isPlaying = false;
  57. }
  58. void ProcessBase::play(int durationTo, int /*durationTween*/, int /*loop*/, int tweenEasing)
  59. {
  60. _isComplete = false;
  61. _isPause = false;
  62. _isPlaying = true;
  63. _currentFrame = 0;
  64. /*
  65. * Set m_iTotalFrames to durationTo, it is used for change tween between two animation.
  66. * When changing end, m_iTotalFrames will be set to _durationTween
  67. */
  68. _nextFrameIndex = durationTo;
  69. _tweenEasing = (cocos2d::tweenfunc::TweenType)tweenEasing;
  70. }
  71. void ProcessBase::update(float dt)
  72. {
  73. if (_isComplete || _isPause)
  74. {
  75. return;
  76. }
  77. /*
  78. * Filter the m_iDuration <=0 and dt >1
  79. * If dt>1, generally speaking the reason is the device is stuck.
  80. */
  81. if(_rawDuration <= 0 || dt > 1)
  82. {
  83. return;
  84. }
  85. if (_nextFrameIndex <= 0)
  86. {
  87. _currentPercent = 1;
  88. _currentFrame = 0;
  89. }
  90. else
  91. {
  92. /*
  93. * update _currentFrame, every update add the frame passed.
  94. * dt/_animationInternal determine it is not a frame animation. If frame speed changed, it will not make our
  95. * animation speed slower or quicker.
  96. */
  97. _currentFrame += _processScale * (dt / _animationInternal);
  98. _currentPercent = _currentFrame / _nextFrameIndex;
  99. /*
  100. * if _currentFrame is bigger or equal than m_iTotalFrames, then reduce it until _currentFrame is
  101. * smaller than m_iTotalFrames
  102. */
  103. _currentFrame = fmodf(_currentFrame, _nextFrameIndex);
  104. }
  105. updateHandler();
  106. }
  107. void ProcessBase::gotoFrame(int frameIndex)
  108. {
  109. if (_loopType == ANIMATION_NO_LOOP)
  110. {
  111. _loopType = ANIMATION_MAX;
  112. }
  113. else if (_loopType == ANIMATION_TO_LOOP_FRONT)
  114. {
  115. _loopType = ANIMATION_LOOP_FRONT;
  116. }
  117. _curFrameIndex = frameIndex;
  118. _nextFrameIndex = _durationTween;
  119. }
  120. int ProcessBase::getCurrentFrameIndex()
  121. {
  122. _curFrameIndex = (_rawDuration-1) * _currentPercent;
  123. return _curFrameIndex;
  124. }
  125. }