Torque3D Documentation / _generateds / guiCheckBoxCtrl.cpp

guiCheckBoxCtrl.cpp

Engine/source/gui/buttons/guiCheckBoxCtrl.cpp

More...

Public Functions

ConsoleDocClass(GuiCheckBoxCtrl , "@brief A named checkbox that can be toggled on and <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">off.\n\n</a>" "A <a href="/coding/class/classguicheckboxctrl/">GuiCheckBoxCtrl</a> displays <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> text label next <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> checkbox that can be toggled on and off by the user. " "Checkboxes are usually used <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> present boolean choices like, <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> example, <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> switch <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> toggle fullscreen " "video on and <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">off.\n\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "//Create <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> checkbox that allows <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> toggle fullscreen on and <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">off.\n</a>" "<a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classguicheckboxctrl/">GuiCheckBoxCtrl</a>(FullscreenToggle)\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " text=\"Fullscreen\";\n" "};\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "// Set the initial state <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> match the current fullscreen <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">setting.\n</a>" "FullscreenToggle.setStateOn( Canvas.isFullscreen() );\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "// Define function <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> be called when checkbox state is <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">toggled.\n</a>" "function FullscreenToggle::onClick( %this )\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   Canvas.toggleFullscreen();\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "}\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@ingroup GuiButtons" )
DefineEngineMethod(GuiCheckBoxCtrl , isStateOn , bool , () , "Test whether the checkbox is currently <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">checked.\n</a>" "@return True <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> the checkbox is currently ticked, false <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">otherwise.\n</a>" )
DefineEngineMethod(GuiCheckBoxCtrl , setStateOn , void , (bool newState) , "Set whether the checkbox is ticked or <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">not.\n</a>" "@param newState If true the box will be checked, <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> false, it will be <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">unchecked.\n\n</a>" " @note This method will @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a91b64995742fd30063314f12340b4b5a">b</a> not trigger the command associated with the control. To toggle the " "checkbox state as <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> the user had clicked the control, use performClick()." )

Detailed Description

Public Functions

ConsoleDocClass(GuiCheckBoxCtrl , "@brief A named checkbox that can be toggled on and <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">off.\n\n</a>" "A <a href="/coding/class/classguicheckboxctrl/">GuiCheckBoxCtrl</a> displays <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> text label next <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> checkbox that can be toggled on and off by the user. " "Checkboxes are usually used <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> present boolean choices like, <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> example, <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> switch <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> toggle fullscreen " "video on and <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">off.\n\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "//Create <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> checkbox that allows <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> toggle fullscreen on and <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">off.\n</a>" "<a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classguicheckboxctrl/">GuiCheckBoxCtrl</a>(FullscreenToggle)\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " text=\"Fullscreen\";\n" "};\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "// Set the initial state <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> match the current fullscreen <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">setting.\n</a>" "FullscreenToggle.setStateOn( Canvas.isFullscreen() );\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "// Define function <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> be called when checkbox state is <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">toggled.\n</a>" "function FullscreenToggle::onClick( %this )\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   Canvas.toggleFullscreen();\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "}\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@ingroup GuiButtons" )

DefineEngineMethod(GuiCheckBoxCtrl , isStateOn , bool , () , "Test whether the checkbox is currently <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">checked.\n</a>" "@return True <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> the checkbox is currently ticked, false <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">otherwise.\n</a>" )

DefineEngineMethod(GuiCheckBoxCtrl , setStateOn , void , (bool newState) , "Set whether the checkbox is ticked or <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">not.\n</a>" "@param newState If true the box will be checked, <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> false, it will be <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">unchecked.\n\n</a>" " @note This method will @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a91b64995742fd30063314f12340b4b5a">b</a> not trigger the command associated with the control. To toggle the " "checkbox state as <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> the user had clicked the control, use performClick()." )

IMPLEMENT_CONOBJECT(GuiCheckBoxCtrl )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platform.h"
 25#include "gui/buttons/guiCheckBoxCtrl.h"
 26
 27#include "console/console.h"
 28#include "console/consoleTypes.h"
 29#include "console/engineAPI.h"
 30#include "gfx/gfxDevice.h"
 31#include "gfx/gfxDrawUtil.h"
 32#include "gui/core/guiCanvas.h"
 33#include "sfx/sfxSystem.h"
 34#include "sfx/sfxTrack.h"
 35
 36
 37IMPLEMENT_CONOBJECT( GuiCheckBoxCtrl );
 38
 39ConsoleDocClass( GuiCheckBoxCtrl,
 40   "@brief A named checkbox that can be toggled on and off.\n\n"
 41   
 42   "A GuiCheckBoxCtrl displays a text label next to a checkbox that can be toggled on and off by the user. "
 43   "Checkboxes are usually used to present boolean choices like, for example, a switch to toggle fullscreen "
 44   "video on and off.\n\n"
 45   
 46   "@tsexample\n"
 47   "// Create a checkbox that allows to toggle fullscreen on and off.\n"
 48   "new GuiCheckBoxCtrl( FullscreenToggle )\n"
 49   "{\n"
 50   "   text = \"Fullscreen\";\n"
 51   "};\n"
 52   "\n"
 53   "// Set the initial state to match the current fullscreen setting.\n"
 54   "FullscreenToggle.setStateOn( Canvas.isFullscreen() );\n"
 55   "\n"
 56   "// Define function to be called when checkbox state is toggled.\n"
 57   "function FullscreenToggle::onClick( %this )\n"
 58   "{\n"
 59   "   Canvas.toggleFullscreen();\n"
 60   "}\n"
 61   "@endtsexample\n\n"
 62   
 63   "@ingroup GuiButtons"
 64);
 65
 66
 67//-----------------------------------------------------------------------------
 68
 69GuiCheckBoxCtrl::GuiCheckBoxCtrl()
 70{
 71   setExtent(140, 30);
 72   mStateOn = false;
 73   mIndent = 0;
 74   mButtonType = ButtonTypeCheck;
 75}
 76
 77//-----------------------------------------------------------------------------
 78
 79bool GuiCheckBoxCtrl::onWake()
 80{
 81   if(!Parent::onWake())
 82      return false;
 83
 84   // make sure there is a bitmap array for this control type
 85   // if it is declared as such in the control
 86   if( !mProfile->mBitmapArrayRects.size() && !mProfile->constructBitmapArray() )
 87   {
 88      Con::errorf( "GuiCheckBoxCtrl::onWake - failed to create bitmap array from profile '%s'", mProfile->getName() );
 89      return false;
 90   }
 91
 92   return true;
 93}
 94
 95//-----------------------------------------------------------------------------
 96
 97void GuiCheckBoxCtrl::onRender(Point2I offset, const RectI &updateRect)
 98{
 99   // RLP/Sickhead NOTE: New/experimental code
100   // for notifying the GuiCheckBoxCtrl of changes
101   // to its mConsoleVariable's state.
102   if ( mConsoleVariable[0] )
103   {
104      bool stateOn = Con::getBoolVariable( mConsoleVariable );
105      if ( stateOn != mStateOn )
106         mStateOn = stateOn;
107   }
108
109   ColorI backColor = mActive ? mProfile->mFillColor : mProfile->mFillColorNA;
110   ColorI fontColor = mActive ? (mMouseOver ? mProfile->mFontColorHL : mProfile->mFontColor) : mProfile->mFontColorNA;
111   ColorI insideBorderColor = isFirstResponder() ? mProfile->mBorderColorHL : mProfile->mBorderColor;
112
113   // just draw the check box and the text:
114   S32 xOffset = 0;
115   GFX->getDrawUtil()->clearBitmapModulation();
116   if(mProfile->mBitmapArrayRects.size() >= 4)
117   {
118      S32 index = mStateOn;
119      if(!mActive)
120      {
121         if(mProfile->mBitmapArrayRects.size() >= 6)
122         {
123            // New style checkbox bitmap with 6 images
124            index = 4 + mStateOn;
125         }
126         else
127         {
128            // Old style checkbox bitmap
129            index = 2;
130         }
131      }
132      else if(mDepressed)
133      {
134         index += 2;
135      }
136      xOffset = mProfile->mBitmapArrayRects[0].extent.x + 2 + mIndent;
137      S32 y = (getHeight() - mProfile->mBitmapArrayRects[0].extent.y) / 2;
138      GFX->getDrawUtil()->drawBitmapSR(mProfile->mTextureObject, offset + Point2I(mIndent, y), mProfile->mBitmapArrayRects[index]);
139   }
140   
141   if(mButtonText[0] != '\0')
142   {
143     GFX->getDrawUtil()->setBitmapModulation( fontColor );
144      renderJustifiedText(Point2I(offset.x + xOffset, offset.y),
145                          Point2I(getWidth() - getHeight(), getHeight()),
146                          mButtonText);
147   }
148   //render the children
149   renderChildControls(offset, updateRect);
150}
151
152//-----------------------------------------------------------------------------
153
154void GuiCheckBoxCtrl::autoSize()
155{
156   U32 width, height;
157   U32 bmpArrayRect0Width = 0;
158   
159   if( !mAwake )
160   {
161      mProfile->incLoadCount();
162            
163      if( !mProfile->mBitmapArrayRects.size() )
164         mProfile->constructBitmapArray();
165      if( mProfile->mBitmapArrayRects.size() )
166         bmpArrayRect0Width = mProfile->mBitmapArrayRects[ 0 ].extent.x;
167   }
168
169   U32 bmpWidth = bmpArrayRect0Width + 2 + mIndent;
170   U32 strWidth = mProfile->mFont->getStrWidthPrecise( mButtonText );
171
172   width = bmpWidth + strWidth + 2;
173
174   U32 bmpHeight = mProfile->mBitmapArrayRects[0].extent.y;
175   U32 fontHeight = mProfile->mFont->getHeight();
176
177   height = getMax( bmpHeight, fontHeight ) + 4;
178
179   setExtent( width, height );
180   
181   if( !mAwake )
182      mProfile->decLoadCount();
183}
184
185//=============================================================================
186//    API.
187//=============================================================================
188// MARK: ---- API ----
189
190//-----------------------------------------------------------------------------
191
192DefineEngineMethod( GuiCheckBoxCtrl, setStateOn, void, ( bool newState ),,
193   "Set whether the checkbox is ticked or not.\n"
194   "@param newState If true the box will be checked, if false, it will be unchecked.\n\n"
195   "@note This method will @b not trigger the command associated with the control.  To toggle the "
196      "checkbox state as if the user had clicked the control, use performClick()." )
197{
198   object->setStateOn( newState );
199}
200
201//-----------------------------------------------------------------------------
202
203DefineEngineMethod( GuiCheckBoxCtrl, isStateOn, bool, (),,
204   "Test whether the checkbox is currently checked.\n"
205   "@return True if the checkbox is currently ticked, false otherwise.\n" )
206{
207   return object->getStateOn();
208}
209