componentGroup.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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)
  37. : GuiInspectorGroup(groupName, parent)
  38. {
  39. /*mNeedScroll=false;*/
  40. };
  41. bool GuiInspectorComponentGroup::onAdd()
  42. {
  43. if (!Parent::onAdd())
  44. return false;
  45. return true;
  46. }
  47. //-----------------------------------------------------------------------------
  48. // GuiInspectorComponentGroup - add custom controls
  49. //-----------------------------------------------------------------------------
  50. bool GuiInspectorComponentGroup::createContent()
  51. {
  52. if(!Parent::createContent())
  53. return false;
  54. Con::evaluatef("%d.stack = %d;", this->getId(), mStack->getId());
  55. Con::executef(this, "createContent");
  56. return true;
  57. }
  58. //-----------------------------------------------------------------------------
  59. // GuiInspectorComponentGroup - inspectGroup override
  60. //-----------------------------------------------------------------------------
  61. bool GuiInspectorComponentGroup::inspectGroup()
  62. {
  63. // We can't inspect a group without a target!
  64. if (!mParent || !mParent->getNumInspectObjects())
  65. return false;
  66. clearFields();
  67. // to prevent crazy resizing, we'll just freeze our stack for a sec..
  68. mStack->freeze(true);
  69. bool bNoGroup = false;
  70. bool bNewItems = false;
  71. bool bMakingArray = false;
  72. GuiStackControl *pArrayStack = NULL;
  73. GuiRolloutCtrl *pArrayRollout = NULL;
  74. bool bGrabItems = false;
  75. Component* comp = dynamic_cast<Component*>(getInspector()->getInspectObject(0));
  76. //if this isn't a component, what are we even doing here?
  77. if (!comp)
  78. return false;
  79. for (U32 i = 0; i < comp->getComponentFieldCount(); i++)
  80. {
  81. ComponentField* field = comp->getComponentField(i);
  82. bNewItems = true;
  83. GuiInspectorField *fieldGui = constructField(field->mFieldType);
  84. if (fieldGui == NULL)
  85. fieldGui = new GuiInspectorField();
  86. fieldGui->init(mParent, this);
  87. AbstractClassRep::Field *refField;
  88. //check dynamics
  89. SimFieldDictionary* fieldDictionary = comp->getFieldDictionary();
  90. SimFieldDictionaryIterator itr(fieldDictionary);
  91. while (*itr)
  92. {
  93. SimFieldDictionary::Entry* entry = *itr;
  94. if (entry->slotName == field->mFieldName)
  95. {
  96. AbstractClassRep::Field f;
  97. f.pFieldname = StringTable->insert(field->mFieldName);
  98. if (field->mFieldDescription)
  99. f.pFieldDocs = field->mFieldDescription;
  100. f.type = field->mFieldType;
  101. f.offset = -1;
  102. f.elementCount = 1;
  103. f.validator = NULL;
  104. f.flag = 0; //change to be the component type
  105. f.setDataFn = &defaultProtectedSetFn;
  106. f.getDataFn = &defaultProtectedGetFn;
  107. f.writeDataFn = &defaultProtectedWriteFn;
  108. f.pFieldDocs = field->mFieldDescription;
  109. f.pGroupname = "Component Fields";
  110. ConsoleBaseType* conType = ConsoleBaseType::getType(field->mFieldType);
  111. AssertFatal(conType, "ConsoleObject::addField - invalid console type");
  112. f.table = conType->getEnumTable();
  113. tempFields.push_back(f);
  114. refField = &f;
  115. break;
  116. }
  117. ++itr;
  118. }
  119. if (!refField)
  120. continue;
  121. fieldGui->setInspectorField(&tempFields[tempFields.size() - 1]);
  122. if (fieldGui->registerObject())
  123. {
  124. #ifdef DEBUG_SPEW
  125. Platform::outputDebugString("[GuiInspectorGroup] Adding field '%s'",
  126. field->pFieldname);
  127. #endif
  128. mChildren.push_back(fieldGui);
  129. mStack->addObject(fieldGui);
  130. }
  131. else
  132. {
  133. SAFE_DELETE(fieldGui);
  134. }
  135. }
  136. mStack->freeze(false);
  137. mStack->updatePanes();
  138. // If we've no new items, there's no need to resize anything!
  139. if (bNewItems == false && !mChildren.empty())
  140. return true;
  141. sizeToContents();
  142. setUpdate();
  143. return true;
  144. }
  145. void GuiInspectorComponentGroup::updateAllFields()
  146. {
  147. // We overload this to just reinspect the group.
  148. inspectGroup();
  149. }
  150. void GuiInspectorComponentGroup::onMouseMove(const GuiEvent &event)
  151. {
  152. //mParent->mOverDivider = false;
  153. }
  154. ConsoleMethod(GuiInspectorComponentGroup, inspectGroup, bool, 2, 2, "Refreshes the dynamic fields in the inspector.")
  155. {
  156. return object->inspectGroup();
  157. }
  158. void GuiInspectorComponentGroup::clearFields()
  159. {
  160. // delete everything else
  161. mStack->clear();
  162. // clear the mChildren list.
  163. mChildren.clear();
  164. }
  165. SimFieldDictionary::Entry* GuiInspectorComponentGroup::findDynamicFieldInDictionary(StringTableEntry fieldName)
  166. {
  167. SimFieldDictionary * fieldDictionary = mParent->getInspectObject()->getFieldDictionary();
  168. for (SimFieldDictionaryIterator ditr(fieldDictionary); *ditr; ++ditr)
  169. {
  170. SimFieldDictionary::Entry * entry = (*ditr);
  171. if (entry->slotName == fieldName)
  172. return entry;
  173. }
  174. return NULL;
  175. }
  176. void GuiInspectorComponentGroup::addDynamicField()
  177. {
  178. }
  179. AbstractClassRep::Field* GuiInspectorComponentGroup::findObjectBehaviorField(Component* target, String fieldName)
  180. {
  181. AbstractClassRep::FieldList& fieldList = target->getClassRep()->mFieldList;
  182. for (AbstractClassRep::FieldList::iterator itr = fieldList.begin();
  183. itr != fieldList.end(); ++itr)
  184. {
  185. AbstractClassRep::Field* field = &(*itr);
  186. String fldNm(field->pFieldname);
  187. if (fldNm == fieldName)
  188. return field;
  189. }
  190. return NULL;
  191. }
  192. ConsoleMethod(GuiInspectorComponentGroup, addDynamicField, void, 2, 2, "obj.addDynamicField();")
  193. {
  194. object->addDynamicField();
  195. }
  196. ConsoleMethod(GuiInspectorComponentGroup, removeDynamicField, void, 3, 3, "")
  197. {
  198. }