animationComponent.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "T3D/components/animation/animationComponent.h"
  23. #include "T3D/components/animation/animationComponent_ScriptBinding.h"
  24. #include "T3D/components/render/meshComponent.h"
  25. #include "platform/platform.h"
  26. #include "console/consoleTypes.h"
  27. #include "core/util/safeDelete.h"
  28. #include "core/resourceManager.h"
  29. #include "core/stream/fileStream.h"
  30. #include "console/consoleTypes.h"
  31. #include "console/consoleObject.h"
  32. #include "ts/tsShapeInstance.h"
  33. #include "core/stream/bitStream.h"
  34. #include "sim/netConnection.h"
  35. #include "gfx/gfxTransformSaver.h"
  36. #include "console/engineAPI.h"
  37. #include "lighting/lightQuery.h"
  38. #include "gfx/sim/debugDraw.h"
  39. extern bool gEditingMission;
  40. //////////////////////////////////////////////////////////////////////////
  41. // Callbacks
  42. //////////////////////////////////////////////////////////////////////////
  43. IMPLEMENT_CALLBACK( AnimationComponent, onAnimationStart, void, ( Component* obj, const String& animName ), ( obj, animName ),
  44. "@brief Called when we collide with another object.\n\n"
  45. "@param obj The ShapeBase object\n"
  46. "@param collObj The object we collided with\n"
  47. "@param vec Collision impact vector\n"
  48. "@param len Length of the impact vector\n" );
  49. IMPLEMENT_CALLBACK(AnimationComponent, onAnimationEnd, void, (Component* obj, const char* animName), (obj, animName),
  50. "@brief Called when we collide with another object.\n\n"
  51. "@param obj The ShapeBase object\n"
  52. "@param collObj The object we collided with\n"
  53. "@param vec Collision impact vector\n"
  54. "@param len Length of the impact vector\n" );
  55. IMPLEMENT_CALLBACK(AnimationComponent, onAnimationTrigger, void, (Component* obj, const String& animName, S32 triggerID), (obj, animName, triggerID),
  56. "@brief Called when we collide with another object.\n\n"
  57. "@param obj The ShapeBase object\n"
  58. "@param collObj The object we collided with\n"
  59. "@param vec Collision impact vector\n"
  60. "@param len Length of the impact vector\n" );
  61. //////////////////////////////////////////////////////////////////////////
  62. // Constructor/Destructor
  63. //////////////////////////////////////////////////////////////////////////
  64. AnimationComponent::AnimationComponent() : Component()
  65. {
  66. mNetworked = true;
  67. mNetFlags.set(Ghostable | ScopeAlways);
  68. mFriendlyName = "Animation(Component)";
  69. mComponentType = "Render";
  70. mDescription = getDescriptionText("Allows a rendered mesh to be animated");
  71. mOwnerRenderInst = NULL;
  72. mOwnerShapeInstance = NULL;
  73. for (U32 i = 0; i < MaxScriptThreads; i++)
  74. {
  75. mAnimationThreads[i].sequence = -1;
  76. mAnimationThreads[i].thread = 0;
  77. mAnimationThreads[i].sound = 0;
  78. mAnimationThreads[i].state = Thread::Stop;
  79. mAnimationThreads[i].atEnd = false;
  80. mAnimationThreads[i].timescale = 1.f;
  81. mAnimationThreads[i].position = -1.f;
  82. mAnimationThreads[i].transition = true;
  83. }
  84. }
  85. AnimationComponent::~AnimationComponent()
  86. {
  87. for(S32 i = 0;i < mFields.size();++i)
  88. {
  89. ComponentField &field = mFields[i];
  90. SAFE_DELETE_ARRAY(field.mFieldDescription);
  91. }
  92. SAFE_DELETE_ARRAY(mDescription);
  93. }
  94. IMPLEMENT_CO_NETOBJECT_V1(AnimationComponent);
  95. bool AnimationComponent::onAdd()
  96. {
  97. if (!Parent::onAdd())
  98. return false;
  99. //we need at least one layer
  100. for (U32 i = 0; i < MaxScriptThreads; i++)
  101. {
  102. Thread& st = mAnimationThreads[i];
  103. if (st.sequence != -1)
  104. {
  105. // TG: Need to see about suppressing non-cyclic sounds
  106. // if the sequences were activated before the object was
  107. // ghosted.
  108. // TG: Cyclic animations need to have a random pos if
  109. // they were started before the object was ghosted.
  110. // If there was something running on the old shape, the thread
  111. // needs to be reset. Otherwise we assume that it's been
  112. // initialized either by the constructor or from the server.
  113. bool reset = st.thread != 0;
  114. st.thread = 0;
  115. if (st.sequence != -1)
  116. {
  117. setThreadSequence(i, st.sequence, reset);
  118. }
  119. }
  120. if (st.thread)
  121. updateThread(st);
  122. }
  123. return true;
  124. }
  125. void AnimationComponent::onRemove()
  126. {
  127. Parent::onRemove();
  128. mOwnerRenderInst = NULL;
  129. }
  130. void AnimationComponent::onComponentAdd()
  131. {
  132. //test if this is a shape component!
  133. RenderComponentInterface *shapeInstanceInterface = mOwner->getComponent<RenderComponentInterface>();
  134. if (shapeInstanceInterface)
  135. {
  136. shapeInstanceInterface->onShapeInstanceChanged.notify(this, &AnimationComponent::targetShapeChanged);
  137. targetShapeChanged(shapeInstanceInterface);
  138. }
  139. }
  140. void AnimationComponent::componentAddedToOwner(Component *comp)
  141. {
  142. if (comp->getId() == getId())
  143. return;
  144. //test if this is a shape component!
  145. RenderComponentInterface *shapeInstanceInterface = dynamic_cast<RenderComponentInterface*>(comp);
  146. if (shapeInstanceInterface)
  147. {
  148. shapeInstanceInterface->onShapeInstanceChanged.notify(this, &AnimationComponent::targetShapeChanged);
  149. targetShapeChanged(shapeInstanceInterface);
  150. }
  151. }
  152. void AnimationComponent::componentRemovedFromOwner(Component *comp)
  153. {
  154. if (comp->getId() == getId()) //?????????
  155. return;
  156. //test if this is a shape component!
  157. RenderComponentInterface *shapeInstanceInterface = dynamic_cast<RenderComponentInterface*>(comp);
  158. if (shapeInstanceInterface)
  159. {
  160. shapeInstanceInterface->onShapeInstanceChanged.remove(this, &AnimationComponent::targetShapeChanged);
  161. mOwnerRenderInst = NULL;
  162. }
  163. }
  164. void AnimationComponent::targetShapeChanged(RenderComponentInterface* instanceInterface)
  165. {
  166. mOwnerRenderInst = instanceInterface;
  167. if (!mOwnerRenderInst || !getShape())
  168. return;
  169. MeshComponent* meshComp = dynamic_cast<MeshComponent*>(mOwnerRenderInst);
  170. mOwnerShapeInstance = meshComp->getShapeInstance();
  171. if (!mOwnerShapeInstance)
  172. return;
  173. for (U32 i = 0; i < MaxScriptThreads; i++)
  174. {
  175. Thread& st = mAnimationThreads[i];
  176. st.thread = mOwnerShapeInstance->addThread();
  177. }
  178. }
  179. void AnimationComponent::initPersistFields()
  180. {
  181. Parent::initPersistFields();
  182. }
  183. U32 AnimationComponent::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  184. {
  185. U32 retMask = Parent::packUpdate(con, mask, stream);
  186. //early test if we lack an owner, ghost-wise
  187. //no point in trying, just re-queue the mask and go
  188. if (!mOwner || con->getGhostIndex(mOwner) == -1)
  189. {
  190. stream->writeFlag(false);
  191. return retMask |= ThreadMask;
  192. }
  193. else
  194. {
  195. stream->writeFlag(true);
  196. for (int i = 0; i < MaxScriptThreads; i++)
  197. {
  198. Thread& st = mAnimationThreads[i];
  199. if (stream->writeFlag( (st.sequence != -1 || st.state == Thread::Destroy) && (mask & (ThreadMaskN << i)) ) )
  200. {
  201. stream->writeInt(st.sequence,ThreadSequenceBits);
  202. stream->writeInt(st.state,2);
  203. stream->write(st.timescale);
  204. stream->write(st.position);
  205. stream->writeFlag(st.atEnd);
  206. stream->writeFlag(st.transition);
  207. }
  208. }
  209. }
  210. return retMask;
  211. }
  212. void AnimationComponent::unpackUpdate(NetConnection *con, BitStream *stream)
  213. {
  214. Parent::unpackUpdate(con, stream);
  215. if (stream->readFlag())
  216. {
  217. for (S32 i = 0; i < MaxScriptThreads; i++)
  218. {
  219. if (stream->readFlag())
  220. {
  221. Thread& st = mAnimationThreads[i];
  222. U32 seq = stream->readInt(ThreadSequenceBits);
  223. st.state = stream->readInt(2);
  224. stream->read( &st.timescale );
  225. stream->read( &st.position );
  226. st.atEnd = stream->readFlag();
  227. bool transition = stream->readFlag();
  228. if (!st.thread || st.sequence != seq && st.state != Thread::Destroy)
  229. setThreadSequence(i, seq, false, transition);
  230. else
  231. updateThread(st);
  232. }
  233. }
  234. }
  235. }
  236. void AnimationComponent::processTick()
  237. {
  238. Parent::processTick();
  239. if (!isActive())
  240. return;
  241. if (isServerObject())
  242. {
  243. // Server only...
  244. advanceThreads(TickSec);
  245. }
  246. }
  247. void AnimationComponent::advanceTime(F32 dt)
  248. {
  249. Parent::advanceTime(dt);
  250. // On the client, the shape threads and images are
  251. // advanced at framerate.
  252. advanceThreads(dt);
  253. }
  254. //
  255. const char *AnimationComponent::getThreadSequenceName(U32 slot)
  256. {
  257. Thread& st = mAnimationThreads[slot];
  258. if (st.sequence == -1)
  259. {
  260. // Invalid Animation.
  261. return "";
  262. }
  263. // Name Index
  264. TSShape* shape = getShape();
  265. if (shape)
  266. {
  267. const U32 nameIndex = shape->sequences[st.sequence].nameIndex;
  268. // Return Name.
  269. return shape->getName(nameIndex);
  270. }
  271. return "";
  272. }
  273. bool AnimationComponent::setThreadSequence(U32 slot, S32 seq, bool reset, bool transition, F32 transTime)
  274. {
  275. if (!mOwnerShapeInstance)
  276. return false;
  277. Thread& st = mAnimationThreads[slot];
  278. if (st.thread && st.sequence == seq && st.state == Thread::Play && !reset)
  279. return true;
  280. // Handle a -1 sequence, as this may be set when a thread has been destroyed.
  281. if (seq == -1)
  282. return true;
  283. if (seq < MaxSequenceIndex)
  284. {
  285. setMaskBits(-1);
  286. setMaskBits(ThreadMaskN << slot);
  287. st.sequence = seq;
  288. st.transition = transition;
  289. if (reset)
  290. {
  291. st.state = Thread::Play;
  292. st.atEnd = false;
  293. st.timescale = 1.f;
  294. st.position = 0.f;
  295. }
  296. if (mOwnerShapeInstance)
  297. {
  298. if (!st.thread)
  299. st.thread = mOwnerShapeInstance->addThread();
  300. if (transition)
  301. {
  302. mOwnerShapeInstance->transitionToSequence(st.thread, seq, st.position, transTime, true);
  303. }
  304. else
  305. {
  306. mOwnerShapeInstance->setSequence(st.thread, seq, 0);
  307. stopThreadSound(st);
  308. }
  309. updateThread(st);
  310. }
  311. return true;
  312. }
  313. return false;
  314. }
  315. S32 AnimationComponent::getThreadSequenceID(S32 slot)
  316. {
  317. if (slot >= 0 && slot < AnimationComponent::MaxScriptThreads)
  318. {
  319. return mAnimationThreads[slot].sequence;
  320. }
  321. else
  322. {
  323. return -1;
  324. }
  325. }
  326. void AnimationComponent::updateThread(Thread& st)
  327. {
  328. if (!mOwnerRenderInst)
  329. return;
  330. switch (st.state)
  331. {
  332. case Thread::Stop:
  333. {
  334. mOwnerShapeInstance->setTimeScale(st.thread, 1.f);
  335. mOwnerShapeInstance->setPos(st.thread, (st.timescale > 0.f) ? 0.0f : 1.0f);
  336. } // Drop through to pause state
  337. case Thread::Pause:
  338. {
  339. if (st.position != -1.f)
  340. {
  341. mOwnerShapeInstance->setTimeScale(st.thread, 1.f);
  342. mOwnerShapeInstance->setPos(st.thread, st.position);
  343. }
  344. mOwnerShapeInstance->setTimeScale(st.thread, 0.f);
  345. stopThreadSound(st);
  346. } break;
  347. case Thread::Play:
  348. {
  349. if (st.atEnd)
  350. {
  351. mOwnerShapeInstance->setTimeScale(st.thread, 1);
  352. mOwnerShapeInstance->setPos(st.thread, (st.timescale > 0.f) ? 1.0f : 0.0f);
  353. mOwnerShapeInstance->setTimeScale(st.thread, 0);
  354. stopThreadSound(st);
  355. st.state = Thread::Stop;
  356. }
  357. else
  358. {
  359. if (st.position != -1.f)
  360. {
  361. mOwnerShapeInstance->setTimeScale(st.thread, 1.f);
  362. mOwnerShapeInstance->setPos(st.thread, st.position);
  363. }
  364. mOwnerShapeInstance->setTimeScale(st.thread, st.timescale);
  365. if (!st.sound)
  366. {
  367. startSequenceSound(st);
  368. }
  369. }
  370. } break;
  371. case Thread::Destroy:
  372. {
  373. stopThreadSound(st);
  374. st.atEnd = true;
  375. st.sequence = -1;
  376. if (st.thread)
  377. {
  378. mOwnerShapeInstance->destroyThread(st.thread);
  379. st.thread = 0;
  380. }
  381. } break;
  382. }
  383. }
  384. bool AnimationComponent::stopThread(U32 slot)
  385. {
  386. Thread& st = mAnimationThreads[slot];
  387. if (st.sequence != -1 && st.state != Thread::Stop)
  388. {
  389. setMaskBits(ThreadMaskN << slot);
  390. st.state = Thread::Stop;
  391. updateThread(st);
  392. return true;
  393. }
  394. return false;
  395. }
  396. bool AnimationComponent::destroyThread(U32 slot)
  397. {
  398. Thread& st = mAnimationThreads[slot];
  399. if (st.sequence != -1 && st.state != Thread::Destroy)
  400. {
  401. setMaskBits(ThreadMaskN << slot);
  402. st.state = Thread::Destroy;
  403. updateThread(st);
  404. return true;
  405. }
  406. return false;
  407. }
  408. bool AnimationComponent::pauseThread(U32 slot)
  409. {
  410. Thread& st = mAnimationThreads[slot];
  411. if (st.sequence != -1 && st.state != Thread::Pause)
  412. {
  413. setMaskBits(ThreadMaskN << slot);
  414. st.state = Thread::Pause;
  415. updateThread(st);
  416. return true;
  417. }
  418. return false;
  419. }
  420. bool AnimationComponent::playThread(U32 slot)
  421. {
  422. Thread& st = mAnimationThreads[slot];
  423. if (st.sequence != -1 && st.state != Thread::Play)
  424. {
  425. setMaskBits(ThreadMaskN << slot);
  426. st.state = Thread::Play;
  427. updateThread(st);
  428. return true;
  429. }
  430. return false;
  431. }
  432. bool AnimationComponent::playThread(U32 slot, const char* name, bool transition, F32 transitionTime)
  433. {
  434. if (slot < AnimationComponent::MaxScriptThreads)
  435. {
  436. if (!dStrEqual(name, ""))
  437. {
  438. if (TSShape* shape = getShape())
  439. {
  440. S32 seq = shape->findSequence(name);
  441. if (seq != -1 && setThreadSequence(slot, seq, true, transition, transitionTime))
  442. {
  443. return true;
  444. }
  445. else if (seq == -1)
  446. {
  447. //We tried to play a non-existaint sequence, so stop the thread just in case
  448. destroyThread(slot);
  449. return false;
  450. }
  451. }
  452. }
  453. else
  454. {
  455. if (playThread(slot))
  456. return true;
  457. }
  458. }
  459. return false;
  460. }
  461. bool AnimationComponent::setThreadAnimation(U32 slot, const char* name)
  462. {
  463. if (slot < AnimationComponent::MaxScriptThreads)
  464. {
  465. if (!dStrEqual(name, ""))
  466. {
  467. if (TSShape* shape = getShape())
  468. {
  469. S32 seq = shape->findSequence(name);
  470. if (seq != -1 && setThreadSequence(slot, seq, false, false))
  471. {
  472. Thread& st = mAnimationThreads[slot];
  473. if (st.position == -1)
  474. st.position = 0;
  475. //st.state = Thread::Pause;
  476. return true;
  477. }
  478. else if (seq == -1)
  479. {
  480. //We tried to play a non-existaint sequence, so stop the thread just in case
  481. destroyThread(slot);
  482. return false;
  483. }
  484. }
  485. }
  486. else
  487. {
  488. if (playThread(slot))
  489. return true;
  490. }
  491. }
  492. return false;
  493. }
  494. bool AnimationComponent::setThreadPosition(U32 slot, F32 pos)
  495. {
  496. Thread& st = mAnimationThreads[slot];
  497. if (st.sequence != -1)
  498. {
  499. setMaskBits(ThreadMaskN << slot);
  500. st.position = pos;
  501. st.atEnd = false;
  502. updateThread(st);
  503. return true;
  504. }
  505. return false;
  506. }
  507. bool AnimationComponent::setThreadDir(U32 slot, bool forward)
  508. {
  509. Thread& st = mAnimationThreads[slot];
  510. if (st.sequence != -1)
  511. {
  512. if ((st.timescale >= 0.f) != forward)
  513. {
  514. setMaskBits(ThreadMaskN << slot);
  515. st.timescale *= -1.f;
  516. st.atEnd = false;
  517. updateThread(st);
  518. }
  519. return true;
  520. }
  521. return false;
  522. }
  523. bool AnimationComponent::setThreadTimeScale(U32 slot, F32 timeScale)
  524. {
  525. Thread& st = mAnimationThreads[slot];
  526. if (st.sequence != -1)
  527. {
  528. if (st.timescale != timeScale)
  529. {
  530. setMaskBits(ThreadMaskN << slot);
  531. st.timescale = timeScale;
  532. updateThread(st);
  533. }
  534. return true;
  535. }
  536. return false;
  537. }
  538. void AnimationComponent::stopThreadSound(Thread& thread)
  539. {
  540. return;
  541. }
  542. void AnimationComponent::startSequenceSound(Thread& thread)
  543. {
  544. return;
  545. }
  546. void AnimationComponent::advanceThreads(F32 dt)
  547. {
  548. if (!mOwnerRenderInst)
  549. return;
  550. for (U32 i = 0; i < MaxScriptThreads; i++)
  551. {
  552. Thread& st = mAnimationThreads[i];
  553. if (st.thread && st.sequence != -1)
  554. {
  555. bool cyclic = getShape()->sequences[st.sequence].isCyclic();
  556. if (!getShape()->sequences[st.sequence].isCyclic() &&
  557. !st.atEnd &&
  558. ((st.timescale > 0.f) ? mOwnerShapeInstance->getPos(st.thread) >= 1.0 : mOwnerShapeInstance->getPos(st.thread) <= 0))
  559. {
  560. st.atEnd = true;
  561. updateThread(st);
  562. if (!isGhost())
  563. {
  564. Con::executef(this, "onAnimationEnd", st.thread->getSequenceName());
  565. }
  566. }
  567. // Make sure the thread is still valid after the call to onEndSequence_callback().
  568. // Someone could have called destroyThread() while in there.
  569. if (st.thread)
  570. {
  571. mOwnerShapeInstance->advanceTime(dt, st.thread);
  572. }
  573. if (mOwnerShapeInstance && !isGhost())
  574. {
  575. for (U32 i = 1; i < 32; i++)
  576. {
  577. if (mOwnerShapeInstance->getTriggerState(i))
  578. {
  579. const char* animName = st.thread->getSequenceName().c_str();
  580. onAnimationTrigger_callback(this, animName, i);
  581. }
  582. }
  583. }
  584. if (isGhost())
  585. mOwnerShapeInstance->animate();
  586. }
  587. }
  588. }
  589. TSShape* AnimationComponent::getShape()
  590. {
  591. if (mOwner == NULL)
  592. return NULL;
  593. if (mOwnerRenderInst == NULL)
  594. return NULL;
  595. return mOwnerRenderInst->getShape();
  596. }
  597. S32 AnimationComponent::getAnimationCount()
  598. {
  599. if (getShape())
  600. return getShape()->sequences.size();
  601. else
  602. return 0;
  603. }
  604. S32 AnimationComponent::getAnimationIndex(const char* name)
  605. {
  606. if (getShape())
  607. return getShape()->findSequence(name);
  608. else
  609. return -1;
  610. }
  611. const char* AnimationComponent::getAnimationName(S32 index)
  612. {
  613. if (getShape())
  614. {
  615. if (index >= 0 && index < getShape()->sequences.size())
  616. return getShape()->getName(getShape()->sequences[index].nameIndex);
  617. }
  618. return "";
  619. }