2
0

VController.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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/VController.h"
  24. #include "Verve/Core/VObject.h"
  25. #include "Verve/Core/VGroup.h"
  26. #include "Verve/Core/VTrack.h"
  27. #include "Verve/Extension/Director/VDirectorGroup.h"
  28. #include "console/consoleObject.h"
  29. #include "console/consoleTypes.h"
  30. #include "math/mMathFn.h"
  31. //-----------------------------------------------------------------------------
  32. IMPLEMENT_CONOBJECT( VController );
  33. //-----------------------------------------------------------------------------
  34. VController::VController( void ) :
  35. mStatus( ( k_StatusInit | k_StatusStopped ) ),
  36. mTime( 0 ),
  37. mLastTime( 0 ),
  38. mTimeScale( 1.f ),
  39. mDuration( 5000 ),
  40. mLoop( false ),
  41. mLoopBackwards( false ),
  42. mLoopCount( -1 ),
  43. mLoopIndex( 0 ),
  44. mLoopDelay( 0 ),
  45. mLoopDelayTime( 0 ),
  46. mJump( k_JumpInvalid ),
  47. mJumpTime( 0 ),
  48. mResetOnCompletion( true )
  49. {
  50. // Don't Process Ticks.
  51. setProcessTicks( false );
  52. }
  53. VController::~VController( void )
  54. {
  55. // Void.
  56. }
  57. void VController::initPersistFields( void )
  58. {
  59. addGroup( "Controller" );
  60. addProtectedField( "Time", TypeS32, Offset( mTime, VController ), &setTime, &defaultProtectedGetFn, "Current position of the Controller (in milliseconds)." );
  61. addProtectedField( "Duration", TypeS32, Offset( mDuration, VController ), &setDuration, &defaultProtectedGetFn, "Total length of the sequence (in milliseconds)." );
  62. addProtectedField( "TimeScale", TypeF32, Offset( mTimeScale, VController ), &setTimeScale, &defaultProtectedGetFn, "Speed of playback. A value > 0.0 will enable the Controller to play forwards, while a value < 0.0 will play backwards. If |TimeScale| > 1.0, then playback will be faster than normal, while |TimeScale| < 1.0 will be slower." );
  63. addField( "Loop", TypeBool, Offset( mLoop, VController ), "Instead of stopping once playback is complete, the Controller will reset and resume play." );
  64. addField( "LoopBackwards", TypeBool, Offset( mLoopBackwards, VController ), "When the sequence loops, reverse the direction of play." );
  65. addField( "LoopCount", TypeS32, Offset( mLoopCount, VController ), "The number of times the sequence loops before stopping. -1 will cause the sequence to loop indefinitely." );
  66. addField( "LoopDelay", TypeS32, Offset( mLoopDelay, VController ), "When the sequence loops, delay playback by this value (in milliseconds)." );
  67. addField( "ResetOnCompletion", TypeBool, Offset( mResetOnCompletion, VController ), "When the sequence is completed, reset the state of the Controller." );
  68. endGroup( "Controller" );
  69. // Parent Call.
  70. Parent::initPersistFields();
  71. }
  72. //-----------------------------------------------------------------------------
  73. //
  74. // ITickable Methods.
  75. //
  76. //-----------------------------------------------------------------------------
  77. //-----------------------------------------------------------------------------
  78. //
  79. // VController::processTick();
  80. //
  81. // This method controls the playback of the entire sequence. It integrates all
  82. // of the groups and handles sequence looping and jumping.
  83. //
  84. //-----------------------------------------------------------------------------
  85. void VController::processTick( void )
  86. {
  87. if ( mTimeScale == 0.f )
  88. {
  89. // Pause.
  90. pause();
  91. // Exit.
  92. return;
  93. }
  94. // Calculate Delta.
  95. const S32 time = Sim::getCurrentTime();
  96. S32 delta = ( time - mLastTime );
  97. mLastTime = time;
  98. // Reverse?
  99. if ( mTimeScale < 0.f )
  100. {
  101. // Negative Delta.
  102. delta *= -1;
  103. }
  104. if ( mLoopDelayTime > 0 )
  105. {
  106. // Update Delay Time.
  107. mLoopDelayTime -= getMin( mAbs( delta ), mLoopDelayTime );
  108. // Exit.
  109. return;
  110. }
  111. // Jump Delta?
  112. if ( mJump == k_JumpDelta )
  113. {
  114. // Jump.
  115. delta = mJumpTime;
  116. // Clear.
  117. mJump = k_JumpInvalid;
  118. mJumpTime = 0;
  119. }
  120. if ( ( isPlayingForward() && ( mTime + delta ) > mDuration )
  121. || ( !isPlayingForward() && ( mTime + delta ) < 0 ) )
  122. {
  123. // Clamp Delta.
  124. delta = ( ( mTimeScale > 0.f ) * mDuration ) - mTime;
  125. // Note: If we are playing forwards, we're at the end of the
  126. // sequence and we want to loop/reset the Controller, then we
  127. // need to handle that now.
  128. if ( delta == 0 )
  129. {
  130. onPostTick();
  131. }
  132. }
  133. // Valid Delta?
  134. if ( delta == 0 )
  135. {
  136. // Exit.
  137. return;
  138. }
  139. // Trigger Update.
  140. mControllerUpdateSignal.trigger( mTime, delta );
  141. // Update Time.
  142. mTime += delta;
  143. // Perform Post Tick.
  144. onPostTick();
  145. }
  146. //-----------------------------------------------------------------------------
  147. //
  148. // VController::onPostTick();
  149. //
  150. // This method is called onces a tick has been processed. It will perform the
  151. // the right checks to see if the Controller has finished playing. It also
  152. // handles special cases like Looping or Resetting the Controller.
  153. //
  154. //-----------------------------------------------------------------------------
  155. void VController::onPostTick( void )
  156. {
  157. // Jump Time?
  158. if ( mJump == k_JumpTime )
  159. {
  160. // Jump Post Update.
  161. reset( mJumpTime );
  162. // Clear.
  163. mJump = k_JumpInvalid;
  164. mJumpTime = 0;
  165. }
  166. // Sequence Completed?
  167. if ( isPlayingForward() && mTime >= mDuration
  168. || !isPlayingForward() && mTime <= 0 )
  169. {
  170. bool stopPlaying = true;
  171. if ( mLoop )
  172. {
  173. // Don't Stop.
  174. stopPlaying = false;
  175. if ( mLoopBackwards )
  176. {
  177. // Change Direction.
  178. setTimeScale( -1.f * mTimeScale );
  179. }
  180. else
  181. {
  182. // Reset Time.
  183. reset();
  184. }
  185. if ( mLoopDelay > 0 )
  186. {
  187. // Resume After Delay.
  188. mLoopDelayTime = mLoopDelay;
  189. }
  190. // At the Start of the Sequence?
  191. if ( mTime <= 0 && mLoopCount >= 0 )
  192. {
  193. // Stop Looping?
  194. stopPlaying = ( ++mLoopIndex >= mLoopCount );
  195. }
  196. // Callback.
  197. Con::executef( this, "onLoop" );
  198. // Loop Signal.
  199. postEvent( k_EventLoop );
  200. }
  201. // Stop?
  202. if ( stopPlaying )
  203. {
  204. // Stop Only.
  205. stop( mResetOnCompletion );
  206. }
  207. }
  208. }
  209. //-----------------------------------------------------------------------------
  210. //
  211. // Controller Methods.
  212. //
  213. //-----------------------------------------------------------------------------
  214. //-----------------------------------------------------------------------------
  215. //
  216. // VController::reset();
  217. //
  218. // Reset the Controller to the start of the sequence.
  219. //
  220. //-----------------------------------------------------------------------------
  221. void VController::reset( void )
  222. {
  223. // Reset.
  224. reset( ( isPlayingForward() ) ? 0 : mDuration );
  225. }
  226. //-----------------------------------------------------------------------------
  227. //
  228. // VController::reset( pTime );
  229. //
  230. // Reset the Controller to the target time. This is a very important method as
  231. // it allows tracks and events to reset their state as well as prepare
  232. // themselves for playback.
  233. //
  234. //-----------------------------------------------------------------------------
  235. void VController::reset( const S32 &pTime )
  236. {
  237. // Reset Time.
  238. mTime = pTime;
  239. mLastTime = Sim::getCurrentTime();
  240. // Reset Delay Time.
  241. mLoopDelayTime = 0;
  242. // Post Event.
  243. postEvent( k_EventReset );
  244. }
  245. //-----------------------------------------------------------------------------
  246. //
  247. // VController::play();
  248. //
  249. // Start playing the sequence from the current time and execute a number of
  250. // callbacks.
  251. //
  252. //-----------------------------------------------------------------------------
  253. void VController::play( void )
  254. {
  255. if ( isPlaying() || mTime < 0 || mTime > mDuration )
  256. {
  257. // Sanity!
  258. return;
  259. }
  260. // Reset Time.
  261. mLastTime = Sim::getCurrentTime();
  262. // Start Updating.
  263. setProcessTicks( true );
  264. if ( mStatus & k_StatusInit )
  265. {
  266. // Init Signal.
  267. postEvent( k_EventInit );
  268. // Clear Init Status.
  269. mStatus &= ~k_StatusInit;
  270. }
  271. // Update Status.
  272. updateStatus( k_StatusPlaying );
  273. // Play Signal.
  274. postEvent( k_EventPlay );
  275. // Callback.
  276. Con::executef( this, "onPlay" );
  277. }
  278. //-----------------------------------------------------------------------------
  279. //
  280. // VController::play( pTime );
  281. //
  282. // Start playing the sequence from the desired time.
  283. //
  284. //-----------------------------------------------------------------------------
  285. void VController::play( const S32 &pTime )
  286. {
  287. // Reset.
  288. reset( pTime );
  289. // Play.
  290. play();
  291. }
  292. //-----------------------------------------------------------------------------
  293. //
  294. // VController::pause();
  295. //
  296. // Cease playback of the sequence, but maintain the current time.
  297. //
  298. //-----------------------------------------------------------------------------
  299. void VController::pause( void )
  300. {
  301. // Stop Updating.
  302. setProcessTicks( false );
  303. // Update Status.
  304. updateStatus( k_StatusPaused );
  305. // Pause Signal.
  306. postEvent( k_EventPause );
  307. // Callback.
  308. Con::executef( this, "onPause" );
  309. }
  310. //-----------------------------------------------------------------------------
  311. //
  312. // VController::stop( pReset );
  313. //
  314. // Stop playback altogether and reset the Controller to the start of the
  315. // sequence.
  316. //
  317. //-----------------------------------------------------------------------------
  318. void VController::stop( const bool &pReset )
  319. {
  320. // Stop Updating.
  321. setProcessTicks( false );
  322. // Reset Loop Index.
  323. mLoopIndex = 0;
  324. // Update Status.
  325. updateStatus( ( k_StatusInit | k_StatusStopped ) );
  326. // Reset?
  327. if ( pReset )
  328. {
  329. // Reset.
  330. reset();
  331. }
  332. // Stop Signal.
  333. postEvent( k_EventStop );
  334. // Callback.
  335. Con::executef( this, "onStop" );
  336. }
  337. //-----------------------------------------------------------------------------
  338. //
  339. // VController::jump();
  340. //
  341. // Jump the Controller time forward 1 tick (32ms).
  342. //
  343. //-----------------------------------------------------------------------------
  344. void VController::jump( void )
  345. {
  346. // Jump 1 tick.
  347. jump( k_JumpDelta, ( isPlayingForward() ) ? TickMs : -TickMs );
  348. }
  349. //-----------------------------------------------------------------------------
  350. //
  351. // VController::jump( pType, pDelta );
  352. //
  353. // Jump the Controller time by the target Delta.
  354. //
  355. //-----------------------------------------------------------------------------
  356. void VController::jump( const eControllerJumpType &pType, const S32 &pDelta )
  357. {
  358. // Jump.
  359. mJump = pType;
  360. mJumpTime = pDelta;
  361. }
  362. //-----------------------------------------------------------------------------
  363. //
  364. // VController::updateStatus( pStatus );
  365. //
  366. // Clear the regular playback states and add the updated state.
  367. //
  368. //-----------------------------------------------------------------------------
  369. void VController::updateStatus( const S32 &pStatus )
  370. {
  371. // Clear Playback Status.
  372. mStatus &= ~( k_StatusPlaying | k_StatusPaused | k_StatusStopped );
  373. // Add New Status.
  374. mStatus |= pStatus;
  375. }
  376. //-----------------------------------------------------------------------------
  377. //
  378. // Reference Methods.
  379. //
  380. //-----------------------------------------------------------------------------
  381. //-----------------------------------------------------------------------------
  382. //
  383. // VController::getObject( pLabel );
  384. //
  385. // Returns the group with the given name. If no group belongs to the Controller
  386. // with that name, then a NULL value is returned.
  387. //
  388. //-----------------------------------------------------------------------------
  389. VGroup *VController::getObject( const String &pLabel )
  390. {
  391. VGroup *node = ( VGroup* )mChildNode;
  392. while ( node )
  393. {
  394. // Compare Names.
  395. if ( node->getLabel().equal( pLabel, String::NoCase ) )
  396. {
  397. // Valid.
  398. return node;
  399. }
  400. // Next Sibling.
  401. node = ( VGroup* )node->mSiblingNextNode;
  402. }
  403. // Invalid.
  404. return NULL;
  405. }
  406. //-----------------------------------------------------------------------------
  407. //
  408. // VController::getDirectorGroup();
  409. //
  410. // Returns the DirectorGroup reference if the Controller has a one.
  411. //
  412. //-----------------------------------------------------------------------------
  413. VDirectorGroup *VController::getDirectorGroup( void )
  414. {
  415. for ( ITreeNode *node = mChildNode; node != NULL; node = node->mSiblingNextNode )
  416. {
  417. if ( VDirectorGroup *group = dynamic_cast<VDirectorGroup*>( node ) )
  418. {
  419. // Return Group.
  420. return group;
  421. }
  422. }
  423. // Invalid Group.
  424. return NULL;
  425. }
  426. //-----------------------------------------------------------------------------
  427. //
  428. // VController::getDirectorTrack();
  429. //
  430. // Returns the DirectorTrack reference if the DirectorGroup has one.
  431. //
  432. //-----------------------------------------------------------------------------
  433. VDirectorTrack *VController::getDirectorTrack( void )
  434. {
  435. VDirectorGroup *group = getDirectorGroup();
  436. if ( !group )
  437. {
  438. // Invalid Track.
  439. return NULL;
  440. }
  441. // Return Track.
  442. return group->getDirectorTrack();
  443. }
  444. //-----------------------------------------------------------------------------
  445. //
  446. // VController::getDataValue( pFieldName, *pValue );
  447. //
  448. // Returns true if the field is a DataTable member and can be correctly
  449. // evaluated. If this is the case, then pValue will contain the result.
  450. //
  451. //-----------------------------------------------------------------------------
  452. bool VController::getDataValue( const String &pFieldName, String &pValue )
  453. {
  454. return mDataTable.getValue( this, pFieldName, pValue );
  455. }
  456. //-----------------------------------------------------------------------------
  457. //
  458. // VController::clearData();
  459. //
  460. // Clear the contents of the DataTable entirely.
  461. //
  462. //-----------------------------------------------------------------------------
  463. void VController::clearData( void )
  464. {
  465. while ( mDataTable.getCount() > 0 )
  466. {
  467. // Clear Item.
  468. clearData( 0 );
  469. }
  470. }
  471. //-----------------------------------------------------------------------------
  472. //
  473. // VController::clearData( pIndex );
  474. //
  475. // Clear the DataTable entry with the given index.
  476. //
  477. //-----------------------------------------------------------------------------
  478. void VController::clearData( const S32 &pIndex )
  479. {
  480. VDataTable::sDataItem data;
  481. if ( mDataTable.getItem( pIndex, &data ) )
  482. {
  483. // Clear Data.
  484. clearData( data.FieldName );
  485. }
  486. }
  487. //-----------------------------------------------------------------------------
  488. //
  489. // VController::clearData( pIndex );
  490. //
  491. // Clear the DataTable entry with the given field name.
  492. //
  493. //-----------------------------------------------------------------------------
  494. void VController::clearData( const String &pFieldName )
  495. {
  496. // Clear Dynamic Field.
  497. setDataField( pFieldName, NULL, "" );
  498. // Clear Item.
  499. mDataTable.clear( pFieldName );
  500. }
  501. //-----------------------------------------------------------------------------
  502. //
  503. // VController::sort();
  504. //
  505. // Sort each track in each of the child groups.
  506. //
  507. //-----------------------------------------------------------------------------
  508. void VController::sort( void )
  509. {
  510. for ( ITreeNode *group = mChildNode; group != NULL; group = group->mSiblingNextNode )
  511. {
  512. for ( ITreeNode *track = group->mChildNode; track != NULL; track = track->mSiblingNextNode )
  513. {
  514. // Sort Track.
  515. ( ( VTrack* )track )->sort();
  516. }
  517. }
  518. }
  519. //-----------------------------------------------------------------------------
  520. //
  521. // Write Methods.
  522. //
  523. //-----------------------------------------------------------------------------
  524. //-----------------------------------------------------------------------------
  525. //
  526. // VController::writeDataTable( pElement );
  527. //
  528. // Write the DataTable out to a TinyXML document.
  529. //
  530. //-----------------------------------------------------------------------------
  531. bool VController::writeDataTable( tinyxml2::XMLElement *pElement )
  532. {
  533. // Create Data Table Root.
  534. tinyxml2::XMLElement *dataTableRoot = pElement->GetDocument()->NewElement( "DataTable" );
  535. pElement->LinkEndChild( dataTableRoot );
  536. for ( VDataTable::VDataMap::Iterator itr = mDataTable.mDataMap.begin(); itr != mDataTable.mDataMap.end(); ++itr )
  537. {
  538. // Fetch Data.
  539. VDataTable::sDataItem *data = &itr->value;
  540. // Create Element.
  541. tinyxml2::XMLElement* dataElement = pElement->GetDocument()->NewElement( "DataItem" );
  542. // Apply Attributes.
  543. dataElement->SetAttribute( "Type", VDataTable::getDataTypeDescription( data->Type ) );
  544. dataElement->SetAttribute( "Name", data->FieldName.c_str() );
  545. dataElement->SetAttribute( "Value", getDataField( StringTable->insert( data->FieldName.c_str() ), NULL ) );
  546. // Add.
  547. dataTableRoot->LinkEndChild( dataElement );
  548. }
  549. return true;
  550. }
  551. //-----------------------------------------------------------------------------
  552. //
  553. // Read Methods.
  554. //
  555. //-----------------------------------------------------------------------------
  556. //-----------------------------------------------------------------------------
  557. //
  558. // VController::readDataTable( pElement );
  559. //
  560. // Read the DataTable from a TinyXML document.
  561. //
  562. //-----------------------------------------------------------------------------
  563. bool VController::readDataTable( tinyxml2::XMLElement *pElement )
  564. {
  565. tinyxml2::XMLElement *dataTableRoot = pElement->FirstChildElement( "DataTable" );
  566. if ( dataTableRoot )
  567. {
  568. for ( tinyxml2::XMLElement *child = dataTableRoot->FirstChildElement(); child != NULL; child = child->NextSiblingElement() )
  569. {
  570. // Get Field Data.
  571. const char *fieldType = child->Attribute( "Type" );
  572. const char *fieldName = child->Attribute( "Name" );
  573. const char *fieldValue = child->Attribute( "Value" );
  574. // Add Data Item.
  575. mDataTable.insert( VDataTable::getDataTypeEnum( fieldType ), fieldName );
  576. // Set Field Value.
  577. setDataField( StringTable->insert( fieldName ), NULL, fieldValue );
  578. }
  579. }
  580. // Valid Read.
  581. return true;
  582. }
  583. //-----------------------------------------------------------------------------
  584. //
  585. // Property Methods.
  586. //
  587. //-----------------------------------------------------------------------------
  588. //-----------------------------------------------------------------------------
  589. //
  590. // VController::postEvent( pEvent );
  591. //
  592. // Process an event signal to all event subscribers. This method is used to
  593. // signal changes in the Controller's status.
  594. //
  595. // For a full list of possible events, see the 'eControllerEventType'
  596. // declaration in VController.h.
  597. //
  598. //-----------------------------------------------------------------------------
  599. void VController::postEvent( const eControllerEventType &pEvent )
  600. {
  601. // Signal Event.
  602. mControllerEventSignal.trigger( pEvent );
  603. }
  604. //-----------------------------------------------------------------------------
  605. //
  606. // VController::setTimeScale( pTimeScale );
  607. //
  608. // Set the speed of playback. In effect, a value of 0.5 will double the real
  609. // time taken to complete the playback of the sequence, while a value of 2.0
  610. // will halve the time needed.
  611. //
  612. //-----------------------------------------------------------------------------
  613. void VController::setTimeScale( const F32 &pTimeScale )
  614. {
  615. // Need an Update?
  616. const bool update = ( pTimeScale != 0.f && ( mTimeScale == 0.f || ( ( mTimeScale > 0.f ) != ( pTimeScale > 0.f ) ) ) );
  617. // Store.
  618. mTimeScale = pTimeScale;
  619. // Update $timeScale Variable.
  620. Con::setFloatVariable( "timeScale", mFabs( mTimeScale ) );
  621. if ( update )
  622. {
  623. // Reset.
  624. reset( mTime );
  625. }
  626. }
  627. //-----------------------------------------------------------------------------
  628. //
  629. // Console Methods.
  630. //
  631. //-----------------------------------------------------------------------------
  632. DefineEngineMethod( VController, readFile, bool, (String fileName), (""), "( string pFileName ) - Clears the object and loads the new data from the given filename.\n"
  633. "@param pFileName The target file to read from.\n"
  634. "@return Returns true if the read was successful." )
  635. {
  636. // Clear Sequence Lists.
  637. object->clear();
  638. // Clear Data Table.
  639. object->clearData();
  640. // Read Target File.
  641. if ( !VPersistence::readFile( fileName, object ) )
  642. {
  643. // Re-Clear.
  644. object->clear();
  645. // Invalid Read.
  646. return false;
  647. }
  648. // Initial Sort.
  649. object->sort();
  650. // Reset.
  651. object->reset();
  652. // Valid Read.
  653. return true;
  654. }
  655. DefineEngineMethod( VController, clear, void, (), , "( void ) - Detaches and deletes all of the child objects.\n"
  656. "@return No return value." )
  657. {
  658. // Clear Sequence Lists.
  659. object->clear();
  660. // Clear Data Table.
  661. object->clearData();
  662. }
  663. DefineEngineMethod( VController, reset, void, (S32 time), (-1), "( [int pTime] ) - Reset the Controller's and child object's state.\n"
  664. "@param pTime The target time to reset to.\n"
  665. "@return No return value." )
  666. {
  667. if (time != -1)
  668. {
  669. // Reset Sequence.
  670. object->reset(time);
  671. return;
  672. }
  673. // Default Reset.
  674. object->reset();
  675. }
  676. DefineEngineMethod( VController, isPlaying, bool, (), , "( void ) - Is the sequence currently playing?\n"
  677. "@return Returns true if the Controller is playing." )
  678. {
  679. // Is Playing?
  680. return ( object->isPlaying() );
  681. }
  682. DefineEngineMethod( VController, play, void, (S32 time), (-1), "( [int pTime] ) - Play the sequence. If a value for pTime is specified, the Controller is reset and played from that time.\n"
  683. "@param pTime The time to start playing the sequence from.\n"
  684. "@return No return value." )
  685. {
  686. S32 startTime = object->getTime();
  687. if (time != -1)
  688. {
  689. startTime = time;
  690. }
  691. // Play From Specified Time.
  692. object->play( startTime );
  693. }
  694. DefineEngineMethod( VController, step, void, (),, "( void ) - Step forward one frame.\n"
  695. "@return No return value." )
  696. {
  697. if ( object->isPlaying() )
  698. {
  699. // Sanity!
  700. return;
  701. }
  702. // Play.
  703. object->play( object->getTime() );
  704. // Jump.
  705. object->jump();
  706. // Step Forward One Frame.
  707. object->processTick();
  708. // Stop.
  709. object->stop( false );
  710. }
  711. DefineEngineMethod( VController, isPaused, bool, (), , "( void ) - Is the sequence currently paused?\n"
  712. "@return Returns true if the Controller is paused." )
  713. {
  714. // Is Paused?
  715. return ( object->isPaused() );
  716. }
  717. DefineEngineMethod( VController, pause, void, (), , "( void ) - Pause the sequence. Playback can resume by calling VController::play().\n"
  718. "@return No return value." )
  719. {
  720. // Pause Sequence.
  721. object->pause();
  722. }
  723. DefineEngineMethod( VController, isStopped, bool, (), , "( void ) - Is the sequence currently stopped?\n"
  724. "@return Returns true if the Controller is stopped." )
  725. {
  726. // Is Stopped?
  727. return ( object->isStopped() );
  728. }
  729. DefineEngineMethod( VController, stop, void, (bool reset), (true), "( [bool pReset] ) - Stop the sequence and optionally reset it.\n"
  730. "@param pReset Reset the Controller after stopping.\n"
  731. "@return No return value." )
  732. {
  733. // Stop Sequence.
  734. object->stop(reset);
  735. }
  736. DefineEngineMethod( VController, getTimeScale, F32, (), , "( void ) - Get the playback speed. A value > 0.0 will enable the Controller to play forwards, while a value < 0.0 will play backwards.\n"
  737. "@return Playback Speed." )
  738. {
  739. // Get Time Scale.
  740. return object->getTimeScale();
  741. }
  742. DefineEngineMethod( VController, setTimeScale, void, (float timeScale), (1), "( float pTimeScale ) - Set the playback speed. A value > 0.0 will enable the Controller to play forwards, while a value < 0.0 will play backwards. If |pTimeScale| > 1.0, then playback will be faster than normal, while |pTimeScale| < 1.0 will be slower.\n"
  743. "@param pTimeScale Playback speed.\n"
  744. "@return No return value." )
  745. {
  746. // Set Time Scale.
  747. object->setTimeScale(timeScale);
  748. }
  749. DefineEngineMethod( VController, isDataField, bool, (String fieldName), (""), "( string pFieldName ) - Is the field a member of the Data Table?\n"
  750. "@param pFieldName The name of the dynamic field you wish to check.\n"
  751. "@return Returns true if the field is a member of the Data Table." )
  752. {
  753. if (fieldName.isEmpty())
  754. {
  755. return false;
  756. }
  757. // Is Field.
  758. return object->getDataTable().getItem(fieldName);
  759. }
  760. DefineEngineMethod( VController, getDataFieldCount, S32, (), , "( void ) - Get the number of data elements in the Data Table.\n"
  761. "@return Returns the size of the Data Table." )
  762. {
  763. // Return Count.
  764. return object->getDataTable().getCount();
  765. }
  766. DefineEngineMethod( VController, getDataFieldName, const char *, (S32 index), (0), "( int pIndex ) - Get the name of the field given by the passed index.\n"
  767. "@param pIndex The index of the data field you wish to check.\n"
  768. "@return Returns the name of the field corresponding to the given index." )
  769. {
  770. VDataTable::sDataItem data;
  771. if ( !object->getDataTable().getItem(index, &data ) || data.Type == VDataTable::k_TypeInvalid )
  772. {
  773. // Invalid Field.
  774. return "";
  775. }
  776. // Return Field Name.
  777. return data.FieldName;
  778. }
  779. DefineEngineMethod( VController, getDataFieldValue, const char *, (String fieldName), (""), "( string pFieldName ) - Get the evaluated data from the data field.\n"
  780. "@param pFieldName The name of the field you wish to evaluate.\n"
  781. "@return Returns the evaluated data from the field." )
  782. {
  783. String fieldValue;
  784. if ( object->getDataValue(fieldName, fieldValue ) )
  785. {
  786. // Create Buffer.
  787. char *buffer = Con::getReturnBuffer( 256 );
  788. dStrcpy( buffer, fieldValue.c_str(), 256 );
  789. // Return Value.
  790. return buffer;
  791. }
  792. // Return NULL.
  793. return "0";
  794. }
  795. DefineEngineMethod( VController, getDataFieldType, const char *, (String fieldName), (""), "( string pFieldName ) - Get the type of data for the given field.\n"
  796. "@param pFieldName The name of the field you wish to check.\n"
  797. "@return Returns the data type." )
  798. {
  799. VDataTable::sDataItem data;
  800. if ( !object->getDataTable().getItem(fieldName, &data ) || data.Type == VDataTable::k_TypeInvalid )
  801. {
  802. // Invalid Field.
  803. return "";
  804. }
  805. // Return Field Type.
  806. return VDataTable::getDataTypeDescription( data.Type );
  807. }
  808. #ifdef VT_EDITOR
  809. //-----------------------------------------------------------------------------
  810. //
  811. // Debug Methods.
  812. //
  813. //-----------------------------------------------------------------------------
  814. DefineEngineMethod( VController, writeFile, bool, (String fileName), (""), "( string pFileName ) - Save to a given filename.\n"
  815. "@param pFileName The target file to write to.\n"
  816. "@return Returns true if the write was successful." )
  817. {
  818. // Write Target File.
  819. return VPersistence::writeFile(fileName.c_str(), object );
  820. }
  821. DefineEngineMethod( VController, readTemplate, bool, (String fileName), (""), "( string pFileName ) - Load data from given filename.\n"
  822. "@param pFileName The target file to read from.\n"
  823. "@return Returns true if the read was successful." )
  824. {
  825. // Read Target File.
  826. return VPersistence::readFile(fileName.c_str(), object );
  827. }
  828. DefineEngineMethod( VController, getCount, S32, (),, "( void ) - Get the number of child objects.\n"
  829. "@return Returns the number of child objects." )
  830. {
  831. // Size.
  832. return object->size();
  833. }
  834. DefineEngineMethod( VController, getObject, S32, (S32 index), (0), "( int pIndex ) - Get the object corresponding to the given index.\n"
  835. "@param pIndex The index of the object you wish to retrieve.\n"
  836. "@return Returns the SimObjectID for the object." )
  837. {
  838. // Fetch Object.
  839. VObject *objectRef = ( VObject* )object->at(index);
  840. // Return Group ID.
  841. return ( objectRef ) ? objectRef->getId() : 0;
  842. }
  843. DefineEngineMethod( VController, addObject, void, (SimObject* simObj), (nullAsType<SimObject*>()), "( SimObject pObject ) - Add a child object to this node.\n"
  844. "@param pObject The SimObjectID of the object to be added to this node.\n"
  845. "@return No return value." )
  846. {
  847. if (simObj == nullptr)
  848. return;
  849. VObject *child = dynamic_cast<VObject*>(simObj);
  850. if ( child )
  851. {
  852. // Add Child.
  853. child->addTo( object );
  854. }
  855. }
  856. DefineEngineMethod( VController, removeObject, void, (SimObject* simObj), (nullAsType<SimObject*>()), "( SimObject pObject ) - Remove the target object from this node.\n"
  857. "@param pObject The SimObjectID of the object to be removed from this node.\n"
  858. "@return No return value." )
  859. {
  860. if (simObj == nullptr)
  861. return;
  862. VObject *child = dynamic_cast<VObject*>(simObj);
  863. if ( child && child->getParent() == object )
  864. {
  865. child->remove();
  866. }
  867. }
  868. DefineEngineMethod( VController, sortGroups, void, (),, "( void ) - Sort Groups by their Labels.\n"
  869. "@return No return value." )
  870. {
  871. // Ensure that the Director Group is the First Group.
  872. VDirectorGroup *directorGroup = object->getDirectorGroup();
  873. if ( directorGroup && directorGroup != object->mChildNode )
  874. {
  875. // Detach.
  876. directorGroup->remove();
  877. // Add to the Front of the Controller.
  878. directorGroup->addToFront( object );
  879. }
  880. const S32 count = object->size();
  881. for ( S32 j = 0; j < count; j++ )
  882. {
  883. ITreeNode *node = object->mChildNode;
  884. if ( dynamic_cast<VDirectorGroup*>( node ) != NULL )
  885. {
  886. // Skip Director Group.
  887. node = node->mSiblingNextNode;
  888. }
  889. for ( ; node != NULL; node = node->mSiblingNextNode )
  890. {
  891. VGroup *groupA = ( VGroup* )node;
  892. VGroup *groupB = ( VGroup* )node->mSiblingNextNode;
  893. if ( !groupB )
  894. {
  895. // No Node.
  896. break;
  897. }
  898. // Swap?
  899. if ( groupA->getLabel().compare(groupB->getLabel()) > 0 )
  900. {
  901. // Get Outer Siblings.
  902. ITreeNode *prevNode = groupA->mSiblingPrevNode;
  903. ITreeNode *nextNode = groupB->mSiblingNextNode;
  904. if ( groupA->mParentNode && groupA->mParentNode->mChildNode == groupA )
  905. {
  906. // New Child Node.
  907. groupA->mParentNode->mChildNode = groupB;
  908. }
  909. //
  910. // Move A.
  911. groupA->mSiblingPrevNode = groupB;
  912. groupA->mSiblingNextNode = nextNode;
  913. if ( nextNode )
  914. {
  915. // Update Outer Sibling.
  916. nextNode->mSiblingPrevNode = groupA;
  917. }
  918. //
  919. // Move B.
  920. groupB->mSiblingPrevNode = prevNode;
  921. groupB->mSiblingNextNode = groupA;
  922. if ( prevNode )
  923. {
  924. // Update Outer Sibling.
  925. prevNode->mSiblingNextNode = groupB;
  926. }
  927. }
  928. }
  929. }
  930. }
  931. DefineEngineMethod( VController, sortTracks, void, (),, "( void ) - Sort Tracks by their Labels.\n"
  932. "@return No return value." )
  933. {
  934. for ( ITreeNode *group = object->mChildNode; group != NULL; group = group->mSiblingNextNode )
  935. {
  936. const S32 count = ( ( VGroup* )group )->size();
  937. for ( S32 j = 0; j < count; j++ )
  938. {
  939. for ( ITreeNode *node = group->mChildNode; node != NULL; node = node->mSiblingNextNode )
  940. {
  941. VTrack *trackA = ( VTrack* )node;
  942. VTrack *trackB = ( VTrack* )node->mSiblingNextNode;
  943. if ( !trackB )
  944. {
  945. // No Node.
  946. break;
  947. }
  948. // Swap?
  949. if ( trackA->getLabel().compare(trackB->getLabel()) > 0 )
  950. {
  951. // Get Outer Siblings.
  952. ITreeNode *prevNode = trackA->mSiblingPrevNode;
  953. ITreeNode *nextNode = trackB->mSiblingNextNode;
  954. if ( trackA->mParentNode && trackA->mParentNode->mChildNode == trackA )
  955. {
  956. // New Child Node.
  957. trackA->mParentNode->mChildNode = trackB;
  958. }
  959. //
  960. // Move A.
  961. trackA->mSiblingPrevNode = trackB;
  962. trackA->mSiblingNextNode = nextNode;
  963. if ( nextNode )
  964. {
  965. // Update Outer Sibling.
  966. nextNode->mSiblingPrevNode = trackA;
  967. }
  968. //
  969. // Move B.
  970. trackB->mSiblingPrevNode = prevNode;
  971. trackB->mSiblingNextNode = trackA;
  972. if ( prevNode )
  973. {
  974. // Update Outer Sibling.
  975. prevNode->mSiblingNextNode = trackB;
  976. }
  977. }
  978. }
  979. }
  980. }
  981. }
  982. DefineEngineMethod( VController, addDataField, void, (String fieldType, String fieldName), ("", ""), "( string pFieldType, string pFieldName ) - Add a new data entry to the Data Table.\n"
  983. "@param pFieldType The method of evaluating the field's data.\n"
  984. "@param pFieldName The name of the field to be added to the Data Table.\n"
  985. "@return No return value." )
  986. {
  987. // Insert Data.
  988. object->getDataTable().insert( VDataTable::getDataTypeEnum(fieldType), fieldName);
  989. }
  990. DefineEngineMethod( VController, removeDataField, void, (String fieldName), (""), "( string pFieldName ) - Remove a data entry from the Data Table.\n"
  991. "@param pFieldName The name of the field to be removed from the Data Table.\n"
  992. "@return No return value." )
  993. {
  994. // Clear Data Item.
  995. object->clearData(fieldName);
  996. }
  997. #endif