VEvent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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/VEvent.h"
  24. #include "Verve/Core/VGroup.h"
  25. #include "Verve/Core/VTrack.h"
  26. #include "console/consoleTypes.h"
  27. #include "math/mMathFn.h"
  28. //-----------------------------------------------------------------------------
  29. IMPLEMENT_CONOBJECT( VEvent );
  30. //-----------------------------------------------------------------------------
  31. VEvent::VEvent( void ) :
  32. mIsPlaying( false ),
  33. mTriggered( false ),
  34. mTriggerTime( 0 ),
  35. mDuration( 0 )
  36. {
  37. setLabel( "DefaultEvent" );
  38. }
  39. void VEvent::initPersistFields( void )
  40. {
  41. Parent::initPersistFields();
  42. addProtectedField( "TriggerTime", TypeS32, Offset( mTriggerTime, VEvent ), &setTriggerTime, &defaultProtectedGetFn, "The time that this event is triggered." );
  43. addProtectedField( "Duration", TypeS32, Offset( mDuration, VEvent ), &setDuration, &defaultProtectedGetFn, "The total duration that this event plays for." );
  44. }
  45. //-----------------------------------------------------------------------------
  46. //
  47. // Controller Methods.
  48. //
  49. //-----------------------------------------------------------------------------
  50. //-----------------------------------------------------------------------------
  51. //
  52. // VEvent::onControllerReset( pTime, pForward );
  53. //
  54. // Reset the status of the event. If the given time is between the event's
  55. // start and finish times, then the isPlaying flag will be true. This means
  56. // that the event is free to be triggered upon playback.
  57. //
  58. //-----------------------------------------------------------------------------
  59. void VEvent::onControllerReset( const S32 &pTime, const bool &pForward )
  60. {
  61. // Reset Status.
  62. mIsPlaying = ( pTime > mTriggerTime && pTime < ( mTriggerTime + mDuration ) );
  63. mTriggered = false;
  64. }
  65. //-----------------------------------------------------------------------------
  66. //
  67. // VEvent::onControllerUpdate( pTime, pDelta )
  68. //
  69. // Integrate is only called when this event is the Next Event for the parent
  70. // track. For each track, there is only ever *one* event being integrated - the
  71. // event that needs to be triggered next.
  72. //
  73. // If the event has a duration greater than 0, then this event will continue to
  74. // integrate until its time is up, or the controller finishes playing
  75. // (whichever happens first).
  76. //
  77. // If a value of true is returned, then this event will continue to integrate
  78. // until a value of false is returned to the parent track. When this happens,
  79. // this event ceases to be the track's Next Event and will not continue
  80. // updating.
  81. //
  82. //-----------------------------------------------------------------------------
  83. bool VEvent::onControllerUpdate( const S32 &pTime, const S32 &pDelta )
  84. {
  85. if ( !isEnabled() )
  86. {
  87. return false;
  88. }
  89. const S32 newTime = ( pTime + pDelta );
  90. const S32 &startTime = getStartTime();
  91. const S32 &finishTime = getFinishTime();
  92. if ( !mIsPlaying || !mTriggered )
  93. {
  94. if ( !mIsPlaying )
  95. {
  96. if ( ( pDelta > 0 && newTime < startTime )
  97. || ( pDelta < 0 && newTime > startTime ) )
  98. {
  99. // Not Time to Trigger.
  100. return true;
  101. }
  102. if ( ( pDelta > 0 && pTime > startTime )
  103. || ( pDelta < 0 && pTime < startTime ) )
  104. {
  105. //AssertFatal( false, "VEvent::onControllerUpdate() - Event has been skipped." );
  106. return false;
  107. }
  108. }
  109. if ( !mTriggered )
  110. {
  111. // Play and Trigger.
  112. mIsPlaying = ( mDuration > 0 );
  113. mTriggered = true;
  114. // Callback.
  115. onTrigger( pTime, pDelta );
  116. if ( mDuration == 0 )
  117. {
  118. // Stop Integrating.
  119. return false;
  120. }
  121. // Return Here.
  122. // Note: If Duration is non-zero this event will continue to update
  123. // so that VEvent:: onUpdate is processed for the full event
  124. // duration.
  125. return ( mDuration != 0 );
  126. }
  127. }
  128. // Complete?
  129. const bool isComplete = ( ( pDelta > 0 && newTime > finishTime )
  130. || ( pDelta < 0 && newTime < finishTime ) );
  131. if ( !isComplete )
  132. {
  133. // Callback.
  134. onUpdate( pTime, pDelta );
  135. }
  136. else
  137. {
  138. // Complete.
  139. mIsPlaying = false;
  140. // Callback.
  141. onComplete( pTime, pDelta );
  142. }
  143. // Continue?
  144. return !isComplete;
  145. }
  146. //-----------------------------------------------------------------------------
  147. //
  148. // Callback Methods.
  149. //
  150. //-----------------------------------------------------------------------------
  151. //-----------------------------------------------------------------------------
  152. //
  153. // VEvent::onTrigger( pTime, pDelta );
  154. //
  155. // This method is called when an event is due to be triggered. This method is
  156. // meant to be overloaded by derived classes.
  157. //
  158. // For examples of what an event might do, please refer to some of the included
  159. // events with Verve.
  160. //
  161. //-----------------------------------------------------------------------------
  162. void VEvent::onTrigger( const S32 &pTime, const S32 &pDelta )
  163. {
  164. // Void.
  165. }
  166. //-----------------------------------------------------------------------------
  167. //
  168. // VEvent::onUpdate( pTime, pDelta );
  169. //
  170. // This method is called each tick once an event has been triggered and ceases
  171. // to be called when it is completed. This method is meant to be overloaded by
  172. // derived classes.
  173. //
  174. //-----------------------------------------------------------------------------
  175. void VEvent::onUpdate( const S32 &pTime, const S32 &pDelta )
  176. {
  177. // Void.
  178. }
  179. //-----------------------------------------------------------------------------
  180. //
  181. // VEvent::onComplete( pTime, pDelta );
  182. //
  183. // This method is called once an event has finished being updated. It is not
  184. // called on events that have a duration of 0. This method is meant to be
  185. // overloaded by derived classes.
  186. //
  187. //-----------------------------------------------------------------------------
  188. void VEvent::onComplete( const S32 &pTime, const S32 &pDelta )
  189. {
  190. // Void.
  191. }
  192. //-----------------------------------------------------------------------------
  193. //
  194. // Property Methods.
  195. //
  196. //-----------------------------------------------------------------------------
  197. //-----------------------------------------------------------------------------
  198. //
  199. // VEvent::getGroup();
  200. //
  201. // Returns the parent group.
  202. //
  203. //-----------------------------------------------------------------------------
  204. VGroup *VEvent::getGroup( void )
  205. {
  206. VTrack *track = getTrack();
  207. if ( track )
  208. {
  209. return track->getGroup();
  210. }
  211. return NULL;
  212. }
  213. //-----------------------------------------------------------------------------
  214. //
  215. // VEvent::getTrack();
  216. //
  217. // Returns the parent track.
  218. //
  219. //-----------------------------------------------------------------------------
  220. VTrack *VEvent::getTrack( void )
  221. {
  222. return dynamic_cast<VTrack*>( mParentNode );
  223. }
  224. //-----------------------------------------------------------------------------
  225. //
  226. // VEvent::getNextEvent();
  227. //
  228. // Returns the next event.
  229. //
  230. //-----------------------------------------------------------------------------
  231. VEvent *VEvent::getNextEvent( void )
  232. {
  233. if ( !isControllerPlayingForward() )
  234. {
  235. return dynamic_cast<VEvent*>( mSiblingPrevNode );
  236. }
  237. return dynamic_cast<VEvent*>( mSiblingNextNode );
  238. }
  239. //-----------------------------------------------------------------------------
  240. //
  241. // VEvent::getPreviousEvent();
  242. //
  243. // Returns the previous event.
  244. //
  245. //-----------------------------------------------------------------------------
  246. VEvent *VEvent::getPreviousEvent( void )
  247. {
  248. if ( !isControllerPlayingForward() )
  249. {
  250. return dynamic_cast<VEvent*>( mSiblingNextNode );
  251. }
  252. return dynamic_cast<VEvent*>( mSiblingPrevNode );
  253. }
  254. //-----------------------------------------------------------------------------
  255. //
  256. // VEvent::getStartTime();
  257. //
  258. // Returns the time, in milliseconds, that the event is due to trigger.
  259. //
  260. //-----------------------------------------------------------------------------
  261. S32 VEvent::getStartTime( void )
  262. {
  263. return ( mTriggerTime + ( !isControllerPlayingForward() * mDuration ) );
  264. }
  265. //-----------------------------------------------------------------------------
  266. //
  267. // VEvent::getFinishTime();
  268. //
  269. // Returns the time, in milliseconds, that the event will cease updating.
  270. //
  271. //-----------------------------------------------------------------------------
  272. S32 VEvent::getFinishTime( void )
  273. {
  274. return ( mTriggerTime + ( isControllerPlayingForward() * mDuration ) );
  275. }
  276. //-----------------------------------------------------------------------------
  277. //
  278. // VEvent::setTriggerTime( pTime );
  279. //
  280. // Apply the given trigger time to the object.
  281. //
  282. // If the project was built using the VT_EDITOR preprocessor argument, then
  283. // the validity of the passed value is verified. It also cannot be changed
  284. // while the controller is playing.
  285. //
  286. //-----------------------------------------------------------------------------
  287. void VEvent::setTriggerTime( const S32 &pTime )
  288. {
  289. #ifdef VT_EDITOR
  290. VTrack *track = getTrack();
  291. if ( !track )
  292. {
  293. // Apply Time.
  294. mTriggerTime = pTime;
  295. return;
  296. }
  297. if ( track->isControllerPlaying() )
  298. {
  299. // Don't Change While Playing.
  300. return;
  301. }
  302. /*
  303. // Check For Overlap.
  304. for ( ITreeNode *node = mChildNode; node != NULL; node = node->mSiblingNextNode )
  305. {
  306. VEvent *event = ( VEvent* )node;
  307. if ( event == this )
  308. {
  309. // Skip.
  310. continue;
  311. }
  312. const U32 startTime = getStartTime();
  313. const U32 finishTime = getFinishTime();
  314. if ( ( pTime > startTime && pTime < finishTime )
  315. || ( ( pTime + mDuration ) > startTime && ( pTime + mDuration ) < finishTime )
  316. || ( pTime < startTime && ( pTime + mDuration ) > finishTime ) )
  317. {
  318. // Overlap!
  319. return;
  320. }
  321. }
  322. */
  323. // Apply Time.
  324. mTriggerTime = mClamp( pTime, 0, getControllerDuration() );
  325. // Sort Events.
  326. track->sort();
  327. // Reset Track.
  328. track->onControllerReset( getControllerTime(), isControllerPlayingForward() );
  329. #else
  330. // Apply Time.
  331. mTriggerTime = pTime;
  332. #endif
  333. }
  334. //-----------------------------------------------------------------------------
  335. //
  336. // VEvent::setDuration( pDuration );
  337. //
  338. // Apply the given duration time to the object.
  339. //
  340. // If the project was built using the VT_EDITOR preprocessor argument, then
  341. // the validity of the passed value is verified. It also cannot be changed
  342. // while the controller is playing.
  343. //
  344. //-----------------------------------------------------------------------------
  345. void VEvent::setDuration( const S32 &pDuration )
  346. {
  347. #ifdef VT_EDITOR
  348. if ( isControllerPlaying() )
  349. {
  350. // Don't Change While Playing.
  351. return;
  352. }
  353. #endif
  354. // Apply Duration.
  355. mDuration = pDuration;
  356. }