REDAnimationManager.cpp 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539
  1. #include "REDAnimationManager.h"
  2. #include "CCNodeLoader.h"
  3. #include "REDReader.h"
  4. #include "CCNode+REDRelativePositioning.h"
  5. #include "audio/include/AudioEngine.h"
  6. #include "REDSelectorResolver.h"
  7. #include <spine/spine-cocos2dx.h>
  8. #include <string>
  9. #include <sstream>
  10. #include <set>
  11. #include "common/CocosConfig.h"
  12. #include "BezierEqualPointsAction.hpp"
  13. #include "REDActionInstant.h"
  14. #include "RedWise.hpp"
  15. using namespace cocos2d;
  16. using namespace std;
  17. using namespace cocos2d::extension;
  18. using namespace experimental;
  19. namespace redream {
  20. // Implementation of REDAinmationManager
  21. #define LE_CHR(a,b,c,d) ( ((a)<<24) | ((b)<<16) | ((c)<<8) | (d) )
  22. static int animationTag = LE_CHR('c','c','b','i');
  23. REDAnimationManager::REDAnimationManager()
  24. : _owner(nullptr)
  25. , _autoPlaySequenceId(0)
  26. , _rootNode(nullptr)
  27. , _rootContainerSize(Size::ZERO)
  28. , _delegate(nullptr)
  29. , _runningSequence(std::make_pair(nullptr, nullptr))
  30. , _speed(1.0f)
  31. , _listen(nullptr)
  32. , _lastTag(0)
  33. , _nowTag(0)
  34. {
  35. init();
  36. }
  37. REDAnimationManager *REDAnimationManager::fromNode(Node *node)
  38. {
  39. return dynamic_cast<REDAnimationManager*>(node->getUserObject());
  40. }
  41. bool REDAnimationManager::init()
  42. {
  43. _target = nullptr;
  44. _animationCompleteCallbackFunc = nullptr;
  45. return true;
  46. }
  47. REDAnimationManager::~REDAnimationManager()
  48. {
  49. // DictElement *pElement = nullptr;
  50. // CCDICT_FOREACH(_nodeSequences, pElement)
  51. // {
  52. // Node *node = (Node*)pElement->getIntKey();
  53. // node->release();
  54. // }
  55. //
  56. // CCDICT_FOREACH(_baseValues, pElement)
  57. // {
  58. // Node *node = (Node*)pElement->getIntKey();
  59. // node->release();
  60. // }
  61. if (_rootNode)
  62. {
  63. _rootNode->stopAllActions();
  64. }
  65. if (!_needResetSpriteFrames.empty()) {
  66. _needResetSpriteFrames.clear();
  67. }
  68. setRootNode(nullptr);
  69. setDelegate(nullptr);
  70. for (auto iter = _objects.begin(); iter != _objects.end(); ++iter)
  71. {
  72. for (auto iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2)
  73. {
  74. iter2->second->release();
  75. }
  76. }
  77. CC_SAFE_RELEASE(_target);
  78. }
  79. Vector<REDSequence*>& REDAnimationManager::getSequences()
  80. {
  81. return _sequences;
  82. }
  83. void REDAnimationManager::setSequences(const Vector<REDSequence*>& seq)
  84. {
  85. _sequences = seq;
  86. }
  87. int REDAnimationManager::getAutoPlaySequenceId()
  88. {
  89. return _autoPlaySequenceId;
  90. }
  91. void REDAnimationManager::setAutoPlaySequenceId(int autoPlaySequenceId)
  92. {
  93. _autoPlaySequenceId = autoPlaySequenceId;
  94. }
  95. Node* REDAnimationManager::getRootNode()
  96. {
  97. return _rootNode;
  98. }
  99. void REDAnimationManager::setRootNode(Node *pRootNode)
  100. {
  101. _rootNode = pRootNode;
  102. }
  103. void REDAnimationManager::setDocumentControllerName(const std::string &name)
  104. {
  105. _documentControllerName = name;
  106. }
  107. std::string REDAnimationManager::getDocumentControllerName()
  108. {
  109. return _documentControllerName;
  110. }
  111. void REDAnimationManager::addDocumentCallbackNode(Node *node)
  112. {
  113. _documentCallbackNodes.pushBack(node);
  114. }
  115. void REDAnimationManager::addDocumentCallbackName(std::string name)
  116. {
  117. _documentCallbackNames.push_back(Value(name));
  118. }
  119. void REDAnimationManager::addDocumentCallbackControlEvents(Control::EventType eventType)
  120. {
  121. _documentCallbackControlEvents.push_back(Value(static_cast<int>(eventType)));
  122. }
  123. ValueVector& REDAnimationManager::getDocumentCallbackNames()
  124. {
  125. return _documentCallbackNames;
  126. }
  127. Vector<Node*>& REDAnimationManager::getDocumentCallbackNodes()
  128. {
  129. return _documentCallbackNodes;
  130. }
  131. ValueVector& REDAnimationManager::getDocumentCallbackControlEvents()
  132. {
  133. return _documentCallbackControlEvents;
  134. }
  135. void REDAnimationManager::addDocumentOutletNode(Node *node)
  136. {
  137. _documentOutletNodes.pushBack(node);
  138. }
  139. void REDAnimationManager::addDocumentOutletName(std::string name)
  140. {
  141. _documentOutletNames.push_back(Value(name));
  142. }
  143. ValueVector& REDAnimationManager::getDocumentOutletNames()
  144. {
  145. return _documentOutletNames;
  146. }
  147. Vector<Node*>& REDAnimationManager::getDocumentOutletNodes()
  148. {
  149. return _documentOutletNodes;
  150. }
  151. std::string REDAnimationManager::getLastCompletedSequenceName()
  152. {
  153. return _lastCompletedSequenceName;
  154. }
  155. ValueVector& REDAnimationManager::getKeyframeCallbacks()
  156. {
  157. return _keyframeCallbacks;
  158. }
  159. const Size& REDAnimationManager::getRootContainerSize()
  160. {
  161. return _rootContainerSize;
  162. }
  163. void REDAnimationManager::setRootContainerSize(const Size &rootContainerSize)
  164. {
  165. _rootContainerSize.setSize(rootContainerSize.width, rootContainerSize.height);
  166. }
  167. REDAnimationManagerDelegate* REDAnimationManager::getDelegate()
  168. {
  169. return _delegate;
  170. }
  171. void REDAnimationManager::setDelegate(REDAnimationManagerDelegate *pDelegate)
  172. {
  173. CC_SAFE_RELEASE(dynamic_cast<Ref*>(_delegate));
  174. _delegate = pDelegate;
  175. CC_SAFE_RETAIN(dynamic_cast<Ref*>(_delegate));
  176. }
  177. const char* REDAnimationManager::getRunningSequenceName()
  178. {
  179. if (_runningSequence.first)
  180. {
  181. return _runningSequence.first->getName();
  182. }
  183. return nullptr;
  184. }
  185. const Size& REDAnimationManager::getContainerSize(Node *pNode)
  186. {
  187. if (pNode)
  188. {
  189. return pNode->getContentSize();
  190. }
  191. else
  192. {
  193. return _rootContainerSize;
  194. }
  195. }
  196. // refer to REDReader::readNodeGraph() for data structure of pSeq
  197. void REDAnimationManager::addNode(Node *pNode, const std::unordered_map<int, Map<std::string, REDSequenceProperty*>>& seq)
  198. {
  199. // pNode->retain();
  200. _nodeSequences[pNode] = seq;
  201. }
  202. void REDAnimationManager::setBaseValue(const Value& value, Node *pNode, const std::string& propName)
  203. {
  204. auto& props = _baseValues[pNode];
  205. props[propName] = value;
  206. }
  207. const Value& REDAnimationManager::getBaseValue(Node *pNode, const std::string& propName)
  208. {
  209. auto& props = _baseValues[pNode];
  210. return props[propName];
  211. }
  212. void REDAnimationManager::setObject(Ref* obj, Node *pNode, const std::string& propName)
  213. {
  214. auto& props = _objects[pNode];
  215. auto iter = props.find(propName);
  216. if (iter != props.end())
  217. iter->second->release();
  218. props[propName] = obj;
  219. obj->retain();
  220. }
  221. Ref* REDAnimationManager::getObject(Node *pNode, const std::string& propName)
  222. {
  223. auto& props = _objects[pNode];
  224. auto iter = props.find(propName);
  225. if (iter != props.end())
  226. return iter->second;
  227. return nullptr;
  228. }
  229. int REDAnimationManager::getSequenceId(const char* pSequenceName)
  230. {
  231. string seqName(pSequenceName);
  232. for (auto& seq : _sequences)
  233. {
  234. if (seqName.compare(seq->getName()) == 0)
  235. {
  236. return seq->getSequenceId();
  237. }
  238. }
  239. return -1;
  240. }
  241. REDSequence* REDAnimationManager::getSequence(int nSequenceId)
  242. {
  243. for (auto& seq : _sequences)
  244. {
  245. if (seq->getSequenceId() == nSequenceId)
  246. {
  247. return seq;
  248. }
  249. }
  250. return nullptr;
  251. }
  252. float REDAnimationManager::getSequenceDuration(const char *pSequenceName)
  253. {
  254. int id = getSequenceId(pSequenceName);
  255. if (id != -1)
  256. return getSequence(id)->getDuration();
  257. return 0;
  258. }
  259. std::string REDAnimationManager::getREDFileName(){
  260. return _ccbFileName;
  261. }
  262. void REDAnimationManager::setREDFileName(std::string filename){
  263. _ccbFileName = filename;
  264. }
  265. void REDAnimationManager::moveAnimationsFromNode(Node* fromNode, Node* toNode)
  266. {
  267. // Move base values
  268. auto baseValueIter = _baseValues.find(fromNode);
  269. if(baseValueIter != _baseValues.end())
  270. {
  271. _baseValues[toNode] = baseValueIter->second;
  272. _baseValues.erase(baseValueIter);
  273. // fromNode->release();
  274. // toNode->retain();
  275. }
  276. auto objIter = _objects.find(fromNode);
  277. if (objIter != _objects.end())
  278. {
  279. _objects[toNode] = objIter->second;
  280. _objects.erase(objIter);
  281. }
  282. // Move seqs
  283. auto seqsIter = _nodeSequences.find(fromNode);
  284. if (seqsIter != _nodeSequences.end())
  285. {
  286. _nodeSequences[toNode] = seqsIter->second;
  287. _nodeSequences.erase(seqsIter);
  288. // fromNode->release();
  289. // toNode->retain();
  290. }
  291. }
  292. // Refer to REDReader::readKeyframe() for the real type of value
  293. ActionInterval* REDAnimationManager::getAction(REDKeyframe *pKeyframe0, REDKeyframe *pKeyframe1, const std::string& propName, Node *pNode)
  294. {
  295. float duration = pKeyframe1->getTime() - (pKeyframe0 ? pKeyframe0->getTime() : 0);
  296. if (propName == "rotationX")
  297. {
  298. return REDRotateXTo::create(duration, pKeyframe1->getValue().asFloat());
  299. }
  300. else if (propName == "rotationY")
  301. {
  302. return REDRotateYTo::create(duration, pKeyframe1->getValue().asFloat());
  303. }
  304. else if (propName == "rotation")
  305. {
  306. return REDRotateTo::create(duration, pKeyframe1->getValue().asFloat());
  307. }
  308. else if (propName == "opacity")
  309. {
  310. return FadeTo::create(duration, pKeyframe1->getValue().asByte());
  311. }
  312. else if (propName == "color")
  313. {
  314. auto c = pKeyframe1->getValue().asValueMap();
  315. unsigned char r = c["r"].asByte();
  316. unsigned char g = c["g"].asByte();
  317. unsigned char b = c["b"].asByte();
  318. return TintTo::create(duration, r, g, b);
  319. }
  320. else if (propName == "visible")
  321. {
  322. if (pKeyframe1->getValue().asBool())
  323. {
  324. return Sequence::createWithTwoActions(DelayTime::create(duration), Show::create());
  325. }
  326. else
  327. {
  328. return Sequence::createWithTwoActions(DelayTime::create(duration), Hide::create());
  329. }
  330. }
  331. else if (propName == "displayFrame")
  332. {
  333. return Sequence::createWithTwoActions(DelayTime::create(duration),
  334. REDSetSpriteFrame::create(static_cast<SpriteFrame*>(pKeyframe1->getObject())));
  335. }
  336. else if (propName == "position")
  337. {
  338. /*
  339. // Get position type
  340. auto& array = getBaseValue(pNode, propName).asValueVector();
  341. REDReader::PositionType type = (REDReader::PositionType)array[2].asInt();
  342. // Get relative position
  343. auto value = pKeyframe1->getValue().asValueVector();
  344. float x = value[0].asFloat();
  345. float y = value[1].asFloat();
  346. Size containerSize = getContainerSize(pNode->getParent());
  347. Vec2 absPos = getAbsolutePosition(Vec2(x,y), type, containerSize, propName);
  348. return MoveTo::create(duration, absPos);*/
  349. auto& array = getBaseValue(pNode, propName).asValueVector();
  350. PositionReferenceCorner corner = (PositionReferenceCorner)array[2].asInt();
  351. PositionUnit xUnit = (PositionUnit)array[3].asInt();
  352. PositionUnit yUnit = (PositionUnit)array[4].asInt();
  353. // Get relative position
  354. auto value = pKeyframe1->getValue().asValueVector();
  355. float x = value[0].asFloat();
  356. float y = value[1].asFloat();
  357. Size containerSize = getContainerSize(pNode->getParent());
  358. Vec2 absPos = getAbsolutePosition(1.0f, 1.0f, Vec2(x,y), corner, xUnit, yUnit, containerSize);
  359. // return MoveTo::create(duration, absPos);
  360. std::vector<Vec2> equalPoints = pKeyframe0->getEqualPoints();
  361. std::vector<Vec2> newEqualPoints = {};
  362. for (Vec2 point : equalPoints) {
  363. Vec2 newPoint = getAbsolutePosition(1.0f, 1.0f, point, corner, xUnit, yUnit, containerSize);
  364. newEqualPoints.push_back(newPoint);
  365. }
  366. if (!newEqualPoints.empty()) { // 有贝塞尔信息
  367. return BezierEqualPointsAction::create(duration, newEqualPoints);
  368. } else { // 没有贝塞尔信息,直线运动
  369. return MoveTo::create(duration, absPos);
  370. }
  371. }
  372. else if (propName == "scale")
  373. {
  374. // Get position type
  375. auto& array = getBaseValue(pNode, propName).asValueVector();
  376. REDReader::ScaleType type = (REDReader::ScaleType)array[2].asInt();
  377. // Get relative scale
  378. auto value = pKeyframe1->getValue().asValueVector();
  379. float x = value[0].asFloat();
  380. float y = value[1].asFloat();
  381. if (type == REDReader::ScaleType::MULTIPLY_RESOLUTION)
  382. {
  383. float resolutionScale = REDReader::getResolutionScale();
  384. x *= resolutionScale;
  385. y *= resolutionScale;
  386. }
  387. return ScaleTo::create(duration, x, y);
  388. }
  389. else if (propName == "skew")
  390. {
  391. // Get relative skew
  392. auto& value = pKeyframe1->getValue().asValueVector();
  393. float x = value[0].asFloat();
  394. float y = value[1].asFloat();
  395. return SkewTo::create(duration, x, y);
  396. }else if (propName == "frame")
  397. {
  398. auto value0 = pKeyframe1->getValue().asValueMap();
  399. std::string anim = value0["animation"].asString();
  400. bool loop = value0["loop"].asBool();
  401. spine::SkeletonFrame frame;
  402. frame.animation = anim;
  403. frame.progress = 0.0f;
  404. frame.loop = loop;
  405. return Sequence::createWithTwoActions(DelayTime::create(duration), spine::SpineActionInstant::create(frame));
  406. //TODO
  407. // log("REDReader: TODO implement the spine action !!!");
  408. }else if (propName == "animation")
  409. {
  410. // Get sequence id
  411. int value = pKeyframe1->getValue().asInt();
  412. return Sequence::createWithTwoActions(DelayTime::create(duration), REDAnimation::create(value));
  413. }
  414. else if (propName == "frameIndex")
  415. {
  416. return REDFrameIndexTo::create(duration, pKeyframe0->getValue().asByte(), pKeyframe1->getValue().asByte());
  417. }
  418. else if (propName == "posVar"){
  419. //粒子发射器大小
  420. auto& value = pKeyframe1->getValue().asValueVector();
  421. float x = value[0].asFloat();
  422. float y = value[1].asFloat();
  423. return REDParticlePosVarTo::create(duration, Vec2(x,y));
  424. }
  425. else if (propName == "gravity"){
  426. // 粒子重力
  427. auto& value = pKeyframe1->getValue().asValueVector();
  428. float x = value[0].asFloat();
  429. float y = value[1].asFloat();
  430. return REDGravityTo::create(duration, Vec2(x,y));
  431. }
  432. else if(propName == "speed"){
  433. // 粒子速度
  434. auto& value = pKeyframe1->getValue().asValueVector();
  435. float x = value[0].asFloat();
  436. float y = value[1].asFloat();
  437. return REDParticleSpeedTo::create(duration, Vec2(x,y));
  438. }
  439. else if(propName == "life"){
  440. // 粒子生命
  441. auto& value = pKeyframe1->getValue().asValueVector();
  442. float x = value[0].asFloat();
  443. float y = value[1].asFloat();
  444. return REDParticleLifeTo::create(duration, Vec2(x,y));
  445. }
  446. else if(propName == "startSize"){
  447. // 初始尺寸
  448. auto& value = pKeyframe1->getValue().asValueVector();
  449. float x = value[0].asFloat();
  450. float y = value[1].asFloat();
  451. return REDParticleStartSizeTo::create(duration, Vec2(x,y));
  452. }
  453. else if(propName == "endSize"){
  454. // 结束尺寸
  455. auto& value = pKeyframe1->getValue().asValueVector();
  456. float x = value[0].asFloat();
  457. float y = value[1].asFloat();
  458. return REDParticleEndSizeTo::create(duration, Vec2(x,y));
  459. }
  460. else if(propName == "startSpin"){
  461. // 开始旋转角度:
  462. auto& value = pKeyframe1->getValue().asValueVector();
  463. float x = value[0].asFloat();
  464. float y = value[1].asFloat();
  465. return REDParticleStartSpinTo::create(duration, Vec2(x,y));
  466. }
  467. else if(propName == "endSpin"){
  468. // 结束旋转角度
  469. auto& value = pKeyframe1->getValue().asValueVector();
  470. float x = value[0].asFloat();
  471. float y = value[1].asFloat();
  472. return REDParticleEndSpinTo::create(duration, Vec2(x,y));
  473. }
  474. else if(propName == "angle"){
  475. // 发射角度
  476. auto& value = pKeyframe1->getValue().asValueVector();
  477. float x = value[0].asFloat();
  478. float y = value[1].asFloat();
  479. return REDParticleAngleTo::create(duration, Vec2(x,y));
  480. }
  481. else if(propName == "tangentialAccel"){
  482. // 切向加速度
  483. auto& value = pKeyframe1->getValue().asValueVector();
  484. float x = value[0].asFloat();
  485. float y = value[1].asFloat();
  486. return REDParticleTangentialAccelTo::create(duration, Vec2(x,y));
  487. }
  488. else if(propName == "radialAccel"){
  489. // 径向加速度:
  490. auto& value = pKeyframe1->getValue().asValueVector();
  491. float x = value[0].asFloat();
  492. float y = value[1].asFloat();
  493. return REDParticleRadialAccelTo::create(duration, Vec2(x,y));
  494. }
  495. else if(propName == "startRadius"){
  496. // 开始半径:
  497. auto& value = pKeyframe1->getValue().asValueVector();
  498. float x = value[0].asFloat();
  499. float y = value[1].asFloat();
  500. return REDParticleStartRadiusTo::create(duration, Vec2(x,y));
  501. }
  502. else if(propName == "endRadius"){
  503. // 结束半径
  504. auto& value = pKeyframe1->getValue().asValueVector();
  505. float x = value[0].asFloat();
  506. float y = value[1].asFloat();
  507. return REDParticleEndRadiusTo::create(duration, Vec2(x,y));
  508. }
  509. else if(propName == "rotatePerSecond"){
  510. // 旋转:
  511. auto& value = pKeyframe1->getValue().asValueVector();
  512. float x = value[0].asFloat();
  513. float y = value[1].asFloat();
  514. return REDParticleRotatePerSecondTo::create(duration, Vec2(x,y));
  515. }
  516. else if(propName == "enabled"){
  517. if (pKeyframe1->getValue().asBool()) {
  518. return Sequence::createWithTwoActions(DelayTime::create(duration), Enable::create());
  519. } else {
  520. return Sequence::createWithTwoActions(DelayTime::create(duration), Disable::create());
  521. }
  522. }
  523. else if(propName == "string"){
  524. std::string text = pKeyframe1->getValue().asString();
  525. return Sequence::createWithTwoActions(DelayTime::create(duration), REDLabelString::actionWithText(text));
  526. }
  527. else if (propName == "useMaterial") {
  528. if (pKeyframe1->getValue().asBool()) {
  529. return Sequence::createWithTwoActions(DelayTime::create(duration), EnableMaterial::create());
  530. } else {
  531. return Sequence::createWithTwoActions(DelayTime::create(duration), DisableMaterial::create());
  532. }
  533. }
  534. else if (propName == "bakeAnimation") {
  535. RedBakeNodeFrame frame;
  536. frame.animationName = pKeyframe1->getValue().asValueVector()[0].asString();
  537. frame.elapsedTime = 0;
  538. frame.loop = pKeyframe1->getValue().asValueVector()[2].asBool();;
  539. return Sequence::createWithTwoActions(DelayTime::create(duration), RedBakeAnimationInstantAction::create(frame));
  540. }
  541. else
  542. {
  543. log("REDReader: Failed to create animation for property: %s", propName.c_str());
  544. }
  545. return nullptr;
  546. }
  547. void REDAnimationManager::setAnimatedProperty(const std::string& propName, Node *pNode, const Value& value, Ref* obj, float fTweenDuration, bool isBaseValue)
  548. {
  549. if (fTweenDuration > 0)
  550. {
  551. // Create a fake keyframe to generate the action from
  552. REDKeyframe *kf1 = new (std::nothrow) REDKeyframe();
  553. kf1->autorelease();
  554. kf1->setObject(obj);
  555. kf1->setValue(value);
  556. kf1->setTime(fTweenDuration);
  557. kf1->setEasingType(REDKeyframe::EasingType::LINEAR);
  558. // Animate
  559. ActionInterval *tweenAction = getAction(nullptr, kf1, propName, pNode);
  560. pNode->runAction(tweenAction);
  561. }
  562. else
  563. {
  564. // Just set the value
  565. if (propName == "position")
  566. {
  567. /*
  568. // Get position type
  569. auto& array = getBaseValue(pNode, propName).asValueVector();
  570. REDReader::PositionType type = (REDReader::PositionType)array[2].asInt();
  571. // Get relative position
  572. auto& valueVector = value.asValueVector();
  573. float x = valueVector[0].asFloat();
  574. float y = valueVector[1].asFloat();
  575. pNode->setPosition(getAbsolutePosition(Vec2(x,y), type, getContainerSize(pNode->getParent()), propName));
  576. pNode->setPosition(getAbsolutePosition(Vec2(x,y), type, getContainerSize(pNode->getParent()), propName));*/
  577. // Get position type
  578. auto& array = getBaseValue(pNode, propName).asValueVector();
  579. PositionReferenceCorner corner = (PositionReferenceCorner)array[2].asInt();
  580. PositionUnit xUnit = (PositionUnit)array[3].asInt();
  581. PositionUnit yUnit = (PositionUnit)array[4].asInt();
  582. // Get relative position
  583. auto& valueVector = value.asValueVector();
  584. float x = valueVector[0].asFloat();
  585. float y = valueVector[1].asFloat();
  586. pNode->setPosition(getAbsolutePosition(1.0f, 1.0f, Vec2(x,y), corner, xUnit, yUnit, getContainerSize(pNode->getParent())));
  587. }
  588. else if (propName == "scale")
  589. {
  590. // Get scale type
  591. auto& array = getBaseValue(pNode, propName).asValueVector();
  592. REDReader::ScaleType type = (REDReader::ScaleType)array[2].asInt();
  593. // Get relative scale
  594. auto& valueVector = value.asValueVector();
  595. float x = valueVector[0].asFloat();
  596. float y = valueVector[1].asFloat();
  597. setRelativeScale(pNode, x, y, type, propName);
  598. }
  599. else if(propName == "skew")
  600. {
  601. // Get relative scale
  602. auto& valueVector = value.asValueVector();
  603. float x = valueVector[0].asFloat();
  604. float y = valueVector[1].asFloat();
  605. pNode->setSkewX(x);
  606. pNode->setSkewY(y);
  607. }
  608. else if (propName == "animation")
  609. {
  610. int sequenceId = value.asInt();
  611. REDAnimationManager *animationManager = REDAnimationManager::fromNode(pNode);
  612. if(animationManager)
  613. {
  614. if(sequenceId == -2 || sequenceId == -1)
  615. animationManager->refreshStatusOnFirstFream(animationManager->getAutoPlaySequenceId(), 0);
  616. else
  617. animationManager->refreshStatusOnFirstFream(sequenceId, 0);
  618. }
  619. }
  620. else if (propName == "frame")
  621. {
  622. auto frame = value.asValueMap();
  623. std::string anim = frame["animation"].asString();
  624. auto node = static_cast<spine::SkeletonAnimation*>(pNode);
  625. if(strcmp(anim.c_str(), "Null") == 0 || strcmp(anim.c_str(), "") == 0){
  626. node->setEmptyAnimations(0);
  627. node->update(0);
  628. } else {
  629. node->clearTrack();
  630. node->setAnimation(0, anim, false);
  631. node->update(0);
  632. node->clearTrack();
  633. }
  634. }
  635. else
  636. {
  637. // [node setValue:value forKey:name];
  638. // TODO: only handle rotation, opacity, displayFrame, color
  639. if (propName == "rotation")
  640. {
  641. float rotate = value.asFloat();
  642. pNode->setRotation(rotate);
  643. } else if(propName == "rotationX")
  644. {
  645. float rotate = value.asFloat();
  646. pNode->setRotationSkewX(rotate);
  647. }else if(propName == "rotationY")
  648. {
  649. float rotate = value.asFloat();
  650. pNode->setRotationSkewY(rotate);
  651. }
  652. else if (propName == "opacity")
  653. {
  654. unsigned char opacity = value.asByte();
  655. pNode->setOpacity(opacity);
  656. }
  657. else if (propName == "displayFrame")
  658. {
  659. static_cast<Sprite*>(pNode)->setSpriteFrame(static_cast<SpriteFrame*>(obj));
  660. }
  661. else if (propName == "color")
  662. {
  663. auto c = value.asValueMap();
  664. unsigned char r = c["r"].asByte();
  665. unsigned char g = c["g"].asByte();
  666. unsigned char b = c["b"].asByte();
  667. pNode->setColor(Color3B(r, g, b));
  668. }
  669. else if (propName == "visible")
  670. {
  671. bool visible = value.asBool();
  672. pNode->setVisible(visible);
  673. }
  674. else if (propName == "frameIndex") {
  675. unsigned char frameIndex = value.asByte();
  676. static_cast<ZGFrameActionSprite*>(pNode)->setFrameIndex(frameIndex);
  677. }
  678. else if (propName == "posVar"){
  679. // 粒子发射器尺寸
  680. auto& valueVector = value.asValueVector();
  681. float gx = valueVector[0].asFloat();
  682. float gy = valueVector[1].asFloat();
  683. static_cast<ZMLCCParticleSystem*>(pNode)->setPosVar(Vec2(gx, gy));
  684. }
  685. else if (propName == "gravity") {
  686. // 粒子重力
  687. auto& valueVector = value.asValueVector();
  688. float gx = valueVector[0].asFloat();
  689. float gy = valueVector[1].asFloat();
  690. static_cast<ZMLCCParticleSystem*>(pNode)->setGravity(Vec2(gx, gy));
  691. }
  692. else if (propName == "speed") {
  693. // 粒子速度
  694. auto& valueVector = value.asValueVector();
  695. float speed = valueVector[0].asFloat();
  696. float speedVar = valueVector[1].asFloat();
  697. static_cast<ZMLCCParticleSystem*>(pNode)->setSpeed(speed);
  698. static_cast<ZMLCCParticleSystem*>(pNode)->setSpeedVar(speedVar);
  699. }
  700. else if(propName == "life"){
  701. // 粒子生命
  702. auto& valueVector = value.asValueVector();
  703. float speed = valueVector[0].asFloat();
  704. float speedVar = valueVector[1].asFloat();
  705. static_cast<ZMLCCParticleSystem*>(pNode)->setLife(speed);
  706. static_cast<ZMLCCParticleSystem*>(pNode)->setLifeVar(speedVar);
  707. }
  708. else if(propName == "startSize"){
  709. // 初始尺寸
  710. auto& valueVector = value.asValueVector();
  711. float speed = valueVector[0].asFloat();
  712. float speedVar = valueVector[1].asFloat();
  713. static_cast<ZMLCCParticleSystem*>(pNode)->setStartSize(speed);
  714. static_cast<ZMLCCParticleSystem*>(pNode)->setStartSizeVar(speedVar);
  715. }
  716. else if(propName == "endSize"){
  717. // 结束尺寸
  718. auto& valueVector = value.asValueVector();
  719. float speed = valueVector[0].asFloat();
  720. float speedVar = valueVector[1].asFloat();
  721. static_cast<ZMLCCParticleSystem*>(pNode)->setEndSize(speed);
  722. static_cast<ZMLCCParticleSystem*>(pNode)->setEndSizeVar(speedVar);
  723. }
  724. else if(propName == "startSpin"){
  725. // 开始旋转角度:
  726. auto& valueVector = value.asValueVector();
  727. float speed = valueVector[0].asFloat();
  728. float speedVar = valueVector[1].asFloat();
  729. static_cast<ZMLCCParticleSystem*>(pNode)->setStartSpin(speed);
  730. static_cast<ZMLCCParticleSystem*>(pNode)->setStartSpinVar(speedVar);
  731. }
  732. else if(propName == "endSpin"){
  733. // 结束旋转角度
  734. auto& valueVector = value.asValueVector();
  735. float speed = valueVector[0].asFloat();
  736. float speedVar = valueVector[1].asFloat();
  737. static_cast<ZMLCCParticleSystem*>(pNode)->setEndSpin(speed);
  738. static_cast<ZMLCCParticleSystem*>(pNode)->setEndSpinVar(speedVar);
  739. }
  740. else if(propName == "angle"){
  741. // 发射角度
  742. auto& valueVector = value.asValueVector();
  743. float speed = valueVector[0].asFloat();
  744. float speedVar = valueVector[1].asFloat();
  745. static_cast<ZMLCCParticleSystem*>(pNode)->setAngle(speed);
  746. static_cast<ZMLCCParticleSystem*>(pNode)->setAngleVar(speedVar);
  747. }
  748. else if(propName == "tangentialAccel"){
  749. // 切向加速度
  750. auto& valueVector = value.asValueVector();
  751. float speed = valueVector[0].asFloat();
  752. float speedVar = valueVector[1].asFloat();
  753. static_cast<ZMLCCParticleSystem*>(pNode)->setTangentialAccel(speed);
  754. static_cast<ZMLCCParticleSystem*>(pNode)->setTangentialAccelVar(speedVar);
  755. }
  756. else if(propName == "radialAccel"){
  757. // 径向加速度:
  758. auto& valueVector = value.asValueVector();
  759. float speed = valueVector[0].asFloat();
  760. float speedVar = valueVector[1].asFloat();
  761. static_cast<ZMLCCParticleSystem*>(pNode)->setRadialAccel(speed);
  762. static_cast<ZMLCCParticleSystem*>(pNode)->setRadialAccelVar(speedVar);
  763. }
  764. else if(propName == "startRadius"){
  765. // 开始半径:
  766. auto& valueVector = value.asValueVector();
  767. float speed = valueVector[0].asFloat();
  768. float speedVar = valueVector[1].asFloat();
  769. static_cast<ZMLCCParticleSystem*>(pNode)->setStartSpin(speed);
  770. static_cast<ZMLCCParticleSystem*>(pNode)->setStartSpinVar(speedVar);
  771. }
  772. else if(propName == "endRadius"){
  773. // 结束半径
  774. auto& valueVector = value.asValueVector();
  775. float speed = valueVector[0].asFloat();
  776. float speedVar = valueVector[1].asFloat();
  777. static_cast<ZMLCCParticleSystem*>(pNode)->setEndRadius(speed);
  778. static_cast<ZMLCCParticleSystem*>(pNode)->setEndRadiusVar(speedVar);
  779. }
  780. else if(propName == "rotatePerSecond"){
  781. // 旋转:
  782. auto& valueVector = value.asValueVector();
  783. float speed = valueVector[0].asFloat();
  784. float speedVar = valueVector[1].asFloat();
  785. static_cast<ZMLCCParticleSystem*>(pNode)->setRotatePerSecond(speed);
  786. static_cast<ZMLCCParticleSystem*>(pNode)->setRotatePerSecondVar(speedVar);
  787. }
  788. else if (propName == "enabled")
  789. {
  790. bool enabled = value.asBool();
  791. static_cast<Control*>(pNode)->setEnabled(enabled);
  792. }
  793. else if(propName == "string"){
  794. auto value = getBaseValue(pNode, propName);
  795. static_cast<Label*>(pNode)->setString(value.asString());
  796. }
  797. else if (propName == "useMaterial") {
  798. auto isUseMaterial = value.asBool();
  799. static_cast<Sprite*>(pNode)->setMaterialEnable(isUseMaterial);
  800. }
  801. else if (propName == "bakeAnimation") {
  802. RedBakeNodeFrame frame;
  803. frame.animationName = value.asValueVector()[0].asString();
  804. frame.elapsedTime = 0;
  805. frame.loop = value.asValueVector()[2].asBool();
  806. RedBakeNode *bakeNode = dynamic_cast<RedBakeNode*>(pNode);
  807. bakeNode->setBakeFrame(frame);
  808. }
  809. else
  810. {
  811. log("unsupported property name is %s", propName.c_str());
  812. CCASSERT(false, "unsupported property now");
  813. }
  814. }
  815. }
  816. }
  817. void REDAnimationManager::setFirstFrame(Node *pNode, REDSequenceProperty *pSeqProp, float fTweenDuration)
  818. {
  819. auto& keyframes = pSeqProp->getKeyframes();
  820. if (keyframes.empty())
  821. {
  822. // Use base value (no animation)
  823. auto& baseValue = getBaseValue(pNode, pSeqProp->getName());
  824. auto obj = getObject(pNode, pSeqProp->getName());
  825. CCASSERT(!baseValue.isNull(), "No baseValue found for property");
  826. setAnimatedProperty(pSeqProp->getName(), pNode, baseValue, obj, fTweenDuration, false);
  827. }
  828. else
  829. {
  830. // Use first keyframe
  831. REDKeyframe *keyframe = keyframes.at(0);
  832. setAnimatedProperty(pSeqProp->getName(), pNode, keyframe->getValue(), keyframe->getObject(), fTweenDuration, false);
  833. }
  834. }
  835. ActionInterval* REDAnimationManager::getEaseAction(ActionInterval *pAction, REDKeyframe::EasingType easingType, float* fEasingOpt)
  836. {
  837. if (dynamic_cast<Sequence*>(pAction))
  838. {
  839. return pAction;
  840. }
  841. if (easingType == REDKeyframe::EasingType::LINEAR)
  842. {
  843. return pAction;
  844. }
  845. else if (easingType == REDKeyframe::EasingType::INSTANT)
  846. {
  847. return REDEaseInstant::create(pAction);
  848. }
  849. else if (easingType == REDKeyframe::EasingType::CUBIC_IN)
  850. {
  851. return EaseIn::create(pAction, *fEasingOpt);
  852. }
  853. else if (easingType == REDKeyframe::EasingType::CUBIC_OUT)
  854. {
  855. return EaseOut::create(pAction, *fEasingOpt);
  856. }
  857. else if (easingType == REDKeyframe::EasingType::CUBIC_INOUT)
  858. {
  859. return EaseInOut::create(pAction, *fEasingOpt);
  860. }
  861. else if (easingType == REDKeyframe::EasingType::BACK_IN)
  862. {
  863. return EaseBackIn::create(pAction);
  864. }
  865. else if (easingType == REDKeyframe::EasingType::BACK_OUT)
  866. {
  867. return EaseBackOut::create(pAction);
  868. }
  869. else if (easingType == REDKeyframe::EasingType::BACK_INOUT)
  870. {
  871. return EaseBackInOut::create(pAction);
  872. }
  873. else if (easingType == REDKeyframe::EasingType::BOUNCE_IN)
  874. {
  875. return EaseBounceIn::create(pAction);
  876. }
  877. else if (easingType == REDKeyframe::EasingType::BOUNCE_OUT)
  878. {
  879. return EaseBounceOut::create(pAction);
  880. }
  881. else if (easingType == REDKeyframe::EasingType::BOUNCE_INOUT)
  882. {
  883. return EaseBounceInOut::create(pAction);
  884. }
  885. else if (easingType == REDKeyframe::EasingType::ELASTIC_IN)
  886. {
  887. return EaseElasticIn::create(pAction, *fEasingOpt);
  888. }
  889. else if (easingType == REDKeyframe::EasingType::ELASTIC_OUT)
  890. {
  891. return EaseElasticOut::create(pAction, *fEasingOpt);
  892. }
  893. else if (easingType == REDKeyframe::EasingType::ELASTIC_INOUT)
  894. {
  895. return EaseElasticInOut::create(pAction, *fEasingOpt);
  896. }
  897. //add new
  898. else if (easingType == REDKeyframe::EasingType::SineIn)
  899. {
  900. return EaseSineIn::create(pAction);
  901. }
  902. else if (easingType == REDKeyframe::EasingType::SineOut)
  903. {
  904. return EaseSineOut::create(pAction);
  905. }
  906. else if (easingType == REDKeyframe::EasingType::SineInOut)
  907. {
  908. return EaseSineInOut::create(pAction);
  909. }
  910. else if (easingType == REDKeyframe::EasingType::ExponentialIn)
  911. {
  912. return EaseExponentialIn::create(pAction);
  913. }
  914. else if (easingType == REDKeyframe::EasingType::ExponentialOut)
  915. {
  916. return EaseExponentialOut::create(pAction);
  917. }
  918. else if (easingType == REDKeyframe::EasingType::ExponentialInOut)
  919. {
  920. return EaseExponentialInOut::create(pAction);
  921. }
  922. else if (easingType == REDKeyframe::EasingType::CircleActionIn)
  923. {
  924. return EaseCircleActionIn::create(pAction);
  925. }
  926. else if (easingType == REDKeyframe::EasingType::CircleActionOut)
  927. {
  928. return EaseCircleActionOut::create(pAction);
  929. }
  930. else if (easingType == REDKeyframe::EasingType::CircleActionInOut)
  931. {
  932. return EaseCircleActionInOut::create(pAction);
  933. }
  934. else if (easingType == REDKeyframe::EasingType::QuadraticActionIn)
  935. {
  936. return EaseQuadraticActionIn::create(pAction);
  937. }
  938. else if (easingType == REDKeyframe::EasingType::QuadraticActionOut)
  939. {
  940. return EaseQuadraticActionOut::create(pAction);
  941. }
  942. else if (easingType == REDKeyframe::EasingType::QuadraticActionInOut)
  943. {
  944. return EaseQuadraticActionInOut::create(pAction);
  945. }
  946. else if (easingType == REDKeyframe::EasingType::CubicActionIn)
  947. {
  948. return EaseCubicActionIn::create(pAction);
  949. }
  950. else if (easingType == REDKeyframe::EasingType::CubicActionOut)
  951. {
  952. return EaseCubicActionOut::create(pAction);
  953. }
  954. else if (easingType == REDKeyframe::EasingType::CubicActionInOut)
  955. {
  956. return EaseCubicActionInOut::create(pAction);
  957. }
  958. else if (easingType == REDKeyframe::EasingType::QuarticActionIn)
  959. {
  960. return EaseQuarticActionIn::create(pAction);
  961. }
  962. else if (easingType == REDKeyframe::EasingType::QuarticActionOut)
  963. {
  964. return EaseQuarticActionOut::create(pAction);
  965. }
  966. else if (easingType == REDKeyframe::EasingType::QuarticActionInOut)
  967. {
  968. return EaseQuarticActionInOut::create(pAction);
  969. }
  970. else if (easingType == REDKeyframe::EasingType::QuinticActionIn)
  971. {
  972. return EaseQuinticActionIn::create(pAction);
  973. }
  974. else if (easingType == REDKeyframe::EasingType::QuinticActionOut)
  975. {
  976. return EaseQuinticActionOut::create(pAction);
  977. }
  978. else if (easingType == REDKeyframe::EasingType::QuinticActionInOut)
  979. {
  980. return EaseQuinticActionInOut::create(pAction);
  981. }
  982. else if (easingType == REDKeyframe::EasingType::Custom)
  983. {
  984. auto ease = EaseBezierByTimeAction::create(pAction);
  985. ease->setBezierParamer(fEasingOpt[0], fEasingOpt[1], fEasingOpt[2], fEasingOpt[3]);
  986. return ease;
  987. }
  988. else
  989. {
  990. log("REDReader: Unknown easing type %d", static_cast<int>(easingType));
  991. return pAction;
  992. }
  993. }
  994. Sequence* REDAnimationManager::actionForCallbackChannel(REDSequenceProperty* channel) {
  995. float lastKeyframeTime = 0;
  996. Vector<FiniteTimeAction*> actions;
  997. auto& keyframes = channel->getKeyframes();
  998. ssize_t numKeyframes = keyframes.size();
  999. for (long i = 0; i < numKeyframes; ++i)
  1000. {
  1001. REDKeyframe *keyframe = keyframes.at(i);
  1002. float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime;
  1003. lastKeyframeTime = keyframe->getTime();
  1004. if(timeSinceLastKeyframe > 0) {
  1005. actions.pushBack(DelayTime::create(timeSinceLastKeyframe));
  1006. }
  1007. auto& keyVal = keyframe->getValue().asValueVector();
  1008. std::string selectorName = keyVal[0].asString();
  1009. REDReader::TargetType selectorTarget = (REDReader::TargetType)keyVal[1].asInt();
  1010. Ref* target = nullptr;
  1011. if(selectorTarget == REDReader::TargetType::DOCUMENT_ROOT)
  1012. target = _rootNode;
  1013. else if (selectorTarget == REDReader::TargetType::OWNER)
  1014. target = _owner;
  1015. if (selectorTarget == REDReader::TargetType::REDWISE_EVENT) {
  1016. CallFunc* wiseCall = CallFunc::create([=](){
  1017. EventCustom wiseEvent("REDAnimationManager_ActionForCallbackChannel_RedWiseEvent");
  1018. string name = selectorName;
  1019. wiseEvent.setUserData(&name);
  1020. Director::getInstance()->getEventDispatcher()->dispatchEvent(&wiseEvent);
  1021. });
  1022. actions.pushBack(wiseCall);
  1023. continue;
  1024. }
  1025. if(target != nullptr)
  1026. {
  1027. if(!selectorName.empty())
  1028. {
  1029. SEL_CallFuncN selCallFunc = 0;
  1030. REDSelectorResolver* targetAsREDSelectorResolver = dynamic_cast<REDSelectorResolver *>(target);
  1031. if(targetAsREDSelectorResolver != nullptr)
  1032. {
  1033. selCallFunc = targetAsREDSelectorResolver->onResolveREDCCCallFuncSelector(target, selectorName.c_str ());
  1034. }
  1035. if(selCallFunc == 0)
  1036. {
  1037. CCLOG("Skipping selector '%s' since no REDSelectorResolver is present.", selectorName.c_str());
  1038. }
  1039. else
  1040. {
  1041. auto savedTarget = std::make_shared<Vector<Ref*>>();
  1042. savedTarget->pushBack(target);
  1043. ///bug : 若actions不析构,将会导致target无法释放,出现内存泄漏
  1044. auto callback = CallFuncN::create([savedTarget, selectorName,selCallFunc](Node* sender){
  1045. Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("__CCCallFuncSelector__"+selectorName);
  1046. auto t = savedTarget->at(0);
  1047. (t->*selCallFunc)(sender);
  1048. });
  1049. actions.pushBack(callback);
  1050. }
  1051. }
  1052. else
  1053. {
  1054. CCLOG("Unexpected empty selector.");
  1055. }
  1056. }
  1057. }
  1058. if(actions.size() < 1) return nullptr;
  1059. return Sequence::create(actions);
  1060. }
  1061. Sequence* REDAnimationManager::actionForSoundChannel(REDSequenceProperty* channel) {
  1062. float lastKeyframeTime = 0;
  1063. Vector<FiniteTimeAction*> actions;
  1064. auto& keyframes = channel->getKeyframes();
  1065. ssize_t numKeyframes = keyframes.size();
  1066. for (int i = 0; i < numKeyframes; ++i)
  1067. {
  1068. REDKeyframe *keyframe = keyframes.at(i);
  1069. float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime;
  1070. lastKeyframeTime = keyframe->getTime();
  1071. if(timeSinceLastKeyframe > 0) {
  1072. actions.pushBack(DelayTime::create(timeSinceLastKeyframe));
  1073. }
  1074. stringstream ss (stringstream::in | stringstream::out);
  1075. auto& keyVal = keyframe->getValue().asValueVector();
  1076. std::string soundFile = keyVal[0].asString();
  1077. float pitch = 0.0f, pan = 0.0f, gain = 0.0f;
  1078. ss << keyVal[1].asString();
  1079. ss >> pitch;
  1080. ss.flush();
  1081. ss << keyVal[2].asString();
  1082. ss >> pan;
  1083. ss.flush();
  1084. ss << keyVal[3].asString();
  1085. ss >> gain;
  1086. ss.flush();
  1087. actions.pushBack(REDSoundEffect::actionWithSoundFile(soundFile, pitch, pan, gain));
  1088. }
  1089. if(actions.size() < 1) return nullptr;
  1090. return Sequence::create(actions);
  1091. }
  1092. Sequence* REDAnimationManager::actionForWiseChannel(REDSequenceProperty* channel) {
  1093. float lastKeyframeTime = 0;
  1094. Vector<FiniteTimeAction*> actions;
  1095. auto& keyframes = channel->getKeyframes();
  1096. ssize_t numKeyframes = keyframes.size();
  1097. for (int i = 0; i < numKeyframes; ++i) {
  1098. REDKeyframe *keyframe = keyframes.at(i);
  1099. float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime;
  1100. lastKeyframeTime = keyframe->getTime();
  1101. if(timeSinceLastKeyframe > 0) {
  1102. actions.pushBack(DelayTime::create(timeSinceLastKeyframe));
  1103. }
  1104. auto& keyVal = keyframe->getValue().asValueVector();
  1105. std::string btnFileName = keyVal.at(0).asString();
  1106. std::string eventName = keyVal.at(1).asString();
  1107. bool forcePostEvent = keyVal.at(2).asBool();
  1108. const ValueVector& params = keyVal.at(3).asValueVector();
  1109. actions.pushBack(REDWiseEffect::actionWithConfig(btnFileName, eventName, forcePostEvent, params));
  1110. }
  1111. if(actions.size() < 1) return nullptr;
  1112. return Sequence::create(actions);
  1113. }
  1114. void REDAnimationManager::runAction(Node *pNode, REDSequenceProperty *pSeqProp, float fTweenDuration)
  1115. {
  1116. auto& keyframes = pSeqProp->getKeyframes();
  1117. ssize_t numKeyframes = keyframes.size();
  1118. const char* propType = pSeqProp->getName();
  1119. if (numKeyframes > 1)
  1120. {
  1121. // Make an animation!
  1122. Vector<FiniteTimeAction*> actions;
  1123. REDKeyframe *keyframeFirst = keyframes.at(0);
  1124. float timeFirst = keyframeFirst->getTime() + fTweenDuration;
  1125. if (timeFirst > 0)
  1126. {
  1127. actions.pushBack(DelayTime::create(timeFirst));
  1128. }
  1129. for (ssize_t i = 0; i < numKeyframes - 1; ++i)
  1130. {
  1131. REDKeyframe *kf0 = keyframes.at(i);
  1132. REDKeyframe *kf1 = keyframes.at(i+1);
  1133. if(i == 0 && (strcmp(propType, "animation") == 0 || strcmp(propType, "frame") == 0 || strcmp(propType, "string") == 0 || strcmp(propType, "bakeAnimation") == 0)){
  1134. //第一个关键帧,子red文件和spine动画需要处理一下
  1135. ActionInterval *action = getAction(kf0, kf0, pSeqProp->getName(), pNode);
  1136. actions.pushBack(action);
  1137. }
  1138. ActionInterval *action = getAction(kf0, kf1, pSeqProp->getName(), pNode);
  1139. if (action)
  1140. {
  1141. // Apply easing
  1142. action = getEaseAction(action, kf0->getEasingType(), kf0->getEasingOpt());
  1143. actions.pushBack(action);
  1144. }
  1145. }
  1146. //add song
  1147. auto seq = Sequence::create(actions);
  1148. auto speed = Speed::create(seq, _speed);
  1149. speed->setTag(101);
  1150. pNode->runAction(speed);
  1151. }
  1152. else if (numKeyframes == 1 && (strcmp(propType, "animation") == 0 || strcmp(propType, "frame") == 0 || strcmp(propType, "bakeAnimation")))
  1153. {
  1154. // Make an animation!
  1155. Vector<FiniteTimeAction*> actions;
  1156. REDKeyframe *keyframeFirst = keyframes.at(0);
  1157. float timeFirst = keyframeFirst->getTime() + fTweenDuration;
  1158. if (timeFirst > 0)
  1159. {
  1160. actions.pushBack(DelayTime::create(timeFirst));
  1161. }
  1162. ActionInterval *action = getAction(keyframeFirst, keyframeFirst, pSeqProp->getName(), pNode);
  1163. actions.pushBack(action);
  1164. //add song
  1165. auto seq = Sequence::create(actions);
  1166. auto speed = Speed::create(seq, _speed);
  1167. speed->setTag(101);
  1168. pNode->runAction(speed);
  1169. }
  1170. }
  1171. void REDAnimationManager::updateSpeed(float speed, Node* node) {
  1172. std::vector<Action*> actions;
  1173. _rootNode->getActionManager()->getActionsByTag(101, node, actions);
  1174. std::vector<Action*>::iterator iter = actions.begin();
  1175. while (iter != actions.end()) {
  1176. Speed* sp = dynamic_cast<Speed*>(*iter);
  1177. if (sp != nullptr) {
  1178. sp->setSpeed(_speed);
  1179. }
  1180. iter ++;
  1181. }
  1182. Vector<Node*> children = node->getChildren();
  1183. Vector<Node*>::iterator iter2 = children.begin();
  1184. while (iter2 != children.end()) {
  1185. updateSpeed(speed, *iter2);
  1186. iter2 ++;
  1187. }
  1188. }
  1189. void REDAnimationManager::updateSpeed(float speed) {
  1190. _speed = speed;
  1191. updateSpeed(speed, _rootNode);
  1192. }
  1193. void REDAnimationManager::runAnimations(const char *pName, float fTweenDuration)
  1194. {
  1195. runAnimationsForSequenceNamedTweenDuration(pName, fTweenDuration);
  1196. }
  1197. void REDAnimationManager::runAnimations(const char *pName)
  1198. {
  1199. runAnimationsForSequenceNamed(pName);
  1200. }
  1201. void REDAnimationManager::runAnimationsWithTag(int nSeqId, int tag){
  1202. _lastTag = _nowTag;
  1203. _nowTag = tag;
  1204. runAnimationsForSequenceIdTweenDuration(nSeqId, 0);
  1205. }
  1206. void REDAnimationManager::runAnimationsWithListen(int nSeqId, REDAnimationManagerDelegate* listen){
  1207. runAnimationsForSequenceIdTweenDuration(nSeqId, 0, nullptr, listen);
  1208. }
  1209. void REDAnimationManager::runAnimations(int nSeqId, float fTweenDuraiton)
  1210. {
  1211. runAnimationsForSequenceIdTweenDuration(nSeqId, fTweenDuraiton);
  1212. }
  1213. void REDAnimationManager::refreshStatusOnFirstFream(int nSeqId, float fTweenDuration){
  1214. REDSequence* lastSeq = _runningSequence.first;
  1215. auto lastCallback = _runningSequence.second;
  1216. _runningSequence.first = nullptr;
  1217. _runningSequence.second = nullptr;
  1218. stopRootNodeAction(lastSeq, lastCallback);
  1219. resetAllChildrenAnimtion(nSeqId, fTweenDuration, false);
  1220. }
  1221. void REDAnimationManager::runAnimationsForSequenceIdTweenDuration(int nSeqId, float fTweenDuration, const std::function<void(cocos2d::Node*, AnimationCompleteType)> &callback, REDAnimationManagerDelegate* listen)
  1222. {
  1223. REDSequence *seq = getSequence(nSeqId);
  1224. CCASSERT(seq, "Sequence id couldn't be found");
  1225. REDSequence* lastSeq = _runningSequence.first;
  1226. auto lastCallback = _runningSequence.second;
  1227. _runningSequence.first = seq;
  1228. _runningSequence.second = callback;
  1229. stopRootNodeAction(lastSeq, lastCallback);
  1230. resetAllChildrenAnimtion(nSeqId, fTweenDuration, true);
  1231. _listen = listen;
  1232. // Set the running scene
  1233. if(seq->getCallbackChannel() != nullptr) {
  1234. Action* action = (Action *)actionForCallbackChannel(seq->getCallbackChannel());
  1235. if(action != nullptr) {
  1236. action->setTag(animationTag);
  1237. _rootNode->runAction(action);
  1238. _runningActions[_rootNode].pushBack(action);
  1239. }
  1240. }
  1241. if(seq->getSoundChannel() != nullptr) {
  1242. Action* action = (Action *)actionForSoundChannel(seq->getSoundChannel());
  1243. if(action != nullptr) {
  1244. action->setTag(animationTag);
  1245. _rootNode->runAction(action);
  1246. _runningActions[_rootNode].pushBack(action);
  1247. }
  1248. }
  1249. if (seq->getWiseChannel() != nullptr) {
  1250. Action* action = (Action *)actionForWiseChannel(seq->getWiseChannel());
  1251. if (action != nullptr) {
  1252. action->setTag(animationTag);
  1253. _rootNode->runAction(action);
  1254. _runningActions[_rootNode].pushBack(action);
  1255. }
  1256. }
  1257. // Make callback at end of sequence
  1258. Action *completeAction = Sequence::createWithTwoActions(DelayTime::create(seq->getDuration() + fTweenDuration),
  1259. CallFunc::create(std::bind(&REDAnimationManager::sequenceCompleted, this, callback)));
  1260. completeAction->setTag(animationTag);
  1261. _rootNode->runAction(completeAction);
  1262. _runningActions[_rootNode].pushBack(completeAction);
  1263. }
  1264. //add by djd
  1265. Spawn* REDAnimationManager::getNodeAniByName(const char *pName, Node* pNode,Vec2 orginPos){
  1266. Spawn *tAction = nullptr;
  1267. int nSeqId = getSequenceId(pName);
  1268. for (auto nodeSeqIter = _nodeSequences.begin(); nodeSeqIter != _nodeSequences.end(); ++nodeSeqIter)
  1269. {
  1270. Node *node = nodeSeqIter->first;
  1271. if (node != pNode) {
  1272. continue;
  1273. }
  1274. // Refer to REDReader::readKeyframe() for the real type of value
  1275. auto seqs = nodeSeqIter->second;
  1276. auto seqNodeProps = seqs[nSeqId];
  1277. if (!seqNodeProps.empty())
  1278. {
  1279. // Reset nodes that have sequence node properties, and run actions on them
  1280. Vector<FiniteTimeAction*> seqList;
  1281. for (auto iter = seqNodeProps.begin(); iter != seqNodeProps.end(); ++iter)
  1282. {
  1283. const std::string propName = iter->first;
  1284. REDSequenceProperty *pSeqProp = iter->second;
  1285. auto& keyframes = pSeqProp->getKeyframes();
  1286. ssize_t numKeyframes = keyframes.size();
  1287. if (numKeyframes > 1)
  1288. {
  1289. // Make an animation!
  1290. Vector<FiniteTimeAction*> actions;
  1291. REDKeyframe *keyframeFirst = keyframes.at(0);
  1292. float timeFirst = keyframeFirst->getTime();
  1293. if (timeFirst > 0)
  1294. {
  1295. actions.pushBack(DelayTime::create(timeFirst));
  1296. }
  1297. for (ssize_t i = 0; i < numKeyframes - 1; ++i)
  1298. {
  1299. REDKeyframe *kf0 = keyframes.at(i);
  1300. REDKeyframe *kf1 = keyframes.at(i+1);
  1301. ActionInterval *action = nullptr;
  1302. //重写position
  1303. if (strcmp(pSeqProp->getName(), "position") == 0)
  1304. {
  1305. float duration = kf1->getTime() - (kf0 ? kf0->getTime() : 0);
  1306. // Get position type
  1307. auto& array = getBaseValue(pNode, propName).asValueVector();
  1308. REDReader::PositionType type = (REDReader::PositionType)array[2].asInt();
  1309. // Get relative position
  1310. auto value = kf1->getValue().asValueVector();
  1311. float x = value[0].asFloat();
  1312. float y = value[1].asFloat();
  1313. Size containerSize = getContainerSize(pNode->getParent());
  1314. Vec2 absPos = getAbsolutePosition(Vec2(x,y), type, containerSize, propName);
  1315. action = MoveTo::create(duration, Vec2(absPos.x + orginPos.x, absPos.y + orginPos.y));
  1316. }else{
  1317. action = getAction(kf0, kf1, pSeqProp->getName(), pNode);
  1318. }
  1319. if (action)
  1320. {
  1321. // Apply easing
  1322. action = getEaseAction(action, kf0->getEasingType(), kf0->getEasingOpt());
  1323. actions.pushBack(action);
  1324. }
  1325. }
  1326. auto seq = Sequence::create(actions);
  1327. seqList.pushBack(seq);
  1328. }
  1329. }
  1330. tAction = Spawn::create(seqList);
  1331. }
  1332. break;
  1333. }
  1334. return tAction;
  1335. }
  1336. void REDAnimationManager::runAnimationsForSequenceNamedTweenDuration(const char *pName, float fTweenDuration, const std::function<void(cocos2d::Node*, AnimationCompleteType)> &callback)
  1337. {
  1338. int seqId = getSequenceId(pName);
  1339. if (seqId != -1) {
  1340. runAnimationsForSequenceIdTweenDuration(seqId, fTweenDuration, callback);
  1341. }else{
  1342. CCASSERT(false, "Sequence id couldn't be found");
  1343. }
  1344. }
  1345. void REDAnimationManager::runAnimationsForSequenceNamed(const char *pName)
  1346. {
  1347. runAnimationsForSequenceNamedTweenDuration(pName, 0);
  1348. }
  1349. void REDAnimationManager::debug()
  1350. {
  1351. }
  1352. void REDAnimationManager::setAnimationCompletedCallback(Ref *target, SEL_CallFunc callbackFunc) {
  1353. if (target)
  1354. {
  1355. target->retain();
  1356. }
  1357. if (_target)
  1358. {
  1359. _target->release();
  1360. }
  1361. _target = target;
  1362. _animationCompleteCallbackFunc = callbackFunc;
  1363. }
  1364. void REDAnimationManager::setCallFunc(CallFunc* callFunc, const std::string &callbackNamed)
  1365. {
  1366. _keyframeCallFuncs.insert(callbackNamed, callFunc);
  1367. }
  1368. void REDAnimationManager::sequenceCompleted(const std::function<void(cocos2d::Node*, AnimationCompleteType)> &callback)
  1369. {
  1370. REDSequence* sequence = _runningSequence.first;
  1371. const char *runningSequenceName = sequence->getName();
  1372. int seqId = sequence->getSequenceId();
  1373. int nextSeqId = sequence->getChainedSequenceId();
  1374. _runningSequence.first = nullptr;
  1375. _runningSequence.second = nullptr;
  1376. if(_lastCompletedSequenceName != runningSequenceName) {
  1377. _lastCompletedSequenceName = runningSequenceName;
  1378. }
  1379. if (nextSeqId != -1)
  1380. {
  1381. if(callback) {
  1382. callback(_rootNode, AnimationCompleteType::CHAINED);
  1383. }
  1384. REDAnimationManagerDelegate* animationManagerDelegate = dynamic_cast<REDAnimationManagerDelegate*>(_rootNode);
  1385. if (animationManagerDelegate) { // 方式1 新加的 用强转触发
  1386. //播完时间线,自动播放下一条
  1387. animationManagerDelegate->completedAnimationAndPlayNextSequenceNamed(runningSequenceName, seqId, _nowTag);
  1388. animationManagerDelegate->completedAnimationSequenceNamed(runningSequenceName, seqId, _nowTag);
  1389. } else if (_delegate) { // 方式2 原有的 用代理触发
  1390. //播完时间线,自动播放下一条
  1391. _delegate->completedAnimationAndPlayNextSequenceNamed(runningSequenceName, seqId, _nowTag);
  1392. _delegate->completedAnimationSequenceNamed(runningSequenceName, seqId, _nowTag);
  1393. }
  1394. if(_listen){
  1395. //播完时间线,自动播放下一条
  1396. auto listen = _listen;
  1397. _listen = nullptr;
  1398. listen->completedAnimationAndPlayNextSequenceNamed(runningSequenceName, seqId, _nowTag);
  1399. listen->completedAnimationSequenceNamed(runningSequenceName, seqId, _nowTag);
  1400. }
  1401. runAnimationsForSequenceIdTweenDuration(nextSeqId, 0, callback);
  1402. }
  1403. else
  1404. {
  1405. if(callback){
  1406. callback(_rootNode, AnimationCompleteType::COMPLETED);
  1407. }
  1408. REDAnimationManagerDelegate* animationManagerDelegate = dynamic_cast<REDAnimationManagerDelegate*>(_rootNode);
  1409. if (animationManagerDelegate) { // 方式1 新加的 用强转触发
  1410. animationManagerDelegate->completedAnimationSequenceNamed(runningSequenceName, seqId, _nowTag);
  1411. } else if (_delegate) { // 方式2 原有的
  1412. // There may be another runAnimation() call in this delegate method
  1413. // which will assign _runningSequence
  1414. _delegate->completedAnimationSequenceNamed(runningSequenceName, seqId, _nowTag);
  1415. }
  1416. if(_listen){
  1417. auto listen = _listen;
  1418. _listen = nullptr;
  1419. listen->completedAnimationSequenceNamed(runningSequenceName, seqId, _nowTag);
  1420. }
  1421. }
  1422. }
  1423. void REDAnimationManager::stopAllNodeAction(){
  1424. _lastTag = _nowTag;
  1425. REDSequence* lastSeq = _runningSequence.first;
  1426. auto lastCallback = _runningSequence.second;
  1427. _runningSequence.first = nullptr;
  1428. _runningSequence.second = nullptr;
  1429. stopRootNodeAction(lastSeq, lastCallback);
  1430. for (auto nodeSeqIter = _nodeSequences.begin(); nodeSeqIter != _nodeSequences.end(); ++nodeSeqIter)
  1431. {
  1432. Node *node = nodeSeqIter->first;
  1433. node->stopAllActions();
  1434. }
  1435. }
  1436. //MARK: - 私有封装
  1437. void REDAnimationManager::stopRootNodeAction(REDSequence* sequence, std::function<void(cocos2d::Node*, AnimationCompleteType)> callback){
  1438. // REDSequence* sequence = _runningSequence.first;
  1439. if(sequence)
  1440. {
  1441. // if(_runningSequence.second){
  1442. // _runningSequence.second(_rootNode, AnimationCompleteType::STOPED);
  1443. // }
  1444. if(callback){
  1445. callback(_rootNode, AnimationCompleteType::STOPED);
  1446. }
  1447. const char *runningSequenceName = sequence->getName();
  1448. int seqId = sequence->getSequenceId();
  1449. REDAnimationManagerDelegate* animationManagerDelegate = dynamic_cast<REDAnimationManagerDelegate*>(_rootNode);
  1450. if (animationManagerDelegate) { // 方式1 新加的 用强转触发
  1451. animationManagerDelegate->stopAnimationSequenceNamed(runningSequenceName, seqId, _lastTag);
  1452. } else if (_delegate) { // 方式2 原有的 用代理触发
  1453. _delegate->stopAnimationSequenceNamed(runningSequenceName, seqId, _lastTag);
  1454. }
  1455. if(_listen){
  1456. auto listen = _listen;
  1457. _listen = nullptr;
  1458. listen->stopAnimationSequenceNamed(runningSequenceName, seqId, _lastTag);
  1459. }
  1460. }
  1461. // _runningSequence.first = nullptr;
  1462. // _runningSequence.second = nullptr;
  1463. for(const auto &pair:_runningActions)
  1464. {
  1465. for(const auto action:pair.second)
  1466. pair.first->stopAction(action);
  1467. }
  1468. _runningActions.clear();
  1469. }
  1470. void REDAnimationManager::resetAllChildrenAnimtion(int nSeqId, float fTweenDuration, bool isRunAction){
  1471. for (auto nodeSeqIter = _nodeSequences.begin(); nodeSeqIter != _nodeSequences.end(); ++nodeSeqIter)
  1472. {
  1473. Node *node = nodeSeqIter->first;
  1474. node->stopAllActions();
  1475. // Refer to REDReader::readKeyframe() for the real type of value
  1476. auto seqs = nodeSeqIter->second;
  1477. auto seqNodeProps = seqs[nSeqId];
  1478. std::set<std::string> seqNodePropNames;
  1479. if (!seqNodeProps.empty())
  1480. {
  1481. // Reset nodes that have sequence node properties, and run actions on them
  1482. for (auto iter = seqNodeProps.begin(); iter != seqNodeProps.end(); ++iter)
  1483. {
  1484. const std::string propName = iter->first;
  1485. REDSequenceProperty *seqProp = iter->second;
  1486. seqNodePropNames.insert(propName);
  1487. setFirstFrame(node, seqProp, fTweenDuration);
  1488. if(isRunAction){
  1489. runAction(node, seqProp, fTweenDuration);
  1490. }
  1491. }
  1492. }
  1493. // Reset the nodes that may have been changed by other timelines
  1494. auto& nodeBaseValues = _baseValues[node];
  1495. if (!nodeBaseValues.empty())
  1496. {
  1497. for (auto iter = nodeBaseValues.begin(); iter != nodeBaseValues.end(); ++iter)
  1498. {
  1499. if (seqNodePropNames.find(iter->first) == seqNodePropNames.end())
  1500. {
  1501. setAnimatedProperty(iter->first, node, iter->second, nullptr, fTweenDuration,true);
  1502. }
  1503. }
  1504. }
  1505. auto& nodeObject = _objects[node];
  1506. if (!nodeObject.empty())
  1507. {
  1508. for (auto iter = nodeObject.begin(); iter != nodeObject.end(); ++iter)
  1509. {
  1510. if (seqNodePropNames.find(iter->first) == seqNodePropNames.end())
  1511. {
  1512. setAnimatedProperty(iter->first, node, Value(), iter->second, fTweenDuration,true);
  1513. }
  1514. }
  1515. }
  1516. }
  1517. }
  1518. //void REDAnimationManager::addListen(REDAnimationManagerDelegate* pDelegate){
  1519. // _listen = pDelegate;
  1520. //}
  1521. //
  1522. //void REDAnimationManager::removeListen(REDAnimationManagerDelegate* pDelegate){
  1523. // _listen = nullptr;
  1524. //}
  1525. #pragma mark - Begin BatchNode
  1526. ///add by songqingyu
  1527. void REDAnimationManager::setREDRootPath(std::string redRootPath){
  1528. _redRootPath = redRootPath;
  1529. }
  1530. void REDAnimationManager::setReaderPlist(std::unordered_set<std::string> plistSt,std::unordered_set<std::string> replacePlistSt){
  1531. _readerPlistSt = plistSt;
  1532. _repleacePlistSt=replacePlistSt;
  1533. }
  1534. void REDAnimationManager::addDisplayFrameKeykframe(REDKeyframe* kf){
  1535. _needResetSpriteFrames.pushBack(kf);
  1536. }
  1537. void REDAnimationManager::addSpriteFrameForNode(cocos2d::Node* pNode,const std::string& propName,const std::string& spriteFile,const std::string& spriteSheet){
  1538. _needResetObjProps[pNode][propName] = {spriteFile,spriteSheet};
  1539. }
  1540. void REDAnimationManager::resetSpriteFrameOfAllChildrenAnimtion(){
  1541. for (REDKeyframe *kf : _needResetSpriteFrames) {
  1542. const ValueVector& vv = kf->getValue().asValueVector();
  1543. std::string spriteSheet = vv[0].asString();
  1544. std::string spriteFile = vv[1].asString();
  1545. kf->setObject(getSpriteFrameByName(spriteFile, spriteSheet));
  1546. }
  1547. for (auto it : _needResetObjProps) {
  1548. for (auto pp : it.second) {
  1549. SpriteFrame* sf = dynamic_cast<SpriteFrame*>(getObject(it.first, pp.first));
  1550. if (sf) {
  1551. SpriteFrame* tmp = getSpriteFrameByName(pp.second[0], pp.second[1]);
  1552. if(sf != tmp && tmp){
  1553. setObject(tmp, it.first, pp.first);
  1554. }
  1555. }
  1556. }
  1557. }
  1558. }
  1559. ///
  1560. cocos2d::SpriteFrame* REDAnimationManager::getSpriteFrameByName(const string& spriteFile,const string& spriteSheet,bool useDefaultEmpty){
  1561. cocos2d::SpriteFrame* spriteFrame = nullptr;
  1562. SpriteFrameCache* frameCache = SpriteFrameCache::getInstance();
  1563. TextureCache* textCache = Director::getInstance()->getTextureCache();
  1564. spriteFrame = frameCache->getSpriteFrameByName(spriteFile,_readerPlistSt);
  1565. if (spriteFrame == nullptr) {
  1566. if (CocosConfig::getIgnoreCCBPath()) {
  1567. string spFile = _redRootPath + spriteFile;
  1568. Texture2D * texture = textCache->addImage(spFile.c_str());
  1569. if(texture != NULL) {
  1570. Rect bounds = Rect(0, 0,
  1571. texture->getContentSize().width,
  1572. texture->getContentSize().height);
  1573. spriteFrame = SpriteFrame::createWithTexture(texture, bounds);
  1574. if (spriteFrame) {
  1575. spriteFrame->setTextureFilename(spriteFile);
  1576. }
  1577. }
  1578. }
  1579. else{
  1580. if (spriteSheet.empty())
  1581. {
  1582. string spFile = _redRootPath + spriteFile;
  1583. Texture2D * texture = textCache->addImage(spFile.c_str());
  1584. if(texture != NULL) {
  1585. Rect bounds = Rect(0, 0,
  1586. texture->getContentSize().width,
  1587. texture->getContentSize().height);
  1588. spriteFrame = SpriteFrame::createWithTexture(texture, bounds);
  1589. if (spriteFrame) {
  1590. spriteFrame->setTextureFilename(spFile);
  1591. }
  1592. }
  1593. }
  1594. else
  1595. {
  1596. string spSheet = _redRootPath + spriteSheet;
  1597. frameCache->addSpriteFramesWithFile(spSheet);
  1598. spriteFrame = frameCache->getSpriteFrameByName(spriteFile,{spSheet});
  1599. }
  1600. }
  1601. }
  1602. //add by djd 防止崩溃,用默认空白图
  1603. if (!spriteFrame&&useDefaultEmpty) {
  1604. string spFile = CocosConfig::getDefaultEmptyPic();
  1605. Texture2D * texture = textCache->addImage(spFile.c_str());
  1606. if(texture != NULL) {
  1607. Rect bounds = Rect(0, 0,
  1608. texture->getContentSize().width,
  1609. texture->getContentSize().height);
  1610. spriteFrame = SpriteFrame::createWithTexture(texture, bounds);
  1611. if (spriteFrame) {
  1612. spriteFrame->setTextureFilename(spFile);
  1613. }
  1614. }
  1615. }
  1616. return spriteFrame;
  1617. }
  1618. ///add by songqingyu
  1619. #pragma mark End BatchNode -
  1620. // Custom actions
  1621. /************************************************************
  1622. REDSetSpriteFrame
  1623. ************************************************************/
  1624. REDSetSpriteFrame* REDSetSpriteFrame::create(SpriteFrame *pSpriteFrame)
  1625. {
  1626. REDSetSpriteFrame *ret = new (std::nothrow) REDSetSpriteFrame();
  1627. if (ret)
  1628. {
  1629. if (ret->initWithSpriteFrame(pSpriteFrame))
  1630. {
  1631. ret->autorelease();
  1632. }
  1633. else
  1634. {
  1635. CC_SAFE_DELETE(ret);
  1636. }
  1637. }
  1638. return ret;
  1639. }
  1640. bool REDSetSpriteFrame::initWithSpriteFrame(SpriteFrame *pSpriteFrame)
  1641. {
  1642. _spriteFrame = pSpriteFrame;
  1643. CC_SAFE_RETAIN(_spriteFrame);
  1644. return true;
  1645. }
  1646. REDSetSpriteFrame::~REDSetSpriteFrame()
  1647. {
  1648. CC_SAFE_RELEASE_NULL(_spriteFrame);
  1649. }
  1650. REDSetSpriteFrame* REDSetSpriteFrame::clone() const
  1651. {
  1652. // no copy constructor
  1653. auto a = new (std::nothrow) REDSetSpriteFrame();
  1654. a->initWithSpriteFrame(_spriteFrame);
  1655. a->autorelease();
  1656. return a;
  1657. }
  1658. REDSetSpriteFrame* REDSetSpriteFrame::reverse() const
  1659. {
  1660. // returns a copy of itself
  1661. return this->clone();
  1662. }
  1663. void REDSetSpriteFrame::update(float time)
  1664. {
  1665. ActionInstant::update(time);
  1666. static_cast<Sprite*>(_target)->setSpriteFrame(_spriteFrame);
  1667. }
  1668. /************************************************************
  1669. REDSoundEffect
  1670. ************************************************************/
  1671. REDSoundEffect* REDSoundEffect::actionWithSoundFile(const std::string &filename, float pitch, float pan, float gain) {
  1672. REDSoundEffect* pRet = new (std::nothrow) REDSoundEffect();
  1673. if (pRet != nullptr && pRet->initWithSoundFile(filename, pitch, pan, gain))
  1674. {
  1675. pRet->autorelease();
  1676. }
  1677. else
  1678. {
  1679. CC_SAFE_DELETE(pRet);
  1680. }
  1681. return pRet;
  1682. }
  1683. /************************************************************
  1684. REDWiseEffect
  1685. ************************************************************/
  1686. REDWiseEffect* REDWiseEffect::actionWithConfig(const std::string &btnFileName, const std::string &eventName, bool forcePostEvent, const ValueVector &params) {
  1687. REDWiseEffect* pRet = new (std::nothrow) REDWiseEffect();
  1688. if (pRet != nullptr && pRet->initWithConfig(btnFileName, eventName, forcePostEvent, params)) {
  1689. pRet->autorelease();
  1690. } else {
  1691. CC_SAFE_RELEASE(pRet);
  1692. }
  1693. return pRet;
  1694. }
  1695. REDWiseEffect::~REDWiseEffect() {}
  1696. bool REDWiseEffect::initWithConfig(const std::string &btnFileName, const std::string &eventName, bool forcePostEvent, const ValueVector &params) {
  1697. _btnFileName = btnFileName;
  1698. _eventName = eventName;
  1699. _forcePostEvent = forcePostEvent;
  1700. _params = params;
  1701. return true;
  1702. }
  1703. REDWiseEffect* REDWiseEffect::clone() const {
  1704. // no copy constructor
  1705. auto a = new (std::nothrow) REDWiseEffect();
  1706. a->initWithConfig(_btnFileName, _eventName, _forcePostEvent, _params);
  1707. a->autorelease();
  1708. return a;
  1709. }
  1710. REDWiseEffect* REDWiseEffect::reverse() const {
  1711. // returns a copy of itself
  1712. return this->clone();
  1713. }
  1714. void REDWiseEffect::update(float time) {
  1715. ActionInstant::update(time);
  1716. if (!CocosConfig::getCCBAudioEnable()) return;
  1717. std::vector<RWRedreamParam> params;
  1718. for (const auto & paramVector : _params) {
  1719. const auto & vec = paramVector.asValueVector();
  1720. std::string key = vec.at(0).asString();
  1721. float value = vec.at(1).asFloat();
  1722. RWRedreamParam param;
  1723. param.name = key;
  1724. param.value = value;
  1725. param.min = 0.0f;
  1726. param.max = 0.0f;
  1727. params.push_back(param);
  1728. }
  1729. RedWise::getInstance()->postEventByBnk(_btnFileName, _eventName, params, _forcePostEvent);
  1730. }
  1731. /************************************************************
  1732. REDAnimation
  1733. ************************************************************/
  1734. REDAnimation* REDAnimation::create(int animationId)
  1735. {
  1736. REDAnimation *ret = new (std::nothrow) REDAnimation();
  1737. if (ret)
  1738. {
  1739. if (ret->initWithAnimationId(animationId))
  1740. {
  1741. ret->autorelease();
  1742. }
  1743. else
  1744. {
  1745. CC_SAFE_DELETE(ret);
  1746. }
  1747. }
  1748. return ret;
  1749. }
  1750. bool REDAnimation::initWithAnimationId(int animationId)
  1751. {
  1752. _animationId = animationId;
  1753. return true;
  1754. }
  1755. REDAnimation::~REDAnimation()
  1756. {
  1757. }
  1758. REDAnimation* REDAnimation::clone() const
  1759. {
  1760. // no copy constructor
  1761. auto a = new (std::nothrow) REDAnimation();
  1762. a->initWithAnimationId(_animationId);
  1763. a->autorelease();
  1764. return a;
  1765. }
  1766. REDAnimation* REDAnimation::reverse() const
  1767. {
  1768. // returns a copy of itself
  1769. return this->clone();
  1770. }
  1771. void REDAnimation::update(float time)
  1772. {
  1773. REDAnimationManager *manager = REDAnimationManager::fromNode(_target);
  1774. if(manager){
  1775. if(_animationId == -2 || _animationId == -1){
  1776. // 选择了默认或者空,去获取自动播放的时间线
  1777. _animationId = manager->getAutoPlaySequenceId();
  1778. }
  1779. if(_animationId == -1){
  1780. //没有回去到auto时间线
  1781. manager->refreshStatusOnFirstFream(_animationId, 0.0f);
  1782. } else {
  1783. manager->runAnimationsForSequenceIdTweenDuration(_animationId, 0.0f);
  1784. }
  1785. }
  1786. }
  1787. REDSoundEffect::~REDSoundEffect()
  1788. {
  1789. }
  1790. bool REDSoundEffect::initWithSoundFile(const std::string &filename, float pitch, float pan, float gain) {
  1791. _soundFile = filename;
  1792. _pitch = pitch;
  1793. _pan = pan;
  1794. _gain = gain;
  1795. return true;
  1796. }
  1797. REDSoundEffect* REDSoundEffect::clone() const
  1798. {
  1799. // no copy constructor
  1800. auto a = new (std::nothrow) REDSoundEffect();
  1801. a->initWithSoundFile(_soundFile, _pitch, _pan, _gain);
  1802. a->autorelease();
  1803. return a;
  1804. }
  1805. REDSoundEffect* REDSoundEffect::reverse() const
  1806. {
  1807. // returns a copy of itself
  1808. return this->clone();
  1809. }
  1810. void REDSoundEffect::update(float time)
  1811. {
  1812. ActionInstant::update(time);
  1813. if (!CocosConfig::getCCBAudioEnable()) return;
  1814. // AudioEngine::play2d(_soundFile);
  1815. ///强制改为播放wwise
  1816. const std::string& eventName = _soundFile.substr(0, _soundFile.rfind("."));
  1817. RedWise::getInstance()->postEvent(eventName);
  1818. }
  1819. /************************************************************
  1820. REDLabelString
  1821. ************************************************************/
  1822. REDLabelString* REDLabelString::actionWithText(const std::string &text) {
  1823. REDLabelString* pRet = new (std::nothrow) REDLabelString();
  1824. if (pRet != nullptr && pRet->initWithText(text))
  1825. {
  1826. pRet->autorelease();
  1827. }
  1828. else
  1829. {
  1830. CC_SAFE_DELETE(pRet);
  1831. }
  1832. return pRet;
  1833. }
  1834. REDLabelString::~REDLabelString()
  1835. {
  1836. }
  1837. bool REDLabelString::initWithText(const std::string &text) {
  1838. _text = text;
  1839. return true;
  1840. }
  1841. REDLabelString* REDLabelString::clone() const
  1842. {
  1843. // no copy constructor
  1844. auto a = new (std::nothrow) REDLabelString();
  1845. a->initWithText(_text);
  1846. a->autorelease();
  1847. return a;
  1848. }
  1849. REDLabelString* REDLabelString::reverse() const
  1850. {
  1851. // returns a copy of itself
  1852. return this->clone();
  1853. }
  1854. void REDLabelString::update(float time)
  1855. {
  1856. ActionInstant::update(time);
  1857. auto label = static_cast<Label*>(_target);
  1858. auto model = label->getLocalizationModel();
  1859. if(model.isLocalization)
  1860. {
  1861. if(model.isCustom)
  1862. {
  1863. auto text = LocalizationMgr::getInstance()->getString(_text, model.customFilePath);
  1864. static_cast<Label*>(_target)->setString(text);
  1865. }
  1866. else
  1867. {
  1868. auto text = LocalizationMgr::getInstance()->getString(_text, LOCALIZATION_DEFAULT_FILE);
  1869. static_cast<Label*>(_target)->setString(text);
  1870. }
  1871. }
  1872. else
  1873. {
  1874. static_cast<Label*>(_target)->setString(_text);
  1875. }
  1876. }
  1877. /************************************************************
  1878. REDRotateTo
  1879. ************************************************************/
  1880. REDRotateTo* REDRotateTo::create(float fDuration, float fAngle)
  1881. {
  1882. REDRotateTo *ret = new (std::nothrow) REDRotateTo();
  1883. if (ret)
  1884. {
  1885. if (ret->initWithDuration(fDuration, fAngle))
  1886. {
  1887. ret->autorelease();
  1888. }
  1889. else
  1890. {
  1891. CC_SAFE_DELETE(ret);
  1892. }
  1893. }
  1894. return ret;
  1895. }
  1896. bool REDRotateTo::initWithDuration(float fDuration, float fAngle)
  1897. {
  1898. if (ActionInterval::initWithDuration(fDuration))
  1899. {
  1900. _dstAngle = fAngle;
  1901. return true;
  1902. }
  1903. else
  1904. {
  1905. return false;
  1906. }
  1907. }
  1908. REDRotateTo* REDRotateTo::clone() const
  1909. {
  1910. // no copy constructor
  1911. auto a = new (std::nothrow) REDRotateTo();
  1912. a->initWithDuration(_duration, _dstAngle);
  1913. a->autorelease();
  1914. return a;
  1915. }
  1916. REDRotateTo* REDRotateTo::reverse() const
  1917. {
  1918. CCASSERT(false, "reverse() is not supported in REDRotateTo");
  1919. return nullptr;
  1920. }
  1921. void REDRotateTo::startWithTarget(Node *pNode)
  1922. {
  1923. ActionInterval::startWithTarget(pNode);
  1924. _startAngle = _target->getRotation();
  1925. _diffAngle = _dstAngle - _startAngle;
  1926. }
  1927. void REDRotateTo::update(float time)
  1928. {
  1929. _target->setRotation(_startAngle + (_diffAngle * time))
  1930. ;
  1931. }
  1932. /************************************************************
  1933. REDRotateXTO
  1934. ************************************************************/
  1935. REDRotateXTo* REDRotateXTo::create(float fDuration, float fAngle)
  1936. {
  1937. REDRotateXTo *ret = new (std::nothrow) REDRotateXTo();
  1938. if (ret)
  1939. {
  1940. if (ret->initWithDuration(fDuration, fAngle))
  1941. {
  1942. ret->autorelease();
  1943. }
  1944. else
  1945. {
  1946. CC_SAFE_DELETE(ret);
  1947. }
  1948. }
  1949. return ret;
  1950. }
  1951. bool REDRotateXTo::initWithDuration(float fDuration, float fAngle)
  1952. {
  1953. if (ActionInterval::initWithDuration(fDuration))
  1954. {
  1955. _dstAngle = fAngle;
  1956. return true;
  1957. }
  1958. else
  1959. {
  1960. return false;
  1961. }
  1962. }
  1963. void REDRotateXTo::startWithTarget(Node *pNode)
  1964. {
  1965. //CCActionInterval::startWithTarget(pNode);
  1966. _originalTarget = pNode;
  1967. _target = pNode;
  1968. _elapsed = 0.0f;
  1969. _firstTick = true;
  1970. _startAngle = _target->getRotationSkewX();
  1971. _diffAngle = _dstAngle - _startAngle;
  1972. }
  1973. REDRotateXTo* REDRotateXTo::clone() const
  1974. {
  1975. // no copy constructor
  1976. auto a = new (std::nothrow) REDRotateXTo();
  1977. a->initWithDuration(_duration, _dstAngle);
  1978. a->autorelease();
  1979. return a;
  1980. }
  1981. REDRotateXTo* REDRotateXTo::reverse() const
  1982. {
  1983. CCASSERT(false, "reverse() is not supported in REDRotateXTo");
  1984. return nullptr;
  1985. }
  1986. void REDRotateXTo::update(float time)
  1987. {
  1988. _target->setRotationSkewX(_startAngle + (_diffAngle * time));
  1989. }
  1990. /************************************************************
  1991. REDRotateYTO
  1992. ************************************************************/
  1993. REDRotateYTo* REDRotateYTo::create(float fDuration, float fAngle)
  1994. {
  1995. REDRotateYTo *ret = new (std::nothrow) REDRotateYTo();
  1996. if (ret)
  1997. {
  1998. if (ret->initWithDuration(fDuration, fAngle))
  1999. {
  2000. ret->autorelease();
  2001. }
  2002. else
  2003. {
  2004. CC_SAFE_DELETE(ret);
  2005. }
  2006. }
  2007. return ret;
  2008. }
  2009. bool REDRotateYTo::initWithDuration(float fDuration, float fAngle)
  2010. {
  2011. if (ActionInterval::initWithDuration(fDuration))
  2012. {
  2013. _dstAngle = fAngle;
  2014. return true;
  2015. }
  2016. else
  2017. {
  2018. return false;
  2019. }
  2020. }
  2021. REDRotateYTo* REDRotateYTo::clone() const
  2022. {
  2023. // no copy constructor
  2024. auto a = new (std::nothrow) REDRotateYTo();
  2025. a->initWithDuration(_duration, _dstAngle);
  2026. a->autorelease();
  2027. return a;
  2028. }
  2029. REDRotateYTo* REDRotateYTo::reverse() const
  2030. {
  2031. CCASSERT(false, "reverse() is not supported in REDRotateXTo");
  2032. return nullptr;
  2033. }
  2034. void REDRotateYTo::startWithTarget(Node *pNode)
  2035. {
  2036. // ActionInterval::startWithTarget(pNode);
  2037. _originalTarget = pNode;
  2038. _target = pNode;
  2039. _elapsed = 0.0f;
  2040. _firstTick = true;
  2041. _startAngle = _target->getRotationSkewY();
  2042. _diffAngle = _dstAngle - _startAngle;
  2043. }
  2044. void REDRotateYTo::update(float time)
  2045. {
  2046. _target->setRotationSkewY(_startAngle + (_diffAngle * time));
  2047. }
  2048. /************************************************************
  2049. REDFrameIndexTo
  2050. ************************************************************/
  2051. REDFrameIndexTo* REDFrameIndexTo::create(float fDuration, int startIndex, int endIndex) {
  2052. REDFrameIndexTo *ret = new (std::nothrow) REDFrameIndexTo();
  2053. if (ret) {
  2054. if (ret->initWithDuration(fDuration, startIndex, endIndex)) {
  2055. ret->autorelease();
  2056. } else {
  2057. CC_SAFE_DELETE(ret);
  2058. }
  2059. }
  2060. return ret;
  2061. }
  2062. bool REDFrameIndexTo::initWithDuration(float fDuration, int startIndex, int endIndex) {
  2063. if (ActionInterval::initWithDuration(fDuration)) {
  2064. _startIndex = startIndex;
  2065. _endIndex = endIndex;
  2066. return true;
  2067. } else {
  2068. return false;
  2069. }
  2070. }
  2071. REDFrameIndexTo* REDFrameIndexTo::clone() const {
  2072. auto a = new (std::nothrow) REDFrameIndexTo();
  2073. a->initWithDuration(_duration, _startIndex, _endIndex);
  2074. a->autorelease();
  2075. return a;
  2076. }
  2077. REDFrameIndexTo* REDFrameIndexTo::reverse() const {
  2078. CCASSERT(false, "reverse() is not supported in REDFrameIndexTo");
  2079. return nullptr;
  2080. }
  2081. void REDFrameIndexTo::startWithTarget(cocos2d::Node *pNode) {
  2082. ActionInterval::startWithTarget(pNode);
  2083. _diffIndex = _endIndex - _startIndex;
  2084. }
  2085. void REDFrameIndexTo::update(float time) {
  2086. auto fasp = dynamic_cast<ZGFrameActionSprite*>(_target);
  2087. CCASSERT(fasp != nullptr, "REDFrameIndexTo 只支持 ZGFrameActionSprite类型的图片");
  2088. auto frameIndex = (int)(_startIndex + (_diffIndex * time));
  2089. fasp->setFrameIndex(frameIndex);
  2090. }
  2091. /************************************************************
  2092. REDParticlePosVarTo
  2093. ************************************************************/
  2094. REDParticlePosVarTo* REDParticlePosVarTo::create(float fDuration, cocos2d::Vec2 pos){
  2095. REDParticlePosVarTo *ret = new (std::nothrow) REDParticlePosVarTo();
  2096. if (ret)
  2097. {
  2098. if (ret->initWithDuration(fDuration, pos))
  2099. {
  2100. ret->autorelease();
  2101. }
  2102. else
  2103. {
  2104. CC_SAFE_DELETE(ret);
  2105. }
  2106. }
  2107. return ret;
  2108. }
  2109. bool REDParticlePosVarTo::initWithDuration(float fDuration, cocos2d::Vec2 pos){
  2110. if (ActionInterval::initWithDuration(fDuration))
  2111. {
  2112. _endGravity = pos;
  2113. return true;
  2114. }
  2115. else
  2116. {
  2117. return false;
  2118. }
  2119. }
  2120. REDParticlePosVarTo* REDParticlePosVarTo::clone() const {
  2121. // no copy constructor
  2122. auto a = new (std::nothrow) REDParticlePosVarTo();
  2123. a->initWithDuration(_duration, _endGravity);
  2124. a->autorelease();
  2125. return a;
  2126. }
  2127. REDParticlePosVarTo* REDParticlePosVarTo::reverse() const {
  2128. CCASSERT(false, "reverse() is not supported in REDParticlePosVarTo");
  2129. return nullptr;
  2130. }
  2131. void REDParticlePosVarTo::startWithTarget(cocos2d::Node *pNode) {
  2132. ActionInterval::startWithTarget(pNode);
  2133. _startGravity = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getPosVar();
  2134. _diffGravity = _endGravity - _startGravity;
  2135. }
  2136. void REDParticlePosVarTo::update(float time){
  2137. auto fasp = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2138. CCASSERT(fasp != nullptr, "REDGravityTo 只支持 ZMLCCParticleSystem");
  2139. auto gravity = _startGravity + (_diffGravity * time);
  2140. fasp->setPosVar(gravity);
  2141. }
  2142. /************************************************************
  2143. REDGravityTo
  2144. ************************************************************/
  2145. REDGravityTo* REDGravityTo::create(float fDuration, cocos2d::Vec2 gravity){
  2146. REDGravityTo *ret = new (std::nothrow) REDGravityTo();
  2147. if (ret)
  2148. {
  2149. if (ret->initWithDuration(fDuration, gravity))
  2150. {
  2151. ret->autorelease();
  2152. }
  2153. else
  2154. {
  2155. CC_SAFE_DELETE(ret);
  2156. }
  2157. }
  2158. return ret;
  2159. }
  2160. bool REDGravityTo::initWithDuration(float fDuration, cocos2d::Vec2 gravity){
  2161. if (ActionInterval::initWithDuration(fDuration))
  2162. {
  2163. _endGravity = gravity;
  2164. return true;
  2165. }
  2166. else
  2167. {
  2168. return false;
  2169. }
  2170. }
  2171. REDGravityTo* REDGravityTo::clone() const {
  2172. // no copy constructor
  2173. auto a = new (std::nothrow) REDGravityTo();
  2174. a->initWithDuration(_duration, _endGravity);
  2175. a->autorelease();
  2176. return a;
  2177. }
  2178. REDGravityTo* REDGravityTo::reverse() const {
  2179. CCASSERT(false, "reverse() is not supported in REDGravityTo");
  2180. return nullptr;
  2181. }
  2182. void REDGravityTo::startWithTarget(cocos2d::Node *pNode) {
  2183. ActionInterval::startWithTarget(pNode);
  2184. _startGravity = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getGravity();
  2185. _diffGravity = _endGravity - _startGravity;
  2186. }
  2187. void REDGravityTo::update(float time){
  2188. auto fasp = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2189. CCASSERT(fasp != nullptr, "REDGravityTo 只支持 ZMLCCParticleSystem");
  2190. auto gravity = _startGravity + (_diffGravity * time);
  2191. fasp->setGravity(gravity);
  2192. }
  2193. /************************************************************
  2194. REDParticleSpeedTo
  2195. ************************************************************/
  2196. //粒子速度
  2197. REDParticleSpeedTo* REDParticleSpeedTo::create(float fDuration, cocos2d::Vec2 gravity){
  2198. REDParticleSpeedTo *ret = new (std::nothrow) REDParticleSpeedTo();
  2199. if (ret)
  2200. {
  2201. if (ret->initWithDuration(fDuration, gravity))
  2202. {
  2203. ret->autorelease();
  2204. }
  2205. else
  2206. {
  2207. CC_SAFE_DELETE(ret);
  2208. }
  2209. }
  2210. return ret;
  2211. }
  2212. bool REDParticleSpeedTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2213. if (ActionInterval::initWithDuration(fDuration))
  2214. {
  2215. _endFloatVar = folatVar;
  2216. return true;
  2217. }
  2218. else
  2219. {
  2220. return false;
  2221. }
  2222. }
  2223. REDParticleSpeedTo* REDParticleSpeedTo::clone() const {
  2224. // no copy constructor
  2225. auto a = new (std::nothrow) REDParticleSpeedTo();
  2226. a->initWithDuration(_duration, _endFloatVar);
  2227. a->autorelease();
  2228. return a;
  2229. }
  2230. REDParticleSpeedTo* REDParticleSpeedTo::reverse() const {
  2231. CCASSERT(false, "reverse() is not supported in REDParticleSpeedTo");
  2232. return nullptr;
  2233. }
  2234. void REDParticleSpeedTo::startWithTarget(cocos2d::Node *pNode) {
  2235. ActionInterval::startWithTarget(pNode);
  2236. float a = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getSpeed();
  2237. float aVar = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getSpeedVar();
  2238. _startFloatVar = Vec2(a, aVar);
  2239. _diffFloatVar = _endFloatVar - _startFloatVar;
  2240. }
  2241. void REDParticleSpeedTo::update(float time){
  2242. auto par = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2243. CCASSERT(par != nullptr, "REDParticleSpeedTo 只支持 ZMLCCParticleSystem");
  2244. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2245. par->setSpeed(floatVar.x);
  2246. par->setSpeedVar(floatVar.y);
  2247. }
  2248. /************************************************************
  2249. REDParticleLifeTo
  2250. ************************************************************/
  2251. //粒子生命
  2252. REDParticleLifeTo* REDParticleLifeTo::create(float fDuration, cocos2d::Vec2 folatVar){
  2253. REDParticleLifeTo *ret = new (std::nothrow) REDParticleLifeTo();
  2254. if (ret)
  2255. {
  2256. if (ret->initWithDuration(fDuration, folatVar))
  2257. {
  2258. ret->autorelease();
  2259. }
  2260. else
  2261. {
  2262. CC_SAFE_DELETE(ret);
  2263. }
  2264. }
  2265. return ret;
  2266. }
  2267. bool REDParticleLifeTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2268. if (ActionInterval::initWithDuration(fDuration))
  2269. {
  2270. _endFloatVar = folatVar;
  2271. return true;
  2272. }
  2273. else
  2274. {
  2275. return false;
  2276. }
  2277. }
  2278. REDParticleLifeTo* REDParticleLifeTo::clone() const {
  2279. // no copy constructor
  2280. auto a = new (std::nothrow) REDParticleLifeTo();
  2281. a->initWithDuration(_duration, _endFloatVar);
  2282. a->autorelease();
  2283. return a;
  2284. }
  2285. REDParticleLifeTo* REDParticleLifeTo::reverse() const {
  2286. CCASSERT(false, "reverse() is not supported in REDParticleLifeTo");
  2287. return nullptr;
  2288. }
  2289. void REDParticleLifeTo::startWithTarget(cocos2d::Node *pNode) {
  2290. ActionInterval::startWithTarget(pNode);
  2291. float a = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getLife();
  2292. float aVar = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getLifeVar();
  2293. _startFloatVar = Vec2(a, aVar);
  2294. _diffFloatVar = _endFloatVar - _startFloatVar;
  2295. }
  2296. void REDParticleLifeTo::update(float time){
  2297. auto par = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2298. CCASSERT(par != nullptr, "REDParticleSpeedTo 只支持 ZMLCCParticleSystem");
  2299. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2300. par->setLife(floatVar.x);
  2301. par->setLifeVar(floatVar.y);
  2302. }
  2303. /************************************************************
  2304. REDParticleStartSizeTo
  2305. ************************************************************/
  2306. //粒子初始尺寸
  2307. REDParticleStartSizeTo* REDParticleStartSizeTo::create(float fDuration, cocos2d::Vec2 folatVar){
  2308. REDParticleStartSizeTo *ret = new (std::nothrow) REDParticleStartSizeTo();
  2309. if (ret)
  2310. {
  2311. if (ret->initWithDuration(fDuration, folatVar))
  2312. {
  2313. ret->autorelease();
  2314. }
  2315. else
  2316. {
  2317. CC_SAFE_DELETE(ret);
  2318. }
  2319. }
  2320. return ret;
  2321. }
  2322. bool REDParticleStartSizeTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2323. if (ActionInterval::initWithDuration(fDuration))
  2324. {
  2325. _endFloatVar = folatVar;
  2326. return true;
  2327. }
  2328. else
  2329. {
  2330. return false;
  2331. }
  2332. }
  2333. REDParticleStartSizeTo* REDParticleStartSizeTo::clone() const {
  2334. // no copy constructor
  2335. auto a = new (std::nothrow) REDParticleStartSizeTo();
  2336. a->initWithDuration(_duration, _endFloatVar);
  2337. a->autorelease();
  2338. return a;
  2339. }
  2340. REDParticleStartSizeTo* REDParticleStartSizeTo::reverse() const {
  2341. CCASSERT(false, "reverse() is not supported in REDParticleStartSizeTo");
  2342. return nullptr;
  2343. }
  2344. void REDParticleStartSizeTo::startWithTarget(cocos2d::Node *pNode) {
  2345. ActionInterval::startWithTarget(pNode);
  2346. float a = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getStartSize();
  2347. float aVar = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getStartSizeVar();
  2348. _startFloatVar = Vec2(a, aVar);
  2349. _diffFloatVar = _endFloatVar - _startFloatVar;
  2350. }
  2351. void REDParticleStartSizeTo::update(float time){
  2352. auto par = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2353. CCASSERT(par != nullptr, "REDParticleSpeedTo 只支持 ZMLCCParticleSystem");
  2354. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2355. par->setStartSize(floatVar.x);
  2356. par->setStartSizeVar(floatVar.y);
  2357. }
  2358. /************************************************************
  2359. REDParticleEndSizeTo
  2360. ************************************************************/
  2361. //粒子结束时的尺寸大小
  2362. REDParticleEndSizeTo* REDParticleEndSizeTo::create(float fDuration, cocos2d::Vec2 folatVar){
  2363. REDParticleEndSizeTo *ret = new (std::nothrow) REDParticleEndSizeTo();
  2364. if (ret)
  2365. {
  2366. if (ret->initWithDuration(fDuration, folatVar))
  2367. {
  2368. ret->autorelease();
  2369. }
  2370. else
  2371. {
  2372. CC_SAFE_DELETE(ret);
  2373. }
  2374. }
  2375. return ret;
  2376. }
  2377. bool REDParticleEndSizeTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2378. if (ActionInterval::initWithDuration(fDuration))
  2379. {
  2380. _endFloatVar = folatVar;
  2381. return true;
  2382. }
  2383. else
  2384. {
  2385. return false;
  2386. }
  2387. }
  2388. REDParticleEndSizeTo* REDParticleEndSizeTo::clone() const {
  2389. // no copy constructor
  2390. auto a = new (std::nothrow) REDParticleEndSizeTo();
  2391. a->initWithDuration(_duration, _endFloatVar);
  2392. a->autorelease();
  2393. return a;
  2394. }
  2395. REDParticleEndSizeTo* REDParticleEndSizeTo::reverse() const {
  2396. CCASSERT(false, "reverse() is not supported in REDParticleEndSizeTo");
  2397. return nullptr;
  2398. }
  2399. void REDParticleEndSizeTo::startWithTarget(cocos2d::Node *pNode) {
  2400. ActionInterval::startWithTarget(pNode);
  2401. float a = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getEndSize();
  2402. float aVar = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getEndSizeVar();
  2403. _startFloatVar = Vec2(a, aVar);
  2404. _diffFloatVar = _endFloatVar - _startFloatVar;
  2405. }
  2406. void REDParticleEndSizeTo::update(float time){
  2407. auto par = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2408. CCASSERT(par != nullptr, "REDParticleSpeedTo 只支持 ZMLCCParticleSystem");
  2409. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2410. par->setEndSize(floatVar.x);
  2411. par->setEndSizeVar(floatVar.y);
  2412. }
  2413. /************************************************************
  2414. REDParticleStartSpinTo
  2415. ************************************************************/
  2416. //粒子速度
  2417. REDParticleStartSpinTo* REDParticleStartSpinTo::create(float fDuration, cocos2d::Vec2 folatVar){
  2418. REDParticleStartSpinTo *ret = new (std::nothrow) REDParticleStartSpinTo();
  2419. if (ret)
  2420. {
  2421. if (ret->initWithDuration(fDuration, folatVar))
  2422. {
  2423. ret->autorelease();
  2424. }
  2425. else
  2426. {
  2427. CC_SAFE_DELETE(ret);
  2428. }
  2429. }
  2430. return ret;
  2431. }
  2432. bool REDParticleStartSpinTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2433. if (ActionInterval::initWithDuration(fDuration))
  2434. {
  2435. _endFloatVar = folatVar;
  2436. return true;
  2437. }
  2438. else
  2439. {
  2440. return false;
  2441. }
  2442. }
  2443. REDParticleStartSpinTo* REDParticleStartSpinTo::clone() const {
  2444. // no copy constructor
  2445. auto a = new (std::nothrow) REDParticleStartSpinTo();
  2446. a->initWithDuration(_duration, _endFloatVar);
  2447. a->autorelease();
  2448. return a;
  2449. }
  2450. REDParticleStartSpinTo* REDParticleStartSpinTo::reverse() const {
  2451. CCASSERT(false, "reverse() is not supported in REDParticleStartSpinTo");
  2452. return nullptr;
  2453. }
  2454. void REDParticleStartSpinTo::startWithTarget(cocos2d::Node *pNode) {
  2455. ActionInterval::startWithTarget(pNode);
  2456. float a = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getStartSpin();
  2457. float aVar = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getStartSpinVar();
  2458. _startFloatVar = Vec2(a, aVar);
  2459. _diffFloatVar = _endFloatVar - _startFloatVar;
  2460. }
  2461. void REDParticleStartSpinTo::update(float time){
  2462. auto par = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2463. CCASSERT(par != nullptr, "REDParticleSpeedTo 只支持 ZMLCCParticleSystem");
  2464. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2465. par->setStartSpin(floatVar.x);
  2466. par->setStartSpinVar(floatVar.y);
  2467. }
  2468. /************************************************************
  2469. REDParticleEndSpinTo
  2470. ************************************************************/
  2471. //粒子结束角度
  2472. REDParticleEndSpinTo* REDParticleEndSpinTo::create(float fDuration, cocos2d::Vec2 folatVar){
  2473. REDParticleEndSpinTo *ret = new (std::nothrow) REDParticleEndSpinTo();
  2474. if (ret)
  2475. {
  2476. if (ret->initWithDuration(fDuration, folatVar))
  2477. {
  2478. ret->autorelease();
  2479. }
  2480. else
  2481. {
  2482. CC_SAFE_DELETE(ret);
  2483. }
  2484. }
  2485. return ret;
  2486. }
  2487. bool REDParticleEndSpinTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2488. if (ActionInterval::initWithDuration(fDuration))
  2489. {
  2490. _endFloatVar = folatVar;
  2491. return true;
  2492. }
  2493. else
  2494. {
  2495. return false;
  2496. }
  2497. }
  2498. REDParticleEndSpinTo* REDParticleEndSpinTo::clone() const {
  2499. // no copy constructor
  2500. auto a = new (std::nothrow) REDParticleEndSpinTo();
  2501. a->initWithDuration(_duration, _endFloatVar);
  2502. a->autorelease();
  2503. return a;
  2504. }
  2505. REDParticleEndSpinTo* REDParticleEndSpinTo::reverse() const {
  2506. CCASSERT(false, "reverse() is not supported in REDParticleEndSpinTo");
  2507. return nullptr;
  2508. }
  2509. void REDParticleEndSpinTo::startWithTarget(cocos2d::Node *pNode) {
  2510. ActionInterval::startWithTarget(pNode);
  2511. float a = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getEndSpin();
  2512. float aVar = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getEndSpinVar();
  2513. _startFloatVar = Vec2(a, aVar);
  2514. _diffFloatVar = _endFloatVar - _startFloatVar;
  2515. }
  2516. void REDParticleEndSpinTo::update(float time){
  2517. auto par = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2518. CCASSERT(par != nullptr, "REDParticleSpeedTo 只支持 ZMLCCParticleSystem");
  2519. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2520. par->setEndSpin(floatVar.x);
  2521. par->setEndSpinVar(floatVar.y);
  2522. }
  2523. /************************************************************
  2524. REDParticleAngleTo
  2525. ************************************************************/
  2526. //粒子发射角度
  2527. REDParticleAngleTo* REDParticleAngleTo::create(float fDuration, cocos2d::Vec2 gravity){
  2528. REDParticleAngleTo *ret = new (std::nothrow) REDParticleAngleTo();
  2529. if (ret)
  2530. {
  2531. if (ret->initWithDuration(fDuration, gravity))
  2532. {
  2533. ret->autorelease();
  2534. }
  2535. else
  2536. {
  2537. CC_SAFE_DELETE(ret);
  2538. }
  2539. }
  2540. return ret;
  2541. }
  2542. bool REDParticleAngleTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2543. if (ActionInterval::initWithDuration(fDuration))
  2544. {
  2545. _endFloatVar = folatVar;
  2546. return true;
  2547. }
  2548. else
  2549. {
  2550. return false;
  2551. }
  2552. }
  2553. REDParticleAngleTo* REDParticleAngleTo::clone() const {
  2554. // no copy constructor
  2555. auto a = new (std::nothrow) REDParticleAngleTo();
  2556. a->initWithDuration(_duration, _endFloatVar);
  2557. a->autorelease();
  2558. return a;
  2559. }
  2560. REDParticleAngleTo* REDParticleAngleTo::reverse() const {
  2561. CCASSERT(false, "reverse() is not supported in REDParticleAngleTo");
  2562. return nullptr;
  2563. }
  2564. void REDParticleAngleTo::startWithTarget(cocos2d::Node *pNode) {
  2565. ActionInterval::startWithTarget(pNode);
  2566. float a = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getAngle();
  2567. float aVar = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getAngleVar();
  2568. _startFloatVar = Vec2(a, aVar);
  2569. _diffFloatVar = _endFloatVar - _startFloatVar;
  2570. }
  2571. void REDParticleAngleTo::update(float time){
  2572. auto par = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2573. CCASSERT(par != nullptr, "REDParticleSpeedTo 只支持 ZMLCCParticleSystem");
  2574. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2575. par->setAngle(floatVar.x);
  2576. par->setAngleVar(floatVar.y);
  2577. }
  2578. /************************************************************
  2579. REDParticleTangentialAccelTo
  2580. ************************************************************/
  2581. //粒子重力模式切向加速度
  2582. REDParticleTangentialAccelTo* REDParticleTangentialAccelTo::create(float fDuration, cocos2d::Vec2 gravity){
  2583. REDParticleTangentialAccelTo *ret = new (std::nothrow) REDParticleTangentialAccelTo();
  2584. if (ret)
  2585. {
  2586. if (ret->initWithDuration(fDuration, gravity))
  2587. {
  2588. ret->autorelease();
  2589. }
  2590. else
  2591. {
  2592. CC_SAFE_DELETE(ret);
  2593. }
  2594. }
  2595. return ret;
  2596. }
  2597. bool REDParticleTangentialAccelTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2598. if (ActionInterval::initWithDuration(fDuration))
  2599. {
  2600. _endFloatVar = folatVar;
  2601. return true;
  2602. }
  2603. else
  2604. {
  2605. return false;
  2606. }
  2607. }
  2608. REDParticleTangentialAccelTo* REDParticleTangentialAccelTo::clone() const {
  2609. // no copy constructor
  2610. auto a = new (std::nothrow) REDParticleTangentialAccelTo();
  2611. a->initWithDuration(_duration, _endFloatVar);
  2612. a->autorelease();
  2613. return a;
  2614. }
  2615. REDParticleTangentialAccelTo* REDParticleTangentialAccelTo::reverse() const {
  2616. CCASSERT(false, "reverse() is not supported in REDParticleTangentialAccelTo");
  2617. return nullptr;
  2618. }
  2619. void REDParticleTangentialAccelTo::startWithTarget(cocos2d::Node *pNode) {
  2620. ActionInterval::startWithTarget(pNode);
  2621. float a = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getTangentialAccel();
  2622. float aVar = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getTangentialAccelVar();
  2623. _startFloatVar = Vec2(a, aVar);
  2624. _diffFloatVar = _endFloatVar - _startFloatVar;
  2625. }
  2626. void REDParticleTangentialAccelTo::update(float time){
  2627. auto par = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2628. CCASSERT(par != nullptr, "REDParticleSpeedTo 只支持 ZMLCCParticleSystem");
  2629. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2630. par->setTangentialAccel(floatVar.x);
  2631. par->setTangentialAccelVar(floatVar.y);
  2632. }
  2633. /************************************************************
  2634. REDParticleRadialAccelTo
  2635. ************************************************************/
  2636. //粒子径向加速度
  2637. REDParticleRadialAccelTo* REDParticleRadialAccelTo::create(float fDuration, cocos2d::Vec2 gravity){
  2638. REDParticleRadialAccelTo *ret = new (std::nothrow) REDParticleRadialAccelTo();
  2639. if (ret)
  2640. {
  2641. if (ret->initWithDuration(fDuration, gravity))
  2642. {
  2643. ret->autorelease();
  2644. }
  2645. else
  2646. {
  2647. CC_SAFE_DELETE(ret);
  2648. }
  2649. }
  2650. return ret;
  2651. }
  2652. bool REDParticleRadialAccelTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2653. if (ActionInterval::initWithDuration(fDuration))
  2654. {
  2655. _endFloatVar = folatVar;
  2656. return true;
  2657. }
  2658. else
  2659. {
  2660. return false;
  2661. }
  2662. }
  2663. REDParticleRadialAccelTo* REDParticleRadialAccelTo::clone() const {
  2664. // no copy constructor
  2665. auto a = new (std::nothrow) REDParticleRadialAccelTo();
  2666. a->initWithDuration(_duration, _endFloatVar);
  2667. a->autorelease();
  2668. return a;
  2669. }
  2670. REDParticleRadialAccelTo* REDParticleRadialAccelTo::reverse() const {
  2671. CCASSERT(false, "reverse() is not supported in REDParticleRadialAccelTo");
  2672. return nullptr;
  2673. }
  2674. void REDParticleRadialAccelTo::startWithTarget(cocos2d::Node *pNode) {
  2675. ActionInterval::startWithTarget(pNode);
  2676. float a = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getRadialAccel();
  2677. float aVar = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getRadialAccelVar();
  2678. _startFloatVar = Vec2(a, aVar);
  2679. _diffFloatVar = _endFloatVar - _startFloatVar;
  2680. }
  2681. void REDParticleRadialAccelTo::update(float time){
  2682. auto par = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2683. CCASSERT(par != nullptr, "REDParticleSpeedTo 只支持 ZMLCCParticleSystem");
  2684. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2685. par->setRadialAccel(floatVar.x);
  2686. par->setRadialAccelVar(floatVar.y);
  2687. }
  2688. /************************************************************
  2689. REDParticleStartRadiusTo
  2690. ************************************************************/
  2691. //粒子半径模式开始半径
  2692. REDParticleStartRadiusTo* REDParticleStartRadiusTo::create(float fDuration, cocos2d::Vec2 gravity){
  2693. REDParticleStartRadiusTo *ret = new (std::nothrow) REDParticleStartRadiusTo();
  2694. if (ret)
  2695. {
  2696. if (ret->initWithDuration(fDuration, gravity))
  2697. {
  2698. ret->autorelease();
  2699. }
  2700. else
  2701. {
  2702. CC_SAFE_DELETE(ret);
  2703. }
  2704. }
  2705. return ret;
  2706. }
  2707. bool REDParticleStartRadiusTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2708. if (ActionInterval::initWithDuration(fDuration))
  2709. {
  2710. _endFloatVar = folatVar;
  2711. return true;
  2712. }
  2713. else
  2714. {
  2715. return false;
  2716. }
  2717. }
  2718. REDParticleStartRadiusTo* REDParticleStartRadiusTo::clone() const {
  2719. // no copy constructor
  2720. auto a = new (std::nothrow) REDParticleStartRadiusTo();
  2721. a->initWithDuration(_duration, _endFloatVar);
  2722. a->autorelease();
  2723. return a;
  2724. }
  2725. REDParticleStartRadiusTo* REDParticleStartRadiusTo::reverse() const {
  2726. CCASSERT(false, "reverse() is not supported in REDParticleStartRadiusTo");
  2727. return nullptr;
  2728. }
  2729. void REDParticleStartRadiusTo::startWithTarget(cocos2d::Node *pNode) {
  2730. ActionInterval::startWithTarget(pNode);
  2731. float a = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getStartRadius();
  2732. float aVar = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getStartRadiusVar();
  2733. _startFloatVar = Vec2(a, aVar);
  2734. _diffFloatVar = _endFloatVar - _startFloatVar;
  2735. }
  2736. void REDParticleStartRadiusTo::update(float time){
  2737. auto par = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2738. CCASSERT(par != nullptr, "REDParticleSpeedTo 只支持 ZMLCCParticleSystem");
  2739. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2740. par->setStartRadius(floatVar.x);
  2741. par->setStartRadiusVar(floatVar.y);
  2742. }
  2743. /************************************************************
  2744. REDParticleEndRadiusTo
  2745. ************************************************************/
  2746. //粒子半径模式结束半径
  2747. REDParticleEndRadiusTo* REDParticleEndRadiusTo::create(float fDuration, cocos2d::Vec2 gravity){
  2748. REDParticleEndRadiusTo *ret = new (std::nothrow) REDParticleEndRadiusTo();
  2749. if (ret)
  2750. {
  2751. if (ret->initWithDuration(fDuration, gravity))
  2752. {
  2753. ret->autorelease();
  2754. }
  2755. else
  2756. {
  2757. CC_SAFE_DELETE(ret);
  2758. }
  2759. }
  2760. return ret;
  2761. }
  2762. bool REDParticleEndRadiusTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2763. if (ActionInterval::initWithDuration(fDuration))
  2764. {
  2765. _endFloatVar = folatVar;
  2766. return true;
  2767. }
  2768. else
  2769. {
  2770. return false;
  2771. }
  2772. }
  2773. REDParticleEndRadiusTo* REDParticleEndRadiusTo::clone() const {
  2774. // no copy constructor
  2775. auto a = new (std::nothrow) REDParticleEndRadiusTo();
  2776. a->initWithDuration(_duration, _endFloatVar);
  2777. a->autorelease();
  2778. return a;
  2779. }
  2780. REDParticleEndRadiusTo* REDParticleEndRadiusTo::reverse() const {
  2781. CCASSERT(false, "reverse() is not supported in REDParticleEndRadiusTo");
  2782. return nullptr;
  2783. }
  2784. void REDParticleEndRadiusTo::startWithTarget(cocos2d::Node *pNode) {
  2785. ActionInterval::startWithTarget(pNode);
  2786. float a = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getEndRadius();
  2787. float aVar = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getEndRadiusVar();
  2788. _startFloatVar = Vec2(a, aVar);
  2789. _diffFloatVar = _endFloatVar - _startFloatVar;
  2790. }
  2791. void REDParticleEndRadiusTo::update(float time){
  2792. auto par = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2793. CCASSERT(par != nullptr, "REDParticleSpeedTo 只支持 ZMLCCParticleSystem");
  2794. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2795. par->setEndRadius(floatVar.x);
  2796. par->setEndRadiusVar(floatVar.y);
  2797. }
  2798. /************************************************************
  2799. REDParticleRotatePerSecondTo
  2800. ************************************************************/
  2801. //粒子速度
  2802. REDParticleRotatePerSecondTo* REDParticleRotatePerSecondTo::create(float fDuration, cocos2d::Vec2 gravity){
  2803. REDParticleRotatePerSecondTo *ret = new (std::nothrow) REDParticleRotatePerSecondTo();
  2804. if (ret)
  2805. {
  2806. if (ret->initWithDuration(fDuration, gravity))
  2807. {
  2808. ret->autorelease();
  2809. }
  2810. else
  2811. {
  2812. CC_SAFE_DELETE(ret);
  2813. }
  2814. }
  2815. return ret;
  2816. }
  2817. bool REDParticleRotatePerSecondTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2818. if (ActionInterval::initWithDuration(fDuration))
  2819. {
  2820. _endFloatVar = folatVar;
  2821. return true;
  2822. }
  2823. else
  2824. {
  2825. return false;
  2826. }
  2827. }
  2828. REDParticleRotatePerSecondTo* REDParticleRotatePerSecondTo::clone() const {
  2829. // no copy constructor
  2830. auto a = new (std::nothrow) REDParticleRotatePerSecondTo();
  2831. a->initWithDuration(_duration, _endFloatVar);
  2832. a->autorelease();
  2833. return a;
  2834. }
  2835. REDParticleRotatePerSecondTo* REDParticleRotatePerSecondTo::reverse() const {
  2836. CCASSERT(false, "reverse() is not supported in REDParticleSpeedTo");
  2837. return nullptr;
  2838. }
  2839. void REDParticleRotatePerSecondTo::startWithTarget(cocos2d::Node *pNode) {
  2840. ActionInterval::startWithTarget(pNode);
  2841. float a = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getRotatePerSecond();
  2842. float aVar = dynamic_cast<ZMLCCParticleSystem*>(pNode)->getRotatePerSecondVar();
  2843. _startFloatVar = Vec2(a, aVar);
  2844. _diffFloatVar = _endFloatVar - _startFloatVar;
  2845. }
  2846. void REDParticleRotatePerSecondTo::update(float time){
  2847. auto par = dynamic_cast<ZMLCCParticleSystem*>(_target);
  2848. CCASSERT(par != nullptr, "REDParticleSpeedTo 只支持 ZMLCCParticleSystem");
  2849. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2850. par->setRotatePerSecond(floatVar.x);
  2851. par->setRotatePerSecondVar(floatVar.y);
  2852. }
  2853. /************************************************************
  2854. REDEaseInstant
  2855. ************************************************************/
  2856. REDEaseInstant* REDEaseInstant::create(ActionInterval *pAction)
  2857. {
  2858. REDEaseInstant *pRet = new (std::nothrow) REDEaseInstant();
  2859. if (pRet && pRet->initWithAction(pAction))
  2860. {
  2861. pRet->autorelease();
  2862. }
  2863. else
  2864. {
  2865. CC_SAFE_RELEASE_NULL(pRet);
  2866. }
  2867. return pRet;
  2868. }
  2869. REDEaseInstant* REDEaseInstant::clone() const
  2870. {
  2871. // no copy constructor
  2872. auto a = new (std::nothrow) REDEaseInstant();
  2873. a->initWithAction(_inner);
  2874. a->autorelease();
  2875. return a;
  2876. }
  2877. REDEaseInstant* REDEaseInstant::reverse() const
  2878. {
  2879. return REDEaseInstant::create(_inner->reverse());
  2880. }
  2881. void REDEaseInstant::update(float dt)
  2882. {
  2883. if (dt < 0)
  2884. {
  2885. _inner->update(0);
  2886. }
  2887. else
  2888. {
  2889. _inner->update(1);
  2890. }
  2891. }
  2892. /************************************************************
  2893. EaseBezierByTimeAction
  2894. ************************************************************/
  2895. float crt(float v) {
  2896. if (v < 0) {
  2897. return -pow(-v, 1 / 3.0);
  2898. } else {
  2899. return pow(v, 1 / 3.0);
  2900. }
  2901. }
  2902. float max(float a, float b){
  2903. return a > b ? a : b;
  2904. }
  2905. float cardano(float xx1, float xx2, float x) {
  2906. float pa = x - 0;
  2907. float pb = x - xx1;
  2908. float pc = x - xx2;
  2909. float pd = x - 1; // to [t^3 + at^2 + bt + c] form:
  2910. float pa3 = pa * 3;
  2911. float pb3 = pb * 3;
  2912. float pc3 = pc * 3;
  2913. float d = -pa + pb3 - pc3 + pd;
  2914. float rd = 1 / d;
  2915. float r3 = 1 / 3.0;
  2916. float a = (pa3 - 6 * pb + pc3) * rd;
  2917. float a3 = a * r3;
  2918. float b = (-pa3 + pb3) * rd;
  2919. float c = pa * rd;
  2920. // then, determine p and q:
  2921. float p = (3 * b - a * a) * r3;
  2922. float p3 = p * r3;
  2923. float q = (2 * a * a * a - 9 * a * b + 27 * c) / 27.0;
  2924. float q2 = q / 2.0;
  2925. // and determine the discriminant:
  2926. float discriminant = q2 * q2 + p3 * p3 * p3;
  2927. // and some reserved variables
  2928. float u1;
  2929. float v1;
  2930. float x1;
  2931. float x2;
  2932. float x3; // If the discriminant is negative, use polar coordinates
  2933. // to get around square roots of negative numbers
  2934. if (discriminant < 0) {
  2935. float mp3 = -p * r3;
  2936. float mp33 = mp3 * mp3 * mp3;
  2937. float r = sqrt(mp33);
  2938. // compute cosphi corrected for IEEE float rounding:
  2939. float t = -q / (2.0 * r);
  2940. float cosphi = t < -1 ? -1 : t > 1 ? 1 : t;
  2941. float phi = acos(cosphi);
  2942. float crtr = crt(r);
  2943. float t1 = 2 * crtr;
  2944. float tau = 2 * M_PI;
  2945. x1 = t1 * cos(phi * r3) - a3;
  2946. x2 = t1 * cos((phi + tau) * r3) - a3;
  2947. x3 = t1 * cos((phi + 2 * tau) * r3) - a3; // choose best percentage
  2948. x1 = (float)((int)(x1*10000)/10000.0);
  2949. x2 = (float)((int)(x2*10000)/10000.0);
  2950. x3 = (float)((int)(x3*10000)/10000.0);
  2951. if (0 <= x1 && x1 <= 1) {
  2952. if (0 <= x2 && x2 <= 1) {
  2953. if (0 <= x3 && x3 <= 1) {
  2954. return max(max(x1, x2), x3);
  2955. } else {
  2956. return max(x1, x2);
  2957. }
  2958. } else if (0 <= x3 && x3 <= 1) {
  2959. return max(x1, x3);
  2960. } else {
  2961. return x1;
  2962. }
  2963. } else {
  2964. if (0 <= x2 && x2 <= 1) {
  2965. if (0 <= x3 && x3 <= 1) {
  2966. return max(x2, x3);
  2967. } else {
  2968. return x2;
  2969. }
  2970. } else {
  2971. return x3;
  2972. }
  2973. }
  2974. } else if (discriminant == 0) {
  2975. u1 = q2 < 0 ? crt(-q2) : -crt(q2);
  2976. x1 = 2 * u1 - a3;
  2977. x2 = -u1 - a3; // choose best percentage
  2978. if (0 <= x1 && x1 <= 1) {
  2979. if (0 <= x2 && x2 <= 1) {
  2980. return max(x1, x2);
  2981. } else {
  2982. return x1;
  2983. }
  2984. } else {
  2985. return x2;
  2986. }
  2987. } // one real root, and two imaginary roots
  2988. else {
  2989. float sd = sqrt(discriminant);
  2990. u1 = crt(-q2 + sd);
  2991. v1 = crt(q2 + sd);
  2992. x1 = u1 - v1 - a3;
  2993. return x1;
  2994. }
  2995. }
  2996. /// 贝塞尔曲线的计算,200710 by zml
  2997. /// @param x1 起始控制点的X
  2998. /// @param y1 起始控制点的Y
  2999. /// @param x2 结束控制点的X
  3000. /// @param y2 结束控制点的Y
  3001. /// @param x 输入的时间
  3002. /// @return 更新后的时间
  3003. float bezierByTime( float x1, float y1, float x2, float y2, float x ){
  3004. float percent = cardano(x1, x2, x); // t
  3005. return ((1 - percent) * (y1 + (y2 - y1) * percent) * 3 + percent * percent) * percent;
  3006. }
  3007. EaseBezierByTimeAction* EaseBezierByTimeAction::create(cocos2d::ActionInterval* action)
  3008. {
  3009. EaseBezierByTimeAction *ret = new (std::nothrow) EaseBezierByTimeAction();
  3010. if (ret && ret->initWithAction(action))
  3011. {
  3012. ret->autorelease();
  3013. return ret;
  3014. }
  3015. delete ret;
  3016. return nullptr;
  3017. }
  3018. void EaseBezierByTimeAction::setBezierParamer( float p0, float p1, float p2, float p3)
  3019. {
  3020. _p0 = p0;
  3021. _p1 = p1;
  3022. _p2 = p2;
  3023. _p3 = p3;
  3024. }
  3025. EaseBezierByTimeAction* EaseBezierByTimeAction::clone() const
  3026. {
  3027. // no copy constructor
  3028. if (_inner)
  3029. {
  3030. auto ret = EaseBezierByTimeAction::create(_inner->clone());
  3031. if (ret)
  3032. {
  3033. ret->setBezierParamer(_p0,_p1,_p2,_p3);
  3034. }
  3035. return ret;
  3036. }
  3037. return nullptr;
  3038. }
  3039. void EaseBezierByTimeAction::update(float time)
  3040. {
  3041. _inner->update(bezierByTime(_p0,_p1,_p2,_p3,time));
  3042. }
  3043. EaseBezierByTimeAction* EaseBezierByTimeAction::reverse() const
  3044. {
  3045. EaseBezierByTimeAction* reverseAction = EaseBezierByTimeAction::create(_inner->reverse());
  3046. reverseAction->setBezierParamer(_p3,_p2,_p1,_p0);
  3047. return reverseAction;
  3048. }
  3049. #pragma mark - RedBakeAnimationInstantAction
  3050. RedBakeAnimationInstantAction* RedBakeAnimationInstantAction::create(const RedBakeNodeFrame& frame) {
  3051. auto ret = new (std::nothrow) RedBakeAnimationInstantAction();
  3052. if (ret && ret->init(frame)) {
  3053. ret->autorelease();
  3054. return ret;
  3055. }
  3056. delete ret;
  3057. ret = nullptr;
  3058. return nullptr;
  3059. }
  3060. bool RedBakeAnimationInstantAction::init(const RedBakeNodeFrame& frame) {
  3061. _bakeFrame = frame;
  3062. return true;
  3063. }
  3064. void RedBakeAnimationInstantAction::update(float time) {
  3065. auto bakeNode = dynamic_cast<RedBakeNode*>(_target);
  3066. if (bakeNode == nullptr) {
  3067. return;
  3068. }
  3069. if (_bakeFrame.animationName.empty()) {
  3070. return;
  3071. }
  3072. bakeNode->playAnimation(_bakeFrame.animationName, _bakeFrame.loop);
  3073. }
  3074. RedBakeAnimationInstantAction* RedBakeAnimationInstantAction::clone() const {
  3075. auto ret = new (std::nothrow) RedBakeAnimationInstantAction();
  3076. if (ret && ret->init(_bakeFrame)) {
  3077. ret->autorelease();
  3078. return ret;
  3079. }
  3080. delete ret;
  3081. ret = nullptr;
  3082. return nullptr;
  3083. }
  3084. RedBakeAnimationInstantAction* RedBakeAnimationInstantAction::reverse() const {
  3085. return this->clone();
  3086. }
  3087. }