CCActionTimeline.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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/CCActionTimeline.h"
  21. #include "editor-support/cocostudio/CCComExtensionData.h"
  22. USING_NS_CC;
  23. NS_TIMELINE_BEGIN
  24. // ActionTimelineData
  25. ActionTimelineData* ActionTimelineData::create(int actionTag)
  26. {
  27. ActionTimelineData * ret = new (std::nothrow) ActionTimelineData();
  28. if (ret && ret->init(actionTag))
  29. {
  30. ret->autorelease();
  31. }
  32. else
  33. {
  34. CC_SAFE_DELETE(ret);
  35. }
  36. return ret;
  37. }
  38. ActionTimelineData::ActionTimelineData()
  39. : _actionTag(0)
  40. {
  41. }
  42. bool ActionTimelineData::init(int actionTag)
  43. {
  44. _actionTag = actionTag;
  45. return true;
  46. }
  47. // ActionTimeline
  48. ActionTimeline* ActionTimeline::create()
  49. {
  50. ActionTimeline* object = new (std::nothrow) ActionTimeline();
  51. if (object && object->init())
  52. {
  53. object->autorelease();
  54. return object;
  55. }
  56. CC_SAFE_DELETE(object);
  57. return nullptr;
  58. }
  59. ActionTimeline::ActionTimeline()
  60. : _duration(0)
  61. , _time(0)
  62. , _timeSpeed(1)
  63. , _frameInternal(1/60.0f)
  64. , _playing(false)
  65. , _currentFrame(0)
  66. , _startFrame(0)
  67. , _endFrame(0)
  68. , _frameEventListener(nullptr)
  69. , _lastFrameListener(nullptr)
  70. {
  71. }
  72. ActionTimeline::~ActionTimeline()
  73. {
  74. }
  75. bool ActionTimeline::init()
  76. {
  77. return true;
  78. }
  79. void ActionTimeline::play(std::string name, bool loop)
  80. {
  81. if (_animationInfos.find(name) == _animationInfos.end())
  82. {
  83. CCLOG("Can't find animation info for %s", name.c_str());
  84. return;
  85. }
  86. AnimationInfo& index = _animationInfos[name];
  87. gotoFrameAndPlay(index.startIndex, index.endIndex, loop);
  88. }
  89. void ActionTimeline::gotoFrameAndPlay(int startIndex)
  90. {
  91. gotoFrameAndPlay(startIndex, true);
  92. }
  93. void ActionTimeline::gotoFrameAndPlay(int startIndex, bool loop)
  94. {
  95. gotoFrameAndPlay(startIndex, _duration, loop);
  96. }
  97. void ActionTimeline::gotoFrameAndPlay(int startIndex, int endIndex, bool loop)
  98. {
  99. gotoFrameAndPlay(startIndex, endIndex, startIndex, loop);
  100. }
  101. void ActionTimeline::gotoFrameAndPlay(int startIndex, int endIndex, int currentFrameIndex, bool loop)
  102. {
  103. _startFrame = startIndex;
  104. _endFrame = endIndex;
  105. _currentFrame = currentFrameIndex;
  106. _loop = loop;
  107. _time = _currentFrame*_frameInternal;
  108. resume();
  109. gotoFrame(_currentFrame);
  110. }
  111. void ActionTimeline::gotoFrameAndPause(int startIndex)
  112. {
  113. _startFrame = _currentFrame = startIndex;
  114. _time = _currentFrame * _frameInternal;
  115. pause();
  116. gotoFrame(_currentFrame);
  117. }
  118. void ActionTimeline::pause()
  119. {
  120. _playing = false;
  121. }
  122. void ActionTimeline::resume()
  123. {
  124. _playing = true;
  125. }
  126. bool ActionTimeline::isPlaying() const
  127. {
  128. return _playing;
  129. }
  130. void ActionTimeline::setCurrentFrame(int frameIndex)
  131. {
  132. if (frameIndex >= _startFrame && frameIndex <= _endFrame)
  133. {
  134. _currentFrame = frameIndex;
  135. _time = _currentFrame*_frameInternal;
  136. }
  137. else
  138. {
  139. CCLOG("frame index is not between start frame and end frame");
  140. }
  141. }
  142. ActionTimeline* ActionTimeline::clone() const
  143. {
  144. ActionTimeline* newAction = ActionTimeline::create();
  145. newAction->setDuration(_duration);
  146. newAction->setTimeSpeed(_timeSpeed);
  147. for (auto timelines : _timelineMap)
  148. {
  149. for(auto timeline : timelines.second)
  150. {
  151. Timeline* newTimeline = timeline->clone();
  152. newAction->addTimeline(newTimeline);
  153. }
  154. }
  155. for( auto info : _animationInfos)
  156. {
  157. newAction->addAnimationInfo(info.second);
  158. }
  159. return newAction;
  160. }
  161. void ActionTimeline::step(float delta)
  162. {
  163. if (!_playing || _timelineMap.size() == 0 || _duration == 0)
  164. {
  165. return;
  166. }
  167. _time += delta * _timeSpeed;
  168. float deltaCurrFrameTime = std::abs(_time - _currentFrame * _frameInternal);
  169. if (deltaCurrFrameTime < _frameInternal)
  170. return;
  171. const float endtoffset = _time - _endFrame * _frameInternal;
  172. if (endtoffset < _frameInternal)
  173. {
  174. _currentFrame = (int)(_time / _frameInternal);
  175. stepToFrame(_currentFrame);
  176. emitFrameEndCallFuncs(_currentFrame);
  177. if (endtoffset >= 0 && _lastFrameListener != nullptr) // last frame
  178. _lastFrameListener();
  179. }
  180. else
  181. {
  182. _playing = _loop;
  183. if (!_playing)
  184. {
  185. _time = _endFrame * _frameInternal;
  186. if (_currentFrame != _endFrame)
  187. {
  188. _currentFrame = _endFrame;
  189. stepToFrame(_currentFrame);
  190. emitFrameEndCallFuncs(_currentFrame);
  191. if (_lastFrameListener != nullptr) // last frame
  192. _lastFrameListener();
  193. }
  194. }
  195. else
  196. gotoFrameAndPlay(_startFrame, _endFrame, _loop);
  197. }
  198. }
  199. typedef std::function<void(Node*)> tCallBack;
  200. void foreachNodeDescendant(Node* parent, tCallBack callback)
  201. {
  202. callback(parent);
  203. auto& children = parent->getChildren();
  204. for (auto child : children)
  205. {
  206. foreachNodeDescendant(child, callback);
  207. }
  208. }
  209. void ActionTimeline::startWithTarget(Node *target)
  210. {
  211. Action::startWithTarget(target);
  212. this->setTag(target->getTag());
  213. foreachNodeDescendant(target,
  214. [this, target](Node* child)
  215. {
  216. ComExtensionData* data = dynamic_cast<ComExtensionData*>(child->getComponent("ComExtensionData"));
  217. if(data)
  218. {
  219. int actionTag = data->getActionTag();
  220. if(_timelineMap.find(actionTag) != _timelineMap.end())
  221. {
  222. auto timelines = this->_timelineMap[actionTag];
  223. for (auto timeline : timelines)
  224. {
  225. timeline->setNode(child);
  226. }
  227. }
  228. }
  229. });
  230. }
  231. void ActionTimeline::addTimeline(Timeline* timeline)
  232. {
  233. int tag = timeline->getActionTag();
  234. if (_timelineMap.find(tag) == _timelineMap.end())
  235. {
  236. _timelineMap[tag] = Vector<Timeline*>();
  237. }
  238. if (!_timelineMap[tag].contains(timeline))
  239. {
  240. _timelineList.pushBack(timeline);
  241. _timelineMap[tag].pushBack(timeline);
  242. timeline->setActionTimeline(this);
  243. }
  244. }
  245. void ActionTimeline::removeTimeline(Timeline* timeline)
  246. {
  247. int tag = timeline->getActionTag();
  248. if (_timelineMap.find(tag) != _timelineMap.end())
  249. {
  250. if(_timelineMap[tag].contains(timeline))
  251. {
  252. _timelineMap[tag].eraseObject(timeline);
  253. _timelineList.eraseObject(timeline);
  254. timeline->setActionTimeline(nullptr);
  255. }
  256. }
  257. }
  258. void ActionTimeline::addAnimationInfo(const AnimationInfo& animationInfo)
  259. {
  260. if (_animationInfos.find(animationInfo.name) != _animationInfos.end())
  261. {
  262. CCLOG("Animation (%s) already exists.", animationInfo.name.c_str());
  263. return;
  264. }
  265. _animationInfos[animationInfo.name] = animationInfo;
  266. addFrameEndCallFunc(animationInfo.endIndex, animationInfo.name, animationInfo.clipEndCallBack);
  267. }
  268. void ActionTimeline::removeAnimationInfo(std::string animationName)
  269. {
  270. auto clipIter = _animationInfos.find(animationName);
  271. if (clipIter == _animationInfos.end())
  272. {
  273. CCLOG("AnimationInfo (%s) not exists.", animationName.c_str());
  274. return;
  275. }
  276. removeFrameEndCallFunc((*clipIter).second.endIndex, animationName);
  277. _animationInfos.erase(animationName);
  278. }
  279. bool ActionTimeline::IsAnimationInfoExists(const std::string& animationName)
  280. {
  281. return _animationInfos.find(animationName) != _animationInfos.end();
  282. }
  283. const AnimationInfo& ActionTimeline::getAnimationInfo(const std::string &animationName)
  284. {
  285. return _animationInfos.find(animationName)->second;
  286. }
  287. void ActionTimeline::setAnimationEndCallFunc(const std::string animationName, std::function<void()> func)
  288. {
  289. auto clipIter = _animationInfos.find(animationName);
  290. if (clipIter == _animationInfos.end())
  291. {
  292. CCLOG("AnimationInfo (%s) not exists.", animationName.c_str());
  293. return;
  294. }
  295. clipIter->second.clipEndCallBack = func;
  296. addFrameEndCallFunc(clipIter->second.endIndex, animationName, func);
  297. }
  298. void ActionTimeline::setFrameEventCallFunc(std::function<void(Frame *)> listener)
  299. {
  300. _frameEventListener = listener;
  301. }
  302. void ActionTimeline::clearFrameEventCallFunc()
  303. {
  304. _frameEventListener = nullptr;
  305. }
  306. void ActionTimeline::setLastFrameCallFunc(std::function<void()> listener)
  307. {
  308. _lastFrameListener = listener;
  309. }
  310. void ActionTimeline::clearLastFrameCallFunc()
  311. {
  312. _lastFrameListener = nullptr;
  313. }
  314. void ActionTimeline::emitFrameEvent(Frame* frame)
  315. {
  316. if(_frameEventListener)
  317. {
  318. _frameEventListener(frame);
  319. }
  320. }
  321. void ActionTimeline::addFrameEndCallFunc(int frameIndex, const std::string& funcKey, std::function<void()> func)
  322. {
  323. if (func != nullptr)
  324. {
  325. _frameEndCallFuncs[frameIndex][funcKey] = func;
  326. }
  327. }
  328. void ActionTimeline::removeFrameEndCallFunc(int frameIndex, const std::string& funcKey)
  329. {
  330. auto endClipCallsIter = _frameEndCallFuncs.find(frameIndex);
  331. if (endClipCallsIter != _frameEndCallFuncs.end())
  332. {
  333. auto funcIter = (*endClipCallsIter).second.find(funcKey);
  334. if (funcIter != (*endClipCallsIter).second.end())
  335. (*endClipCallsIter).second.erase(funcKey);
  336. if ((*endClipCallsIter).second.empty())
  337. _frameEndCallFuncs.erase(endClipCallsIter);
  338. }
  339. }
  340. void ActionTimeline::removeFrameEndCallFuncs(int frameIndex)
  341. {
  342. auto endClipCallsIter = _frameEndCallFuncs.find(frameIndex);
  343. if (endClipCallsIter != _frameEndCallFuncs.end())
  344. {
  345. _frameEndCallFuncs.erase(endClipCallsIter);
  346. }
  347. }
  348. void ActionTimeline::clearFrameEndCallFuncs()
  349. {
  350. _frameEndCallFuncs.clear();
  351. }
  352. void ActionTimeline::emitFrameEndCallFuncs(int frameIndex)
  353. {
  354. auto clipEndCallsIter = _frameEndCallFuncs.find(frameIndex);
  355. if (clipEndCallsIter != _frameEndCallFuncs.end())
  356. {
  357. auto clipEndCalls = (*clipEndCallsIter).second;
  358. for (auto call : clipEndCalls)
  359. (call).second();
  360. }
  361. }
  362. void ActionTimeline::gotoFrame(int frameIndex)
  363. {
  364. if(_target == nullptr)
  365. return;
  366. ssize_t size = _timelineList.size();
  367. for(ssize_t i = 0; i < size; i++)
  368. {
  369. _timelineList.at(i)->gotoFrame(frameIndex);
  370. }
  371. }
  372. void ActionTimeline::stepToFrame(int frameIndex)
  373. {
  374. ssize_t size = _timelineList.size();
  375. for(ssize_t i = 0; i < size; i++)
  376. {
  377. _timelineList.at(i)->stepToFrame(frameIndex);
  378. }
  379. }
  380. void ActionTimeline::start()
  381. {
  382. gotoFrameAndPlay(0);
  383. }
  384. void ActionTimeline::stop()
  385. {
  386. pause();
  387. }
  388. NS_TIMELINE_END