simSet.cpp 31 KB

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