variableField.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. {
  40. }
  41. GuiInspectorVariableField::~GuiInspectorVariableField()
  42. {
  43. }
  44. bool GuiInspectorVariableField::onAdd()
  45. {
  46. if( !mInspector )
  47. {
  48. Con::errorf("GuiInspectorVariableField::onAdd - Fail - No inspector");
  49. return false;
  50. }
  51. setInspectorProfile();
  52. // Hack: skip our immediate parent
  53. if ( !Parent::Parent::onAdd() )
  54. return false;
  55. {
  56. GuiTextEditCtrl *edit = new GuiTextEditCtrl();
  57. edit->setDataField( StringTable->insert("profile"), NULL, "GuiInspectorTextEditProfile" );
  58. edit->registerObject();
  59. char szBuffer[512];
  60. dSprintf( szBuffer, 512, "%d.apply(%d.getText());",getId(), edit->getId() );
  61. edit->setField("AltCommand", szBuffer );
  62. edit->setField("Validate", szBuffer );
  63. mEdit = edit;
  64. }
  65. setBounds(0,0,100,18);
  66. // Add our edit as a child
  67. addObject( mEdit );
  68. // Calculate Caption and EditCtrl Rects
  69. updateRects();
  70. // Force our editField to set it's value
  71. updateValue();
  72. return true;
  73. }
  74. void GuiInspectorVariableField::setData( const char* data, bool callbacks )
  75. {
  76. if (mOwnerObject == nullptr && mVariableName == StringTable->EmptyString())
  77. {
  78. if (!mCaption || mCaption[0] == 0)
  79. return;
  80. Con::setVariable(mCaption, data);
  81. }
  82. else
  83. {
  84. if (mOwnerObject != nullptr)
  85. {
  86. //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
  87. //otherwise, use fields as normal
  88. Settings* setting = dynamic_cast<Settings*>(mOwnerObject);
  89. if (setting)
  90. {
  91. setting->setValue(mVariableName, data);
  92. }
  93. else
  94. {
  95. mOwnerObject->setDataField(mVariableName, NULL, data);
  96. }
  97. }
  98. else
  99. {
  100. //probably a global var if we have no object attached, so just let it set
  101. Con::setVariable(mVariableName, data);
  102. }
  103. }
  104. // Force our edit to update
  105. updateValue();
  106. }
  107. const char* GuiInspectorVariableField::getData( U32 inspectObjectIndex )
  108. {
  109. if ( !mCaption || mCaption[0] == 0 )
  110. return "";
  111. Settings* setting = dynamic_cast<Settings*>(mOwnerObject);
  112. if (setting)
  113. {
  114. return setting->value(mVariableName);
  115. }
  116. else
  117. {
  118. return Con::getVariable(mCaption);
  119. }
  120. }
  121. void GuiInspectorVariableField::setValue( const char* newValue )
  122. {
  123. GuiTextEditCtrl *ctrl = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  124. if( ctrl != NULL )
  125. ctrl->setText( newValue );
  126. }
  127. void GuiInspectorVariableField::updateValue()
  128. {
  129. if ( !mCaption || mCaption[0] == 0 )
  130. return;
  131. setValue( getData() );
  132. }