VTrack.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "Verve/Core/VTrack.h"
  24. #include "Verve/Core/VGroup.h"
  25. #include "Verve/Core/VController.h"
  26. #include "math/mMath.h"
  27. //-----------------------------------------------------------------------------
  28. IMPLEMENT_CONOBJECT( VTrack );
  29. //-----------------------------------------------------------------------------
  30. VTrack::VTrack( void ) :
  31. mNextEvent( NULL )
  32. {
  33. setLabel( "DefaultTrack" );
  34. }
  35. //-----------------------------------------------------------------------------
  36. //
  37. // Tree Methods.
  38. //
  39. //-----------------------------------------------------------------------------
  40. //-----------------------------------------------------------------------------
  41. //
  42. // VTrack::onAttach();
  43. //
  44. // This callback subscribes this object to the controller's event signal.
  45. //
  46. //-----------------------------------------------------------------------------
  47. void VTrack::onAttach( void )
  48. {
  49. Parent::onAttach();
  50. // Valid Controller?
  51. if ( getController() )
  52. {
  53. // Subscribe to Updates.
  54. getController()->getControllerUpdateSignal().notify( this, &VTrack::onControllerUpdate );
  55. // Subscribe to Events.
  56. getController()->getControllerEventSignal().notify( this, &VTrack::onControllerEvent );
  57. }
  58. }
  59. //-----------------------------------------------------------------------------
  60. //
  61. // VTrack::onAttach();
  62. //
  63. // This callback removes this object from the controller's event signal
  64. // notification list.
  65. //
  66. //-----------------------------------------------------------------------------
  67. void VTrack::onDetach( void )
  68. {
  69. // Valid Controller?
  70. if ( getController() )
  71. {
  72. // Remove Update Notification.
  73. getController()->getControllerUpdateSignal().remove( this, &VTrack::onControllerUpdate );
  74. // Remove Event Notification.
  75. getController()->getControllerEventSignal().remove( this, &VTrack::onControllerEvent );
  76. }
  77. Parent::onDetach();
  78. }
  79. //-----------------------------------------------------------------------------
  80. //
  81. // Controller Methods.
  82. //
  83. //-----------------------------------------------------------------------------
  84. //-----------------------------------------------------------------------------
  85. //
  86. // VTrack::onControllerUpdate( pTime, pDelta );
  87. //
  88. // The Next Event is integrated until has finished its execution. Once it has
  89. // finished, the next event to be triggered becomes the Current Event. Doing
  90. // this means that only one event is ever checked to see if it should be
  91. // triggered.
  92. //
  93. //-----------------------------------------------------------------------------
  94. void VTrack::onControllerUpdate( const S32 &pTime, const S32 &pDelta )
  95. {
  96. if ( !isEnabled() || !mNextEvent )
  97. {
  98. // Don't Update.
  99. return;
  100. }
  101. // Update Next Event.
  102. while ( !mNextEvent->onControllerUpdate( pTime, pDelta ) )
  103. {
  104. // Next Event?
  105. if ( !updateNextEvent() )
  106. {
  107. // No Valid Events.
  108. mNextEvent = NULL;
  109. break;
  110. }
  111. }
  112. }
  113. //-----------------------------------------------------------------------------
  114. //
  115. // VTrack::onControllerEvent( pEvent );
  116. //
  117. // When the controller's state changes, this method is called. If the
  118. // controller is reset the virtual method, onControllerReset is called.
  119. //
  120. // For a full list of possible events, see the 'eControllerEventType'
  121. // declaration in VController.h.
  122. //
  123. //-----------------------------------------------------------------------------
  124. bool VTrack::onControllerEvent( VController::eControllerEventType pEvent )
  125. {
  126. if ( !getController() )
  127. {
  128. AssertFatal( false, "VTrack::onControllerEvent() - Invalid Controller." );
  129. return false;
  130. }
  131. // Enabled?
  132. if ( !isEnabled() )
  133. {
  134. // Continue Processing Events.
  135. return true;
  136. }
  137. switch( pEvent )
  138. {
  139. case VController::k_EventReset :
  140. {
  141. // Reset.
  142. onControllerReset( getControllerTime(), isControllerPlayingForward() );
  143. } break;
  144. }
  145. // Continue Processing Events.
  146. return true;
  147. }
  148. //-----------------------------------------------------------------------------
  149. //
  150. // VTrack::onControllerReset( pTime, pForward );
  151. //
  152. // Reset the status of the track. The Next Event is allocated here.
  153. //
  154. //-----------------------------------------------------------------------------
  155. void VTrack::onControllerReset( const S32 &pTime, const bool &pForward )
  156. {
  157. // Clear Next Event.
  158. mNextEvent = NULL;
  159. for ( ITreeNode *node = mChildNode; node != NULL; node = node->mSiblingNextNode )
  160. {
  161. VEvent *event = ( VEvent* )node;
  162. // Reset Event.
  163. event->onControllerReset( pTime, pForward );
  164. if ( ( event->isPlaying() )
  165. || ( pForward && event->getTriggerTime() >= pTime ) )
  166. {
  167. if ( !mNextEvent )
  168. {
  169. // Use as Next Event.
  170. mNextEvent = event;
  171. }
  172. }
  173. else if ( !pForward && pTime >= event->getTriggerTime() )
  174. {
  175. VEvent *nextEvent = ( VEvent* )node->mSiblingNextNode;
  176. if ( !nextEvent || pTime < nextEvent->getTriggerTime() )
  177. {
  178. // Use as Next Event.
  179. mNextEvent = event;
  180. }
  181. }
  182. }
  183. }
  184. //-----------------------------------------------------------------------------
  185. //
  186. // Reference Methods.
  187. //
  188. //-----------------------------------------------------------------------------
  189. //-----------------------------------------------------------------------------
  190. //
  191. // VTrack::sort();
  192. //
  193. // Sort the track's events by the event's trigger time.
  194. //
  195. //-----------------------------------------------------------------------------
  196. void VTrack::sort( void )
  197. {
  198. const S32 count = size();
  199. for ( S32 j = 0; j < count; j++ )
  200. {
  201. for ( ITreeNode *node = mChildNode; node != NULL; node = node->mSiblingNextNode )
  202. {
  203. VEvent *eventA = ( VEvent* )node;
  204. VEvent *eventB = ( VEvent* )node->mSiblingNextNode;
  205. if ( !eventB )
  206. {
  207. // No Node.
  208. break;
  209. }
  210. // Swap?
  211. if ( eventA->getTriggerTime() > eventB->getTriggerTime() )
  212. {
  213. // Get Outer Siblings.
  214. ITreeNode *prevNode = eventA->mSiblingPrevNode;
  215. ITreeNode *nextNode = eventB->mSiblingNextNode;
  216. if ( eventA->mParentNode && eventA->mParentNode->mChildNode == eventA )
  217. {
  218. // New Child Node.
  219. eventA->mParentNode->mChildNode = eventB;
  220. }
  221. //
  222. // Move A.
  223. eventA->mSiblingPrevNode = eventB;
  224. eventA->mSiblingNextNode = nextNode;
  225. if ( nextNode )
  226. {
  227. // Update Outer Sibling.
  228. nextNode->mSiblingPrevNode = eventA;
  229. }
  230. //
  231. // Move B.
  232. eventB->mSiblingPrevNode = prevNode;
  233. eventB->mSiblingNextNode = eventA;
  234. if ( prevNode )
  235. {
  236. // Update Outer Sibling.
  237. prevNode->mSiblingNextNode = eventB;
  238. }
  239. }
  240. }
  241. }
  242. }
  243. //-----------------------------------------------------------------------------
  244. //
  245. // VTrack::updateNextEvent( pForward );
  246. //
  247. // Point mNextEvent to the next valid event in the track's sequence.
  248. //
  249. //-----------------------------------------------------------------------------
  250. bool VTrack::updateNextEvent( void )
  251. {
  252. if ( !mNextEvent )
  253. {
  254. // Invalid Event.
  255. return false;
  256. }
  257. while ( ( mNextEvent = mNextEvent->getNextEvent() ) != NULL )
  258. {
  259. if ( mNextEvent->isEnabled() )
  260. {
  261. // Valid Event.
  262. return true;
  263. }
  264. }
  265. // Invalid Event.
  266. return false;
  267. }
  268. //-----------------------------------------------------------------------------
  269. //
  270. // Property Methods.
  271. //
  272. //-----------------------------------------------------------------------------
  273. //-----------------------------------------------------------------------------
  274. //
  275. // VTrack::getGroup();
  276. //
  277. // Returns the Track's parent group.
  278. //
  279. //-----------------------------------------------------------------------------
  280. VGroup *VTrack::getGroup( void )
  281. {
  282. return dynamic_cast<VGroup*>( mParentNode );
  283. }
  284. //-----------------------------------------------------------------------------
  285. //
  286. // VTrack::getNextEvent();
  287. //
  288. // Returns the Event that the Track is currently observing.
  289. //
  290. //-----------------------------------------------------------------------------
  291. VEvent *VTrack::getNextEvent( void )
  292. {
  293. return mNextEvent;
  294. }
  295. //-----------------------------------------------------------------------------
  296. //
  297. // VTrack::getCurrentEvent();
  298. //
  299. // Returns the Event that the Track is currently observing and playing. This
  300. // will only ever be non-null when the track is observing an Event that has a
  301. // non-zero duration and has been triggered.
  302. //
  303. //-----------------------------------------------------------------------------
  304. VEvent *VTrack::getCurrentEvent( void )
  305. {
  306. if ( mNextEvent && mNextEvent->isPlaying() )
  307. {
  308. return mNextEvent;
  309. }
  310. return NULL;
  311. }
  312. //-----------------------------------------------------------------------------
  313. //
  314. // VTrack::getPreviousEvent();
  315. //
  316. // Returns the Event that the Track was last intergrating.
  317. //
  318. //-----------------------------------------------------------------------------
  319. VEvent *VTrack::getPreviousEvent( void )
  320. {
  321. if ( mNextEvent )
  322. {
  323. return mNextEvent->getPreviousEvent();
  324. }
  325. if ( !isControllerPlayingForward() )
  326. {
  327. return dynamic_cast<VEvent*>( getChild() );
  328. }
  329. return dynamic_cast<VEvent*>( getLastChild() );
  330. }
  331. //-----------------------------------------------------------------------------
  332. //
  333. // VTrack::calclateInterp( pTime );
  334. //
  335. // This method returns the interp time between or within events. If the given
  336. // time is between two events, the return time is:
  337. //
  338. // ( pTime - last_event_finish_time )
  339. // / ( next_event_start_time - last_event_finish_time )
  340. //
  341. // If the given time is within an event, the return time is:
  342. //
  343. // ( pTime - event_start_time ) / ( event_duration )
  344. //
  345. // The value returned here is between 0.0 and 1.0.
  346. //
  347. //-----------------------------------------------------------------------------
  348. F32 VTrack::calculateInterp( S32 pTime )
  349. {
  350. if ( !isControllerPlayingForward() )
  351. {
  352. return ( 1.f - _calculateInterp( pTime ) );
  353. }
  354. return _calculateInterp( pTime );
  355. }
  356. F32 VTrack::_calculateInterp( S32 pTime )
  357. {
  358. // Fetch Duration.
  359. const S32 sequenceDuration = getControllerDuration();
  360. if ( sequenceDuration == 0 || pTime == sequenceDuration )
  361. {
  362. // Sanity!
  363. return 1.f;
  364. }
  365. if ( !mChildNode )
  366. {
  367. // Quick Interp.
  368. return F32( pTime / sequenceDuration );
  369. }
  370. // Last Time.
  371. S32 lastTime = 0;
  372. VEvent *walk = ( VEvent* )mChildNode;
  373. while ( walk )
  374. {
  375. const S32 startTime = walk->getStartTime();
  376. const S32 finishTime = walk->getFinishTime();
  377. if ( pTime < startTime )
  378. {
  379. return ( F32( pTime - lastTime ) / F32( startTime - lastTime ) );
  380. }
  381. // Update Last Time.
  382. lastTime = startTime;
  383. if ( pTime < finishTime )
  384. {
  385. return ( F32( pTime - lastTime ) / F32( finishTime - lastTime ) );
  386. }
  387. // Update Last Time.
  388. lastTime = finishTime;
  389. // Fetch Next Node.
  390. walk = ( VEvent* )walk->mSiblingNextNode;
  391. }
  392. // Return.
  393. return ( F32( pTime - lastTime ) / F32( sequenceDuration - lastTime ) );
  394. }