group.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 "gui/editor/guiInspector.h"
  23. #include "gui/editor/inspector/group.h"
  24. #include "console/script.h"
  25. #include "gui/editor/inspector/dynamicField.h"
  26. #include "gui/editor/inspector/datablockField.h"
  27. #include "gui/buttons/guiIconButtonCtrl.h"
  28. #include "T3D/assets/MaterialAsset.h"
  29. #include "T3D/assets/ShapeAsset.h"
  30. #include "T3D/assets/ImageAsset.h"
  31. #include "T3D/assets/SoundAsset.h"
  32. IMPLEMENT_CONOBJECT(GuiInspectorGroup);
  33. ConsoleDocClass( GuiInspectorGroup,
  34. "@brief The GuiInspectorGroup control is a helper control that the inspector "
  35. "makes use of which houses a collapsible pane type control for separating "
  36. "inspected objects fields into groups.\n\n"
  37. "Editor use only.\n\n"
  38. "@internal"
  39. );
  40. //#define DEBUG_SPEW
  41. //-----------------------------------------------------------------------------
  42. GuiInspectorGroup::GuiInspectorGroup()
  43. : mParent( NULL ),
  44. mStack(NULL)
  45. {
  46. setBounds(0,0,200,20);
  47. mChildren.clear();
  48. setCanSave( false );
  49. mForcedArrayIndex = -1;
  50. // Make sure we receive our ticks.
  51. setProcessTicks();
  52. mMargin.set(0,0,5,0);
  53. }
  54. //-----------------------------------------------------------------------------
  55. GuiInspectorGroup::GuiInspectorGroup( const String& groupName,
  56. SimObjectPtr<GuiInspector> parent )
  57. : mParent( parent ),
  58. mStack(NULL)
  59. {
  60. setBounds(0,0,200,20);
  61. mCaption = groupName;
  62. setCanSave( false );
  63. mChildren.clear();
  64. mMargin.set(0,0,4,0);
  65. }
  66. //-----------------------------------------------------------------------------
  67. GuiInspectorGroup::~GuiInspectorGroup()
  68. {
  69. }
  70. //-----------------------------------------------------------------------------
  71. bool GuiInspectorGroup::onAdd()
  72. {
  73. setDataField( StringTable->insert("profile"), NULL, "GuiInspectorGroupProfile" );
  74. if( !Parent::onAdd() )
  75. return false;
  76. // Create our inner controls. Allow subclasses to provide other content.
  77. if(!createContent())
  78. return false;
  79. inspectGroup();
  80. return true;
  81. }
  82. //-----------------------------------------------------------------------------
  83. bool GuiInspectorGroup::createContent()
  84. {
  85. // Create our field stack control
  86. mStack = new GuiStackControl();
  87. // Prefer GuiTransperantProfile for the stack.
  88. mStack->setDataField( StringTable->insert("profile"), NULL, "GuiInspectorStackProfile" );
  89. mStack->setInternalName(StringTable->insert("stack"));
  90. if( !mStack->registerObject() )
  91. {
  92. SAFE_DELETE( mStack );
  93. return false;
  94. }
  95. addObject( mStack );
  96. mStack->setField( "padding", "0" );
  97. return true;
  98. }
  99. //-----------------------------------------------------------------------------
  100. void GuiInspectorGroup::animateToContents()
  101. {
  102. calculateHeights();
  103. if(size() > 0)
  104. animateTo( mExpanded.extent.y );
  105. else
  106. animateTo( mHeader.extent.y );
  107. }
  108. //-----------------------------------------------------------------------------
  109. GuiInspectorField* GuiInspectorGroup::constructField( S32 fieldType )
  110. {
  111. // See if we can construct a field of this type
  112. ConsoleBaseType *cbt = ConsoleBaseType::getType(fieldType);
  113. if( !cbt )
  114. return NULL;
  115. // Alright, is it a datablock?
  116. if(cbt->isDatablock())
  117. {
  118. // Default to GameBaseData
  119. StringTableEntry typeClassName = cbt->getTypeClassName();
  120. if( mParent->getNumInspectObjects() == 1 && !dStricmp(typeClassName, "GameBaseData") )
  121. {
  122. // Try and setup the classname based on the object type
  123. char className[256];
  124. dSprintf(className,256,"%sData", mParent->getInspectObject( 0 )->getClassName());
  125. // Walk the ACR list and find a matching class if any.
  126. AbstractClassRep *walk = AbstractClassRep::getClassList();
  127. while(walk)
  128. {
  129. if(!dStricmp(walk->getClassName(), className))
  130. break;
  131. walk = walk->getNextClass();
  132. }
  133. // We found a valid class
  134. if (walk)
  135. typeClassName = walk->getClassName();
  136. }
  137. GuiInspectorDatablockField *dbFieldClass = new GuiInspectorDatablockField( typeClassName );
  138. // return our new datablock field with correct datablock type enumeration info
  139. return dbFieldClass;
  140. }
  141. // Nope, not a datablock. So maybe it has a valid inspector field override we can use?
  142. if(!cbt->getInspectorFieldType())
  143. // Nothing, so bail.
  144. return NULL;
  145. // Otherwise try to make it!
  146. ConsoleObject *co = create(cbt->getInspectorFieldType());
  147. GuiInspectorField *gif = dynamic_cast<GuiInspectorField*>(co);
  148. if(!gif)
  149. {
  150. // Wasn't appropriate type, bail.
  151. delete co;
  152. return NULL;
  153. }
  154. return gif;
  155. }
  156. //-----------------------------------------------------------------------------
  157. GuiInspectorField *GuiInspectorGroup::findField( const char *fieldName )
  158. {
  159. // If we don't have any field children we can't very well find one then can we?
  160. if( mChildren.empty() )
  161. return NULL;
  162. Vector<GuiInspectorField*>::iterator i = mChildren.begin();
  163. for( ; i != mChildren.end(); i++ )
  164. {
  165. if( ((*i)->getFieldName() != NULL && dStricmp( (*i)->getFieldName(), fieldName ) == 0) || ((*i)->getCaption() != StringTable->EmptyString() && dStricmp((*i)->getCaption(), fieldName) == 0) )
  166. return (*i);
  167. }
  168. return NULL;
  169. }
  170. //-----------------------------------------------------------------------------
  171. void GuiInspectorGroup::clearFields()
  172. {
  173. // Deallocates all field related controls.
  174. mStack->clear();
  175. // Then just cleanup our vectors which also point to children
  176. // that we keep for our own convenience.
  177. mArrayCtrls.clear();
  178. mChildren.clear();
  179. }
  180. //-----------------------------------------------------------------------------
  181. bool GuiInspectorGroup::inspectGroup()
  182. {
  183. // We can't inspect a group without a target!
  184. if( !mParent || !mParent->getNumInspectObjects() )
  185. return false;
  186. // to prevent crazy resizing, we'll just freeze our stack for a sec..
  187. mStack->freeze(true);
  188. bool bNoGroup = false;
  189. // Un-grouped fields are all sorted into the 'general' group
  190. if ( dStricmp( mCaption, "General" ) == 0 )
  191. bNoGroup = true;
  192. // Just delete all fields and recreate them (like the dynamicGroup)
  193. // because that makes creating controls for array fields a lot easier
  194. clearFields();
  195. bool bNewItems = false;
  196. bool bMakingArray = false;
  197. GuiStackControl *pArrayStack = NULL;
  198. GuiRolloutCtrl *pArrayRollout = NULL;
  199. bool bGrabItems = false;
  200. AbstractClassRep* commonAncestorClass = findCommonAncestorClass();
  201. AbstractClassRep::FieldList& fieldList = commonAncestorClass->mFieldList;
  202. for (AbstractClassRep::FieldList::iterator itr = fieldList.begin();
  203. itr != fieldList.end(); ++itr)
  204. {
  205. AbstractClassRep::Field* field = &(*itr);
  206. if (field->type == AbstractClassRep::StartGroupFieldType)
  207. {
  208. // If we're dealing with general fields, always set grabItems to true (to skip them)
  209. if (bNoGroup == true)
  210. bGrabItems = true;
  211. else if (dStricmp(field->pGroupname, mCaption) == 0)
  212. bGrabItems = true;
  213. continue;
  214. }
  215. else if (field->type == AbstractClassRep::EndGroupFieldType)
  216. {
  217. // If we're dealing with general fields, always set grabItems to false (to grab them)
  218. if (bNoGroup == true)
  219. bGrabItems = false;
  220. else if (dStricmp(field->pGroupname, mCaption) == 0)
  221. bGrabItems = false;
  222. continue;
  223. }
  224. // Skip field if it has the HideInInspectors flag set.
  225. if (field->flag.test(AbstractClassRep::FIELD_HideInInspectors))
  226. continue;
  227. String searchText = mParent->getSearchText();
  228. if (searchText != String::EmptyString) {
  229. if (String(field->pFieldname).find(searchText, 0, String::NoCase | String::Left) == String::NPos)
  230. continue;
  231. }
  232. if ((bGrabItems == true || (bNoGroup == true && bGrabItems == false)) && itr->type != AbstractClassRep::DeprecatedFieldType)
  233. {
  234. if (bNoGroup == true && bGrabItems == true)
  235. continue;
  236. if ((field->type == AbstractClassRep::StartArrayFieldType || field->type == AbstractClassRep::EndArrayFieldType) && mForcedArrayIndex != -1)
  237. {
  238. continue;
  239. }
  240. else
  241. {
  242. if (field->type == AbstractClassRep::StartArrayFieldType)
  243. {
  244. #ifdef DEBUG_SPEW
  245. Platform::outputDebugString("[GuiInspectorGroup] Beginning array '%s'",
  246. field->pFieldname );
  247. #endif
  248. // Starting an array...
  249. // Create a rollout for the Array, give it the array's name.
  250. GuiRolloutCtrl* arrayRollout = new GuiRolloutCtrl();
  251. GuiControlProfile* arrayRolloutProfile = dynamic_cast<GuiControlProfile*>(Sim::findObject("GuiInspectorRolloutProfile0"));
  252. arrayRollout->setControlProfile(arrayRolloutProfile);
  253. //arrayRollout->mCaption = StringTable->insert( String::ToString( "%s (%i)", field->pGroupname, field->elementCount ) );
  254. arrayRollout->setCaption(field->pGroupname);
  255. //arrayRollout->setMargin( 14, 0, 0, 0 );
  256. arrayRollout->registerObject();
  257. GuiStackControl* arrayStack = new GuiStackControl();
  258. arrayStack->registerObject();
  259. arrayStack->freeze(true);
  260. arrayRollout->addObject(arrayStack);
  261. // Allocate a rollout for each element-count in the array
  262. // Give it the element count name.
  263. for (U32 i = 0; i < field->elementCount; i++)
  264. {
  265. GuiRolloutCtrl* elementRollout = new GuiRolloutCtrl();
  266. GuiControlProfile* elementRolloutProfile = dynamic_cast<GuiControlProfile*>(Sim::findObject("GuiInspectorRolloutProfile0"));
  267. char buf[256];
  268. dSprintf(buf, 256, " [%i]", i);
  269. elementRollout->setControlProfile(elementRolloutProfile);
  270. elementRollout->setCaption(buf);
  271. //elementRollout->setMargin( 14, 0, 0, 0 );
  272. elementRollout->registerObject();
  273. GuiStackControl* elementStack = new GuiStackControl();
  274. elementStack->registerObject();
  275. elementRollout->addObject(elementStack);
  276. elementRollout->instantCollapse();
  277. arrayStack->addObject(elementRollout);
  278. }
  279. pArrayRollout = arrayRollout;
  280. pArrayStack = arrayStack;
  281. arrayStack->freeze(false);
  282. pArrayRollout->instantCollapse();
  283. mStack->addObject(arrayRollout);
  284. bMakingArray = true;
  285. continue;
  286. }
  287. else if (field->type == AbstractClassRep::EndArrayFieldType)
  288. {
  289. #ifdef DEBUG_SPEW
  290. Platform::outputDebugString("[GuiInspectorGroup] Ending array '%s'",
  291. field->pFieldname );
  292. #endif
  293. bMakingArray = false;
  294. continue;
  295. }
  296. }
  297. if ( bMakingArray )
  298. {
  299. // Add a GuiInspectorField for this field,
  300. // for every element in the array...
  301. for ( U32 i = 0; i < pArrayStack->size(); i++ )
  302. {
  303. FrameTemp<char> intToStr( 64 );
  304. dSprintf( intToStr, 64, "%d", i );
  305. // The array stack should have a rollout for each element
  306. // as children...
  307. GuiRolloutCtrl *pRollout = dynamic_cast<GuiRolloutCtrl*>(pArrayStack->at(i));
  308. // And the each of those rollouts should have a stack for
  309. // fields...
  310. GuiStackControl *pStack = dynamic_cast<GuiStackControl*>(pRollout->at(0));
  311. // And we add a new GuiInspectorField to each of those stacks...
  312. GuiInspectorField *fieldGui = constructField( field->type );
  313. if ( fieldGui == NULL )
  314. fieldGui = new GuiInspectorField();
  315. fieldGui->init( mParent, this );
  316. StringTableEntry caption = field->pFieldname;
  317. fieldGui->setInspectorField( field, caption, intToStr );
  318. if( fieldGui->registerObject() )
  319. {
  320. #ifdef DEBUG_SPEW
  321. Platform::outputDebugString( "[GuiInspectorGroup] Adding array element '%s[%i]'",
  322. field->pFieldname, i );
  323. #endif
  324. mChildren.push_back( fieldGui );
  325. pStack->addObject( fieldGui );
  326. }
  327. else
  328. delete fieldGui;
  329. }
  330. continue;
  331. }
  332. // This is weird, but it should work for now. - JDD
  333. // We are going to check to see if this item is an array
  334. // if so, we're going to construct a field for each array element
  335. if( field->elementCount > 1)
  336. {
  337. if (mForcedArrayIndex == -1)
  338. {
  339. // Make a rollout control for this array
  340. //
  341. GuiRolloutCtrl* rollout = new GuiRolloutCtrl();
  342. rollout->setDataField(StringTable->insert("profile"), NULL, "GuiInspectorRolloutProfile0");
  343. rollout->setCaption(String::ToString("%s (%i)", field->pFieldname, field->elementCount));
  344. rollout->setMargin(14, 0, 0, 0);
  345. rollout->registerObject();
  346. mArrayCtrls.push_back(rollout);
  347. // Put a stack control within the rollout
  348. //
  349. GuiStackControl* stack = new GuiStackControl();
  350. stack->setDataField(StringTable->insert("profile"), NULL, "GuiInspectorStackProfile");
  351. stack->registerObject();
  352. stack->freeze(true);
  353. rollout->addObject(stack);
  354. mStack->addObject(rollout);
  355. // Create each field and add it to the stack.
  356. //
  357. for (S32 nI = 0; nI < field->elementCount; nI++)
  358. {
  359. FrameTemp<char> intToStr(64);
  360. dSprintf(intToStr, 64, "%d", nI);
  361. // Construct proper ValueName[nI] format which is "ValueName0" for index 0, etc.
  362. String fieldName = String::ToString("%s%d", field->pFieldname, nI);
  363. // If the field already exists, just update it
  364. GuiInspectorField* fieldGui = findField(fieldName);
  365. if (fieldGui != NULL)
  366. {
  367. fieldGui->updateValue();
  368. continue;
  369. }
  370. bNewItems = true;
  371. fieldGui = constructField(field->type);
  372. if (fieldGui == NULL)
  373. fieldGui = new GuiInspectorField();
  374. fieldGui->init(mParent, this);
  375. StringTableEntry caption = StringTable->insert(String::ToString(" [%i]", nI));
  376. fieldGui->setInspectorField(field, caption, intToStr);
  377. if (fieldGui->registerObject())
  378. {
  379. mChildren.push_back(fieldGui);
  380. stack->addObject(fieldGui);
  381. }
  382. else
  383. delete fieldGui;
  384. }
  385. stack->freeze(false);
  386. stack->updatePanes();
  387. rollout->instantCollapse();
  388. }
  389. else
  390. {
  391. FrameTemp<char> intToStr(64);
  392. dSprintf(intToStr, 64, "%d", mForcedArrayIndex);
  393. // Construct proper ValueName[nI] format which is "ValueName0" for index 0, etc.
  394. String fieldName = String::ToString("%s%d", field->pFieldname, mForcedArrayIndex);
  395. // If the field already exists, just update it
  396. GuiInspectorField* fieldGui = findField(fieldName);
  397. if (fieldGui != NULL)
  398. {
  399. fieldGui->updateValue();
  400. continue;
  401. }
  402. bNewItems = true;
  403. fieldGui = constructField(field->type);
  404. if (fieldGui == NULL)
  405. fieldGui = new GuiInspectorField();
  406. fieldGui->init(mParent, this);
  407. fieldGui->setInspectorField(field, field->pFieldname, intToStr);
  408. if (fieldGui->registerObject())
  409. {
  410. mChildren.push_back(fieldGui);
  411. mStack->addObject(fieldGui);
  412. }
  413. else
  414. delete fieldGui;
  415. }
  416. }
  417. else
  418. {
  419. // If the field already exists, just update it
  420. GuiInspectorField* fieldGui = findField(field->pFieldname);
  421. if (fieldGui != NULL)
  422. {
  423. fieldGui->updateValue();
  424. continue;
  425. }
  426. bNewItems = true;
  427. fieldGui = constructField(field->type);
  428. if (fieldGui == NULL)
  429. fieldGui = new GuiInspectorField();
  430. fieldGui->init(mParent, this);
  431. fieldGui->setInspectorField(field);
  432. if (fieldGui->registerObject())
  433. {
  434. #ifdef DEBUG_SPEW
  435. Platform::outputDebugString("[GuiInspectorGroup] Adding field '%s'",
  436. field->pFieldname);
  437. #endif
  438. mChildren.push_back(fieldGui);
  439. mStack->addObject(fieldGui);
  440. }
  441. else
  442. {
  443. SAFE_DELETE(fieldGui);
  444. }
  445. }
  446. }
  447. }
  448. mStack->freeze(false);
  449. mStack->updatePanes();
  450. // If we've no new items, there's no need to resize anything!
  451. if( bNewItems == false && !mChildren.empty() )
  452. return true;
  453. sizeToContents();
  454. setUpdate();
  455. return true;
  456. }
  457. //-----------------------------------------------------------------------------
  458. bool GuiInspectorGroup::updateFieldValue( StringTableEntry fieldName, StringTableEntry arrayIdx )
  459. {
  460. // Check if we contain a field of this name,
  461. // if so update its value and return true.
  462. Vector<GuiInspectorField*>::iterator iter = mChildren.begin();
  463. if( arrayIdx == StringTable->EmptyString() )
  464. arrayIdx = NULL;
  465. for( ; iter != mChildren.end(); iter++ )
  466. {
  467. GuiInspectorField *field = (*iter);
  468. if ( field->mField &&
  469. field->mField->pFieldname == fieldName &&
  470. field->mFieldArrayIndex == arrayIdx )
  471. {
  472. field->updateValue();
  473. return true;
  474. }
  475. }
  476. return false;
  477. }
  478. //-----------------------------------------------------------------------------
  479. void GuiInspectorGroup::updateAllFields()
  480. {
  481. Vector<GuiInspectorField*>::iterator iter = mChildren.begin();
  482. for( ; iter != mChildren.end(); iter++ )
  483. (*iter)->updateValue();
  484. }
  485. //-----------------------------------------------------------------------------
  486. AbstractClassRep* GuiInspectorGroup::findCommonAncestorClass()
  487. {
  488. AbstractClassRep* classRep = getInspector()->getInspectObject( 0 )->getClassRep();
  489. const U32 numInspectObjects = getInspector()->getNumInspectObjects();
  490. for( U32 i = 1; i < numInspectObjects; ++ i )
  491. {
  492. SimObject* object = getInspector()->getInspectObject( i );
  493. while( !object->getClassRep()->isClass( classRep ) )
  494. {
  495. classRep = classRep->getParentClass();
  496. AssertFatal( classRep, "GuiInspectorGroup::findcommonAncestorClass - Walked above ConsoleObject!" );
  497. }
  498. }
  499. return classRep;
  500. }
  501. GuiInspectorField* GuiInspectorGroup::createInspectorField()
  502. {
  503. GuiInspectorField* newField = new GuiInspectorField();
  504. newField->init(mParent, this);
  505. newField->setSpecialEditField(true);
  506. if (newField->registerObject())
  507. {
  508. return newField;
  509. }
  510. return NULL;
  511. }
  512. void GuiInspectorGroup::addInspectorField(StringTableEntry name, StringTableEntry typeName, const char* description, const char* callbackName)
  513. {
  514. S32 fieldType = -1;
  515. String typeNameTyped = typeName;
  516. if (!typeNameTyped.startsWith("Type"))
  517. typeNameTyped = String("Type") + typeNameTyped;
  518. ConsoleBaseType* typeRef = AbstractClassRep::getTypeByName(typeNameTyped.c_str());
  519. if(typeRef)
  520. {
  521. fieldType = typeRef->getTypeID();
  522. }
  523. else
  524. {
  525. if (typeName == StringTable->insert("int"))
  526. fieldType = TypeS32;
  527. else if (typeName == StringTable->insert("float"))
  528. fieldType = TypeF32;
  529. else if (typeName == StringTable->insert("vector"))
  530. fieldType = TypePoint3F;
  531. else if (typeName == StringTable->insert("vector2"))
  532. fieldType = TypePoint2F;
  533. else if (typeName == StringTable->insert("material"))
  534. fieldType = TypeMaterialAssetId;
  535. else if (typeName == StringTable->insert("image"))
  536. fieldType = TypeImageAssetPtr;
  537. else if (typeName == StringTable->insert("shape"))
  538. fieldType = TypeShapeAssetPtr;
  539. else if (typeName == StringTable->insert("sound"))
  540. fieldType = TypeSoundAssetId;
  541. else if (typeName == StringTable->insert("bool"))
  542. fieldType = TypeBool;
  543. else if (typeName == StringTable->insert("object"))
  544. fieldType = TypeSimObjectPtr;
  545. else if (typeName == StringTable->insert("string"))
  546. fieldType = TypeString;
  547. else if (typeName == StringTable->insert("colorI"))
  548. fieldType = TypeColorI;
  549. else if (typeName == StringTable->insert("colorF"))
  550. fieldType = TypeColorF;
  551. else if (typeName == StringTable->insert("ease"))
  552. fieldType = TypeEaseF;
  553. else if (typeName == StringTable->insert("command"))
  554. fieldType = TypeCommand;
  555. else if (typeName == StringTable->insert("filename"))
  556. fieldType = TypeStringFilename;
  557. }
  558. GuiInspectorField* fieldGui;
  559. //Currently the default GuiInspectorField IS the string type, so we'll control
  560. //for that type here. If it's not TypeString, we allow the normal creation process
  561. //to continue
  562. if (fieldType == TypeString)
  563. fieldGui = new GuiInspectorField();
  564. else
  565. fieldGui = constructField(fieldType);
  566. if (fieldGui == nullptr)
  567. {
  568. //call down into script and see if there's special handling for that type of field
  569. //this allows us to have completely special-case field types implemented entirely in script
  570. if (isMethod("onConstructField"))
  571. {
  572. //ensure our stack variable is bound if we need it
  573. Con::evaluatef("%d.stack = %d;", this->getId(), mStack->getId());
  574. Con::executef(this, "onConstructField", name, name, typeName, description, StringTable->EmptyString(), StringTable->EmptyString(), callbackName, mParent->getInspectObject(0)->getId());
  575. }
  576. }
  577. else
  578. {
  579. fieldGui->init(mParent, this);
  580. fieldGui->setSpecialEditField(true);
  581. fieldGui->setTargetObject(mParent->getInspectObject(0));
  582. StringTableEntry fieldName = StringTable->insert(name);
  583. fieldGui->setSpecialEditVariableName(fieldName);
  584. fieldGui->setSpecialEditVariableType(typeName);
  585. fieldGui->setSpecialEditCallbackName(StringTable->insert(callbackName));
  586. fieldGui->setInspectorField(NULL, fieldName);
  587. fieldGui->setDocs(description);
  588. if (fieldGui->registerObject())
  589. {
  590. fieldGui->setValue(mParent->getInspectObject(0)->getDataField(fieldName, NULL));
  591. mStack->addObject(fieldGui);
  592. }
  593. else
  594. {
  595. SAFE_DELETE(fieldGui);
  596. }
  597. }
  598. }
  599. void GuiInspectorGroup::addInspectorField(GuiInspectorField* field)
  600. {
  601. mStack->addObject(field);
  602. mChildren.push_back(field);
  603. mStack->updatePanes();
  604. }
  605. void GuiInspectorGroup::removeInspectorField(StringTableEntry name)
  606. {
  607. for (U32 i = 0; i < mStack->size(); i++)
  608. {
  609. GuiInspectorField* field = dynamic_cast<GuiInspectorField*>(mStack->getObject(i));
  610. if (field == nullptr)
  611. continue;
  612. if (field->getFieldName() == name || field->getSpecialEditVariableName() == name)
  613. {
  614. mStack->removeObject(field);
  615. return;
  616. }
  617. }
  618. }
  619. void GuiInspectorGroup::hideInspectorField(StringTableEntry fieldName, bool setHidden)
  620. {
  621. SimObject* inspectObj = mParent->getInspectObject();
  622. if (inspectObj == nullptr)
  623. return;
  624. AbstractClassRep::Field* field = const_cast<AbstractClassRep::Field*>(inspectObj->getClassRep()->findField(fieldName));
  625. if (field == NULL)
  626. {
  627. Con::errorf("fieldName not found: %s.%s", inspectObj->getName(), fieldName);
  628. return;
  629. }
  630. if (setHidden)
  631. field->flag.set(AbstractClassRep::FIELD_HideInInspectors);
  632. else
  633. field->flag.clear(AbstractClassRep::FIELD_HideInInspectors);
  634. }
  635. DefineEngineMethod(GuiInspectorGroup, createInspectorField, GuiInspectorField*, (), , "createInspectorField()")
  636. {
  637. return object->createInspectorField();
  638. }
  639. DefineEngineMethod(GuiInspectorGroup, addField, void, (const char* fieldName, const char* fieldTypeName, const char* description, const char* callbackName),
  640. ("", "", "", ""),
  641. "Adds a new Inspector field to this group.\n"
  642. "@param fieldName The name of the field to add. The field will associate to a variable of the same name on the inspected object for editing purposes."
  643. "@param fieldTypeName The name of the type of field it is. If it's an understood existing type, it will create it as normal. If it's an unknown type, it will attempt to call into script to create it."
  644. "@param description (Optional) Description of the field."
  645. "@param callbackName (Optional) Sets a special callback function to be called when this field is edited.")
  646. {
  647. if (dStrEqual(fieldName, "") || dStrEqual(fieldTypeName, ""))
  648. return;
  649. object->addInspectorField(StringTable->insert(fieldName), StringTable->insert(fieldTypeName), description, callbackName);
  650. }
  651. DefineEngineMethod(GuiInspectorGroup, addInspectorField, void, (GuiInspectorField* field), (nullAsType<GuiInspectorField*>()), "addInspectorField( GuiInspectorFieldObject )")
  652. {
  653. if(field)
  654. object->addInspectorField(field);
  655. }
  656. DefineEngineMethod(GuiInspectorGroup, removeField, void, (const char* fieldName),
  657. (""),
  658. "Removes a Inspector field to this group of a given name.\n"
  659. "@param fieldName The name of the field to be removed.")
  660. {
  661. if (dStrEqual(fieldName, ""))
  662. return;
  663. object->removeInspectorField(StringTable->insert(fieldName));
  664. }
  665. DefineEngineMethod(GuiInspectorGroup, hideField, void, (const char* fieldName, bool setHidden), (true),
  666. "Removes a Inspector field to this group of a given name.\n"
  667. "@param fieldName The name of the field to be removed.")
  668. {
  669. if (dStrEqual(fieldName, ""))
  670. return;
  671. object->hideInspectorField(StringTable->insert(fieldName), setHidden);
  672. }
  673. DefineEngineMethod(GuiInspectorGroup, setForcedArrayIndex, void, (S32 arrayIndex), (-1),
  674. "Sets the ForcedArrayIndex for the group. Used to force presentation of arrayed fields to only show a specific field index."
  675. "@param arrayIndex The specific field index for arrayed fields to show. Use -1 or blank arg to go back to normal behavior.")
  676. {
  677. object->setForcedArrayIndex(arrayIndex);
  678. }
  679. DefineEngineMethod(GuiInspectorGroup, findField, S32, (const char* fieldName),,
  680. "Finds an Inspector field in this group of a given name.\n"
  681. "@param fieldName The name of the field to be found.\n"
  682. "@return Field SimObjectId")
  683. {
  684. if (dStrEqual(fieldName, ""))
  685. return 0;
  686. GuiInspectorField* field = object->findField(StringTable->insert(fieldName));
  687. if (field == nullptr)
  688. return 0;
  689. return field->getId();
  690. }
  691. DefineEngineMethod(GuiInspectorGroup, refresh, void, (), ,
  692. "Finds an Inspector field in this group of a given name.\n"
  693. "@param fieldName The name of the field to be found.\n"
  694. "@return Field SimObjectId")
  695. {
  696. object->inspectGroup();
  697. }