variableField.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/variableField.h"
  24. #include "gui/buttons/guiIconButtonCtrl.h"
  25. #include "gui/editor/guiInspector.h"
  26. #include "core/util/safeDelete.h"
  27. #include "gfx/gfxDrawUtil.h"
  28. #include "util/settings.h"
  29. //-----------------------------------------------------------------------------
  30. // GuiInspectorVariableField
  31. //-----------------------------------------------------------------------------
  32. IMPLEMENT_CONOBJECT(GuiInspectorVariableField);
  33. ConsoleDocClass( GuiInspectorVariableField,
  34. "@brief Inspector support for variables in a GuiVariableInspector.\n\n"
  35. "Editor use only.\n\n"
  36. "@internal"
  37. );
  38. GuiInspectorVariableField::GuiInspectorVariableField()
  39. : mVariableName(StringTable->EmptyString()),
  40. mSetCallbackName(StringTable->EmptyString()),
  41. mOwnerObject(NULL)
  42. {
  43. }
  44. GuiInspectorVariableField::~GuiInspectorVariableField()
  45. {
  46. }
  47. bool GuiInspectorVariableField::onAdd()
  48. {
  49. if( !mInspector )
  50. {
  51. Con::errorf("GuiInspectorVariableField::onAdd - Fail - No inspector");
  52. return false;
  53. }
  54. setInspectorProfile();
  55. // Hack: skip our immediate parent
  56. if ( !Parent::Parent::onAdd() )
  57. return false;
  58. {
  59. GuiTextEditCtrl *edit = new GuiTextEditCtrl();
  60. edit->setDataField( StringTable->insert("profile"), NULL, "GuiInspectorTextEditProfile" );
  61. edit->registerObject();
  62. char szBuffer[512];
  63. dSprintf( szBuffer, 512, "%d.apply(%d.getText());",getId(), edit->getId() );
  64. edit->setField("AltCommand", szBuffer );
  65. edit->setField("Validate", szBuffer );
  66. mEdit = edit;
  67. }
  68. setBounds(0,0,100,18);
  69. // Add our edit as a child
  70. addObject( mEdit );
  71. // Calculate Caption and EditCtrl Rects
  72. updateRects();
  73. // Force our editField to set it's value
  74. updateValue();
  75. return true;
  76. }
  77. void GuiInspectorVariableField::setData( const char* data, bool callbacks )
  78. {
  79. if (mOwnerObject == nullptr && mVariableName == StringTable->EmptyString())
  80. {
  81. if (!mCaption || mCaption[0] == 0)
  82. return;
  83. Con::setVariable(mCaption, data);
  84. }
  85. else
  86. {
  87. if (mOwnerObject != nullptr)
  88. {
  89. //Special case: if our object is a Settings class, we'll assume that we're trying to get/set the fields via the Setting class's normal behavior
  90. //otherwise, use fields as normal
  91. Settings* setting = dynamic_cast<Settings*>(mOwnerObject);
  92. if (setting)
  93. {
  94. setting->setValue(mVariableName, data);
  95. }
  96. else
  97. {
  98. mOwnerObject->setDataField(mVariableName, NULL, data);
  99. }
  100. }
  101. else
  102. {
  103. //probably a global var if we have no object attached, so just let it set
  104. Con::setVariable(mVariableName, data);
  105. }
  106. }
  107. // Force our edit to update
  108. updateValue();
  109. }
  110. const char* GuiInspectorVariableField::getData( U32 inspectObjectIndex )
  111. {
  112. if ( !mCaption || mCaption[0] == 0 )
  113. return "";
  114. Settings* setting = dynamic_cast<Settings*>(mOwnerObject);
  115. if (setting)
  116. {
  117. return setting->value(mVariableName);
  118. }
  119. else
  120. {
  121. return Con::getVariable(mCaption);
  122. }
  123. }
  124. void GuiInspectorVariableField::setValue( const char* newValue )
  125. {
  126. GuiTextEditCtrl *ctrl = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  127. if( ctrl != NULL )
  128. ctrl->setText( newValue );
  129. }
  130. void GuiInspectorVariableField::updateValue()
  131. {
  132. if ( !mCaption || mCaption[0] == 0 )
  133. return;
  134. setValue( getData() );
  135. }