guiButtonCtrl.cc 6.8 KB

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