group.cpp 19 KB

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