guiButtonCtrl.cc 7.0 KB

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