VMotionTrack.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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/VGroup.h"
  24. #include "Verve/Extension/Motion/VMotionTrack.h"
  25. #include "Verve/Extension/Motion/VMotionEvent.h"
  26. #include "console/consoleTypes.h"
  27. //-----------------------------------------------------------------------------
  28. IMPLEMENT_CONOBJECT( VMotionTrack );
  29. //-----------------------------------------------------------------------------
  30. VMotionTrack::VMotionTrack( void ) :
  31. mDataReference( String::EmptyString ),
  32. mOrientationMode( "FREE" ),
  33. mOrientationData( String::EmptyString ),
  34. mRelative( false )
  35. {
  36. setLabel( "MotionTrack" );
  37. }
  38. void VMotionTrack::initPersistFields()
  39. {
  40. docsURL;
  41. Parent::initPersistFields();
  42. addField( "Reference", TypeRealString, Offset( mDataReference, VMotionTrack ), "The name of the data field referencing the object to be attached to the path." );
  43. addProtectedField( "OrientationMode", TypeRealString, Offset( mOrientationMode, VMotionTrack ), &setOrientationMode, &defaultProtectedGetFn, "The orientation mode of the object attached to the path." );
  44. addProtectedField( "OrientationData", TypeRealString, Offset( mOrientationData, VMotionTrack ), &setOrientationData, &defaultProtectedGetFn, "The name of the data field holding the orientation data (used for Orientation Modes, ToObject & ToPoint)." );
  45. addField( "Relative", TypeBool, Offset( mRelative, VMotionTrack ), "Attach the object with an offset based on its initial position." );
  46. }
  47. //-----------------------------------------------------------------------------
  48. //
  49. // Controller Methods.
  50. //
  51. //-----------------------------------------------------------------------------
  52. //-----------------------------------------------------------------------------
  53. //
  54. // VMotionTrack::onControllerEvent( pEvent );
  55. //
  56. // When the controller's state changes, this method is called. If the
  57. // controller is paused, then the path object will cease to move. If the
  58. // controller resumes play, the object will continue on its path.
  59. //
  60. // For a full list of possible events, see the 'eControllerEventType'
  61. // declaration in VController.h.
  62. //
  63. //-----------------------------------------------------------------------------
  64. bool VMotionTrack::onControllerEvent( VController::eControllerEventType pEvent )
  65. {
  66. if ( !Parent::onControllerEvent( pEvent ) )
  67. {
  68. // Skip.
  69. return false;
  70. }
  71. // Enabled?
  72. if ( !isEnabled() )
  73. {
  74. // Continue Processing Events.
  75. return true;
  76. }
  77. // Fetch Path & Reference Object.
  78. VTorque::PathObjectType *path = getPath();
  79. VTorque::SceneObjectType *object = getSceneObject();
  80. if ( !path || !object || !VTorque::isPathObjectAttached( path, object ) )
  81. {
  82. // Invalid.
  83. return true;
  84. }
  85. switch ( pEvent )
  86. {
  87. case VController::k_EventPlay :
  88. {
  89. // Continue Advancing.
  90. VTorque::setPathObjectActive( path, object, true );
  91. } break;
  92. case VController::k_EventPause :
  93. {
  94. // Stop Advancing.
  95. VTorque::setPathObjectActive( path, object, false );
  96. } break;
  97. case VController::k_EventStop :
  98. {
  99. // Detach the Object.
  100. detachObject();
  101. } break;
  102. }
  103. return true;
  104. }
  105. //-----------------------------------------------------------------------------
  106. //
  107. // VMotionTrack::onControllerReset( pTime, pForward );
  108. //
  109. // Reposition the path object on the path appropriately. The position is
  110. // interpolated between two nodes, the last node and the next node. These
  111. // correspond to the last and current events.
  112. //
  113. //-----------------------------------------------------------------------------
  114. void VMotionTrack::onControllerReset( const S32 &pTime, const bool &pForward )
  115. {
  116. // Parent Reset.
  117. Parent::onControllerReset( pTime, pForward );
  118. // Valid Track?
  119. // Note: We must have at least 2 Events/Nodes to path.
  120. if ( size() < 2 )
  121. {
  122. // Invalid.
  123. return;
  124. }
  125. // Get Object References.
  126. VController *controller = getController();
  127. VTorque::PathObjectType *path = getPath();
  128. VTorque::SceneObjectType *object = getSceneObject();
  129. if ( !controller || !path || !object )
  130. {
  131. // Invalid Object(s).
  132. return;
  133. }
  134. // Attached?
  135. if ( !VTorque::isPathObjectAttached( path, object ) )
  136. {
  137. // No, Attach Now.
  138. attachObject();
  139. }
  140. // Reset Object.
  141. resetObject( pTime );
  142. }
  143. //-----------------------------------------------------------------------------
  144. //
  145. // Reference Methods.
  146. //
  147. //-----------------------------------------------------------------------------
  148. //-----------------------------------------------------------------------------
  149. //
  150. // VMotionTrack::getPath();
  151. //
  152. // Returns the path that this track is referencing.
  153. //
  154. //-----------------------------------------------------------------------------
  155. VTorque::PathObjectType *VMotionTrack::getPath( void )
  156. {
  157. // Fetch the Controller.
  158. VController *controller = getController();
  159. if ( !controller )
  160. {
  161. // Invalid Controller.
  162. return NULL;
  163. }
  164. // Evalulate the Data Field.
  165. String fieldValue;
  166. if ( controller->getDataValue( mDataReference, fieldValue ) )
  167. {
  168. // Return Object.
  169. return dynamic_cast<VTorque::PathObjectType*>( Sim::findObject( fieldValue ) );
  170. }
  171. // No Data!
  172. return NULL;
  173. }
  174. //-----------------------------------------------------------------------------
  175. //
  176. // VMotionTrack::attachObject();
  177. //
  178. // Attach the underlying Scene Object to the target Path at the first Node.
  179. // Default settings are applied and must be updated after the object is
  180. // attached.
  181. //
  182. //-----------------------------------------------------------------------------
  183. void VMotionTrack::attachObject( void )
  184. {
  185. // Get Object References.
  186. VTorque::PathObjectType *path = getPath();
  187. VTorque::SceneObjectType *object = getSceneObject();
  188. if ( !path || !object )
  189. {
  190. // Invalid Object(s).
  191. return;
  192. }
  193. // Object Attached?
  194. if ( VTorque::isPathObjectAttached( path, object ) )
  195. {
  196. // Already Attached.
  197. return;
  198. }
  199. // Fetch Forwards.
  200. const bool &forward = isControllerPlayingForward();
  201. // Select the Node.
  202. const S32 node = ( forward ) ? 0 : ( size() - 1 );
  203. // Fetch the value from the controller data table.
  204. String orientationDataValue = String::EmptyString;
  205. if ( mOrientationData != String::EmptyString
  206. && !getController()->getDataValue( mOrientationData, orientationDataValue ) )
  207. {
  208. // Sanity!
  209. Con::warnf( "Unable to located the value for the given orientation data key, '%s'", mOrientationData.c_str() );
  210. // Clear.
  211. orientationDataValue = String::EmptyString;
  212. }
  213. // Attach Object.
  214. VTorque::attachPathObject( path, object, forward, mRelative, node, -1, mOrientationMode, orientationDataValue );
  215. }
  216. //-----------------------------------------------------------------------------
  217. //
  218. // VMotionTrack::detachObject( void );
  219. //
  220. //
  221. //
  222. //-----------------------------------------------------------------------------
  223. void VMotionTrack::detachObject( void )
  224. {
  225. // Get Object References.
  226. VTorque::PathObjectType *path = getPath();
  227. VTorque::SceneObjectType *object = getSceneObject();
  228. if ( !path || !object )
  229. {
  230. // Invalid Object(s).
  231. return;
  232. }
  233. // Object Attached?
  234. if ( !VTorque::isPathObjectAttached( path, object ) )
  235. {
  236. // Not Attached.
  237. return;
  238. }
  239. // Detach.
  240. VTorque::detachPathObject( path, object );
  241. }
  242. //-----------------------------------------------------------------------------
  243. //
  244. // VMotionTrack::resetObject( pTime );
  245. //
  246. //
  247. //
  248. //-----------------------------------------------------------------------------
  249. void VMotionTrack::resetObject( const S32 &pTime )
  250. {
  251. // Get Object References.
  252. VTorque::PathObjectType *path = getPath();
  253. VTorque::SceneObjectType *object = getSceneObject();
  254. if ( !path || !object )
  255. {
  256. // Invalid Object(s).
  257. return;
  258. }
  259. // Fetch Controller Info.
  260. const bool &isPlaying = isControllerPlaying();
  261. const bool &isPlayingForward = isControllerPlayingForward();
  262. const bool &isLooping = isControllerLooping();
  263. // Init Variables.
  264. bool objectActive = false;
  265. F32 objectInterp = 0.f;
  266. F32 objectSpeed = 0.f;
  267. S32 srcNodeIndex = 0;
  268. S32 dstNodeIndex = 0;
  269. VMotionEvent *event;
  270. if ( !getNextEvent( event ) || event->getTriggerTime() == pTime )
  271. {
  272. // Note: This case deals with a target time that is greater than the
  273. // trigger time of the Last Event on this track. It will clamp
  274. // the position of the object to the corresponding node of the
  275. // Last Event.
  276. // Note: If pTime is exactly equal to the Next Event's trigger time,
  277. // then it will set the Source Node to the Last Node and
  278. // set its Interp to 0.f - which is incorrect!
  279. if ( !event || event->getTriggerTime() != pTime )
  280. {
  281. // Fetch the Last Event.
  282. getPreviousEvent( event );
  283. }
  284. // Set the Info.
  285. objectInterp = 0.f;
  286. objectSpeed = event->getObjectSpeed();
  287. srcNodeIndex = event->getNodeIndex();
  288. dstNodeIndex = srcNodeIndex;
  289. }
  290. else if ( !event->getPreviousEvent() )
  291. {
  292. // Note: This case deals with a target time that is less than the
  293. // trigger time of the First Event on this track. It will clamp
  294. // the position of the object to the corresponding node of the
  295. // First Event.
  296. // Set the Info.
  297. objectInterp = 0.f;
  298. objectSpeed = event->getObjectSpeed();
  299. srcNodeIndex = event->getNodeIndex();
  300. dstNodeIndex = srcNodeIndex;
  301. }
  302. else
  303. {
  304. // Note: This case deals with a target time that is between two Events
  305. // on this track. It will position the object on the path,
  306. // between the two nodes corresponding to the Events.
  307. // Fetch the Last Event.
  308. VMotionEvent *lastEvent;
  309. getPreviousEvent( lastEvent );
  310. // Set the Info.
  311. objectActive = isPlaying;
  312. objectInterp = calculateInterp( pTime );
  313. objectSpeed = lastEvent->getObjectSpeed();
  314. srcNodeIndex = event->getNodeIndex( ( isPlayingForward ) ? -1 : 1 );
  315. dstNodeIndex = event->getNodeIndex();
  316. }
  317. // Set Active.
  318. VTorque::setPathObjectActive( path, object, objectActive );
  319. // Set Forward.
  320. VTorque::setPathObjectForward( path, object, isPlayingForward );
  321. // Set Speed.
  322. VTorque::setPathObjectSpeed( path, object, objectSpeed );
  323. // Set Current Node.
  324. VTorque::setPathObjectNode( path, object, srcNodeIndex );
  325. // Set End Node.
  326. VTorque::setPathObjectEndNode( path, object, ( ( isLooping ) ? -1 : ( size() - 1 ) ) );
  327. // Set Interp.
  328. VTorque::setPathObjectInterp( path, object, objectInterp );
  329. }
  330. //-----------------------------------------------------------------------------
  331. //
  332. // Static Field Methods.
  333. //
  334. //-----------------------------------------------------------------------------
  335. bool VMotionTrack::setOrientationMode( void *pObject, const char *pArray, const char *pData )
  336. {
  337. // Fetch Track.
  338. VMotionTrack *track = static_cast<VMotionTrack*>( pObject );
  339. // Store Data.
  340. track->mOrientationMode = pData;
  341. VTorque::PathObjectType *path = track->getPath();
  342. VTorque::SceneObjectType *object = track->getSceneObject();
  343. if ( VTorque::isPathObjectAttached( path, object ) )
  344. {
  345. // Set Orientation Mode.
  346. VTorque::setPathObjectOrientation( path, object, track->mOrientationMode, track->mOrientationData );
  347. }
  348. return false;
  349. }
  350. bool VMotionTrack::setOrientationData( void *pObject, const char *pArray, const char *pData )
  351. {
  352. // Fetch Track.
  353. VMotionTrack *track = static_cast<VMotionTrack*>( pObject );
  354. // Store Data.
  355. track->mOrientationData = pData;
  356. VTorque::PathObjectType *path = track->getPath();
  357. VTorque::SceneObjectType *object = track->getSceneObject();
  358. if ( VTorque::isPathObjectAttached( path, object ) )
  359. {
  360. // Set Orientation Mode.
  361. VTorque::setPathObjectOrientation( path, object, track->mOrientationMode, track->mOrientationData );
  362. }
  363. return false;
  364. }
  365. #ifdef VT_EDITOR
  366. //-----------------------------------------------------------------------------
  367. //
  368. // Debug Methods.
  369. //
  370. //-----------------------------------------------------------------------------
  371. DefineEngineMethod( VMotionTrack, getPath, S32, (),, "( void ) - Get the path object this track references.\n"
  372. "@return Returns the SimObjectID for the object." )
  373. {
  374. // Fetch Path.
  375. SimObject *pathReference = object->getPath();
  376. // Return.
  377. return ( pathReference ) ? pathReference->getId() : 0;
  378. }
  379. #endif