simSet.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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( 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, ConsoleValueRef argv[], bool executeOnChildGroups )
  189. {
  190. // Prep the arguments for the console exec...
  191. // Make sure and leave args[1] empty.
  192. ConsoleValueRef args[21] = { };
  193. ConsoleValue name_method;
  194. name_method.setStackStringValue(method.c_str());
  195. args[0] = ConsoleValueRef::fromValue(&name_method);
  196. for (S32 i = 0; i < argc; i++)
  197. args[i + 2] = argv[i];
  198. for( iterator i = begin(); i != end(); i++ )
  199. {
  200. SimObject *childObj = static_cast<SimObject*>(*i);
  201. if( childObj->isMethod( method.c_str() ) )
  202. Con::execute(childObj, argc + 2, args);
  203. if( executeOnChildGroups )
  204. {
  205. SimSet* childSet = dynamic_cast<SimSet*>(*i);
  206. if ( childSet )
  207. childSet->callOnChildren( method, argc, argv, executeOnChildGroups );
  208. }
  209. }
  210. }
  211. //-----------------------------------------------------------------------------
  212. U32 SimSet::sizeRecursive()
  213. {
  214. U32 count = 0;
  215. for ( iterator i = begin(); i != end(); i++ )
  216. {
  217. count++;
  218. SimSet* childSet = dynamic_cast<SimSet*>(*i);
  219. if ( childSet )
  220. count += childSet->sizeRecursive();
  221. }
  222. return count;
  223. }
  224. //-----------------------------------------------------------------------------
  225. bool SimSet::reOrder( SimObject *obj, SimObject *target )
  226. {
  227. MutexHandle handle;
  228. handle.lock(mMutex);
  229. iterator itrS, itrD;
  230. if ( (itrS = find(begin(),end(),obj)) == end() )
  231. {
  232. // object must be in list
  233. return false;
  234. }
  235. if ( obj == target )
  236. {
  237. // don't reorder same object but don't indicate error
  238. return true;
  239. }
  240. if ( !target )
  241. {
  242. // if no target, then put to back of list
  243. // don't move if already last object
  244. if ( itrS != (end()-1) )
  245. {
  246. // remove object from its current location and push to back of list
  247. mObjectList.erase(itrS);
  248. mObjectList.push_back(obj);
  249. }
  250. }
  251. else
  252. {
  253. // if target, insert object in front of target
  254. if ( (itrD = find(begin(),end(),target)) == end() )
  255. // target must be in list
  256. return false;
  257. mObjectList.erase(itrS);
  258. // once itrS has been erased, itrD won't be pointing at the
  259. // same place anymore - re-find...
  260. itrD = find(begin(),end(),target);
  261. mObjectList.insert(itrD, obj);
  262. }
  263. return true;
  264. }
  265. //-----------------------------------------------------------------------------
  266. void SimSet::onDeleteNotify(SimObject *object)
  267. {
  268. removeObject(object);
  269. Parent::onDeleteNotify(object);
  270. }
  271. //-----------------------------------------------------------------------------
  272. void SimSet::onRemove()
  273. {
  274. MutexHandle handle;
  275. handle.lock( mMutex );
  276. if( !mObjectList.empty() )
  277. {
  278. mObjectList.sortId();
  279. // This backwards iterator loop doesn't work if the
  280. // list is empty, check the size first.
  281. for( SimObjectList::iterator ptr = mObjectList.end() - 1;
  282. ptr >= mObjectList.begin(); ptr -- )
  283. clearNotify( *ptr );
  284. }
  285. handle.unlock();
  286. Parent::onRemove();
  287. }
  288. //-----------------------------------------------------------------------------
  289. void SimSet::write(Stream &stream, U32 tabStop, U32 flags)
  290. {
  291. MutexHandle handle;
  292. handle.lock(mMutex);
  293. // export selected only?
  294. if((flags & SelectedOnly) && !isSelected())
  295. {
  296. for(U32 i = 0; i < size(); i++)
  297. (*this)[i]->write(stream, tabStop, flags);
  298. return;
  299. }
  300. stream.writeTabs( tabStop );
  301. char buffer[ 2048 ];
  302. const U32 bufferWriteLen = dSprintf( buffer, sizeof( buffer ), "new %s(%s) {\r\n", getClassName(), getName() && !( flags & NoName ) ? getName() : "" );
  303. stream.write( bufferWriteLen, buffer );
  304. writeFields( stream, tabStop + 1 );
  305. if(size())
  306. {
  307. stream.write(2, "\r\n");
  308. for(U32 i = 0; i < size(); i++)
  309. {
  310. SimObject* child = ( *this )[ i ];
  311. if( child->getCanSave() )
  312. child->write(stream, tabStop + 1, flags);
  313. }
  314. }
  315. stream.writeTabs(tabStop);
  316. stream.write(4, "};\r\n");
  317. }
  318. //-----------------------------------------------------------------------------
  319. void SimSet::clear()
  320. {
  321. lock();
  322. while( !empty() )
  323. popObject();
  324. unlock();
  325. getSetModificationSignal().trigger( SetCleared, this, NULL );
  326. }
  327. //-----------------------------------------------------------------------------
  328. //UNSAFE
  329. void SimSet::deleteAllObjects()
  330. {
  331. lock();
  332. while( !empty() )
  333. {
  334. SimObject* object = mObjectList.last();
  335. mObjectList.pop_back();
  336. object->deleteObject();
  337. }
  338. unlock();
  339. }
  340. //-----------------------------------------------------------------------------
  341. SimObject* SimSet::findObject( SimObject* object )
  342. {
  343. bool found = false;
  344. lock();
  345. for( SimSet::iterator iter = begin(); iter != end(); ++ iter )
  346. if( *iter == object )
  347. {
  348. found = true;
  349. break;
  350. }
  351. unlock();
  352. if( found )
  353. return object;
  354. return NULL;
  355. }
  356. //-----------------------------------------------------------------------------
  357. SimObject* SimSet::findObject( const char *namePath )
  358. {
  359. // find the end of the object name
  360. S32 len;
  361. for(len = 0; namePath[len] != 0 && namePath[len] != '/'; len++)
  362. ;
  363. StringTableEntry stName = StringTable->lookupn(namePath, len);
  364. if(!stName)
  365. return NULL;
  366. lock();
  367. for(SimSet::iterator i = begin(); i != end(); i++)
  368. {
  369. if((*i)->getName() == stName)
  370. {
  371. unlock();
  372. if(namePath[len] == 0)
  373. return *i;
  374. return (*i)->findObject(namePath + len + 1);
  375. }
  376. }
  377. unlock();
  378. return NULL;
  379. }
  380. //-----------------------------------------------------------------------------
  381. SimObject* SimSet::findObjectByInternalName(StringTableEntry internalName, bool searchChildren)
  382. {
  383. iterator i;
  384. for (i = begin(); i != end(); i++)
  385. {
  386. SimObject *childObj = static_cast<SimObject*>(*i);
  387. if(childObj->getInternalName() == internalName)
  388. return childObj;
  389. else if (searchChildren)
  390. {
  391. SimSet* childSet = dynamic_cast<SimSet*>(*i);
  392. if (childSet)
  393. {
  394. SimObject* found = childSet->findObjectByInternalName(internalName, searchChildren);
  395. if (found) return found;
  396. }
  397. }
  398. }
  399. return NULL;
  400. }
  401. //-----------------------------------------------------------------------------
  402. SimObject* SimSet::findObjectByLineNumber(const char* fileName, S32 declarationLine, bool searchChildren)
  403. {
  404. if (!fileName)
  405. return NULL;
  406. if (declarationLine < 0)
  407. return NULL;
  408. StringTableEntry fileEntry = StringTable->insert(fileName);
  409. for (iterator i = begin(); i != end(); i++)
  410. {
  411. SimObject *childObj = static_cast<SimObject*>(*i);
  412. if(childObj->getFilename() == fileEntry && childObj->getDeclarationLine() == declarationLine)
  413. return childObj;
  414. else if (searchChildren)
  415. {
  416. SimSet* childSet = dynamic_cast<SimSet*>(*i);
  417. if (childSet)
  418. {
  419. SimObject* found = childSet->findObjectByLineNumber(fileName, declarationLine, searchChildren);
  420. if (found)
  421. return found;
  422. }
  423. }
  424. }
  425. return NULL;
  426. }
  427. //-----------------------------------------------------------------------------
  428. SimObject* SimSet::getRandom()
  429. {
  430. if (size() > 0)
  431. return mObjectList[mRandI(0, size() - 1)];
  432. return NULL;
  433. }
  434. //-----------------------------------------------------------------------------
  435. SimSet* SimSet::clone()
  436. {
  437. // Clone the set object.
  438. SimObject* object = Parent::clone();
  439. SimSet* set = dynamic_cast< SimSet* >( object );
  440. if( !set )
  441. {
  442. object->deleteObject();
  443. return NULL;
  444. }
  445. // Add all object in the set.
  446. for( iterator iter = begin(); iter != end(); ++ iter )
  447. set->addObject( *iter );
  448. return set;
  449. }
  450. //-----------------------------------------------------------------------------
  451. inline void SimSetIterator::Stack::push_back(SimSet* set)
  452. {
  453. increment();
  454. last().set = set;
  455. last().itr = set->begin();
  456. }
  457. //-----------------------------------------------------------------------------
  458. SimSetIterator::SimSetIterator(SimSet* set)
  459. {
  460. VECTOR_SET_ASSOCIATION(stack);
  461. if (!set->empty())
  462. stack.push_back(set);
  463. }
  464. //-----------------------------------------------------------------------------
  465. SimObject* SimSetIterator::operator++()
  466. {
  467. SimSet* set;
  468. if ((set = dynamic_cast<SimSet*>(*stack.last().itr)) != 0)
  469. {
  470. if (!set->empty())
  471. {
  472. stack.push_back(set);
  473. return *stack.last().itr;
  474. }
  475. }
  476. while (++stack.last().itr == stack.last().set->end())
  477. {
  478. stack.pop_back();
  479. if (stack.empty())
  480. return 0;
  481. }
  482. return *stack.last().itr;
  483. }
  484. //=============================================================================
  485. // SimGroup.
  486. //=============================================================================
  487. // MARK: ---- SimGroup ----
  488. //-----------------------------------------------------------------------------
  489. SimGroup::~SimGroup()
  490. {
  491. for( iterator itr = begin(); itr != end(); itr ++ )
  492. mNameDictionary.remove(*itr);
  493. }
  494. //-----------------------------------------------------------------------------
  495. void SimGroup::_addObject( SimObject* obj, bool forcePushBack )
  496. {
  497. // Make sure we aren't adding ourself. This isn't the most robust check
  498. // but it should be good enough to prevent some self-foot-shooting.
  499. if( obj == this )
  500. {
  501. Con::errorf( "SimGroup::addObject - (%d) can't add self!", getIdString() );
  502. return;
  503. }
  504. if( obj->getGroup() == this )
  505. return;
  506. lock();
  507. obj->incRefCount();
  508. if( obj->getGroup() )
  509. obj->getGroup()->removeObject( obj );
  510. if( forcePushBack ? mObjectList.pushBack( obj ) : mObjectList.pushBackForce( obj ) )
  511. {
  512. mNameDictionary.insert( obj );
  513. obj->mGroup = this;
  514. obj->onGroupAdd();
  515. getSetModificationSignal().trigger( SetObjectAdded, this, obj );
  516. if( obj->isProperlyAdded() )
  517. onObjectAdded_callback( obj );
  518. }
  519. else
  520. obj->decRefCount();
  521. unlock();
  522. // SimObjects will automatically remove them from their group
  523. // when deleted so we don't hook up a delete notification.
  524. }
  525. //-----------------------------------------------------------------------------
  526. void SimGroup::addObject( SimObject* obj )
  527. {
  528. _addObject( obj );
  529. }
  530. //-----------------------------------------------------------------------------
  531. void SimGroup::removeObject( SimObject* obj )
  532. {
  533. lock();
  534. _removeObjectNoLock( obj );
  535. unlock();
  536. }
  537. //-----------------------------------------------------------------------------
  538. void SimGroup::_removeObjectNoLock( SimObject* obj )
  539. {
  540. if( obj->mGroup == this )
  541. {
  542. obj->onGroupRemove();
  543. mNameDictionary.remove( obj );
  544. mObjectList.remove( obj );
  545. obj->mGroup = 0;
  546. getSetModificationSignal().trigger( SetObjectRemoved, this, obj );
  547. if( obj->isProperlyAdded() )
  548. onObjectRemoved_callback( obj );
  549. obj->decRefCount();
  550. }
  551. }
  552. //-----------------------------------------------------------------------------
  553. void SimGroup::pushObject( SimObject* object )
  554. {
  555. _addObject( object, true );
  556. }
  557. //-----------------------------------------------------------------------------
  558. void SimGroup::popObject()
  559. {
  560. MutexHandle handle;
  561. handle.lock( mMutex );
  562. if(mObjectList.empty() )
  563. {
  564. AssertWarn( false, "SimGroup::popObject - Stack underflow" );
  565. return;
  566. }
  567. SimObject* object = mObjectList.last();
  568. mObjectList.pop_back();
  569. object->onGroupRemove();
  570. object->mGroup = NULL;
  571. clearNotify( object );
  572. mNameDictionary.remove( object );
  573. getSetModificationSignal().trigger( SetObjectAdded, this, object );
  574. if( object->isProperlyAdded() )
  575. onObjectRemoved_callback( object );
  576. object->decRefCount();
  577. }
  578. //-----------------------------------------------------------------------------
  579. void SimGroup::onRemove()
  580. {
  581. lock();
  582. if( !mObjectList.empty() )
  583. {
  584. mObjectList.sortId();
  585. clear();
  586. }
  587. SimObject::onRemove();
  588. unlock();
  589. }
  590. //-----------------------------------------------------------------------------
  591. void SimGroup::clear()
  592. {
  593. lock();
  594. while( size() > 0 )
  595. {
  596. SimObject* object = mObjectList.last();
  597. object->onGroupRemove();
  598. mObjectList.pop_back();
  599. mNameDictionary.remove( object );
  600. object->mGroup = 0;
  601. getSetModificationSignal().trigger( SetObjectRemoved, this, object );
  602. if( object->isProperlyAdded() )
  603. onObjectRemoved_callback( object );
  604. if( engineAPI::gUseConsoleInterop )
  605. object->deleteObject();
  606. else
  607. object->decRefCount();
  608. }
  609. unlock();
  610. getSetModificationSignal().trigger( SetCleared, this, NULL );
  611. }
  612. //-----------------------------------------------------------------------------
  613. SimObject *SimGroup::findObject(const char *namePath)
  614. {
  615. // find the end of the object name
  616. S32 len;
  617. for(len = 0; namePath[len] != 0 && namePath[len] != '/'; len++)
  618. ;
  619. StringTableEntry stName = StringTable->lookupn(namePath, len);
  620. if(!stName)
  621. return NULL;
  622. SimObject *root = mNameDictionary.find( stName );
  623. if( !root )
  624. return NULL;
  625. if(namePath[len] == 0)
  626. return root;
  627. return root->findObject(namePath + len + 1);
  628. }
  629. //-----------------------------------------------------------------------------
  630. SimGroup* SimGroup::clone()
  631. {
  632. // Skip SimSet::clone since we do not want to steal the child objects
  633. // from this group.
  634. SimObject* object = SimObject::clone();
  635. SimGroup* group = dynamic_cast< SimGroup* >( object );
  636. if( !group )
  637. {
  638. object->deleteObject();
  639. return NULL;
  640. }
  641. return group;
  642. }
  643. //-----------------------------------------------------------------------------
  644. SimGroup* SimGroup::deepClone()
  645. {
  646. // Clone the group object.
  647. SimObject* object = Parent::deepClone();
  648. SimGroup* group = dynamic_cast< SimGroup* >( object );
  649. if( !group )
  650. {
  651. object->deleteObject();
  652. return NULL;
  653. }
  654. // Clone all child objects.
  655. for( iterator iter = begin(); iter != end(); ++ iter )
  656. group->addObject( ( *iter )->deepClone() );
  657. return group;
  658. }
  659. //-----------------------------------------------------------------------------
  660. bool SimGroup::processArguments(S32, ConsoleValueRef *argv)
  661. {
  662. return true;
  663. }
  664. //-----------------------------------------------------------------------------
  665. SimObject* SimGroupIterator::operator++()
  666. {
  667. SimGroup* set;
  668. if ((set = dynamic_cast<SimGroup*>(*stack.last().itr)) != 0)
  669. {
  670. if (!set->empty())
  671. {
  672. stack.push_back(set);
  673. return *stack.last().itr;
  674. }
  675. }
  676. while (++stack.last().itr == stack.last().set->end())
  677. {
  678. stack.pop_back();
  679. if (stack.empty())
  680. return 0;
  681. }
  682. return *stack.last().itr;
  683. }
  684. //=============================================================================
  685. // API.
  686. //=============================================================================
  687. // MARK: ---- API ----
  688. //-----------------------------------------------------------------------------
  689. DefineEngineMethod( SimSet, listObjects, void, (),,
  690. "Dump a list of all objects contained in the set to the console." )
  691. {
  692. object->lock();
  693. SimSet::iterator itr;
  694. for(itr = object->begin(); itr != object->end(); itr++)
  695. {
  696. SimObject *obj = *itr;
  697. bool isSet = dynamic_cast<SimSet *>(obj) != 0;
  698. const char *name = obj->getName();
  699. if(name)
  700. Con::printf(" %d,\"%s\": %s %s", obj->getId(), name,
  701. obj->getClassName(), isSet ? "(g)":"");
  702. else
  703. Con::printf(" %d: %s %s", obj->getId(), obj->getClassName(),
  704. isSet ? "(g)" : "");
  705. }
  706. object->unlock();
  707. }
  708. //-----------------------------------------------------------------------------
  709. DefineEngineStringlyVariadicMethod( SimSet, add, void, 3, 0,
  710. "( SimObject objects... ) Add the given objects to the set.\n"
  711. "@param objects The objects to add to the set." )
  712. {
  713. for(S32 i = 2; i < argc; i++)
  714. {
  715. SimObject *obj = Sim::findObject( argv[ i ] );
  716. if(obj)
  717. object->addObject( obj );
  718. else
  719. Con::printf("Set::add: Object \"%s\" doesn't exist", (const char*)argv[ i ] );
  720. }
  721. }
  722. //-----------------------------------------------------------------------------
  723. DefineEngineStringlyVariadicMethod( SimSet, remove, void, 3, 0,
  724. "( SimObject objects... ) Remove the given objects from the set.\n"
  725. "@param objects The objects to remove from the set." )
  726. {
  727. for(S32 i = 2; i < argc; i++)
  728. {
  729. SimObject *obj = Sim::findObject(argv[i]);
  730. object->lock();
  731. if(obj && object->find(object->begin(),object->end(),obj) != object->end())
  732. object->removeObject(obj);
  733. else
  734. Con::printf("Set::remove: Object \"%s\" does not exist in set", (const char*)argv[i]);
  735. object->unlock();
  736. }
  737. }
  738. //-----------------------------------------------------------------------------
  739. DefineEngineMethod( SimSet, clear, void, (),,
  740. "Remove all objects from the set." )
  741. {
  742. object->clear();
  743. }
  744. //-----------------------------------------------------------------------------
  745. //UNSAFE; don't want this in the new API
  746. DefineEngineMethod( SimSet, deleteAllObjects, void, (), , "() Delete all objects in the set." )
  747. {
  748. object->deleteAllObjects();
  749. }
  750. //-----------------------------------------------------------------------------
  751. DefineEngineMethod( SimSet, getRandom, SimObject*, (),,
  752. "Return a random object from the set.\n"
  753. "@return A randomly selected object from the set or -1 if the set is empty." )
  754. {
  755. return object->getRandom();
  756. }
  757. //-----------------------------------------------------------------------------
  758. DefineEngineStringlyVariadicMethod( SimSet, callOnChildren, void, 3, 0,
  759. "( string method, string args... ) Call a method on all objects contained in the set.\n\n"
  760. "@param method The name of the method to call.\n"
  761. "@param args The arguments to the method.\n\n"
  762. "@note This method recurses into all SimSets that are children to the set.\n\n"
  763. "@see callOnChildrenNoRecurse" )
  764. {
  765. object->callOnChildren( (const char*)argv[2], argc - 3, argv + 3 );
  766. }
  767. //-----------------------------------------------------------------------------
  768. DefineEngineStringlyVariadicMethod( SimSet, callOnChildrenNoRecurse, void, 3, 0,
  769. "( string method, string args... ) Call a method on all objects contained in the set.\n\n"
  770. "@param method The name of the method to call.\n"
  771. "@param args The arguments to the method.\n\n"
  772. "@note This method does not recurse into child SimSets.\n\n"
  773. "@see callOnChildren" )
  774. {
  775. object->callOnChildren( (const char*)argv[2], argc - 3, argv + 3, false );
  776. }
  777. //-----------------------------------------------------------------------------
  778. DefineEngineMethod( SimSet, reorderChild, void, ( SimObject* child1, SimObject* child2 ),,
  779. "Make sure child1 is ordered right before child2 in the set.\n"
  780. "@param child1 The first child. The object must already be contained in the set.\n"
  781. "@param child2 The second child. The object must already be contained in the set." )
  782. {
  783. SimObject* pObject = child1;
  784. SimObject* pTarget = child2;
  785. if(pObject && pTarget)
  786. {
  787. object->reOrder(pObject,pTarget);
  788. }
  789. }
  790. //-----------------------------------------------------------------------------
  791. DefineEngineMethod( SimSet, getCount, S32, (),,
  792. "Get the number of objects contained in the set.\n"
  793. "@return The number of objects contained in the set." )
  794. {
  795. return object->size();
  796. }
  797. //-----------------------------------------------------------------------------
  798. DEFINE_CALLIN( fnSimSet_getCountRecursive, getCountRecursive, SimSet, U32, ( SimSet* set ),,,
  799. "Get the number of direct and indirect child objects contained in the set.\n"
  800. "@return The number of objects contained in the set as well as in other sets contained directly or indirectly in the set." )
  801. {
  802. return set->sizeRecursive();
  803. }
  804. DefineEngineMethod( SimSet, getFullCount, S32, (), , "() Get the number of direct and indirect child objects contained in the set.\n"
  805. "@return The number of objects contained in the set as well as in other sets contained directly or indirectly in the set." )
  806. {
  807. return object->sizeRecursive();
  808. }
  809. //-----------------------------------------------------------------------------
  810. DefineEngineMethod( SimSet, getObject, SimObject*, ( U32 index ),,
  811. "Get the object at the given index.\n"
  812. "@param index The object index.\n"
  813. "@return The object at the given index or -1 if index is out of range." )
  814. {
  815. if( index < 0 || index >= object->size() )
  816. {
  817. Con::errorf( "Set::getObject - index out of range." );
  818. return NULL;
  819. }
  820. return ( *object )[ index ];
  821. }
  822. //-----------------------------------------------------------------------------
  823. DefineEngineMethod( SimSet, getObjectIndex, S32, ( SimObject* obj ),,
  824. "Return the index of the given object in this set.\n"
  825. "@param obj The object for which to return the index. Must be contained in the set.\n"
  826. "@return The index of the object or -1 if the object is not contained in the set." )
  827. {
  828. if( !obj )
  829. return -1;
  830. object->lock();
  831. S32 count = 0;
  832. for( SimSet::iterator i = object->begin(); i != object->end(); i++)
  833. {
  834. if( *i == obj )
  835. {
  836. object->unlock();
  837. return count;
  838. }
  839. ++count;
  840. }
  841. object->unlock();
  842. return -1;
  843. }
  844. //-----------------------------------------------------------------------------
  845. DefineEngineMethod( SimSet, isMember, bool, ( SimObject* obj ),,
  846. "Test whether the given object belongs to the set.\n"
  847. "@param obj The object.\n"
  848. "@return True if the object is contained in the set; false otherwise." )
  849. {
  850. if( !obj )
  851. return false;
  852. return ( object->find( object->begin(), object->end(), obj ) != object->end() );
  853. }
  854. //-----------------------------------------------------------------------------
  855. DefineEngineMethod( SimSet, findObjectByInternalName, SimObject*, ( const char* internalName, bool searchChildren ), ( false ),
  856. "Find an object in the set by its internal name.\n"
  857. "@param internalName The internal name of the object to look for.\n"
  858. "@param searchChildren If true, SimSets contained in the set will be recursively searched for the object.\n"
  859. "@return The object with the given internal name or 0 if no match was found.\n" )
  860. {
  861. StringTableEntry pcName = StringTable->insert( internalName );
  862. return object->findObjectByInternalName( pcName, searchChildren );
  863. }
  864. //-----------------------------------------------------------------------------
  865. DefineEngineMethod( SimSet, bringToFront, void, ( SimObject* obj ),,
  866. "Make the given object the first object in the set.\n"
  867. "@param obj The object to bring to the frontmost position. Must be contained in the set." )
  868. {
  869. if( obj )
  870. object->bringObjectToFront( obj );
  871. }
  872. //-----------------------------------------------------------------------------
  873. DefineEngineMethod( SimSet, pushToBack, void, ( SimObject* obj ),,
  874. "Make the given object the last object in the set.\n"
  875. "@param obj The object to bring to the last position. Must be contained in the set." )
  876. {
  877. if( obj )
  878. object->pushObjectToBack( obj );
  879. }
  880. //-----------------------------------------------------------------------------
  881. DefineEngineMethod( SimSet, sort, void, ( const char * callbackFunction ), , "( string callbackFunction ) Sort the objects in the set using the given comparison function.\n"
  882. "@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." )
  883. {
  884. object->scriptSort( callbackFunction );
  885. }
  886. //-----------------------------------------------------------------------------
  887. DefineEngineMethod( SimSet, acceptsAsChild, bool, ( SimObject* obj ),,
  888. "Test whether the given object may be added to the set.\n"
  889. "@param obj The object to test for potential membership.\n"
  890. "@return True if the object may be added to the set, false otherwise." )
  891. {
  892. if( !obj )
  893. return false;
  894. return object->acceptsAsChild( obj );
  895. }