variableGroup.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 "platform/platform.h"
  23. #include "gui/editor/inspector/variableGroup.h"
  24. #include "gui/editor/inspector/variableField.h"
  25. #include "gui/editor/guiInspector.h"
  26. #include "gui/buttons/guiIconButtonCtrl.h"
  27. #include "console/consoleInternal.h"
  28. extern ExprEvalState gEvalState;
  29. //-----------------------------------------------------------------------------
  30. // GuiInspectorVariableGroup
  31. //-----------------------------------------------------------------------------
  32. //
  33. //
  34. IMPLEMENT_CONOBJECT(GuiInspectorVariableGroup);
  35. ConsoleDocClass( GuiInspectorVariableGroup,
  36. "@brief Inspector support for variable groups in a GuiVariableInspector.\n\n"
  37. "Editor use only.\n\n"
  38. "@internal"
  39. );
  40. GuiInspectorVariableGroup::GuiInspectorVariableGroup()
  41. {
  42. }
  43. GuiInspectorVariableGroup::~GuiInspectorVariableGroup()
  44. {
  45. }
  46. GuiInspectorField* GuiInspectorVariableGroup::constructField( S32 fieldType )
  47. {
  48. return Parent::constructField(fieldType);
  49. }
  50. bool GuiInspectorVariableGroup::inspectGroup()
  51. {
  52. // to prevent crazy resizing, we'll just freeze our stack for a sec..
  53. mStack->freeze(true);
  54. bool bNewItems = false;
  55. if (!mSearchString.equal(""))
  56. {
  57. Vector<String> names;
  58. gEvalState.globalVars.exportVariables(mSearchString, &names, NULL);
  59. for (U32 i = 0; i < names.size(); i++)
  60. {
  61. const String &varName = names[i];
  62. // If the field already exists, just update it
  63. GuiInspectorVariableField *field = dynamic_cast<GuiInspectorVariableField*>(findField(varName));
  64. if (field != NULL)
  65. {
  66. field->updateValue();
  67. continue;
  68. }
  69. bNewItems = true;
  70. field = new GuiInspectorVariableField();
  71. field->init(mParent, this);
  72. field->setInspectorField(NULL, StringTable->insert(varName));
  73. if (field->registerObject())
  74. {
  75. mChildren.push_back(field);
  76. mStack->addObject(field);
  77. }
  78. else
  79. delete field;
  80. }
  81. }
  82. for (U32 i = 0; i < mFields.size(); i++)
  83. {
  84. bNewItems = true;
  85. GuiInspectorField *fieldGui = findField(mFields[i]->mFieldName);
  86. if (fieldGui != NULL)
  87. {
  88. fieldGui->updateValue();
  89. continue;
  90. }
  91. //first and foremost, nab the field type and check if it's a custom field or not.
  92. //If it's not a custom field, proceed below, if it is, hand it off to script to be handled by the component
  93. if (mFields[i]->mFieldType == -1)
  94. {
  95. if (isMethod("onConstructField"))
  96. {
  97. //ensure our stack variable is bound if we need it
  98. Con::evaluatef("%d.stack = %d;", this->getId(), mStack->getId());
  99. Con::executef(this, "onConstructField", mFields[i]->mFieldName,
  100. mFields[i]->mFieldLabel, mFields[i]->mFieldTypeName, mFields[i]->mFieldDescription,
  101. mFields[i]->mDefaultValue, mFields[i]->mDataValues, mFields[i]->mOwnerObject);
  102. }
  103. continue;
  104. }
  105. bNewItems = true;
  106. fieldGui = constructField(mFields[i]->mFieldType);
  107. if (fieldGui == NULL)
  108. fieldGui = new GuiInspectorField();
  109. fieldGui->init(mParent, this);
  110. fieldGui->setSpecialEditField(true);
  111. if (mFields[i]->mOwnerObject)
  112. {
  113. fieldGui->setTargetObject(mFields[i]->mOwnerObject);
  114. }
  115. else
  116. {
  117. //check if we're binding to a global var first, if we have no owner
  118. if (mFields[i]->mFieldName[0] != '$')
  119. {
  120. fieldGui->setTargetObject(mParent);
  121. }
  122. }
  123. fieldGui->setSpecialEditVariableName(mFields[i]->mFieldName);
  124. fieldGui->setSpecialEditCallbackName(mFields[i]->mSetCallbackName);
  125. fieldGui->setInspectorField(NULL, mFields[i]->mFieldLabel);
  126. fieldGui->setDocs(mFields[i]->mFieldDescription);
  127. /*if (mFields[i]->mSetCallbackName != StringTable->EmptyString())
  128. {
  129. fieldGui->on.notify()
  130. }*/
  131. if (fieldGui->registerObject())
  132. {
  133. #ifdef DEBUG_SPEW
  134. Platform::outputDebugString("[GuiInspectorVariableGroup] Adding field '%s'",
  135. field->pFieldname);
  136. #endif
  137. if (mFields[i]->mOwnerObject)
  138. {
  139. String val = mFields[i]->mOwnerObject->getDataField(mFields[i]->mFieldName, NULL);
  140. if(val.isEmpty())
  141. fieldGui->setValue(mFields[i]->mDefaultValue);
  142. else
  143. fieldGui->setValue(val);
  144. }
  145. else
  146. {
  147. fieldGui->setValue(mFields[i]->mDefaultValue);
  148. }
  149. fieldGui->setActive(mFields[i]->mEnabled);
  150. mChildren.push_back(fieldGui);
  151. mStack->addObject(fieldGui);
  152. }
  153. else
  154. {
  155. SAFE_DELETE(fieldGui);
  156. }
  157. }
  158. mStack->freeze(false);
  159. mStack->updatePanes();
  160. // If we've no new items, there's no need to resize anything!
  161. if( bNewItems == false && !mChildren.empty() )
  162. return true;
  163. sizeToContents();
  164. setUpdate();
  165. return true;
  166. }
  167. void GuiInspectorVariableGroup::clearFields()
  168. {
  169. mFields.clear();
  170. }
  171. void GuiInspectorVariableGroup::addField(VariableField* field)
  172. {
  173. bool found = false;
  174. for (U32 i = 0; i < mFields.size(); i++)
  175. {
  176. if (mFields[i]->mFieldName == field->mFieldName)
  177. {
  178. found = true;
  179. break;
  180. }
  181. }
  182. if(!found)
  183. mFields.push_back(field);
  184. }
  185. void GuiInspectorVariableGroup::addInspectorField(GuiInspectorField* field)
  186. {
  187. mStack->addObject(field);
  188. mChildren.push_back(field);
  189. mStack->updatePanes();
  190. }
  191. GuiInspectorField* GuiInspectorVariableGroup::createInspectorField()
  192. {
  193. GuiInspectorField* newField = new GuiInspectorField();
  194. newField->init(mParent, this);
  195. newField->setSpecialEditField(true);
  196. if (newField->registerObject())
  197. {
  198. return newField;
  199. }
  200. return NULL;
  201. }
  202. DefineConsoleMethod(GuiInspectorVariableGroup, createInspectorField, GuiInspectorField*, (),, "createInspectorField()")
  203. {
  204. return object->createInspectorField();
  205. }
  206. DefineConsoleMethod(GuiInspectorVariableGroup, addInspectorField, void, (GuiInspectorField* field), (nullAsType<GuiInspectorField*>()), "addInspectorField( GuiInspectorFieldObject )")
  207. {
  208. object->addInspectorField(field);
  209. }