group.cpp 19 KB

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