guiCheckBoxCtrl.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/buttons/guiCheckBoxCtrl.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/engineAPI.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "gfx/gfxDrawUtil.h"
  29. #include "gui/core/guiCanvas.h"
  30. #include "sfx/sfxSystem.h"
  31. #include "sfx/sfxTrack.h"
  32. IMPLEMENT_CONOBJECT( GuiCheckBoxCtrl );
  33. ConsoleDocClass( GuiCheckBoxCtrl,
  34. "@brief A named checkbox that can be toggled on and off.\n\n"
  35. "A GuiCheckBoxCtrl displays a text label next to a checkbox that can be toggled on and off by the user. "
  36. "Checkboxes are usually used to present boolean choices like, for example, a switch to toggle fullscreen "
  37. "video on and off.\n\n"
  38. "@tsexample\n"
  39. "// Create a checkbox that allows to toggle fullscreen on and off.\n"
  40. "new GuiCheckBoxCtrl( FullscreenToggle )\n"
  41. "{\n"
  42. " text = \"Fullscreen\";\n"
  43. "};\n"
  44. "\n"
  45. "// Set the initial state to match the current fullscreen setting.\n"
  46. "FullscreenToggle.setStateOn( Canvas.isFullscreen() );\n"
  47. "\n"
  48. "// Define function to be called when checkbox state is toggled.\n"
  49. "function FullscreenToggle::onClick( %this )\n"
  50. "{\n"
  51. " Canvas.toggleFullscreen();\n"
  52. "}\n"
  53. "@endtsexample\n\n"
  54. "@ingroup GuiButtons"
  55. );
  56. //-----------------------------------------------------------------------------
  57. GuiCheckBoxCtrl::GuiCheckBoxCtrl()
  58. {
  59. setExtent(140, 30);
  60. mStateOn = false;
  61. mIndent = 0;
  62. mButtonType = ButtonTypeCheck;
  63. }
  64. //-----------------------------------------------------------------------------
  65. bool GuiCheckBoxCtrl::onWake()
  66. {
  67. if(!Parent::onWake())
  68. return false;
  69. // make sure there is a bitmap array for this control type
  70. // if it is declared as such in the control
  71. if( !mProfile->mBitmapArrayRects.size() && !mProfile->constructBitmapArray() )
  72. {
  73. Con::errorf( "GuiCheckBoxCtrl::onWake - failed to create bitmap array from profile '%s'", mProfile->getName() );
  74. return false;
  75. }
  76. return true;
  77. }
  78. //-----------------------------------------------------------------------------
  79. void GuiCheckBoxCtrl::onRender(Point2I offset, const RectI &updateRect)
  80. {
  81. // RLP/Sickhead NOTE: New/experimental code
  82. // for notifying the GuiCheckBoxCtrl of changes
  83. // to its mConsoleVariable's state.
  84. if ( mConsoleVariable[0] )
  85. {
  86. bool stateOn = Con::getBoolVariable( mConsoleVariable );
  87. if ( stateOn != mStateOn )
  88. mStateOn = stateOn;
  89. }
  90. ColorI backColor = mActive ? mProfile->mFillColor : mProfile->mFillColorNA;
  91. ColorI fontColor = mActive ? (mHighlighted ? mProfile->mFontColorHL : mProfile->mFontColor) : mProfile->mFontColorNA;
  92. ColorI insideBorderColor = isFirstResponder() ? mProfile->mBorderColorHL : mProfile->mBorderColor;
  93. // just draw the check box and the text:
  94. S32 xOffset = 0;
  95. GFX->getDrawUtil()->clearBitmapModulation();
  96. if(mProfile->mBitmapArrayRects.size() >= 4)
  97. {
  98. S32 index = mStateOn;
  99. if(!mActive)
  100. {
  101. if(mProfile->mBitmapArrayRects.size() >= 6)
  102. {
  103. // New style checkbox bitmap with 6 images
  104. index = 4 + mStateOn;
  105. }
  106. else
  107. {
  108. // Old style checkbox bitmap
  109. index = 2;
  110. }
  111. }
  112. else if(mDepressed)
  113. {
  114. index += 2;
  115. }
  116. xOffset = mProfile->mBitmapArrayRects[0].extent.x + 2 + mIndent;
  117. S32 y = (getHeight() - mProfile->mBitmapArrayRects[0].extent.y) / 2;
  118. GFX->getDrawUtil()->drawBitmapSR(mProfile->getBitmapResource(), offset + Point2I(mIndent, y), mProfile->mBitmapArrayRects[index]);
  119. }
  120. if(mButtonText[0] != '\0')
  121. {
  122. GFX->getDrawUtil()->setBitmapModulation( fontColor );
  123. renderJustifiedText(Point2I(offset.x + xOffset, offset.y),
  124. Point2I(getWidth() - getHeight(), getHeight()),
  125. mButtonText);
  126. }
  127. //render the children
  128. renderChildControls(offset, updateRect);
  129. }
  130. //-----------------------------------------------------------------------------
  131. void GuiCheckBoxCtrl::autoSize()
  132. {
  133. U32 width, height;
  134. U32 bmpArrayRect0Width = 0;
  135. if( !mAwake )
  136. {
  137. mProfile->incLoadCount();
  138. if( !mProfile->mBitmapArrayRects.size() )
  139. mProfile->constructBitmapArray();
  140. if( mProfile->mBitmapArrayRects.size() )
  141. bmpArrayRect0Width = mProfile->mBitmapArrayRects[ 0 ].extent.x;
  142. }
  143. U32 bmpWidth = bmpArrayRect0Width + 2 + mIndent;
  144. U32 strWidth = mProfile->mFont->getStrWidthPrecise( mButtonText );
  145. width = bmpWidth + strWidth + 2;
  146. U32 bmpHeight = mProfile->mBitmapArrayRects[0].extent.y;
  147. U32 fontHeight = mProfile->mFont->getHeight();
  148. height = getMax( bmpHeight, fontHeight ) + 4;
  149. setExtent( width, height );
  150. if( !mAwake )
  151. mProfile->decLoadCount();
  152. }
  153. //=============================================================================
  154. // API.
  155. //=============================================================================
  156. // MARK: ---- API ----
  157. //-----------------------------------------------------------------------------
  158. DefineEngineMethod( GuiCheckBoxCtrl, setStateOn, void, ( bool newState ),,
  159. "Set whether the checkbox is ticked or not.\n"
  160. "@param newState If true the box will be checked, if false, it will be unchecked.\n\n"
  161. "@note This method will @b not trigger the command associated with the control. To toggle the "
  162. "checkbox state as if the user had clicked the control, use performClick()." )
  163. {
  164. object->setStateOn( newState );
  165. }
  166. //-----------------------------------------------------------------------------
  167. DefineEngineMethod( GuiCheckBoxCtrl, isStateOn, bool, (),,
  168. "Test whether the checkbox is currently checked.\n"
  169. "@return True if the checkbox is currently ticked, false otherwise.\n" )
  170. {
  171. return object->getStateOn();
  172. }