guiButtonCtrl.cc 7.1 KB

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