guiCheckBoxCtrl.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. IMPLEMENT_CONOBJECT(GuiCheckBoxCtrl);
  28. //---------------------------------------------------------------------------
  29. GuiCheckBoxCtrl::GuiCheckBoxCtrl()
  30. {
  31. mBounds.extent.set(140, 30);
  32. mStateOn = false;
  33. mIndent = 0;
  34. mButtonType = ButtonTypeCheck;
  35. mUseInactiveState = false;
  36. }
  37. //---------------------------------------------------------------------------
  38. void GuiCheckBoxCtrl::initPersistFields()
  39. {
  40. Parent::initPersistFields();
  41. addField("useInactiveState", TypeBool, Offset(mUseInactiveState, GuiCheckBoxCtrl));
  42. }
  43. bool GuiCheckBoxCtrl::onWake()
  44. {
  45. if(!Parent::onWake())
  46. return false;
  47. // make sure there is a bitmap array for this control type
  48. // if it is declared as such in the control
  49. mProfile->constructBitmapArray();
  50. return true;
  51. }
  52. void GuiCheckBoxCtrl::onMouseDown(const GuiEvent& event)
  53. {
  54. if (!mUseInactiveState)
  55. return Parent::onMouseDown(event);
  56. if (mProfile->mCanKeyFocus)
  57. setFirstResponder();
  58. if (mProfile->mSoundButtonDown)
  59. {
  60. AUDIOHANDLE handle = alxCreateSource(mProfile->mSoundButtonDown);
  61. alxPlay(handle);
  62. }
  63. //lock the mouse
  64. mouseLock();
  65. mDepressed = true;
  66. //update
  67. setUpdate();
  68. }
  69. void GuiCheckBoxCtrl::onMouseUp(const GuiEvent& event)
  70. {
  71. if (!mUseInactiveState)
  72. return Parent::onMouseUp(event);
  73. mouseUnlock();
  74. setUpdate();
  75. //if we released the mouse within this control, perform the action
  76. if (mDepressed)
  77. onAction();
  78. mDepressed = false;
  79. }
  80. void GuiCheckBoxCtrl::onAction()
  81. {
  82. if (!mUseInactiveState)
  83. return Parent::onAction();
  84. if(mButtonType == ButtonTypeCheck)
  85. {
  86. if (!mActive)
  87. {
  88. mActive = true;
  89. mStateOn = true;
  90. }
  91. else if (mStateOn)
  92. mStateOn = false;
  93. else if (!mStateOn)
  94. mActive = false;
  95. // Update the console variable:
  96. if ( mConsoleVariable[0] )
  97. Con::setBoolVariable( mConsoleVariable, mStateOn );
  98. // Execute the console command (if any)
  99. if( mConsoleCommand[0] )
  100. Con::evaluate( mConsoleCommand, false );
  101. }
  102. setUpdate();
  103. // Provide and onClick script callback.
  104. if( isMethod("onClick") )
  105. Con::executef( this, 2, "onClick" );
  106. }
  107. //---------------------------------------------------------------------------
  108. void GuiCheckBoxCtrl::onRender(Point2I offset, const RectI &updateRect)
  109. {
  110. ColorI backColor = mActive ? mProfile->mFillColor : mProfile->mFillColorNA;
  111. ColorI fontColor = mMouseOver ? mProfile->mFontColorHL : mProfile->mFontColor;
  112. ColorI insideBorderColor = isFirstResponder() ? mProfile->mBorderColorHL : mProfile->mBorderColor;
  113. // just draw the check box and the text:
  114. S32 xOffset = 0;
  115. dglClearBitmapModulation();
  116. if(mProfile->mBitmapArrayRects.size() >= 4)
  117. {
  118. // if size >= 4 then the inactive state images should be present
  119. S32 index = mStateOn;
  120. if(!mActive)
  121. index = 4; // inactive state images are indexes 4 and 5.
  122. else if(mDepressed)
  123. index += 2;
  124. xOffset = mProfile->mBitmapArrayRects[0].extent.x + 2 + mIndent;
  125. S32 y = (mBounds.extent.y - mProfile->mBitmapArrayRects[0].extent.y) / 2;
  126. dglDrawBitmapSR(mProfile->mTextureHandle, offset + Point2I(mIndent, y), mProfile->mBitmapArrayRects[index]);
  127. }
  128. if(mButtonText[0] != '\0')
  129. {
  130. dglSetBitmapModulation( fontColor );
  131. renderJustifiedText(Point2I(offset.x + xOffset, offset.y),
  132. Point2I(mBounds.extent.x - mBounds.extent.y, mBounds.extent.y),
  133. mButtonText);
  134. }
  135. //render the children
  136. renderChildControls(offset, updateRect);
  137. }
  138. ConsoleMethod(GuiCheckBoxCtrl, setStateOn, void, 3, 3, "(state) Sets the control as active and updates siblings of the same group."
  139. "@param state This argument may be a boolean value or an integer."
  140. "state < 0: Parent::setStateOn(false), obj::setActive(false)\n"
  141. "state == 0 (or false): Parent::setStateOn(false), obj::setActive(true)\n"
  142. "state > 0 (or true): Parent::setStateOn(true), obj::setActive(true)")
  143. {
  144. if (dStricmp(argv[2], "true") == 0)
  145. object->setStateOn(1);
  146. else if (dStricmp(argv[2], "false") == 0)
  147. object->setStateOn(0);
  148. else
  149. object->setStateOn(dAtoi(argv[2]));
  150. }
  151. void GuiCheckBoxCtrl::setStateOn(S32 state)
  152. {
  153. if (mUseInactiveState)
  154. {
  155. if (state < 0)
  156. {
  157. setActive(false);
  158. Parent::setStateOn(false);
  159. }
  160. else if (state == 0)
  161. {
  162. setActive(true);
  163. Parent::setStateOn(false);
  164. }
  165. else if (state > 0)
  166. {
  167. setActive(true);
  168. Parent::setStateOn(true);
  169. }
  170. }
  171. else
  172. Parent::setStateOn((bool)state);
  173. }
  174. const char* GuiCheckBoxCtrl::getScriptValue()
  175. {
  176. if (mUseInactiveState)
  177. {
  178. if (isActive())
  179. if (mStateOn)
  180. return "1";
  181. else
  182. return "0";
  183. else
  184. return "-1";
  185. }
  186. else
  187. return Parent::getScriptValue();
  188. }
  189. //---------------------------------------------------------------------------
  190. // EOF //