guiCheckBoxCtrl.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "console/console.h"
  23. #include "graphics/dgl.h"
  24. #include "gui/guiCanvas.h"
  25. #include "gui/buttons/guiCheckBoxCtrl.h"
  26. #include "console/consoleTypes.h"
  27. #include "gui/guiDefaultControlRender.h"
  28. #include "guiCheckBoxCtrl_ScriptBinding.h"
  29. IMPLEMENT_CONOBJECT(GuiCheckBoxCtrl);
  30. //---------------------------------------------------------------------------
  31. GuiCheckBoxCtrl::GuiCheckBoxCtrl()
  32. {
  33. mBounds.extent.set(140, 30);
  34. mStateOn = false;
  35. mBoxOffset.set(0,7);
  36. mBoxExtent.set(16, 16);
  37. mTextOffset.set(22, 7);
  38. mTextExtent.set(mBounds.extent.x - mTextOffset.x, mBounds.extent.y - mTextOffset.y);
  39. }
  40. //---------------------------------------------------------------------------
  41. void GuiCheckBoxCtrl::initPersistFields()
  42. {
  43. Parent::initPersistFields();
  44. addField("stateOn", TypeBool, Offset(mStateOn, GuiCheckBoxCtrl));
  45. addField("boxOffset", TypePoint2I, Offset(mBoxOffset, GuiCheckBoxCtrl));
  46. addField("boxExtent", TypePoint2I, Offset(mBoxExtent, GuiCheckBoxCtrl));
  47. addField("textOffset", TypePoint2I, Offset(mTextOffset, GuiCheckBoxCtrl));
  48. addField("textExtent", TypePoint2I, Offset(mTextExtent, GuiCheckBoxCtrl));
  49. }
  50. bool GuiCheckBoxCtrl::onWake()
  51. {
  52. if(!Parent::onWake())
  53. return false;
  54. return true;
  55. }
  56. void GuiCheckBoxCtrl::onRender(Point2I offset, const RectI &updateRect)
  57. {
  58. GuiControlState currentState = mStateOn ? GuiControlState::NormalStateOn : GuiControlState::NormalState;
  59. if (!mActive)
  60. {
  61. currentState = mStateOn ? GuiControlState::DisabledStateOn : GuiControlState::DisabledState;
  62. }
  63. else if (mDepressed)
  64. {
  65. currentState = mStateOn ? GuiControlState::SelectedStateOn : GuiControlState::SelectedState;
  66. }
  67. else if (mMouseOver || isFirstResponder())
  68. {
  69. currentState = mStateOn ? GuiControlState::HighlightStateOn : GuiControlState::HighlightState;
  70. }
  71. RectI ctrlRect = applyMargins(offset, mBounds.extent, currentState, mProfile);
  72. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, mProfile);
  73. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, mProfile);
  74. RectI boxRect(contentRect.point + mBoxOffset, mBoxExtent);
  75. //Contrain the Box Rect. It must fit in the content Rect.
  76. if ((boxRect.point.x + boxRect.extent.x) > (contentRect.point.x + contentRect.extent.x))
  77. {
  78. boxRect.extent.x = contentRect.point.x + contentRect.extent.x - boxRect.point.x;
  79. }
  80. if ((boxRect.point.y + boxRect.extent.y) > (contentRect.point.y + contentRect.extent.y))
  81. {
  82. boxRect.extent.y = contentRect.point.y + contentRect.extent.y - boxRect.point.y;
  83. }
  84. //Draw the checkbox
  85. renderInnerControl(boxRect, currentState);
  86. RectI textRect(contentRect.point + mTextOffset, mTextExtent);
  87. //Contrain the Text Rect. It must fit in the content Rect.
  88. if ((textRect.point.x + textRect.extent.x) > (contentRect.point.x + contentRect.extent.x))
  89. {
  90. textRect.extent.x = contentRect.point.x + contentRect.extent.x - textRect.point.x;
  91. }
  92. if ((textRect.point.y + textRect.extent.y) > (contentRect.point.y + contentRect.extent.y))
  93. {
  94. textRect.extent.y = contentRect.point.y + contentRect.extent.y - textRect.point.y;
  95. }
  96. //Render Text
  97. dglSetBitmapModulation(getFontColor(mProfile, currentState));
  98. renderText(textRect.point, textRect.extent, mText, mProfile);
  99. //Render the childen
  100. renderChildControls(offset, contentRect, updateRect);
  101. }
  102. void GuiCheckBoxCtrl::renderInnerControl(RectI &boxRect, const GuiControlState currentState)
  103. {
  104. renderUniversalRect(boxRect, mProfile, currentState);
  105. if(isFirstResponder())
  106. {
  107. dglDrawRect(boxRect, mProfile->mCursorColor);
  108. }
  109. }
  110. void GuiCheckBoxCtrl::onAction()
  111. {
  112. if (!mActive)
  113. return;
  114. mStateOn = !mStateOn;
  115. Parent::onAction();
  116. }
  117. void GuiCheckBoxCtrl::setStateOn(bool bStateOn)
  118. {
  119. if (!mActive)
  120. return;
  121. mStateOn = bStateOn;
  122. setUpdate();
  123. }
  124. void GuiCheckBoxCtrl::setScriptValue(const char *value)
  125. {
  126. mStateOn = dAtob(value);
  127. setUpdate();
  128. }
  129. const char *GuiCheckBoxCtrl::getScriptValue()
  130. {
  131. return mStateOn ? "1" : "0";
  132. }
  133. void GuiCheckBoxCtrl::onMessage(GuiControl *sender, S32 msg)
  134. {
  135. Parent::onMessage(sender, msg);
  136. }