simSet.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/simSet.h"
  24. #include "core/stringTable.h"
  25. #include "console/console.h"
  26. #include "console/engineAPI.h"
  27. #include "core/stream/fileStream.h"
  28. #include "sim/actionMap.h"
  29. #include "core/fileObject.h"
  30. #include "console/consoleInternal.h"
  31. #include "console/engineAPI.h"
  32. #include "platform/profiler.h"
  33. #include "console/typeValidators.h"
  34. #include "core/frameAllocator.h"
  35. #include "math/mMathFn.h"
  36. IMPLEMENT_CONOBJECT_CHILDREN( SimSet );
  37. IMPLEMENT_CONOBJECT( SimGroup );
  38. ConsoleDocClass( SimSet,
  39. "@brief A collection of SimObjects.\n\n"
  40. "It is often necessary to keep track of an arbitrary set of SimObjects. "
  41. "For instance, Torque's networking code needs to not only keep track of "
  42. "the set of objects which need to be ghosted, but also the set of objects "
  43. "which must <i>always</i> be ghosted. It does this by working with two "
  44. "sets. The first of these is the RootGroup (which is actually a SimGroup) "
  45. "and the second is the GhostAlwaysSet, which contains objects which must "
  46. "always be ghosted to the client.\n\n"
  47. "Some general notes on SimSets:\n\n"
  48. "- Membership is not exclusive. A SimObject may be a member of multiple "
  49. "SimSets.\n\n"
  50. "- A SimSet does not destroy subobjects when it is destroyed.\n\n"
  51. "- A SimSet may hold an arbitrary number of objects.\n\n"
  52. "@ingroup Console\n"
  53. "@ingroup Scripting"
  54. );
  55. ConsoleDocClass( SimGroup,
  56. "@brief A collection of SimObjects that are owned by the group.\n\n"
  57. "A SimGroup is a stricter form of SimSet. SimObjects may only be a member "
  58. "of a single SimGroup at a time. The SimGroup will automatically enforce "
  59. "the single-group-membership rule (ie. adding an object to a SimGroup will "
  60. "cause it to be removed from its current SimGroup, if any).\n\n"
  61. "Deleting a SimGroup will also delete all SimObjects in the SimGroup.\n\n"
  62. "@tsexample\n"
  63. "// Create a SimGroup for particle emitters\n"
  64. "new SimGroup(Emitters)\n"
  65. "{\n"
  66. " canSaveDynamicFields = \"1\";\n\n"
  67. " new ParticleEmitterNode(CrystalEmmiter) {\n"
  68. " active = \"1\";\n"
  69. " emitter = \"dustEmitter\";\n"
  70. " velocity = \"1\";\n"
  71. " dataBlock = \"GenericSmokeEmitterNode\";\n"
  72. " position = \"-61.6276 2.1142 4.45027\";\n"
  73. " rotation = \"1 0 0 0\";\n"
  74. " scale = \"1 1 1\";\n"
  75. " canSaveDynamicFields = \"1\";\n"
  76. " };\n\n"
  77. " new ParticleEmitterNode(Steam1) {\n"
  78. " active = \"1\";\n"
  79. " emitter = \"SlowSteamEmitter\";\n"
  80. " velocity = \"1\";\n"
  81. " dataBlock = \"GenericSmokeEmitterNode\";\n"
  82. " position = \"-25.0458 1.55289 2.51308\";\n"
  83. " rotation = \"1 0 0 0\";\n"
  84. " scale = \"1 1 1\";\n"
  85. " canSaveDynamicFields = \"1\";\n"
  86. " };\n"
  87. "};\n\n"
  88. "@endtsexample\n\n"
  89. "@ingroup Console\n"
  90. "@ingroup Scripting"
  91. );
  92. IMPLEMENT_CALLBACK( SimSet, onObjectAdded, void, ( SimObject* object ), ( object ),
  93. "Called when an object is added to the set.\n"
  94. "@param object The object that was added." );
  95. IMPLEMENT_CALLBACK( SimSet, onObjectRemoved, void, ( SimObject* object ), ( object ),
  96. "Called when an object is removed from the set.\n"
  97. "@param object The object that was removed." );
  98. //=============================================================================
  99. // SimSet.
  100. //=============================================================================
  101. // MARK: ---- SimSet ----
  102. //-----------------------------------------------------------------------------
  103. SimSet::SimSet()
  104. {
  105. VECTOR_SET_ASSOCIATION(mObjectList);
  106. mMutex = Mutex::createMutex();
  107. }
  108. //-----------------------------------------------------------------------------
  109. SimSet::~SimSet()
  110. {
  111. Mutex::destroyMutex( mMutex );
  112. }
  113. //-----------------------------------------------------------------------------
  114. void SimSet::addObject( SimObject* obj )
  115. {
  116. // Prevent SimSet being added to itself.
  117. if( obj == this )
  118. return;
  119. lock();
  120. const bool added = mObjectList.pushBack( obj );
  121. if( added )
  122. deleteNotify( obj );
  123. unlock();
  124. if( added )
  125. {
  126. getSetModificationSignal().trigger( SetObjectAdded, this, obj );
  127. if( obj->isProperlyAdded() )
  128. onObjectAdded_callback( obj );
  129. }
  130. }
  131. //-----------------------------------------------------------------------------
  132. void SimSet::removeObject( SimObject* obj )
  133. {
  134. lock();
  135. const bool removed = mObjectList.remove( obj );
  136. if( removed )
  137. clearNotify( obj );
  138. unlock();
  139. if( removed )
  140. {
  141. getSetModificationSignal().trigger( SetObjectRemoved, this, obj );
  142. if( obj->isProperlyAdded() )
  143. onObjectRemoved_callback( obj );
  144. }
  145. }
  146. //-----------------------------------------------------------------------------
  147. void SimSet::pushObject( SimObject* obj )
  148. {
  149. if( obj == this )
  150. return;
  151. lock();
  152. bool added = mObjectList.pushBackForce( obj );
  153. if( added )
  154. deleteNotify( obj );
  155. unlock();
  156. if( added )
  157. {
  158. getSetModificationSignal().trigger( SetObjectAdded, this, obj );
  159. if( obj->isProperlyAdded() )
  160. onObjectAdded_callback( obj );
  161. }
  162. }
  163. //-----------------------------------------------------------------------------
  164. void SimSet::popObject()
  165. {
  166. if(mObjectList.empty() )
  167. {
  168. AssertWarn(false, "Stack underflow in SimSet::popObject");
  169. return;
  170. }
  171. lock();
  172. SimObject* object = mObjectList.last();
  173. mObjectList.pop_back();
  174. clearNotify( object );
  175. unlock();
  176. getSetModificationSignal().trigger( SetObjectRemoved, this, object );
  177. if( object->isProperlyAdded() )
  178. onObjectRemoved_callback( object );
  179. }
  180. //-----------------------------------------------------------------------------
  181. void SimSet::scriptSort( const String &scriptCallbackFn )
  182. {
  183. lock();
  184. mObjectList.scriptSort( scriptCallbackFn );
  185. unlock();
  186. }
  187. //-----------------------------------------------------------------------------
  188. void SimSet::callOnChildren( const String &method, S32 argc, ConsoleValue argv[], bool executeOnChildGroups )
  189. {
  190. // TODO(JTH): Implement
  191. AssertISV(false, "TODO Implement");
  192. return;
  193. /*
  194. // Prep the arguments for the console exec...
  195. // Make sure and leave args[1] empty.
  196. ConsoleValue args[21] = { };
  197. ConsoleValue name_method;
  198. name_method.setStackStringValue(method.c_str());
  199. args[0] = ConsoleValueRef::fromValue(&name_method);
  200. for (S32 i = 0; i < argc; i++)
  201. args[i + 2] = argv[i];
  202. for( iterator i = begin(); i != end(); i++ )
  203. {
  204. SimObject *childObj = static_cast<SimObject*>(*i);
  205. if( childObj->isMethod( method.c_str() ) )
  206. Con::execute(childObj, argc + 2, args);
  207. if( executeOnChildGroups )
  208. {
  209. SimSet* childSet = dynamic_cast<SimSet*>(*i);
  210. if ( childSet )
  211. childSet->callOnChildren( method, argc, argv, executeOnChildGroups );
  212. }
  213. }
  214. */
  215. }
  216. //-----------------------------------------------------------------------------
  217. U32 SimSet::sizeRecursive()
  218. {
  219. U32 count = 0;
  220. for ( iterator i = begin(); i != end(); i++ )
  221. {
  222. count++;
  223. SimSet* childSet = dynamic_cast<SimSet*>(*i);
  224. if ( childSet )
  225. count += childSet->sizeRecursive();
  226. }
  227. return count;
  228. }
  229. //-----------------------------------------------------------------------------
  230. bool SimSet::reOrder( SimObject *obj, SimObject *target )
  231. {
  232. MutexHandle handle;
  233. handle.lock(mMutex);
  234. iterator itrS, itrD;
  235. if ( (itrS = find(begin(),end(),obj)) == end() )
  236. {
  237. // object must be in list
  238. return false;
  239. }
  240. if ( obj == target )
  241. {
  242. // don't reorder same object but don't indicate error
  243. return true;
  244. }
  245. if ( !target )
  246. {
  247. // if no target, then put to back of list
  248. // don't move if already last object
  249. if ( itrS != (end()-1) )
  250. {
  251. // remove object from its current location and push to back of list
  252. mObjectList.erase(itrS);
  253. mObjectList.push_back(obj);
  254. }
  255. }
  256. else
  257. {
  258. // if target, insert object in front of target
  259. if ( (itrD = find(begin(),end(),target)) == end() )
  260. // target must be in list
  261. return false;
  262. mObjectList.erase(itrS);
  263. // once itrS has been erased, itrD won't be pointing at the
  264. // same place anymore - re-find...
  265. itrD = find(begin(),end(),target);
  266. mObjectList.insert(itrD, obj);
  267. }
  268. return true;
  269. }
  270. //-----------------------------------------------------------------------------
  271. void SimSet::onDeleteNotify(SimObject *object)
  272. {
  273. removeObject(object);
  274. Parent::onDeleteNotify(object);
  275. }
  276. //-----------------------------------------------------------------------------
  277. void SimSet::onRemove()
  278. {
  279. MutexHandle handle;
  280. handle.lock( mMutex );
  281. if( !mObjectList.empty() )
  282. {
  283. mObjectList.sortId();
  284. // This backwards iterator loop doesn't work if the
  285. // list is empty, check the size first.
  286. for( SimObjectList::iterator ptr = mObjectList.end() - 1;
  287. ptr >= mObjectList.begin(); ptr -- )
  288. clearNotify( *ptr );
  289. }
  290. handle.unlock();
  291. Parent::onRemove();
  292. }
  293. //-----------------------------------------------------------------------------
  294. void SimSet::write(Stream &stream, U32 tabStop, U32 flags)
  295. {
  296. MutexHandle handle;
  297. handle.lock(mMutex);
  298. // export selected only?
  299. if((flags & SelectedOnly) && !isSelected())
  300. {
  301. for(U32 i = 0; i < size(); i++)
  302. (*this)[i]->write(stream, tabStop, flags);
  303. return;
  304. }
  305. stream.writeTabs( tabStop );
  306. char buffer[ 2048 ];
  307. const U32 bufferWriteLen = dSprintf( buffer, sizeof( buffer ), "new %s(%s) {\r\n", getClassName(), getName() && !( flags & NoName ) ? getName() : "" );
  308. stream.write( bufferWriteLen, buffer );
  309. writeFields( stream, tabStop + 1 );
  310. if(size())
  311. {
  312. stream.write(2, "\r\n");
  313. for(U32 i = 0; i < size(); i++)
  314. {
  315. SimObject* child = ( *this )[ i ];
  316. if( child->getCanSave() )
  317. child->write(stream, tabStop + 1, flags);
  318. }
  319. }
  320. stream.writeTabs(tabStop);
  321. stream.write(4, "};\r\n");
  322. }
  323. //-----------------------------------------------------------------------------
  324. void SimSet::clear()
  325. {
  326. lock();
  327. while( !empty() )
  328. popObject();
  329. unlock();
  330. getSetModificationSignal().trigger( SetCleared, this, NULL );
  331. }
  332. //-----------------------------------------------------------------------------
  333. //UNSAFE
  334. void SimSet::deleteAllObjects()
  335. {
  336. lock();
  337. while( !empty() )
  338. {
  339. SimObject* object = mObjectList.last();
  340. mObjectList.pop_back();
  341. object->deleteObject();
  342. }
  343. unlock();
  344. }
  345. //-----------------------------------------------------------------------------
  346. SimObject* SimSet::findObject( SimObject* object )
  347. {
  348. bool found = false;
  349. lock();
  350. for( SimSet::iterator iter = begin(); iter != end(); ++ iter )
  351. if( *iter == object )
  352. {
  353. found = true;
  354. break;
  355. }
  356. unlock();
  357. if( found )
  358. return object;
  359. return NULL;
  360. }
  361. //-----------------------------------------------------------------------------
  362. SimObject* SimSet::findObject( const char *namePath )
  363. {
  364. // find the end of the object name
  365. S32 len;
  366. for(len = 0; namePath[len] != 0 && namePath[len] != '/'; len++)
  367. ;
  368. StringTableEntry stName = StringTable->lookupn(namePath, len);
  369. if(!stName)
  370. return NULL;
  371. lock();
  372. for(SimSet::iterator i = begin(); i != end(); i++)
  373. {
  374. if((*i)->getName() == stName)
  375. {
  376. unlock();
  377. if(namePath[len] == 0)
  378. return *i;
  379. return (*i)->findObject(namePath + len + 1);
  380. }
  381. }
  382. unlock();
  383. return NULL;
  384. }
  385. //-----------------------------------------------------------------------------
  386. SimObject* SimSet::findObjectByInternalName(StringTableEntry internalName, bool searchChildren)
  387. {
  388. iterator i;
  389. for (i = begin(); i != end(); i++)
  390. {
  391. SimObject *childObj = static_cast<SimObject*>(*i);
  392. if(childObj->getInternalName() == internalName)
  393. return childObj;
  394. else if (searchChildren)
  395. {
  396. SimSet* childSet = dynamic_cast<SimSet*>(*i);
  397. if (childSet)
  398. {
  399. SimObject* found = childSet->findObjectByInternalName(internalName, searchChildren);
  400. if (found) return found;
  401. }
  402. }
  403. }
  404. return NULL;
  405. }
  406. //-----------------------------------------------------------------------------
  407. SimObject* SimSet::findObjectByLineNumber(const char* fileName, S32 declarationLine, bool searchChildren)
  408. {
  409. if (!fileName)
  410. return NULL;
  411. if (declarationLine < 0)
  412. return NULL;
  413. StringTableEntry fileEntry = StringTable->insert(fileName);
  414. for (iterator i = begin(); i != end(); i++)
  415. {
  416. SimObject *childObj = static_cast<SimObject*>(*i);
  417. if(childObj->getFilename() == fileEntry && childObj->getDeclarationLine() == declarationLine)
  418. return childObj;
  419. else if (searchChildren)
  420. {
  421. SimSet* childSet = dynamic_cast<SimSet*>(*i);
  422. if (childSet)
  423. {
  424. SimObject* found = childSet->findObjectByLineNumber(fileName, declarationLine, searchChildren);
  425. if (found)
  426. return found;
  427. }
  428. }
  429. }
  430. return NULL;
  431. }
  432. //-----------------------------------------------------------------------------
  433. SimObject* SimSet::getRandom()
  434. {
  435. if (size() > 0)
  436. return mObjectList[mRandI(0, size() - 1)];
  437. return NULL;
  438. }
  439. //-----------------------------------------------------------------------------
  440. SimSet* SimSet::clone()
  441. {
  442. // Clone the set object.
  443. SimObject* object = Parent::clone();
  444. SimSet* set = dynamic_cast< SimSet* >( object );
  445. if( !set )
  446. {
  447. object->deleteObject();
  448. return NULL;
  449. }
  450. // Add all object in the set.
  451. for( iterator iter = begin(); iter != end(); ++ iter )
  452. set->addObject( *iter );
  453. return set;
  454. }
  455. //-----------------------------------------------------------------------------
  456. inline void SimSetIterator::Stack::push_back(SimSet* set)
  457. {
  458. increment();
  459. last().set = set;
  460. last().itr = set->begin();
  461. }
  462. //-----------------------------------------------------------------------------
  463. SimSetIterator::SimSetIterator(SimSet* set)
  464. {
  465. VECTOR_SET_ASSOCIATION(stack);
  466. if (!set->empty())
  467. stack.push_back(set);
  468. }
  469. //-----------------------------------------------------------------------------
  470. SimObject* SimSetIterator::operator++()
  471. {
  472. SimSet* set;
  473. if ((set = dynamic_cast<SimSet*>(*stack.last().itr)) != 0)
  474. {
  475. if (!set->empty())
  476. {
  477. stack.push_back(set);
  478. return *stack.last().itr;
  479. }
  480. }
  481. while (++stack.last().itr == stack.last().set->end())
  482. {
  483. stack.pop_back();
  484. if (stack.empty())
  485. return 0;
  486. }
  487. return *stack.last().itr;
  488. }
  489. //=============================================================================
  490. // SimGroup.
  491. //=============================================================================
  492. // MARK: ---- SimGroup ----
  493. //-----------------------------------------------------------------------------
  494. SimGroup::~SimGroup()
  495. {
  496. for( iterator itr = begin(); itr != end(); itr ++ )
  497. mNameDictionary.remove(*itr);
  498. }
  499. //-----------------------------------------------------------------------------
  500. void SimGroup::_addObject( SimObject* obj, bool forcePushBack )
  501. {
  502. // Make sure we aren't adding ourself. This isn't the most robust check
  503. // but it should be good enough to prevent some self-foot-shooting.
  504. if( obj == this )
  505. {
  506. Con::errorf( "SimGroup::addObject - (%d) can't add self!", getIdString() );
  507. return;
  508. }
  509. if( obj->getGroup() == this )
  510. return;
  511. lock();
  512. obj->incRefCount();
  513. if( obj->getGroup() )
  514. obj->getGroup()->removeObject( obj );
  515. if( forcePushBack ? mObjectList.pushBack( obj ) : mObjectList.pushBackForce( obj ) )
  516. {
  517. mNameDictionary.insert( obj );
  518. obj->mGroup = this;
  519. obj->onGroupAdd();
  520. getSetModificationSignal().trigger( SetObjectAdded, this, obj );
  521. if( obj->isProperlyAdded() )
  522. onObjectAdded_callback( obj );
  523. }
  524. else
  525. obj->decRefCount();
  526. unlock();
  527. // SimObjects will automatically remove them from their group
  528. // when deleted so we don't hook up a delete notification.
  529. }
  530. //-----------------------------------------------------------------------------
  531. void SimGroup::addObject( SimObject* obj )
  532. {
  533. _addObject( obj );
  534. }
  535. //-----------------------------------------------------------------------------
  536. void SimGroup::removeObject( SimObject* obj )
  537. {
  538. lock();
  539. _removeObjectNoLock( obj );
  540. unlock();
  541. }
  542. //-----------------------------------------------------------------------------
  543. void SimGroup::_removeObjectNoLock( SimObject* obj )
  544. {
  545. if( obj->mGroup == this )
  546. {
  547. obj->onGroupRemove();
  548. mNameDictionary.remove( obj );
  549. mObjectList.remove( obj );
  550. obj->mGroup = 0;
  551. getSetModificationSignal().trigger( SetObjectRemoved, this, obj );
  552. if( obj->isProperlyAdded() )
  553. onObjectRemoved_callback( obj );
  554. obj->decRefCount();
  555. }
  556. }
  557. //-----------------------------------------------------------------------------
  558. void SimGroup::pushObject( SimObject* object )
  559. {
  560. _addObject( object, true );
  561. }
  562. //-----------------------------------------------------------------------------
  563. void SimGroup::popObject()
  564. {
  565. MutexHandle handle;
  566. handle.lock( mMutex );
  567. if(mObjectList.empty() )
  568. {
  569. AssertWarn( false, "SimGroup::popObject - Stack underflow" );
  570. return;
  571. }
  572. SimObject* object = mObjectList.last();
  573. mObjectList.pop_back();
  574. object->onGroupRemove();
  575. object->mGroup = NULL;
  576. clearNotify( object );
  577. mNameDictionary.remove( object );
  578. getSetModificationSignal().trigger( SetObjectAdded, this, object );
  579. if( object->isProperlyAdded() )
  580. onObjectRemoved_callback( object );
  581. object->decRefCount();
  582. }
  583. //-----------------------------------------------------------------------------
  584. void SimGroup::onRemove()
  585. {
  586. lock();
  587. if( !mObjectList.empty() )
  588. {
  589. mObjectList.sortId();
  590. clear();
  591. }
  592. SimObject::onRemove();
  593. unlock();
  594. }
  595. //-----------------------------------------------------------------------------
  596. void SimGroup::clear()
  597. {
  598. lock();
  599. while( size() > 0 )
  600. {
  601. SimObject* object = mObjectList.last();
  602. object->onGroupRemove();
  603. mObjectList.pop_back();
  604. mNameDictionary.remove( object );
  605. object->mGroup = 0;
  606. getSetModificationSignal().trigger( SetObjectRemoved, this, object );
  607. if( object->isProperlyAdded() )
  608. onObjectRemoved_callback( object );
  609. if( engineAPI::gUseConsoleInterop )
  610. object->deleteObject();
  611. else
  612. object->decRefCount();
  613. }
  614. unlock();
  615. getSetModificationSignal().trigger( SetCleared, this, NULL );
  616. }
  617. //-----------------------------------------------------------------------------
  618. SimObject *SimGroup::findObject(const char *namePath)
  619. {
  620. // find the end of the object name
  621. S32 len;
  622. for(len = 0; namePath[len] != 0 && namePath[len] != '/'; len++)
  623. ;
  624. StringTableEntry stName = StringTable->lookupn(namePath, len);
  625. if(!stName)
  626. return NULL;
  627. SimObject *root = mNameDictionary.find( stName );
  628. if( !root )
  629. return NULL;
  630. if(namePath[len] == 0)
  631. return root;
  632. return root->findObject(namePath + len + 1);
  633. }
  634. //-----------------------------------------------------------------------------
  635. SimGroup* SimGroup::clone()
  636. {
  637. // Skip SimSet::clone since we do not want to steal the child objects
  638. // from this group.
  639. SimObject* object = SimObject::clone();
  640. SimGroup* group = dynamic_cast< SimGroup* >( object );
  641. if( !group )
  642. {
  643. object->deleteObject();
  644. return NULL;
  645. }
  646. return group;
  647. }
  648. //-----------------------------------------------------------------------------
  649. SimGroup* SimGroup::deepClone()
  650. {
  651. // Clone the group object.
  652. SimObject* object = Parent::deepClone();
  653. SimGroup* group = dynamic_cast< SimGroup* >( object );
  654. if( !group )
  655. {
  656. object->deleteObject();
  657. return NULL;
  658. }
  659. // Clone all child objects.
  660. for( iterator iter = begin(); iter != end(); ++ iter )
  661. group->addObject( ( *iter )->deepClone() );
  662. return group;
  663. }
  664. //-----------------------------------------------------------------------------
  665. bool SimGroup::processArguments(S32, ConsoleValue *argv)
  666. {
  667. return true;
  668. }
  669. SimObject* SimGroup::getObject(const S32& index)
  670. {
  671. if (index < 0 || index >= size())
  672. {
  673. Con::errorf("Set::getObject - index out of range.");
  674. return NULL;
  675. }
  676. return (*this)[index];
  677. }
  678. //-----------------------------------------------------------------------------
  679. SimObject* SimGroupIterator::operator++()
  680. {
  681. SimGroup* set;
  682. if ((set = dynamic_cast<SimGroup*>(*stack.last().itr)) != 0)
  683. {
  684. if (!set->empty())
  685. {
  686. stack.push_back(set);
  687. return *stack.last().itr;
  688. }
  689. }
  690. while (++stack.last().itr == stack.last().set->end())
  691. {
  692. stack.pop_back();
  693. if (stack.empty())
  694. return 0;
  695. }
  696. return *stack.last().itr;
  697. }
  698. //=============================================================================
  699. // API.
  700. //=============================================================================
  701. // MARK: ---- API ----
  702. //-----------------------------------------------------------------------------
  703. DefineEngineMethod( SimSet, listObjects, void, (),,
  704. "Dump a list of all objects contained in the set to the console." )
  705. {
  706. object->lock();
  707. SimSet::iterator itr;
  708. for(itr = object->begin(); itr != object->end(); itr++)
  709. {
  710. SimObject *obj = *itr;
  711. bool isSet = dynamic_cast<SimSet *>(obj) != 0;
  712. const char *name = obj->getName();
  713. if(name)
  714. Con::printf(" %d,\"%s\": %s %s", obj->getId(), name,
  715. obj->getClassName(), isSet ? "(g)":"");
  716. else
  717. Con::printf(" %d: %s %s", obj->getId(), obj->getClassName(),
  718. isSet ? "(g)" : "");
  719. }
  720. object->unlock();
  721. }
  722. //-----------------------------------------------------------------------------
  723. DefineEngineStringlyVariadicMethod( SimSet, add, void, 3, 0,
  724. "( SimObject objects... ) Add the given objects to the set.\n"
  725. "@param objects The objects to add to the set." )
  726. {
  727. for(S32 i = 2; i < argc; i++)
  728. {
  729. SimObject *obj = Sim::findObject( argv[ i ] );
  730. if(obj)
  731. object->addObject( obj );
  732. else
  733. Con::printf("Set::add: Object \"%s\" doesn't exist", (const char*)argv[ i ] );
  734. }
  735. }
  736. //-----------------------------------------------------------------------------
  737. DefineEngineStringlyVariadicMethod( SimSet, remove, void, 3, 0,
  738. "( SimObject objects... ) Remove the given objects from the set.\n"
  739. "@param objects The objects to remove from the set." )
  740. {
  741. for(S32 i = 2; i < argc; i++)
  742. {
  743. SimObject *obj = Sim::findObject(argv[i]);
  744. object->lock();
  745. if(obj && object->find(object->begin(),object->end(),obj) != object->end())
  746. object->removeObject(obj);
  747. else
  748. Con::printf("Set::remove: Object \"%s\" does not exist in set", (const char*)argv[i]);
  749. object->unlock();
  750. }
  751. }
  752. //-----------------------------------------------------------------------------
  753. DefineEngineMethod( SimSet, clear, void, (),,
  754. "Remove all objects from the set." )
  755. {
  756. object->clear();
  757. }
  758. //-----------------------------------------------------------------------------
  759. //UNSAFE; don't want this in the new API
  760. DefineEngineMethod( SimSet, deleteAllObjects, void, (), , "() Delete all objects in the set." )
  761. {
  762. object->deleteAllObjects();
  763. }
  764. //-----------------------------------------------------------------------------
  765. DefineEngineMethod( SimSet, getRandom, SimObject*, (),,
  766. "Return a random object from the set.\n"
  767. "@return A randomly selected object from the set or -1 if the set is empty." )
  768. {
  769. return object->getRandom();
  770. }
  771. //-----------------------------------------------------------------------------
  772. DefineEngineStringlyVariadicMethod( SimSet, callOnChildren, void, 3, 0,
  773. "( string method, string args... ) Call a method on all objects contained in the set.\n\n"
  774. "@param method The name of the method to call.\n"
  775. "@param args The arguments to the method.\n\n"
  776. "@note This method recurses into all SimSets that are children to the set.\n\n"
  777. "@see callOnChildrenNoRecurse" )
  778. {
  779. object->callOnChildren( (const char*)argv[2], argc - 3, argv + 3 );
  780. }
  781. //-----------------------------------------------------------------------------
  782. DefineEngineStringlyVariadicMethod( SimSet, callOnChildrenNoRecurse, void, 3, 0,
  783. "( string method, string args... ) Call a method on all objects contained in the set.\n\n"
  784. "@param method The name of the method to call.\n"
  785. "@param args The arguments to the method.\n\n"
  786. "@note This method does not recurse into child SimSets.\n\n"
  787. "@see callOnChildren" )
  788. {
  789. object->callOnChildren( (const char*)argv[2], argc - 3, argv + 3, false );
  790. }
  791. //-----------------------------------------------------------------------------
  792. DefineEngineMethod( SimSet, reorderChild, void, ( SimObject* child1, SimObject* child2 ),,
  793. "Make sure child1 is ordered right before child2 in the set.\n"
  794. "@param child1 The first child. The object must already be contained in the set.\n"
  795. "@param child2 The second child. The object must already be contained in the set." )
  796. {
  797. SimObject* pObject = child1;
  798. SimObject* pTarget = child2;
  799. if(pObject && pTarget)
  800. {
  801. object->reOrder(pObject,pTarget);
  802. }
  803. }
  804. //-----------------------------------------------------------------------------
  805. DefineEngineMethod( SimSet, getCount, S32, (),,
  806. "Get the number of objects contained in the set.\n"
  807. "@return The number of objects contained in the set." )
  808. {
  809. return object->size();
  810. }
  811. //-----------------------------------------------------------------------------
  812. DEFINE_CALLIN( fnSimSet_getCountRecursive, getCountRecursive, SimSet, U32, ( SimSet* set ),,,
  813. "Get the number of direct and indirect child objects contained in the set.\n"
  814. "@return The number of objects contained in the set as well as in other sets contained directly or indirectly in the set." )
  815. {
  816. return set->sizeRecursive();
  817. }
  818. DefineEngineMethod( SimSet, getFullCount, S32, (), , "() Get the number of direct and indirect child objects contained in the set.\n"
  819. "@return The number of objects contained in the set as well as in other sets contained directly or indirectly in the set." )
  820. {
  821. return object->sizeRecursive();
  822. }
  823. //-----------------------------------------------------------------------------
  824. DefineEngineMethod( SimSet, getObject, SimObject*, ( U32 index ),,
  825. "Get the object at the given index.\n"
  826. "@param index The object index.\n"
  827. "@return The object at the given index or -1 if index is out of range." )
  828. {
  829. if( index < 0 || index >= object->size() )
  830. {
  831. Con::errorf( "Set::getObject - index out of range." );
  832. return NULL;
  833. }
  834. return ( *object )[ index ];
  835. }
  836. //-----------------------------------------------------------------------------
  837. DefineEngineMethod( SimSet, getObjectIndex, S32, ( SimObject* obj ),,
  838. "Return the index of the given object in this set.\n"
  839. "@param obj The object for which to return the index. Must be contained in the set.\n"
  840. "@return The index of the object or -1 if the object is not contained in the set." )
  841. {
  842. if( !obj )
  843. return -1;
  844. object->lock();
  845. S32 count = 0;
  846. for( SimSet::iterator i = object->begin(); i != object->end(); i++)
  847. {
  848. if( *i == obj )
  849. {
  850. object->unlock();
  851. return count;
  852. }
  853. ++count;
  854. }
  855. object->unlock();
  856. return -1;
  857. }
  858. //-----------------------------------------------------------------------------
  859. DefineEngineMethod( SimSet, isMember, bool, ( SimObject* obj ),,
  860. "Test whether the given object belongs to the set.\n"
  861. "@param obj The object.\n"
  862. "@return True if the object is contained in the set; false otherwise." )
  863. {
  864. if( !obj )
  865. return false;
  866. return ( object->find( object->begin(), object->end(), obj ) != object->end() );
  867. }
  868. //-----------------------------------------------------------------------------
  869. DefineEngineMethod( SimSet, findObjectByInternalName, SimObject*, ( const char* internalName, bool searchChildren ), ( false ),
  870. "Find an object in the set by its internal name.\n"
  871. "@param internalName The internal name of the object to look for.\n"
  872. "@param searchChildren If true, SimSets contained in the set will be recursively searched for the object.\n"
  873. "@return The object with the given internal name or 0 if no match was found.\n" )
  874. {
  875. StringTableEntry pcName = StringTable->insert( internalName );
  876. return object->findObjectByInternalName( pcName, searchChildren );
  877. }
  878. //-----------------------------------------------------------------------------
  879. DefineEngineMethod( SimSet, bringToFront, void, ( SimObject* obj ),,
  880. "Make the given object the first object in the set.\n"
  881. "@param obj The object to bring to the frontmost position. Must be contained in the set." )
  882. {
  883. if( obj )
  884. object->bringObjectToFront( obj );
  885. }
  886. //-----------------------------------------------------------------------------
  887. DefineEngineMethod( SimSet, pushToBack, void, ( SimObject* obj ),,
  888. "Make the given object the last object in the set.\n"
  889. "@param obj The object to bring to the last position. Must be contained in the set." )
  890. {
  891. if( obj )
  892. object->pushObjectToBack( obj );
  893. }
  894. //-----------------------------------------------------------------------------
  895. DefineEngineMethod( SimSet, sort, void, ( const char * callbackFunction ), , "( string callbackFunction ) Sort the objects in the set using the given comparison function.\n"
  896. "@param callbackFunction Name of a function that takes two object arguments A and B and returns -1 if A is less, 1 if B is less, and 0 if both are equal." )
  897. {
  898. object->scriptSort( callbackFunction );
  899. }
  900. //-----------------------------------------------------------------------------
  901. DefineEngineMethod( SimSet, acceptsAsChild, bool, ( SimObject* obj ),,
  902. "Test whether the given object may be added to the set.\n"
  903. "@param obj The object to test for potential membership.\n"
  904. "@return True if the object may be added to the set, false otherwise." )
  905. {
  906. if( !obj )
  907. return false;
  908. return object->acceptsAsChild( obj );
  909. }