VObject.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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/VObject.h"
  24. #include "Verve/Core/VController.h"
  25. #include "console/consoleTypes.h"
  26. //-----------------------------------------------------------------------------
  27. IMPLEMENT_CONOBJECT( VObject );
  28. //-----------------------------------------------------------------------------
  29. VObject::VObject( void ) :
  30. mController( NULL ),
  31. mLabel( String::EmptyString ),
  32. mEnabled( true )
  33. {
  34. // Void.
  35. };
  36. VObject::~VObject( void )
  37. {
  38. // Remove.
  39. remove();
  40. }
  41. void VObject::initPersistFields( void )
  42. {
  43. // Don't Use Parent Fields.
  44. // Parent::initPersistFields();
  45. addProtectedField( "Enabled", TypeBool, Offset( mEnabled, VObject ), &setEnabled, &defaultProtectedGetFn, "Enable or Disable the object from playback." );
  46. addProtectedField( "Label", TypeRealString, Offset( mLabel, VObject ), &setLabel, &defaultProtectedGetFn, "The label this object is referenced by." );
  47. }
  48. //-----------------------------------------------------------------------------
  49. //
  50. // Reference Methods.
  51. //
  52. //-----------------------------------------------------------------------------
  53. //-----------------------------------------------------------------------------
  54. //
  55. // VObject::getObject( pLabel );
  56. //
  57. // Returns the object with the given label. If no object belongs to this object
  58. // with that label, then a NULL value is returned.
  59. //
  60. //-----------------------------------------------------------------------------
  61. VObject *VObject::getObject( const String &pLabel )
  62. {
  63. VObject *node = ( VObject* )mChildNode;
  64. while ( node )
  65. {
  66. // Compare Names.
  67. if ( node->getLabel().equal( pLabel, String::NoCase ) )
  68. {
  69. // Valid.
  70. return node;
  71. }
  72. // Next Sibling.
  73. node = ( VObject* )node->mSiblingNextNode;
  74. }
  75. // Invalid.
  76. return NULL;
  77. }
  78. //-----------------------------------------------------------------------------
  79. //
  80. // Property Methods.
  81. //
  82. //-----------------------------------------------------------------------------
  83. //-----------------------------------------------------------------------------
  84. //
  85. // VObject::isEnabled();
  86. //
  87. // Returns whether this object is enabled.
  88. //
  89. //-----------------------------------------------------------------------------
  90. bool VObject::isEnabled( void )
  91. {
  92. VObject *parent = dynamic_cast<VObject*>( getParent() );
  93. if ( parent && !parent->isEnabled() )
  94. {
  95. return false;
  96. }
  97. return mEnabled;
  98. }
  99. //-----------------------------------------------------------------------------
  100. //
  101. // VObject::isControllerPlaying();
  102. //
  103. // Returns whether the root controller is currently playing.
  104. //
  105. //-----------------------------------------------------------------------------
  106. bool VObject::isControllerPlaying( void )
  107. {
  108. if ( getController() )
  109. {
  110. return getController()->isPlaying();
  111. }
  112. return false;
  113. }
  114. //-----------------------------------------------------------------------------
  115. //
  116. // VObject::isControllerPaused();
  117. //
  118. // Returns whether the root controller is currently paused.
  119. //
  120. //-----------------------------------------------------------------------------
  121. bool VObject::isControllerPaused( void )
  122. {
  123. if ( getController() )
  124. {
  125. return getController()->isPaused();
  126. }
  127. return false;
  128. }
  129. //-----------------------------------------------------------------------------
  130. //
  131. // VObject::isControllerStopped();
  132. //
  133. // Returns whether the root controller is currently stopped.
  134. //
  135. //-----------------------------------------------------------------------------
  136. bool VObject::isControllerStopped( void )
  137. {
  138. if ( getController() )
  139. {
  140. return getController()->isStopped();
  141. }
  142. return true;
  143. }
  144. //-----------------------------------------------------------------------------
  145. //
  146. // VObject::isControllerPlayingForward();
  147. //
  148. // Returns whether the root controller is currently playing forward.
  149. //
  150. //-----------------------------------------------------------------------------
  151. bool VObject::isControllerPlayingForward( void )
  152. {
  153. if ( getController() )
  154. {
  155. return getController()->isPlayingForward();
  156. }
  157. return true;
  158. }
  159. //-----------------------------------------------------------------------------
  160. //
  161. // VObject::isControllerLooping();
  162. //
  163. // Returns whether the root controller is looping the sequence.
  164. //
  165. //-----------------------------------------------------------------------------
  166. bool VObject::isControllerLooping( void )
  167. {
  168. if ( getController() )
  169. {
  170. return getController()->isLooping();
  171. }
  172. return true;
  173. }
  174. //-----------------------------------------------------------------------------
  175. //
  176. // VObject::getControllerTime();
  177. //
  178. // Returns the current time of the root controller.
  179. //
  180. //-----------------------------------------------------------------------------
  181. S32 VObject::getControllerTime( void )
  182. {
  183. if ( getController() )
  184. {
  185. return getController()->getTime();
  186. }
  187. return 0;
  188. }
  189. //-----------------------------------------------------------------------------
  190. //
  191. // VObject::getControllerTimeScale();
  192. //
  193. // Returns the current timescale of the root controller.
  194. //
  195. //-----------------------------------------------------------------------------
  196. F32 VObject::getControllerTimeScale( void )
  197. {
  198. if ( getController() )
  199. {
  200. return getController()->getTimeScale();
  201. }
  202. return 1.f;
  203. }
  204. //-----------------------------------------------------------------------------
  205. //
  206. // VObject::getControllerDuration();
  207. //
  208. // Returns the duration of the root controller.
  209. //
  210. //-----------------------------------------------------------------------------
  211. S32 VObject::getControllerDuration( void )
  212. {
  213. if ( getController() )
  214. {
  215. return getController()->getDuration();
  216. }
  217. return 0;
  218. }
  219. //-----------------------------------------------------------------------------
  220. //
  221. // VObject::setLabel( pLabel );
  222. //
  223. // Set the label property.
  224. //
  225. // If the project was built using the VT_EDITOR preprocessor argument, then the
  226. // label will not be changed if the target name is already used in the parent
  227. // object.
  228. //
  229. //-----------------------------------------------------------------------------
  230. void VObject::setLabel( const String &pLabel )
  231. {
  232. #ifdef VT_EDITOR
  233. if ( mParentNode )
  234. {
  235. // Empty Label?
  236. if ( mLabel.isEmpty() )
  237. {
  238. // Set Uniqu Label.
  239. setLabelUnique( pLabel );
  240. return;
  241. }
  242. for ( VObject *walk = ( VObject* )mChildNode; walk != NULL; walk = ( VObject* )walk->mSiblingNextNode )
  243. {
  244. if ( walk != this )
  245. {
  246. if ( pLabel == walk->getLabel() )
  247. {
  248. // Exit.
  249. return;
  250. }
  251. }
  252. }
  253. }
  254. #endif
  255. // Set Label.
  256. mLabel = pLabel;
  257. }
  258. //-----------------------------------------------------------------------------
  259. //
  260. // VObject::setLabelUnique( pLabel );
  261. //
  262. // If the label that has been passed is already in use, then a new label will
  263. // be generated by appending an index to the label. For example: MyLabel
  264. // becomes MyLabel0 ... MyLabelN
  265. //
  266. //-----------------------------------------------------------------------------
  267. void VObject::setLabelUnique( const String &pLabel )
  268. {
  269. if ( mParentNode && pLabel.isNotEmpty() )
  270. {
  271. for ( VObject *walk = ( VObject* )mChildNode; walk != NULL; walk = ( VObject* )walk->mSiblingNextNode )
  272. {
  273. if ( walk != this )
  274. {
  275. if ( pLabel == walk->getLabel() )
  276. {
  277. // Strip Trailing Number.
  278. S32 i = -1;
  279. String labelBase( String::GetTrailingNumber( pLabel, i ) );
  280. i++;
  281. // Construct New Name.
  282. String labelBuffer = String::ToString( "%s%d", labelBase.c_str(), i );
  283. // Set Name.
  284. setLabelUnique( labelBuffer );
  285. // Exit.
  286. return;
  287. }
  288. }
  289. }
  290. }
  291. // Set Name.
  292. mLabel = pLabel;
  293. }
  294. //-----------------------------------------------------------------------------
  295. //
  296. // Callback Methods.
  297. //
  298. //-----------------------------------------------------------------------------
  299. //-----------------------------------------------------------------------------
  300. //
  301. // VObject::onAttach();
  302. //
  303. // Callback made when this object is attached to another node.
  304. //
  305. //-----------------------------------------------------------------------------
  306. void VObject::onAttach( void )
  307. {
  308. VTreeNode::onAttach();
  309. // Store Controller.
  310. mController = dynamic_cast<VController*>( getRoot() );
  311. #ifdef VT_EDITOR
  312. if ( isProperlyAdded() )
  313. {
  314. Con::executef( this, "onAttach" );
  315. }
  316. #endif
  317. }
  318. //-----------------------------------------------------------------------------
  319. //
  320. // VObject::onDetach();
  321. //
  322. // Callback made when this object is detached from a parent node.
  323. //
  324. //-----------------------------------------------------------------------------
  325. void VObject::onDetach( void )
  326. {
  327. VTreeNode::onDetach();
  328. // Clear Controller.
  329. mController = NULL;
  330. #ifdef VT_EDITOR
  331. if ( isProperlyAdded() )
  332. {
  333. Con::executef( this, "onDetach" );
  334. }
  335. #endif
  336. }
  337. #ifdef VT_EDITOR
  338. //-----------------------------------------------------------------------------
  339. //
  340. // Debug Methods.
  341. //
  342. //-----------------------------------------------------------------------------
  343. DefineEngineMethod( VObject, writeFile, bool, (String fileName), (""), "( string pFileName ) - Save to a given filename.\n"
  344. "@param pFileName The target file to write to.\n"
  345. "@return Returns true if the write was successful." )
  346. {
  347. // Write Target File.
  348. return VPersistence::writeFile(fileName.c_str(), object );
  349. }
  350. DefineEngineMethod( VObject, readFile, bool, (String fileName), (""), "( string pFileName ) - Clears the object and loads the new data from the given filename.\n"
  351. "@param pFileName The target file to read from.\n"
  352. "@return Returns true if the read was successful." )
  353. {
  354. // Read Target File.
  355. return VPersistence::readFile(fileName.c_str(), object );
  356. }
  357. DefineEngineMethod( VObject, getRoot, S32, (),, "( void ) - Get the root object.\n"
  358. "@return Returns the SimObjectId for the root object." )
  359. {
  360. // Fetch Object.
  361. VObject *objectRef = ( VObject* )object->getRoot();
  362. // Return Object ID.
  363. return ( objectRef ) ? objectRef->getId() : 0;
  364. }
  365. DefineEngineMethod( VObject, getParent, S32, (),, "( void ) - Get the parent object.\n"
  366. "@return Returns the SimObjectId for the parent object." )
  367. {
  368. // Fetch Object.
  369. VObject *objectRef = ( VObject* )object->mParentNode;
  370. // Return Object ID.
  371. return ( objectRef ) ? objectRef->getId() : 0;
  372. }
  373. DefineEngineMethod( VObject, getIndex, S32, (),, "( void ) - Get the index of this object relative to its siblings.\n"
  374. "@return Returns the index of this object." )
  375. {
  376. return object->getIndex();
  377. }
  378. DefineEngineMethod( VObject, getCount, S32, (),, "( void ) - Get the number of child objects.\n"
  379. "@return Returns the number of child objects." )
  380. {
  381. return object->size();
  382. }
  383. DefineEngineMethod( VObject, getObject, S32, (S32 index), (0), "( int pIndex ) - Get the object corresponding to the given index.\n"
  384. "@param pIndex The index of the object you wish to retrieve.\n"
  385. "@return Returns the SimObjectID for the object." )
  386. {
  387. // Fetch Object.
  388. VObject *objectRef = ( VObject* )object->at(index);
  389. // Return Object ID.
  390. return ( objectRef ) ? objectRef->getId() : 0;
  391. }
  392. DefineEngineMethod( VObject, clear, void, (),, "( void ) - Detaches and deletes all of the child objects.\n"
  393. "@return No return value." )
  394. {
  395. // Clear Sequence Lists.
  396. object->clear();
  397. }
  398. DefineEngineMethod( VObject, addObject, void, (SimObject* simObj), (nullAsType<SimObject*>()), "( SimObject pObject ) - Add a child object to this node.\n"
  399. "@param pObject The SimObjectID of the object to be added to this node.\n"
  400. "@return No return value." )
  401. {
  402. if (simObj == nullptr)
  403. return;
  404. VObject *child = dynamic_cast<VObject*>(simObj);
  405. if ( child )
  406. {
  407. child->addTo( object );
  408. }
  409. }
  410. DefineEngineMethod( VObject, removeObject, void, (SimObject* simObj), (nullAsType<SimObject*>()), "( SimObject pObject ) - Remove the target object from this node.\n"
  411. "@param pObject The SimObjectID of the object to be removed from this node.\n"
  412. "@return No return value." )
  413. {
  414. if (simObj == nullptr)
  415. return;
  416. VObject *child = dynamic_cast<VObject*>(simObj);
  417. if ( child && child->getParent() == object )
  418. {
  419. child->remove();
  420. }
  421. }
  422. DefineEngineMethod( VObject, setLabelUnique, void, (String label), (""), "( string pLabel ) - Force this label to be unique.\n"
  423. "@param pLabel The name you wish to reference this object by.\n"
  424. "@return No return value." )
  425. {
  426. // Set Label.
  427. object->setLabelUnique(label);
  428. }
  429. #endif