guiButtonCtrl.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "console/consoleTypes.h"
  25. #include "platform/platformAudio.h"
  26. #include "gui/guiCanvas.h"
  27. #include "gui/buttons/guiButtonCtrl.h"
  28. #include "gui/guiDefaultControlRender.h"
  29. #include "guiButtonCtrl_ScriptBinding.h"
  30. IMPLEMENT_CONOBJECT(GuiButtonCtrl);
  31. GuiButtonCtrl::GuiButtonCtrl()
  32. {
  33. mDepressed = false;
  34. mMouseOver = false;
  35. mActive = true;
  36. mBounds.extent.set(140, 30);
  37. mText = StringTable->insert("Button");
  38. mTextID = StringTable->EmptyString;
  39. mProfile = NULL;
  40. setField("profile", "GuiButtonProfile");
  41. }
  42. void GuiButtonCtrl::initPersistFields()
  43. {
  44. Parent::initPersistFields();
  45. }
  46. void GuiButtonCtrl::setActive(bool value)
  47. {
  48. Parent::setActive(value);
  49. if (!value)
  50. {
  51. mDepressed = false;
  52. mMouseOver = false;
  53. }
  54. }
  55. void GuiButtonCtrl::acceleratorKeyPress(U32)
  56. {
  57. if (!mActive)
  58. return;
  59. //set the bool
  60. mDepressed = true;
  61. if (mProfile->mTabable)
  62. setFirstResponder();
  63. //update
  64. setUpdate();
  65. }
  66. void GuiButtonCtrl::acceleratorKeyRelease(U32)
  67. {
  68. if (!mActive)
  69. return;
  70. if (mDepressed)
  71. {
  72. //set the bool
  73. mDepressed = false;
  74. //perform the action
  75. onAction();
  76. }
  77. //update
  78. setUpdate();
  79. }
  80. void GuiButtonCtrl::onTouchEnter(const GuiEvent &event)
  81. {
  82. if (!mActive)
  83. return;
  84. mMouseOver = true;
  85. if (isMouseLocked())
  86. {
  87. mDepressed = true;
  88. }
  89. Con::executef(this, 1, "onTouchEnter");
  90. //update
  91. setUpdate();
  92. }
  93. void GuiButtonCtrl::onTouchLeave(const GuiEvent &)
  94. {
  95. if (!mActive)
  96. return;
  97. if (isMouseLocked())
  98. mDepressed = false;
  99. mouseUnlock();
  100. mMouseOver = false;
  101. Con::executef(this, 1, "onTouchLeave");
  102. //update
  103. setUpdate();
  104. }
  105. void GuiButtonCtrl::onTouchDown(const GuiEvent &event)
  106. {
  107. if (!mActive)
  108. return;
  109. mDepressed = true;
  110. if (mProfile->mCanKeyFocus)
  111. setFirstResponder();
  112. //lock the mouse
  113. mouseLock();
  114. // Execute callback
  115. char buf[3][32];
  116. dSprintf(buf[0], 32, "%d", event.modifier);
  117. dSprintf(buf[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
  118. dSprintf(buf[2], 32, "%d", event.mouseClickCount);
  119. Con::executef(this, 4, "onTouchDown", buf[0], buf[1], buf[2]);
  120. //update
  121. setUpdate();
  122. }
  123. void GuiButtonCtrl::onTouchUp(const GuiEvent &event)
  124. {
  125. if (!mActive)
  126. return;
  127. mouseUnlock();
  128. //if we released the mouse within this control, perform the action
  129. if (mDepressed)
  130. onAction();
  131. // Execute callback
  132. char buf[3][32];
  133. dSprintf(buf[0], 32, "%d", event.modifier);
  134. dSprintf(buf[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
  135. dSprintf(buf[2], 32, "%d", event.mouseClickCount);
  136. Con::executef(this, 4, "onTouchUp", buf[0], buf[1], buf[2]);
  137. mDepressed = false;
  138. //update
  139. setUpdate();
  140. }
  141. void GuiButtonCtrl::onRightMouseUp(const GuiEvent &event)
  142. {
  143. if (!mActive)
  144. return;
  145. Con::executef(this, 1, "onRightClick");
  146. Parent::onRightMouseUp(event);
  147. }
  148. bool GuiButtonCtrl::onKeyDown(const GuiEvent &event)
  149. {
  150. if (!mActive)
  151. return true;
  152. //see if the key down is a return or space or not
  153. if ((event.keyCode == KEY_RETURN || event.keyCode == KEY_SPACE)
  154. && event.modifier == 0)
  155. {
  156. mDepressed = true;
  157. return true;
  158. }
  159. //otherwise, pass the event to it's parent
  160. return Parent::onKeyDown(event);
  161. }
  162. bool GuiButtonCtrl::onKeyUp(const GuiEvent &event)
  163. {
  164. if (!mActive)
  165. return true;
  166. //see if the key down is a return or space or not
  167. if (mDepressed &&
  168. (event.keyCode == KEY_RETURN || event.keyCode == KEY_SPACE) &&
  169. event.modifier == 0)
  170. {
  171. mDepressed = false;
  172. onAction();
  173. return true;
  174. }
  175. //otherwise, pass the event to it's parent
  176. return Parent::onKeyUp(event);
  177. }
  178. void GuiButtonCtrl::onAction()
  179. {
  180. if (!mActive)
  181. return;
  182. setUpdate();
  183. if (isMethod("onClick"))
  184. Con::executef(this, 2, "onClick");
  185. Parent::onAction();
  186. }
  187. GuiControlState GuiButtonCtrl::getCurrentState()
  188. {
  189. if (!mActive)
  190. return GuiControlState::DisabledState;
  191. else if (mDepressed)
  192. return GuiControlState::SelectedState;
  193. else if (mMouseOver)
  194. return GuiControlState::HighlightState;
  195. else
  196. return GuiControlState::NormalState;
  197. }
  198. S32 GuiButtonCtrl::getBitmapIndex(const GuiControlState state)
  199. {
  200. if (state == GuiControlState::HighlightState)
  201. return 2;
  202. else if (state == GuiControlState::SelectedState)
  203. return 3;
  204. else if (state == GuiControlState::DisabledState)
  205. return 4;
  206. else
  207. return 1;
  208. }
  209. void GuiButtonCtrl::onRender(Point2I offset, const RectI& updateRect)
  210. {
  211. GuiControlState currentState = getCurrentState();
  212. RectI ctrlRect = applyMargins(offset, mBounds.extent, currentState, mProfile);
  213. renderUniversalRect(ctrlRect, mProfile, currentState, getFillColor(currentState), true);
  214. //Render Text
  215. dglSetBitmapModulation(getFontColor(mProfile, currentState));
  216. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, mProfile);
  217. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, mProfile);
  218. renderText(contentRect.point, contentRect.extent, mText, mProfile);
  219. //Render the childen
  220. if(size() > 0)
  221. renderChildControls(offset, contentRect, updateRect);
  222. }
  223. void GuiButtonCtrl::setScriptValue(const char *value)
  224. {
  225. // Update the console variable:
  226. if (mConsoleVariable[0])
  227. Con::setVariable(mConsoleVariable, value);
  228. setUpdate();
  229. }
  230. const char *GuiButtonCtrl::getScriptValue()
  231. {
  232. return StringTable->EmptyString;
  233. }
  234. void GuiButtonCtrl::onMessage(GuiControl *sender, S32 msg)
  235. {
  236. Parent::onMessage(sender, msg);
  237. }