simSet.cpp 31 KB

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