componentGroup.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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/buttons/guiIconButtonCtrl.h"
  23. #include "gui/editor/guiInspector.h"
  24. #include "gui/editor/inspector/componentGroup.h"
  25. #include "core/strings/stringUnit.h"
  26. #include "T3D/components/component.h"
  27. #include "gui/editor/inspector/field.h"
  28. #include "console/engineAPI.h"
  29. IMPLEMENT_CONOBJECT(GuiInspectorComponentGroup);
  30. ConsoleDocClass(GuiInspectorComponentGroup,
  31. "@brief Used to inspect an object's FieldDictionary (dynamic fields) instead "
  32. "of regular persistent fields.\n\n"
  33. "Editor use only.\n\n"
  34. "@internal"
  35. );
  36. GuiInspectorComponentGroup::GuiInspectorComponentGroup(StringTableEntry groupName, SimObjectPtr<GuiInspector> parent, Component* targetComponent)
  37. : GuiInspectorGroup(groupName, parent)
  38. {
  39. /*mNeedScroll=false;*/
  40. mTargetComponent = targetComponent;
  41. };
  42. bool GuiInspectorComponentGroup::onAdd()
  43. {
  44. if (!Parent::onAdd())
  45. return false;
  46. return true;
  47. }
  48. //-----------------------------------------------------------------------------
  49. // GuiInspectorComponentGroup - add custom controls
  50. //-----------------------------------------------------------------------------
  51. bool GuiInspectorComponentGroup::createContent()
  52. {
  53. if(!Parent::createContent())
  54. return false;
  55. Con::evaluatef("%d.stack = %d;", this->getId(), mStack->getId());
  56. Con::executef(this, "createContent");
  57. return true;
  58. }
  59. //-----------------------------------------------------------------------------
  60. // GuiInspectorComponentGroup - inspectGroup override
  61. //-----------------------------------------------------------------------------
  62. bool GuiInspectorComponentGroup::inspectGroup()
  63. {
  64. // We can't inspect a group without a target!
  65. if (!mParent || !mParent->getNumInspectObjects())
  66. return false;
  67. clearFields();
  68. // to prevent crazy resizing, we'll just freeze our stack for a sec..
  69. mStack->freeze(true);
  70. bool bNoGroup = false;
  71. bool bNewItems = false;
  72. bool bMakingArray = false;
  73. GuiStackControl *pArrayStack = NULL;
  74. GuiRolloutCtrl *pArrayRollout = NULL;
  75. bool bGrabItems = false;
  76. //if this isn't a component, what are we even doing here?
  77. if (!mTargetComponent)
  78. return false;
  79. mParent->setComponentGroupTargetId(mTargetComponent->getId());
  80. //first, relevent static fields
  81. AbstractClassRep::FieldList& fieldList = mTargetComponent->getClassRep()->mFieldList;
  82. for (AbstractClassRep::FieldList::iterator itr = fieldList.begin();
  83. itr != fieldList.end(); ++itr)
  84. {
  85. AbstractClassRep::Field* field = &(*itr);
  86. if (field->type == AbstractClassRep::StartGroupFieldType)
  87. {
  88. // If we're dealing with general fields, always set grabItems to true (to skip them)
  89. if (bNoGroup == true)
  90. bGrabItems = true;
  91. else if (dStricmp(field->pGroupname, mCaption) == 0)
  92. bGrabItems = true;
  93. continue;
  94. }
  95. else if (field->type == AbstractClassRep::EndGroupFieldType)
  96. {
  97. // If we're dealing with general fields, always set grabItems to false (to grab them)
  98. if (bNoGroup == true)
  99. bGrabItems = false;
  100. else if (dStricmp(field->pGroupname, mCaption) == 0)
  101. bGrabItems = false;
  102. continue;
  103. }
  104. // Skip field if it has the HideInInspectors flag set.
  105. if (field->flag.test(AbstractClassRep::FIELD_HideInInspectors))
  106. continue;
  107. if (field->pFieldname == StringTable->insert("locked") || field->pFieldname == StringTable->insert("class")
  108. || field->pFieldname == StringTable->insert("internalName"))
  109. continue;
  110. if (/*(bGrabItems == true || (bNoGroup == true && bGrabItems == false)) &&*/ itr->type != AbstractClassRep::DeprecatedFieldType)
  111. {
  112. if (bNoGroup == true && bGrabItems == true)
  113. continue;
  114. if (field->type == AbstractClassRep::StartArrayFieldType)
  115. {
  116. #ifdef DEBUG_SPEW
  117. Platform::outputDebugString("[GuiInspectorGroup] Beginning array '%s'",
  118. field->pFieldname);
  119. #endif
  120. // Starting an array...
  121. // Create a rollout for the Array, give it the array's name.
  122. GuiRolloutCtrl *arrayRollout = new GuiRolloutCtrl();
  123. GuiControlProfile *arrayRolloutProfile = dynamic_cast<GuiControlProfile*>(Sim::findObject("GuiInspectorRolloutProfile0"));
  124. arrayRollout->setControlProfile(arrayRolloutProfile);
  125. //arrayRollout->mCaption = StringTable->insert( String::ToString( "%s (%i)", field->pGroupname, field->elementCount ) );
  126. arrayRollout->setCaption(field->pGroupname);
  127. //arrayRollout->setMargin( 14, 0, 0, 0 );
  128. arrayRollout->registerObject();
  129. GuiStackControl *arrayStack = new GuiStackControl();
  130. arrayStack->registerObject();
  131. arrayStack->freeze(true);
  132. arrayRollout->addObject(arrayStack);
  133. // Allocate a rollout for each element-count in the array
  134. // Give it the element count name.
  135. for (U32 i = 0; i < field->elementCount; i++)
  136. {
  137. GuiRolloutCtrl *elementRollout = new GuiRolloutCtrl();
  138. GuiControlProfile *elementRolloutProfile = dynamic_cast<GuiControlProfile*>(Sim::findObject("GuiInspectorRolloutProfile0"));
  139. char buf[256];
  140. dSprintf(buf, 256, " [%i]", i);
  141. elementRollout->setControlProfile(elementRolloutProfile);
  142. elementRollout->setCaption(buf);
  143. //elementRollout->setMargin( 14, 0, 0, 0 );
  144. elementRollout->registerObject();
  145. GuiStackControl *elementStack = new GuiStackControl();
  146. elementStack->registerObject();
  147. elementRollout->addObject(elementStack);
  148. elementRollout->instantCollapse();
  149. arrayStack->addObject(elementRollout);
  150. }
  151. pArrayRollout = arrayRollout;
  152. pArrayStack = arrayStack;
  153. arrayStack->freeze(false);
  154. pArrayRollout->instantCollapse();
  155. mStack->addObject(arrayRollout);
  156. bMakingArray = true;
  157. continue;
  158. }
  159. else if (field->type == AbstractClassRep::EndArrayFieldType)
  160. {
  161. #ifdef DEBUG_SPEW
  162. Platform::outputDebugString("[GuiInspectorGroup] Ending array '%s'",
  163. field->pFieldname);
  164. #endif
  165. bMakingArray = false;
  166. continue;
  167. }
  168. if (bMakingArray)
  169. {
  170. // Add a GuiInspectorField for this field,
  171. // for every element in the array...
  172. for (U32 i = 0; i < pArrayStack->size(); i++)
  173. {
  174. FrameTemp<char> intToStr(64);
  175. dSprintf(intToStr, 64, "%d", i);
  176. // The array stack should have a rollout for each element
  177. // as children...
  178. GuiRolloutCtrl *pRollout = dynamic_cast<GuiRolloutCtrl*>(pArrayStack->at(i));
  179. // And the each of those rollouts should have a stack for
  180. // fields...
  181. GuiStackControl *pStack = dynamic_cast<GuiStackControl*>(pRollout->at(0));
  182. // And we add a new GuiInspectorField to each of those stacks...
  183. GuiInspectorField *fieldGui = constructField(field->type);
  184. if (fieldGui == NULL)
  185. fieldGui = new GuiInspectorField();
  186. fieldGui->init(mParent, this);
  187. StringTableEntry caption = field->pFieldname;
  188. fieldGui->setInspectorField(field, caption, intToStr);
  189. if (fieldGui->registerObject())
  190. {
  191. #ifdef DEBUG_SPEW
  192. Platform::outputDebugString("[GuiInspectorGroup] Adding array element '%s[%i]'",
  193. field->pFieldname, i);
  194. #endif
  195. mChildren.push_back(fieldGui);
  196. pStack->addObject(fieldGui);
  197. }
  198. else
  199. delete fieldGui;
  200. }
  201. continue;
  202. }
  203. // This is weird, but it should work for now. - JDD
  204. // We are going to check to see if this item is an array
  205. // if so, we're going to construct a field for each array element
  206. if (field->elementCount > 1)
  207. {
  208. // Make a rollout control for this array
  209. //
  210. GuiRolloutCtrl *rollout = new GuiRolloutCtrl();
  211. rollout->setDataField(StringTable->insert("profile"), NULL, "GuiInspectorRolloutProfile0");
  212. rollout->setCaption(String::ToString("%s (%i)", field->pFieldname, field->elementCount));
  213. rollout->setMargin(14, 0, 0, 0);
  214. rollout->registerObject();
  215. mArrayCtrls.push_back(rollout);
  216. // Put a stack control within the rollout
  217. //
  218. GuiStackControl *stack = new GuiStackControl();
  219. stack->setDataField(StringTable->insert("profile"), NULL, "GuiInspectorStackProfile");
  220. stack->registerObject();
  221. stack->freeze(true);
  222. rollout->addObject(stack);
  223. mStack->addObject(rollout);
  224. // Create each field and add it to the stack.
  225. //
  226. for (S32 nI = 0; nI < field->elementCount; nI++)
  227. {
  228. FrameTemp<char> intToStr(64);
  229. dSprintf(intToStr, 64, "%d", nI);
  230. // Construct proper ValueName[nI] format which is "ValueName0" for index 0, etc.
  231. String fieldName = String::ToString("%s%d", field->pFieldname, nI);
  232. // If the field already exists, just update it
  233. GuiInspectorField *fieldGui = findField(fieldName);
  234. if (fieldGui != NULL)
  235. {
  236. fieldGui->updateValue();
  237. continue;
  238. }
  239. bNewItems = true;
  240. fieldGui = constructField(field->type);
  241. if (fieldGui == NULL)
  242. fieldGui = new GuiInspectorField();
  243. fieldGui->init(mParent, this);
  244. StringTableEntry caption = StringTable->insert(String::ToString(" [%i]", nI));
  245. fieldGui->setInspectorField(field, caption, intToStr);
  246. fieldGui->setTargetObject(mTargetComponent);
  247. if (fieldGui->registerObject())
  248. {
  249. mChildren.push_back(fieldGui);
  250. stack->addObject(fieldGui);
  251. }
  252. else
  253. delete fieldGui;
  254. }
  255. stack->freeze(false);
  256. stack->updatePanes();
  257. rollout->instantCollapse();
  258. }
  259. else
  260. {
  261. // If the field already exists, just update it
  262. GuiInspectorField *fieldGui = findField(field->pFieldname);
  263. if (fieldGui != NULL)
  264. {
  265. fieldGui->updateValue();
  266. continue;
  267. }
  268. bNewItems = true;
  269. fieldGui = constructField(field->type);
  270. if (fieldGui == NULL)
  271. fieldGui = new GuiInspectorField();
  272. fieldGui->init(mParent, this);
  273. fieldGui->setInspectorField(field);
  274. fieldGui->setTargetObject(mTargetComponent);
  275. if (fieldGui->registerObject())
  276. {
  277. #ifdef DEBUG_SPEW
  278. Platform::outputDebugString("[GuiInspectorGroup] Adding field '%s'",
  279. field->pFieldname);
  280. #endif
  281. fieldGui->setValue(mTargetComponent->getDataField(field->pFieldname, NULL));
  282. mChildren.push_back(fieldGui);
  283. mStack->addObject(fieldGui);
  284. }
  285. else
  286. {
  287. SAFE_DELETE(fieldGui);
  288. }
  289. }
  290. }
  291. }
  292. for (U32 i = 0; i < mTargetComponent->getComponentFieldCount(); i++)
  293. {
  294. ComponentField* field = mTargetComponent->getComponentField(i);
  295. //first and foremost, nab the field type and check if it's a custom field or not.
  296. //If it's not a custom field, proceed below, if it is, hand it off to script to be handled by the component
  297. if (field->mFieldType == -1)
  298. {
  299. Con::executef(this, "onConstructComponentField", mTargetComponent, field->mFieldName);
  300. continue;
  301. }
  302. bNewItems = true;
  303. GuiInspectorField *fieldGui = constructField(field->mFieldType);
  304. if (fieldGui == NULL)
  305. fieldGui = new GuiInspectorField();
  306. fieldGui->init(mParent, this);
  307. fieldGui->setTargetObject(mTargetComponent);
  308. AbstractClassRep::Field *refField = NULL;
  309. //check dynamics
  310. SimFieldDictionary* fieldDictionary = mTargetComponent->getFieldDictionary();
  311. SimFieldDictionaryIterator itr(fieldDictionary);
  312. while (*itr)
  313. {
  314. SimFieldDictionary::Entry* entry = *itr;
  315. if (entry->slotName == field->mFieldName)
  316. {
  317. AbstractClassRep::Field f;
  318. f.pFieldname = StringTable->insert(field->mFieldName);
  319. if (field->mFieldDescription)
  320. f.pFieldDocs = field->mFieldDescription;
  321. f.type = field->mFieldType;
  322. f.offset = -1;
  323. f.elementCount = 1;
  324. f.validator = NULL;
  325. f.flag = 0; //change to be the component type
  326. f.setDataFn = &defaultProtectedSetFn;
  327. f.getDataFn = &defaultProtectedGetFn;
  328. f.writeDataFn = &defaultProtectedWriteFn;
  329. f.pFieldDocs = field->mFieldDescription;
  330. f.pGroupname = "Component Fields";
  331. ConsoleBaseType* conType = ConsoleBaseType::getType(field->mFieldType);
  332. AssertFatal(conType, "ConsoleObject::addField - invalid console type");
  333. f.table = conType->getEnumTable();
  334. tempFields.push_back(f);
  335. refField = &f;
  336. break;
  337. }
  338. ++itr;
  339. }
  340. if (!refField)
  341. continue;
  342. fieldGui->setInspectorField(&tempFields[tempFields.size() - 1]);
  343. if (fieldGui->registerObject())
  344. {
  345. #ifdef DEBUG_SPEW
  346. Platform::outputDebugString("[GuiInspectorGroup] Adding field '%s'",
  347. field->pFieldname);
  348. #endif
  349. mChildren.push_back(fieldGui);
  350. mStack->addObject(fieldGui);
  351. }
  352. else
  353. {
  354. SAFE_DELETE(fieldGui);
  355. }
  356. }
  357. mStack->freeze(false);
  358. mStack->updatePanes();
  359. // If we've no new items, there's no need to resize anything!
  360. if (bNewItems == false && !mChildren.empty())
  361. return true;
  362. sizeToContents();
  363. setUpdate();
  364. return true;
  365. }
  366. void GuiInspectorComponentGroup::updateAllFields()
  367. {
  368. // We overload this to just reinspect the group.
  369. inspectGroup();
  370. }
  371. void GuiInspectorComponentGroup::onMouseMove(const GuiEvent &event)
  372. {
  373. //mParent->mOverDivider = false;
  374. }
  375. void GuiInspectorComponentGroup::onRightMouseUp(const GuiEvent &event)
  376. {
  377. //mParent->mOverDivider = false;
  378. if (isMethod("onRightMouseUp"))
  379. Con::executef(this, "onRightMouseUp", event.mousePoint);
  380. }
  381. ConsoleMethod(GuiInspectorComponentGroup, inspectGroup, bool, 2, 2, "Refreshes the dynamic fields in the inspector.")
  382. {
  383. return object->inspectGroup();
  384. }
  385. void GuiInspectorComponentGroup::clearFields()
  386. {
  387. // delete everything else
  388. mStack->clear();
  389. // clear the mChildren list.
  390. mChildren.clear();
  391. }
  392. SimFieldDictionary::Entry* GuiInspectorComponentGroup::findDynamicFieldInDictionary(StringTableEntry fieldName)
  393. {
  394. SimFieldDictionary * fieldDictionary = mParent->getInspectObject()->getFieldDictionary();
  395. for (SimFieldDictionaryIterator ditr(fieldDictionary); *ditr; ++ditr)
  396. {
  397. SimFieldDictionary::Entry * entry = (*ditr);
  398. if (entry->slotName == fieldName)
  399. return entry;
  400. }
  401. return NULL;
  402. }
  403. void GuiInspectorComponentGroup::addDynamicField()
  404. {
  405. }
  406. AbstractClassRep::Field* GuiInspectorComponentGroup::findObjectBehaviorField(Component* target, String fieldName)
  407. {
  408. AbstractClassRep::FieldList& fieldList = target->getClassRep()->mFieldList;
  409. for (AbstractClassRep::FieldList::iterator itr = fieldList.begin();
  410. itr != fieldList.end(); ++itr)
  411. {
  412. AbstractClassRep::Field* field = &(*itr);
  413. String fldNm(field->pFieldname);
  414. if (fldNm == fieldName)
  415. return field;
  416. }
  417. return NULL;
  418. }
  419. ConsoleMethod(GuiInspectorComponentGroup, addDynamicField, void, 2, 2, "obj.addDynamicField();")
  420. {
  421. object->addDynamicField();
  422. }
  423. ConsoleMethod(GuiInspectorComponentGroup, removeDynamicField, void, 3, 3, "")
  424. {
  425. }
  426. DefineEngineMethod(GuiInspectorComponentGroup, getComponent, S32, (), ,"")
  427. {
  428. return object->getComponent()->getId();
  429. }