CCTimeLine.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  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/ActionTimeline/CCTimeLine.h"
  21. #include "editor-support/cocostudio/ActionTimeline/CCActionTimeline.h"
  22. USING_NS_CC;
  23. NS_TIMELINE_BEGIN
  24. Timeline* Timeline::create()
  25. {
  26. Timeline* object = new (std::nothrow) Timeline();
  27. if (object)
  28. {
  29. object->autorelease();
  30. return object;
  31. }
  32. CC_SAFE_DELETE(object);
  33. return nullptr;
  34. }
  35. Timeline::Timeline()
  36. : _currentKeyFrame(nullptr)
  37. , _currentKeyFrameIndex(0)
  38. , _fromIndex(0)
  39. , _toIndex(0)
  40. , _betweenDuration(0)
  41. , _actionTag(0)
  42. , _ActionTimeline(nullptr)
  43. , _node(nullptr)
  44. {
  45. }
  46. Timeline::~Timeline()
  47. {
  48. }
  49. void Timeline::gotoFrame(int frameIndex)
  50. {
  51. if(_frames.size() == 0)
  52. return;
  53. binarySearchKeyFrame(frameIndex);
  54. apply(frameIndex);
  55. }
  56. void Timeline::stepToFrame(int frameIndex)
  57. {
  58. if(_frames.size() == 0)
  59. return;
  60. updateCurrentKeyFrame(frameIndex);
  61. apply(frameIndex);
  62. }
  63. Timeline* Timeline::clone()
  64. {
  65. Timeline* timeline = Timeline::create();
  66. timeline->_actionTag = _actionTag;
  67. for (auto frame : _frames)
  68. {
  69. Frame* newFrame = frame->clone();
  70. timeline->addFrame(newFrame);
  71. }
  72. return timeline;
  73. }
  74. void Timeline::addFrame(Frame* frame)
  75. {
  76. _frames.pushBack(frame);
  77. frame->setTimeline(this);
  78. }
  79. void Timeline::insertFrame(Frame* frame, int index)
  80. {
  81. _frames.insert(index, frame);
  82. frame->setTimeline(this);
  83. }
  84. void Timeline::removeFrame(Frame* frame)
  85. {
  86. _frames.eraseObject(frame);
  87. frame->setTimeline(nullptr);
  88. }
  89. void Timeline::setNode(Node* node)
  90. {
  91. for (auto frame : _frames)
  92. {
  93. frame->setNode(node);
  94. }
  95. }
  96. Node* Timeline::getNode() const
  97. {
  98. return _node;
  99. }
  100. void Timeline::apply(unsigned int frameIndex)
  101. {
  102. if (_currentKeyFrame)
  103. {
  104. float currentPercent = _betweenDuration == 0 ? 0 : (frameIndex - _currentKeyFrameIndex) / (float)_betweenDuration;
  105. _currentKeyFrame->apply(currentPercent);
  106. }
  107. }
  108. void Timeline::binarySearchKeyFrame(unsigned int frameIndex)
  109. {
  110. Frame *from = nullptr;
  111. Frame *to = nullptr;
  112. long length = _frames.size();
  113. bool needEnterFrame = false;
  114. do
  115. {
  116. if (frameIndex < _frames.at(0)->getFrameIndex())
  117. {
  118. if(_currentKeyFrameIndex >= _frames.at(0)->getFrameIndex())
  119. needEnterFrame = true;
  120. _fromIndex = 0;
  121. _toIndex = 0;
  122. from = to = _frames.at(0);
  123. _currentKeyFrameIndex = 0;
  124. _betweenDuration = _frames.at(0)->getFrameIndex();
  125. break;
  126. }
  127. else if(frameIndex >= _frames.at(length - 1)->getFrameIndex())
  128. {
  129. _fromIndex = (int)(length - 1);
  130. _toIndex = 0;
  131. from = to = _frames.at(length - 1);
  132. if (from->isEnterWhenPassed())
  133. needEnterFrame = true;
  134. _currentKeyFrameIndex = _frames.at(length - 1)->getFrameIndex();
  135. _betweenDuration = 0;
  136. break;
  137. }
  138. long target = -1;
  139. long low=0,high=length-1,mid=0;
  140. while(low<=high){
  141. mid=(low+high)/2;
  142. if(frameIndex >= _frames.at(mid)->getFrameIndex() && frameIndex < _frames.at(mid+1)->getFrameIndex())
  143. {
  144. target = mid;
  145. break;
  146. }
  147. if(_frames.at(mid)->getFrameIndex()>frameIndex)
  148. high=mid-1;
  149. else
  150. low=mid+1;
  151. }
  152. _fromIndex = (int)target;
  153. if(length > 1)
  154. _toIndex = (int)(target + 1);
  155. else
  156. _toIndex = (int)target;
  157. from = _frames.at(_fromIndex);
  158. to = _frames.at(_toIndex);
  159. if(target == 0 && _currentKeyFrameIndex<from->getFrameIndex())
  160. needEnterFrame = true;
  161. _currentKeyFrameIndex = from->getFrameIndex();
  162. _betweenDuration = to->getFrameIndex() - from->getFrameIndex();
  163. } while (0);
  164. if(needEnterFrame || _currentKeyFrame != from)
  165. {
  166. _currentKeyFrame = from;
  167. _currentKeyFrame->onEnter(to, frameIndex);
  168. }
  169. }
  170. void Timeline::updateCurrentKeyFrame(unsigned int frameIndex)
  171. {
  172. //! If play to current frame's front or back, then find current frame again
  173. if (frameIndex < _currentKeyFrameIndex || frameIndex >= _currentKeyFrameIndex + _betweenDuration)
  174. {
  175. Frame *from = nullptr;
  176. Frame *to = nullptr;
  177. do
  178. {
  179. long length = _frames.size();
  180. if (frameIndex < _frames.at(0)->getFrameIndex())
  181. {
  182. from = to = _frames.at(0);
  183. _currentKeyFrameIndex = 0;
  184. _betweenDuration = _frames.at(0)->getFrameIndex();
  185. break;
  186. }
  187. else if(frameIndex >= _frames.at(length - 1)->getFrameIndex())
  188. {
  189. unsigned int lastFrameIndex = _frames.at(length - 1)->getFrameIndex();
  190. if(_currentKeyFrameIndex >= lastFrameIndex)
  191. return;
  192. frameIndex = lastFrameIndex;
  193. }
  194. do
  195. {
  196. _fromIndex = _toIndex;
  197. from = _frames.at(_fromIndex);
  198. _currentKeyFrameIndex = from->getFrameIndex();
  199. _toIndex = _fromIndex + 1;
  200. if ((ssize_t)_toIndex >= length)
  201. {
  202. _toIndex = 0;
  203. }
  204. to = _frames.at(_toIndex);
  205. if(frameIndex == from->getFrameIndex())
  206. break;
  207. if(frameIndex > from->getFrameIndex() && frameIndex < to->getFrameIndex())
  208. break;
  209. if(from->isEnterWhenPassed())
  210. from->onEnter(to, from->getFrameIndex());
  211. }
  212. while (true);
  213. if(_fromIndex == length-1)
  214. to = from;
  215. _betweenDuration = to->getFrameIndex() - from->getFrameIndex();
  216. } while (0);
  217. _currentKeyFrame = from;
  218. _currentKeyFrame->onEnter(to, frameIndex);
  219. }
  220. }
  221. NS_TIMELINE_END