VEvent.cpp 12 KB

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