variableField.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. //-----------------------------------------------------------------------------
  29. // GuiInspectorVariableField
  30. //-----------------------------------------------------------------------------
  31. IMPLEMENT_CONOBJECT(GuiInspectorVariableField);
  32. ConsoleDocClass( GuiInspectorVariableField,
  33. "@brief Inspector support for variables in a GuiVariableInspector.\n\n"
  34. "Editor use only.\n\n"
  35. "@internal"
  36. );
  37. GuiInspectorVariableField::GuiInspectorVariableField()
  38. {
  39. }
  40. GuiInspectorVariableField::~GuiInspectorVariableField()
  41. {
  42. }
  43. bool GuiInspectorVariableField::onAdd()
  44. {
  45. if( !mInspector )
  46. {
  47. Con::errorf("GuiInspectorVariableField::onAdd - Fail - No inspector");
  48. return false;
  49. }
  50. setInspectorProfile();
  51. // Hack: skip our immediate parent
  52. if ( !Parent::Parent::onAdd() )
  53. return false;
  54. {
  55. GuiTextEditCtrl *edit = new GuiTextEditCtrl();
  56. edit->setDataField( StringTable->insert("profile"), NULL, "GuiInspectorTextEditProfile" );
  57. edit->registerObject();
  58. char szBuffer[512];
  59. dSprintf( szBuffer, 512, "%d.apply(%d.getText());",getId(), edit->getId() );
  60. edit->setField("AltCommand", szBuffer );
  61. edit->setField("Validate", szBuffer );
  62. mEdit = edit;
  63. }
  64. setBounds(0,0,100,18);
  65. // Add our edit as a child
  66. addObject( mEdit );
  67. // Calculate Caption and EditCtrl Rects
  68. updateRects();
  69. // Force our editField to set it's value
  70. updateValue();
  71. return true;
  72. }
  73. void GuiInspectorVariableField::setData( const char* data, bool callbacks )
  74. {
  75. if (mOwnerObject == nullptr && mVariableName == StringTable->EmptyString())
  76. {
  77. if (!mCaption || mCaption[0] == 0)
  78. return;
  79. Con::setVariable(mCaption, data);
  80. }
  81. else
  82. {
  83. if (mOwnerObject != nullptr)
  84. {
  85. mOwnerObject->setDataField(mVariableName, NULL, data);
  86. }
  87. else
  88. {
  89. //probably a global var if we have no object attached, so just let it set
  90. Con::setVariable(mVariableName, data);
  91. }
  92. }
  93. // Force our edit to update
  94. updateValue();
  95. }
  96. const char* GuiInspectorVariableField::getData( U32 inspectObjectIndex )
  97. {
  98. if ( !mCaption || mCaption[0] == 0 )
  99. return "";
  100. return Con::getVariable( mCaption );
  101. }
  102. void GuiInspectorVariableField::setValue( const char* newValue )
  103. {
  104. GuiTextEditCtrl *ctrl = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  105. if( ctrl != NULL )
  106. ctrl->setText( newValue );
  107. }
  108. void GuiInspectorVariableField::updateValue()
  109. {
  110. if ( !mCaption || mCaption[0] == 0 )
  111. return;
  112. setValue( getData() );
  113. }