variableGroup.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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]->mSetCallbackName, 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. fieldGui->setSpecialEditCallbackName(mFields[i]->mSetCallbackName);
  129. /*if (mFields[i]->mSetCallbackName != StringTable->EmptyString())
  130. {
  131. fieldGui->on.notify()
  132. }*/
  133. if (fieldGui->registerObject())
  134. {
  135. #ifdef DEBUG_SPEW
  136. Platform::outputDebugString("[GuiInspectorVariableGroup] Adding field '%s'",
  137. field->pFieldname);
  138. #endif
  139. if (mFields[i]->mOwnerObject)
  140. {
  141. String val = mFields[i]->mOwnerObject->getDataField(mFields[i]->mFieldName, NULL);
  142. if(val.isEmpty())
  143. fieldGui->setValue(mFields[i]->mDefaultValue);
  144. else
  145. fieldGui->setValue(val);
  146. }
  147. else
  148. {
  149. fieldGui->setValue(mFields[i]->mDefaultValue);
  150. }
  151. fieldGui->setActive(mFields[i]->mEnabled);
  152. mChildren.push_back(fieldGui);
  153. mStack->addObject(fieldGui);
  154. }
  155. else
  156. {
  157. SAFE_DELETE(fieldGui);
  158. }
  159. }
  160. mStack->freeze(false);
  161. mStack->updatePanes();
  162. // If we've no new items, there's no need to resize anything!
  163. if( bNewItems == false && !mChildren.empty() )
  164. return true;
  165. sizeToContents();
  166. setUpdate();
  167. return true;
  168. }
  169. void GuiInspectorVariableGroup::clearFields()
  170. {
  171. mFields.clear();
  172. }
  173. void GuiInspectorVariableGroup::addField(VariableField* field)
  174. {
  175. bool found = false;
  176. for (U32 i = 0; i < mFields.size(); i++)
  177. {
  178. if (mFields[i]->mFieldName == field->mFieldName)
  179. {
  180. found = true;
  181. break;
  182. }
  183. }
  184. if(!found)
  185. mFields.push_back(field);
  186. }
  187. void GuiInspectorVariableGroup::addInspectorField(GuiInspectorField* field)
  188. {
  189. mStack->addObject(field);
  190. mChildren.push_back(field);
  191. mStack->updatePanes();
  192. }
  193. GuiInspectorField* GuiInspectorVariableGroup::createInspectorField()
  194. {
  195. GuiInspectorField* newField = new GuiInspectorField();
  196. newField->init(mParent, this);
  197. newField->setSpecialEditField(true);
  198. if (newField->registerObject())
  199. {
  200. return newField;
  201. }
  202. return NULL;
  203. }
  204. DefineEngineMethod(GuiInspectorVariableGroup, createInspectorField, GuiInspectorField*, (),, "createInspectorField()")
  205. {
  206. return object->createInspectorField();
  207. }
  208. DefineEngineMethod(GuiInspectorVariableGroup, addInspectorField, void, (GuiInspectorField* field), (nullAsType<GuiInspectorField*>()), "addInspectorField( GuiInspectorFieldObject )")
  209. {
  210. object->addInspectorField(field);
  211. }