animationComponent.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. }
  129. void AnimationComponent::onComponentAdd()
  130. {
  131. //test if this is a shape component!
  132. RenderComponentInterface *shapeInstanceInterface = mOwner->getComponent<RenderComponentInterface>();
  133. if (shapeInstanceInterface)
  134. {
  135. shapeInstanceInterface->onShapeInstanceChanged.notify(this, &AnimationComponent::targetShapeChanged);
  136. targetShapeChanged(shapeInstanceInterface);
  137. }
  138. }
  139. void AnimationComponent::componentAddedToOwner(Component *comp)
  140. {
  141. if (comp->getId() == getId())
  142. return;
  143. //test if this is a shape component!
  144. RenderComponentInterface *shapeInstanceInterface = dynamic_cast<RenderComponentInterface*>(comp);
  145. if (shapeInstanceInterface)
  146. {
  147. shapeInstanceInterface->onShapeInstanceChanged.notify(this, &AnimationComponent::targetShapeChanged);
  148. targetShapeChanged(shapeInstanceInterface);
  149. }
  150. }
  151. void AnimationComponent::componentRemovedFromOwner(Component *comp)
  152. {
  153. if (comp->getId() == getId()) //?????????
  154. return;
  155. //test if this is a shape component!
  156. RenderComponentInterface *shapeInstanceInterface = dynamic_cast<RenderComponentInterface*>(comp);
  157. if (shapeInstanceInterface)
  158. {
  159. shapeInstanceInterface->onShapeInstanceChanged.remove(this, &AnimationComponent::targetShapeChanged);
  160. mOwnerRenderInst = NULL;
  161. }
  162. }
  163. void AnimationComponent::targetShapeChanged(RenderComponentInterface* instanceInterface)
  164. {
  165. mOwnerRenderInst = instanceInterface;
  166. if (!mOwnerRenderInst || !getShape())
  167. return;
  168. MeshComponent* meshComp = dynamic_cast<MeshComponent*>(mOwnerRenderInst);
  169. mOwnerShapeInstance = meshComp->getShapeInstance();
  170. if (!mOwnerShapeInstance)
  171. return;
  172. for (U32 i = 0; i < MaxScriptThreads; i++)
  173. {
  174. Thread& st = mAnimationThreads[i];
  175. st.thread = mOwnerShapeInstance->addThread();
  176. }
  177. }
  178. void AnimationComponent::initPersistFields()
  179. {
  180. Parent::initPersistFields();
  181. }
  182. U32 AnimationComponent::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  183. {
  184. U32 retMask = Parent::packUpdate(con, mask, stream);
  185. //early test if we lack an owner, ghost-wise
  186. //no point in trying, just re-queue the mask and go
  187. if (!mOwner || con->getGhostIndex(mOwner) == -1)
  188. {
  189. stream->writeFlag(false);
  190. return retMask |= ThreadMask;
  191. }
  192. else
  193. {
  194. stream->writeFlag(true);
  195. for (int i = 0; i < MaxScriptThreads; i++)
  196. {
  197. Thread& st = mAnimationThreads[i];
  198. if (stream->writeFlag( (st.sequence != -1 || st.state == Thread::Destroy) && (mask & (ThreadMaskN << i)) ) )
  199. {
  200. stream->writeInt(st.sequence,ThreadSequenceBits);
  201. stream->writeInt(st.state,2);
  202. stream->write(st.timescale);
  203. stream->write(st.position);
  204. stream->writeFlag(st.atEnd);
  205. stream->writeFlag(st.transition);
  206. }
  207. }
  208. }
  209. return retMask;
  210. }
  211. void AnimationComponent::unpackUpdate(NetConnection *con, BitStream *stream)
  212. {
  213. Parent::unpackUpdate(con, stream);
  214. if (stream->readFlag())
  215. {
  216. for (S32 i = 0; i < MaxScriptThreads; i++)
  217. {
  218. if (stream->readFlag())
  219. {
  220. Thread& st = mAnimationThreads[i];
  221. U32 seq = stream->readInt(ThreadSequenceBits);
  222. st.state = stream->readInt(2);
  223. stream->read( &st.timescale );
  224. stream->read( &st.position );
  225. st.atEnd = stream->readFlag();
  226. bool transition = stream->readFlag();
  227. if (!st.thread || st.sequence != seq && st.state != Thread::Destroy)
  228. setThreadSequence(i, seq, false, transition);
  229. else
  230. updateThread(st);
  231. }
  232. }
  233. }
  234. }
  235. void AnimationComponent::processTick()
  236. {
  237. Parent::processTick();
  238. if (!isActive())
  239. return;
  240. if (isServerObject())
  241. {
  242. // Server only...
  243. advanceThreads(TickSec);
  244. }
  245. }
  246. void AnimationComponent::advanceTime(F32 dt)
  247. {
  248. Parent::advanceTime(dt);
  249. // On the client, the shape threads and images are
  250. // advanced at framerate.
  251. advanceThreads(dt);
  252. }
  253. //
  254. const char *AnimationComponent::getThreadSequenceName(U32 slot)
  255. {
  256. Thread& st = mAnimationThreads[slot];
  257. if (st.sequence == -1)
  258. {
  259. // Invalid Animation.
  260. return "";
  261. }
  262. // Name Index
  263. TSShape* shape = getShape();
  264. if (shape)
  265. {
  266. const U32 nameIndex = shape->sequences[st.sequence].nameIndex;
  267. // Return Name.
  268. return shape->getName(nameIndex);
  269. }
  270. return "";
  271. }
  272. bool AnimationComponent::setThreadSequence(U32 slot, S32 seq, bool reset, bool transition, F32 transTime)
  273. {
  274. if (!mOwnerShapeInstance)
  275. return false;
  276. Thread& st = mAnimationThreads[slot];
  277. if (st.thread && st.sequence == seq && st.state == Thread::Play && !reset)
  278. return true;
  279. // Handle a -1 sequence, as this may be set when a thread has been destroyed.
  280. if (seq == -1)
  281. return true;
  282. if (seq < MaxSequenceIndex)
  283. {
  284. setMaskBits(-1);
  285. setMaskBits(ThreadMaskN << slot);
  286. st.sequence = seq;
  287. st.transition = transition;
  288. if (reset)
  289. {
  290. st.state = Thread::Play;
  291. st.atEnd = false;
  292. st.timescale = 1.f;
  293. st.position = 0.f;
  294. }
  295. if (mOwnerShapeInstance)
  296. {
  297. if (!st.thread)
  298. st.thread = mOwnerShapeInstance->addThread();
  299. if (transition)
  300. {
  301. mOwnerShapeInstance->transitionToSequence(st.thread, seq, st.position, transTime, true);
  302. }
  303. else
  304. {
  305. mOwnerShapeInstance->setSequence(st.thread, seq, 0);
  306. stopThreadSound(st);
  307. }
  308. updateThread(st);
  309. }
  310. return true;
  311. }
  312. return false;
  313. }
  314. S32 AnimationComponent::getThreadSequenceID(S32 slot)
  315. {
  316. if (slot >= 0 && slot < AnimationComponent::MaxScriptThreads)
  317. {
  318. return mAnimationThreads[slot].sequence;
  319. }
  320. else
  321. {
  322. return -1;
  323. }
  324. }
  325. void AnimationComponent::updateThread(Thread& st)
  326. {
  327. if (!mOwnerShapeInstance)
  328. return;
  329. switch (st.state)
  330. {
  331. case Thread::Stop:
  332. {
  333. mOwnerShapeInstance->setTimeScale(st.thread, 1.f);
  334. mOwnerShapeInstance->setPos(st.thread, (st.timescale > 0.f) ? 0.0f : 1.0f);
  335. } // Drop through to pause state
  336. case Thread::Pause:
  337. {
  338. if (st.position != -1.f)
  339. {
  340. mOwnerShapeInstance->setTimeScale(st.thread, 1.f);
  341. mOwnerShapeInstance->setPos(st.thread, st.position);
  342. }
  343. mOwnerShapeInstance->setTimeScale(st.thread, 0.f);
  344. stopThreadSound(st);
  345. } break;
  346. case Thread::Play:
  347. {
  348. if (st.atEnd)
  349. {
  350. mOwnerShapeInstance->setTimeScale(st.thread, 1);
  351. mOwnerShapeInstance->setPos(st.thread, (st.timescale > 0.f) ? 1.0f : 0.0f);
  352. mOwnerShapeInstance->setTimeScale(st.thread, 0);
  353. stopThreadSound(st);
  354. st.state = Thread::Stop;
  355. }
  356. else
  357. {
  358. if (st.position != -1.f)
  359. {
  360. mOwnerShapeInstance->setTimeScale(st.thread, 1.f);
  361. mOwnerShapeInstance->setPos(st.thread, st.position);
  362. }
  363. mOwnerShapeInstance->setTimeScale(st.thread, st.timescale);
  364. if (!st.sound)
  365. {
  366. startSequenceSound(st);
  367. }
  368. }
  369. } break;
  370. case Thread::Destroy:
  371. {
  372. stopThreadSound(st);
  373. st.atEnd = true;
  374. st.sequence = -1;
  375. if (st.thread)
  376. {
  377. mOwnerShapeInstance->destroyThread(st.thread);
  378. st.thread = 0;
  379. }
  380. } break;
  381. }
  382. }
  383. bool AnimationComponent::stopThread(U32 slot)
  384. {
  385. Thread& st = mAnimationThreads[slot];
  386. if (st.sequence != -1 && st.state != Thread::Stop)
  387. {
  388. setMaskBits(ThreadMaskN << slot);
  389. st.state = Thread::Stop;
  390. updateThread(st);
  391. return true;
  392. }
  393. return false;
  394. }
  395. bool AnimationComponent::destroyThread(U32 slot)
  396. {
  397. Thread& st = mAnimationThreads[slot];
  398. if (st.sequence != -1 && st.state != Thread::Destroy)
  399. {
  400. setMaskBits(ThreadMaskN << slot);
  401. st.state = Thread::Destroy;
  402. updateThread(st);
  403. return true;
  404. }
  405. return false;
  406. }
  407. bool AnimationComponent::pauseThread(U32 slot)
  408. {
  409. Thread& st = mAnimationThreads[slot];
  410. if (st.sequence != -1 && st.state != Thread::Pause)
  411. {
  412. setMaskBits(ThreadMaskN << slot);
  413. st.state = Thread::Pause;
  414. updateThread(st);
  415. return true;
  416. }
  417. return false;
  418. }
  419. bool AnimationComponent::playThread(U32 slot)
  420. {
  421. Thread& st = mAnimationThreads[slot];
  422. if (st.sequence != -1 && st.state != Thread::Play)
  423. {
  424. setMaskBits(ThreadMaskN << slot);
  425. st.state = Thread::Play;
  426. updateThread(st);
  427. return true;
  428. }
  429. return false;
  430. }
  431. bool AnimationComponent::playThread(U32 slot, const char* name, bool transition, F32 transitionTime)
  432. {
  433. if (slot < AnimationComponent::MaxScriptThreads)
  434. {
  435. if (!dStrEqual(name, ""))
  436. {
  437. if (TSShape* shape = getShape())
  438. {
  439. S32 seq = shape->findSequence(name);
  440. if (seq != -1 && setThreadSequence(slot, seq, true, transition, transitionTime))
  441. {
  442. return true;
  443. }
  444. else if (seq == -1)
  445. {
  446. //We tried to play a non-existaint sequence, so stop the thread just in case
  447. destroyThread(slot);
  448. return false;
  449. }
  450. }
  451. }
  452. else
  453. {
  454. if (playThread(slot))
  455. return true;
  456. }
  457. }
  458. return false;
  459. }
  460. bool AnimationComponent::setThreadAnimation(U32 slot, const char* name)
  461. {
  462. if (slot < AnimationComponent::MaxScriptThreads)
  463. {
  464. if (!dStrEqual(name, ""))
  465. {
  466. if (TSShape* shape = getShape())
  467. {
  468. S32 seq = shape->findSequence(name);
  469. if (seq != -1 && setThreadSequence(slot, seq, false, false))
  470. {
  471. Thread& st = mAnimationThreads[slot];
  472. if (st.position == -1)
  473. st.position = 0;
  474. //st.state = Thread::Pause;
  475. return true;
  476. }
  477. else if (seq == -1)
  478. {
  479. //We tried to play a non-existaint sequence, so stop the thread just in case
  480. destroyThread(slot);
  481. return false;
  482. }
  483. }
  484. }
  485. else
  486. {
  487. if (playThread(slot))
  488. return true;
  489. }
  490. }
  491. return false;
  492. }
  493. bool AnimationComponent::setThreadPosition(U32 slot, F32 pos)
  494. {
  495. Thread& st = mAnimationThreads[slot];
  496. if (st.sequence != -1)
  497. {
  498. setMaskBits(ThreadMaskN << slot);
  499. st.position = pos;
  500. st.atEnd = false;
  501. updateThread(st);
  502. return true;
  503. }
  504. return false;
  505. }
  506. bool AnimationComponent::setThreadDir(U32 slot, bool forward)
  507. {
  508. Thread& st = mAnimationThreads[slot];
  509. if (st.sequence != -1)
  510. {
  511. if ((st.timescale >= 0.f) != forward)
  512. {
  513. setMaskBits(ThreadMaskN << slot);
  514. st.timescale *= -1.f;
  515. st.atEnd = false;
  516. updateThread(st);
  517. }
  518. return true;
  519. }
  520. return false;
  521. }
  522. bool AnimationComponent::setThreadTimeScale(U32 slot, F32 timeScale)
  523. {
  524. Thread& st = mAnimationThreads[slot];
  525. if (st.sequence != -1)
  526. {
  527. if (st.timescale != timeScale)
  528. {
  529. setMaskBits(ThreadMaskN << slot);
  530. st.timescale = timeScale;
  531. updateThread(st);
  532. }
  533. return true;
  534. }
  535. return false;
  536. }
  537. void AnimationComponent::stopThreadSound(Thread& thread)
  538. {
  539. return;
  540. }
  541. void AnimationComponent::startSequenceSound(Thread& thread)
  542. {
  543. return;
  544. }
  545. void AnimationComponent::advanceThreads(F32 dt)
  546. {
  547. if (!mOwnerShapeInstance)
  548. return;
  549. for (U32 i = 0; i < MaxScriptThreads; i++)
  550. {
  551. Thread& st = mAnimationThreads[i];
  552. if (st.thread && st.sequence != -1)
  553. {
  554. bool cyclic = getShape()->sequences[st.sequence].isCyclic();
  555. if (!getShape()->sequences[st.sequence].isCyclic() &&
  556. !st.atEnd &&
  557. ((st.timescale > 0.f) ? mOwnerShapeInstance->getPos(st.thread) >= 1.0 : mOwnerShapeInstance->getPos(st.thread) <= 0))
  558. {
  559. st.atEnd = true;
  560. updateThread(st);
  561. if (!isGhost())
  562. {
  563. Con::executef(this, "onAnimationEnd", st.thread->getSequenceName());
  564. }
  565. }
  566. // Make sure the thread is still valid after the call to onEndSequence_callback().
  567. // Someone could have called destroyThread() while in there.
  568. if (st.thread)
  569. {
  570. mOwnerShapeInstance->advanceTime(dt, st.thread);
  571. }
  572. if (mOwnerShapeInstance && !isGhost())
  573. {
  574. for (U32 i = 1; i < 32; i++)
  575. {
  576. if (mOwnerShapeInstance->getTriggerState(i))
  577. {
  578. const char* animName = st.thread->getSequenceName().c_str();
  579. onAnimationTrigger_callback(this, animName, i);
  580. }
  581. }
  582. }
  583. if (isGhost())
  584. mOwnerShapeInstance->animate();
  585. }
  586. }
  587. }
  588. TSShape* AnimationComponent::getShape()
  589. {
  590. if (mOwner == NULL)
  591. return NULL;
  592. if (mOwnerRenderInst == NULL)
  593. return NULL;
  594. return mOwnerRenderInst->getShape();
  595. }
  596. S32 AnimationComponent::getAnimationCount()
  597. {
  598. if (getShape())
  599. return getShape()->sequences.size();
  600. else
  601. return 0;
  602. }
  603. S32 AnimationComponent::getAnimationIndex(const char* name)
  604. {
  605. if (getShape())
  606. return getShape()->findSequence(name);
  607. else
  608. return -1;
  609. }
  610. const char* AnimationComponent::getAnimationName(S32 index)
  611. {
  612. if (getShape())
  613. {
  614. if (index >= 0 && index < getShape()->sequences.size())
  615. return getShape()->getName(getShape()->sequences[index].nameIndex);
  616. }
  617. return "";
  618. }