group.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 "gui/editor/inspector/dynamicField.h"
  25. #include "gui/editor/inspector/datablockField.h"
  26. #include "gui/buttons/guiIconButtonCtrl.h"
  27. IMPLEMENT_CONOBJECT(GuiInspectorGroup);
  28. ConsoleDocClass( GuiInspectorGroup,
  29. "@brief The GuiInspectorGroup control is a helper control that the inspector "
  30. "makes use of which houses a collapsible pane type control for separating "
  31. "inspected objects fields into groups.\n\n"
  32. "Editor use only.\n\n"
  33. "@internal"
  34. );
  35. //#define DEBUG_SPEW
  36. //-----------------------------------------------------------------------------
  37. GuiInspectorGroup::GuiInspectorGroup()
  38. : mParent( NULL ),
  39. mStack(NULL)
  40. {
  41. setBounds(0,0,200,20);
  42. mChildren.clear();
  43. setCanSave( false );
  44. // Make sure we receive our ticks.
  45. setProcessTicks();
  46. mMargin.set(0,0,5,0);
  47. }
  48. //-----------------------------------------------------------------------------
  49. GuiInspectorGroup::GuiInspectorGroup( const String& groupName,
  50. SimObjectPtr<GuiInspector> parent )
  51. : mParent( parent ),
  52. mStack(NULL)
  53. {
  54. setBounds(0,0,200,20);
  55. mCaption = groupName;
  56. setCanSave( false );
  57. mChildren.clear();
  58. mMargin.set(0,0,4,0);
  59. }
  60. //-----------------------------------------------------------------------------
  61. GuiInspectorGroup::~GuiInspectorGroup()
  62. {
  63. }
  64. //-----------------------------------------------------------------------------
  65. bool GuiInspectorGroup::onAdd()
  66. {
  67. setDataField( StringTable->insert("profile"), NULL, "GuiInspectorGroupProfile" );
  68. if( !Parent::onAdd() )
  69. return false;
  70. // Create our inner controls. Allow subclasses to provide other content.
  71. if(!createContent())
  72. return false;
  73. inspectGroup();
  74. return true;
  75. }
  76. //-----------------------------------------------------------------------------
  77. bool GuiInspectorGroup::createContent()
  78. {
  79. // Create our field stack control
  80. mStack = new GuiStackControl();
  81. // Prefer GuiTransperantProfile for the stack.
  82. mStack->setDataField( StringTable->insert("profile"), NULL, "GuiInspectorStackProfile" );
  83. mStack->setInternalName(StringTable->insert("stack"));
  84. if( !mStack->registerObject() )
  85. {
  86. SAFE_DELETE( mStack );
  87. return false;
  88. }
  89. addObject( mStack );
  90. mStack->setField( "padding", "0" );
  91. return true;
  92. }
  93. //-----------------------------------------------------------------------------
  94. void GuiInspectorGroup::animateToContents()
  95. {
  96. calculateHeights();
  97. if(size() > 0)
  98. animateTo( mExpanded.extent.y );
  99. else
  100. animateTo( mHeader.extent.y );
  101. }
  102. //-----------------------------------------------------------------------------
  103. GuiInspectorField* GuiInspectorGroup::constructField( S32 fieldType )
  104. {
  105. // See if we can construct a field of this type
  106. ConsoleBaseType *cbt = ConsoleBaseType::getType(fieldType);
  107. if( !cbt )
  108. return NULL;
  109. // Alright, is it a datablock?
  110. if(cbt->isDatablock())
  111. {
  112. // Default to GameBaseData
  113. StringTableEntry typeClassName = cbt->getTypeClassName();
  114. if( mParent->getNumInspectObjects() == 1 && !dStricmp(typeClassName, "GameBaseData") )
  115. {
  116. // Try and setup the classname based on the object type
  117. char className[256];
  118. dSprintf(className,256,"%sData", mParent->getInspectObject( 0 )->getClassName());
  119. // Walk the ACR list and find a matching class if any.
  120. AbstractClassRep *walk = AbstractClassRep::getClassList();
  121. while(walk)
  122. {
  123. if(!dStricmp(walk->getClassName(), className))
  124. break;
  125. walk = walk->getNextClass();
  126. }
  127. // We found a valid class
  128. if (walk)
  129. typeClassName = walk->getClassName();
  130. }
  131. GuiInspectorDatablockField *dbFieldClass = new GuiInspectorDatablockField( typeClassName );
  132. // return our new datablock field with correct datablock type enumeration info
  133. return dbFieldClass;
  134. }
  135. // Nope, not a datablock. So maybe it has a valid inspector field override we can use?
  136. if(!cbt->getInspectorFieldType())
  137. // Nothing, so bail.
  138. return NULL;
  139. // Otherwise try to make it!
  140. ConsoleObject *co = create(cbt->getInspectorFieldType());
  141. GuiInspectorField *gif = dynamic_cast<GuiInspectorField*>(co);
  142. if(!gif)
  143. {
  144. // Wasn't appropriate type, bail.
  145. delete co;
  146. return NULL;
  147. }
  148. return gif;
  149. }
  150. //-----------------------------------------------------------------------------
  151. GuiInspectorField *GuiInspectorGroup::findField( const char *fieldName )
  152. {
  153. // If we don't have any field children we can't very well find one then can we?
  154. if( mChildren.empty() )
  155. return NULL;
  156. Vector<GuiInspectorField*>::iterator i = mChildren.begin();
  157. for( ; i != mChildren.end(); i++ )
  158. {
  159. if( (*i)->getFieldName() != NULL && dStricmp( (*i)->getFieldName(), fieldName ) == 0 )
  160. return (*i);
  161. }
  162. return NULL;
  163. }
  164. //-----------------------------------------------------------------------------
  165. void GuiInspectorGroup::clearFields()
  166. {
  167. // Deallocates all field related controls.
  168. mStack->clear();
  169. // Then just cleanup our vectors which also point to children
  170. // that we keep for our own convenience.
  171. mArrayCtrls.clear();
  172. mChildren.clear();
  173. }
  174. //-----------------------------------------------------------------------------
  175. bool GuiInspectorGroup::inspectGroup()
  176. {
  177. // We can't inspect a group without a target!
  178. if( !mParent || !mParent->getNumInspectObjects() )
  179. return false;
  180. // to prevent crazy resizing, we'll just freeze our stack for a sec..
  181. mStack->freeze(true);
  182. bool bNoGroup = false;
  183. // Un-grouped fields are all sorted into the 'general' group
  184. if ( dStricmp( mCaption, "General" ) == 0 )
  185. bNoGroup = true;
  186. // Just delete all fields and recreate them (like the dynamicGroup)
  187. // because that makes creating controls for array fields a lot easier
  188. clearFields();
  189. bool bNewItems = false;
  190. bool bMakingArray = false;
  191. GuiStackControl *pArrayStack = NULL;
  192. GuiRolloutCtrl *pArrayRollout = NULL;
  193. bool bGrabItems = false;
  194. AbstractClassRep* commonAncestorClass = findCommonAncestorClass();
  195. AbstractClassRep::FieldList& fieldList = commonAncestorClass->mFieldList;
  196. for( AbstractClassRep::FieldList::iterator itr = fieldList.begin();
  197. itr != fieldList.end(); ++ itr )
  198. {
  199. AbstractClassRep::Field* field = &( *itr );
  200. if( field->type == AbstractClassRep::StartGroupFieldType )
  201. {
  202. // If we're dealing with general fields, always set grabItems to true (to skip them)
  203. if( bNoGroup == true )
  204. bGrabItems = true;
  205. else if( dStricmp( field->pGroupname, mCaption ) == 0 )
  206. bGrabItems = true;
  207. continue;
  208. }
  209. else if ( field->type == AbstractClassRep::EndGroupFieldType )
  210. {
  211. // If we're dealing with general fields, always set grabItems to false (to grab them)
  212. if( bNoGroup == true )
  213. bGrabItems = false;
  214. else if( dStricmp( field->pGroupname, mCaption ) == 0 )
  215. bGrabItems = false;
  216. continue;
  217. }
  218. // Skip field if it has the HideInInspectors flag set.
  219. if( field->flag.test( AbstractClassRep::FIELD_HideInInspectors ) )
  220. continue;
  221. if( ( bGrabItems == true || ( bNoGroup == true && bGrabItems == false ) ) && itr->type != AbstractClassRep::DeprecatedFieldType )
  222. {
  223. if( bNoGroup == true && bGrabItems == true )
  224. continue;
  225. if ( field->type == AbstractClassRep::StartArrayFieldType )
  226. {
  227. #ifdef DEBUG_SPEW
  228. Platform::outputDebugString( "[GuiInspectorGroup] Beginning array '%s'",
  229. field->pFieldname );
  230. #endif
  231. // Starting an array...
  232. // Create a rollout for the Array, give it the array's name.
  233. GuiRolloutCtrl *arrayRollout = new GuiRolloutCtrl();
  234. GuiControlProfile *arrayRolloutProfile = dynamic_cast<GuiControlProfile*>( Sim::findObject( "GuiInspectorRolloutProfile0" ) );
  235. arrayRollout->setControlProfile(arrayRolloutProfile);
  236. //arrayRollout->mCaption = StringTable->insert( String::ToString( "%s (%i)", field->pGroupname, field->elementCount ) );
  237. arrayRollout->setCaption( field->pGroupname );
  238. //arrayRollout->setMargin( 14, 0, 0, 0 );
  239. arrayRollout->registerObject();
  240. GuiStackControl *arrayStack = new GuiStackControl();
  241. arrayStack->registerObject();
  242. arrayStack->freeze(true);
  243. arrayRollout->addObject(arrayStack);
  244. // Allocate a rollout for each element-count in the array
  245. // Give it the element count name.
  246. for ( U32 i = 0; i < field->elementCount; i++ )
  247. {
  248. GuiRolloutCtrl *elementRollout = new GuiRolloutCtrl();
  249. GuiControlProfile *elementRolloutProfile = dynamic_cast<GuiControlProfile*>( Sim::findObject( "GuiInspectorRolloutProfile0" ) );
  250. char buf[256];
  251. dSprintf( buf, 256, " [%i]", i );
  252. elementRollout->setControlProfile(elementRolloutProfile);
  253. elementRollout->setCaption(buf);
  254. //elementRollout->setMargin( 14, 0, 0, 0 );
  255. elementRollout->registerObject();
  256. GuiStackControl *elementStack = new GuiStackControl();
  257. elementStack->registerObject();
  258. elementRollout->addObject(elementStack);
  259. elementRollout->instantCollapse();
  260. arrayStack->addObject( elementRollout );
  261. }
  262. pArrayRollout = arrayRollout;
  263. pArrayStack = arrayStack;
  264. arrayStack->freeze(false);
  265. pArrayRollout->instantCollapse();
  266. mStack->addObject(arrayRollout);
  267. bMakingArray = true;
  268. continue;
  269. }
  270. else if ( field->type == AbstractClassRep::EndArrayFieldType )
  271. {
  272. #ifdef DEBUG_SPEW
  273. Platform::outputDebugString( "[GuiInspectorGroup] Ending array '%s'",
  274. field->pFieldname );
  275. #endif
  276. bMakingArray = false;
  277. continue;
  278. }
  279. if ( bMakingArray )
  280. {
  281. // Add a GuiInspectorField for this field,
  282. // for every element in the array...
  283. for ( U32 i = 0; i < pArrayStack->size(); i++ )
  284. {
  285. FrameTemp<char> intToStr( 64 );
  286. dSprintf( intToStr, 64, "%d", i );
  287. // The array stack should have a rollout for each element
  288. // as children...
  289. GuiRolloutCtrl *pRollout = dynamic_cast<GuiRolloutCtrl*>(pArrayStack->at(i));
  290. // And the each of those rollouts should have a stack for
  291. // fields...
  292. GuiStackControl *pStack = dynamic_cast<GuiStackControl*>(pRollout->at(0));
  293. // And we add a new GuiInspectorField to each of those stacks...
  294. GuiInspectorField *fieldGui = constructField( field->type );
  295. if ( fieldGui == NULL )
  296. fieldGui = new GuiInspectorField();
  297. fieldGui->init( mParent, this );
  298. StringTableEntry caption = field->pFieldname;
  299. fieldGui->setInspectorField( field, caption, intToStr );
  300. if( fieldGui->registerObject() )
  301. {
  302. #ifdef DEBUG_SPEW
  303. Platform::outputDebugString( "[GuiInspectorGroup] Adding array element '%s[%i]'",
  304. field->pFieldname, i );
  305. #endif
  306. mChildren.push_back( fieldGui );
  307. pStack->addObject( fieldGui );
  308. }
  309. else
  310. delete fieldGui;
  311. }
  312. continue;
  313. }
  314. // This is weird, but it should work for now. - JDD
  315. // We are going to check to see if this item is an array
  316. // if so, we're going to construct a field for each array element
  317. if( field->elementCount > 1 )
  318. {
  319. // Make a rollout control for this array
  320. //
  321. GuiRolloutCtrl *rollout = new GuiRolloutCtrl();
  322. rollout->setDataField( StringTable->insert("profile"), NULL, "GuiInspectorRolloutProfile0" );
  323. rollout->setCaption(String::ToString( "%s (%i)", field->pFieldname, field->elementCount));
  324. rollout->setMargin( 14, 0, 0, 0 );
  325. rollout->registerObject();
  326. mArrayCtrls.push_back(rollout);
  327. // Put a stack control within the rollout
  328. //
  329. GuiStackControl *stack = new GuiStackControl();
  330. stack->setDataField( StringTable->insert("profile"), NULL, "GuiInspectorStackProfile" );
  331. stack->registerObject();
  332. stack->freeze(true);
  333. rollout->addObject(stack);
  334. mStack->addObject(rollout);
  335. // Create each field and add it to the stack.
  336. //
  337. for (S32 nI = 0; nI < field->elementCount; nI++)
  338. {
  339. FrameTemp<char> intToStr( 64 );
  340. dSprintf( intToStr, 64, "%d", nI );
  341. // Construct proper ValueName[nI] format which is "ValueName0" for index 0, etc.
  342. String fieldName = String::ToString( "%s%d", field->pFieldname, nI );
  343. // If the field already exists, just update it
  344. GuiInspectorField *fieldGui = findField( fieldName );
  345. if( fieldGui != NULL )
  346. {
  347. fieldGui->updateValue();
  348. continue;
  349. }
  350. bNewItems = true;
  351. fieldGui = constructField( field->type );
  352. if ( fieldGui == NULL )
  353. fieldGui = new GuiInspectorField();
  354. fieldGui->init( mParent, this );
  355. StringTableEntry caption = StringTable->insert( String::ToString(" [%i]",nI) );
  356. fieldGui->setInspectorField( field, caption, intToStr );
  357. if ( fieldGui->registerObject() )
  358. {
  359. mChildren.push_back( fieldGui );
  360. stack->addObject( fieldGui );
  361. }
  362. else
  363. delete fieldGui;
  364. }
  365. stack->freeze(false);
  366. stack->updatePanes();
  367. rollout->instantCollapse();
  368. }
  369. else
  370. {
  371. // If the field already exists, just update it
  372. GuiInspectorField *fieldGui = findField( field->pFieldname );
  373. if ( fieldGui != NULL )
  374. {
  375. fieldGui->updateValue();
  376. continue;
  377. }
  378. bNewItems = true;
  379. fieldGui = constructField( field->type );
  380. if ( fieldGui == NULL )
  381. fieldGui = new GuiInspectorField();
  382. fieldGui->init( mParent, this );
  383. fieldGui->setInspectorField( field );
  384. if( fieldGui->registerObject() )
  385. {
  386. #ifdef DEBUG_SPEW
  387. Platform::outputDebugString( "[GuiInspectorGroup] Adding field '%s'",
  388. field->pFieldname );
  389. #endif
  390. mChildren.push_back( fieldGui );
  391. mStack->addObject( fieldGui );
  392. }
  393. else
  394. {
  395. SAFE_DELETE( fieldGui );
  396. }
  397. }
  398. }
  399. }
  400. mStack->freeze(false);
  401. mStack->updatePanes();
  402. // If we've no new items, there's no need to resize anything!
  403. if( bNewItems == false && !mChildren.empty() )
  404. return true;
  405. sizeToContents();
  406. setUpdate();
  407. return true;
  408. }
  409. //-----------------------------------------------------------------------------
  410. bool GuiInspectorGroup::updateFieldValue( StringTableEntry fieldName, StringTableEntry arrayIdx )
  411. {
  412. // Check if we contain a field of this name,
  413. // if so update its value and return true.
  414. Vector<GuiInspectorField*>::iterator iter = mChildren.begin();
  415. if( arrayIdx == StringTable->EmptyString() )
  416. arrayIdx = NULL;
  417. for( ; iter != mChildren.end(); iter++ )
  418. {
  419. GuiInspectorField *field = (*iter);
  420. if ( field->mField &&
  421. field->mField->pFieldname == fieldName &&
  422. field->mFieldArrayIndex == arrayIdx )
  423. {
  424. field->updateValue();
  425. return true;
  426. }
  427. }
  428. return false;
  429. }
  430. //-----------------------------------------------------------------------------
  431. void GuiInspectorGroup::updateAllFields()
  432. {
  433. Vector<GuiInspectorField*>::iterator iter = mChildren.begin();
  434. for( ; iter != mChildren.end(); iter++ )
  435. (*iter)->updateValue();
  436. }
  437. //-----------------------------------------------------------------------------
  438. AbstractClassRep* GuiInspectorGroup::findCommonAncestorClass()
  439. {
  440. AbstractClassRep* classRep = getInspector()->getInspectObject( 0 )->getClassRep();
  441. const U32 numInspectObjects = getInspector()->getNumInspectObjects();
  442. for( U32 i = 1; i < numInspectObjects; ++ i )
  443. {
  444. SimObject* object = getInspector()->getInspectObject( i );
  445. while( !object->getClassRep()->isClass( classRep ) )
  446. {
  447. classRep = classRep->getParentClass();
  448. AssertFatal( classRep, "GuiInspectorGroup::findcommonAncestorClass - Walked above ConsoleObject!" );
  449. }
  450. }
  451. return classRep;
  452. }