2
0

guiMouseEventCtrl.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "gui/utility/guiMouseEventCtrl.h"
  23. #include "console/consoleTypes.h"
  24. #include "console/engineAPI.h"
  25. IMPLEMENT_CONOBJECT(GuiMouseEventCtrl);
  26. ConsoleDocClass( GuiMouseEventCtrl,
  27. "@brief Used to overlaps a 'hot region' where you want to catch inputs with and have specific events occur based on individual callbacks.\n\n"
  28. "Mouse event callbacks supported by this control are: onMouseUp, onMouseDown, onMouseMove, onMouseDragged, onMouseEnter, onMouseLeave,\n"
  29. "onRightMouseDown, onRightMouseUp and onRightMouseDragged.\n\n"
  30. "@tsexample\n"
  31. "new GuiMouseEventCtrl()\n"
  32. "{\n"
  33. " lockMouse = \"0\";\n"
  34. " //Properties not specific to this control have been omitted from this example.\n"
  35. "};\n"
  36. "@endtsexample\n\n"
  37. "@see GuiControl\n\n"
  38. "@ingroup GuiCore\n"
  39. );
  40. IMPLEMENT_CALLBACK( GuiMouseEventCtrl, onMouseDown, void, ( S32 modifier, Point2I mousePoint, S32 mouseClickCount ),
  41. ( modifier, mousePoint, mouseClickCount ),
  42. "@brief Callback that occurs whenever the mouse is pressed down while in this control.\n\n"
  43. "@param modifier Key that was pressed during this callback. Values are:\n\n"
  44. "$EventModifier::RSHIFT\n\n"
  45. "$EventModifier::SHIFT\n\n"
  46. "$EventModifier::LCTRL\n\n"
  47. "$EventModifier::RCTRL\n\n"
  48. "$EventModifier::CTRL\n\n"
  49. "$EventModifier::CTRL\n\n"
  50. "$EventModifier::RALT\n\n"
  51. "$EventModifier::ALT\n\n"
  52. "@param mousePoint X/Y location of the mouse point\n"
  53. "@param mouseClickCount How many mouse clicks have occured for this event\n\n"
  54. "@tsexample\n"
  55. "// Mouse was pressed down in this control, causing the callback\n"
  56. "GuiMouseEventCtrl::onMouseDown(%this,%modifier,%mousePoint,%mouseClickCount)\n"
  57. "{\n"
  58. " // Code to call when a mouse event occurs.\n"
  59. "}\n"
  60. "@endtsexample\n\n"
  61. "@see GuiControl\n\n"
  62. );
  63. IMPLEMENT_CALLBACK( GuiMouseEventCtrl, onMouseUp, void, ( S32 modifier, Point2I mousePoint, S32 mouseClickCount ),
  64. ( modifier, mousePoint, mouseClickCount ),
  65. "@brief Callback that occurs whenever the mouse is released while in this control.\n\n"
  66. "@param modifier Key that was pressed during this callback. Values are:\n\n"
  67. "$EventModifier::RSHIFT\n\n"
  68. "$EventModifier::SHIFT\n\n"
  69. "$EventModifier::LCTRL\n\n"
  70. "$EventModifier::RCTRL\n\n"
  71. "$EventModifier::CTRL\n\n"
  72. "$EventModifier::CTRL\n\n"
  73. "$EventModifier::RALT\n\n"
  74. "$EventModifier::ALT\n\n"
  75. "@param mousePoint X/Y location of the mouse point\n"
  76. "@param mouseClickCount How many mouse clicks have occured for this event\n\n"
  77. "@tsexample\n"
  78. "// Mouse was released in this control, causing the callback\n"
  79. "GuiMouseEventCtrl::onMouseUp(%this,%modifier,%mousePoint,%mouseClickCount)\n"
  80. "{\n"
  81. " // Code to call when a mouse event occurs.\n"
  82. "}\n"
  83. "@endtsexample\n\n"
  84. "@see GuiControl\n\n"
  85. );
  86. IMPLEMENT_CALLBACK( GuiMouseEventCtrl, onMouseMove, void, ( S32 modifier, Point2I mousePoint, S32 mouseClickCount ),
  87. ( modifier, mousePoint, mouseClickCount ),
  88. "@brief Callback that occurs whenever the mouse is moved (without dragging) while in this control.\n\n"
  89. "@param modifier Key that was pressed during this callback. Values are:\n\n"
  90. "$EventModifier::RSHIFT\n\n"
  91. "$EventModifier::SHIFT\n\n"
  92. "$EventModifier::LCTRL\n\n"
  93. "$EventModifier::RCTRL\n\n"
  94. "$EventModifier::CTRL\n\n"
  95. "$EventModifier::CTRL\n\n"
  96. "$EventModifier::RALT\n\n"
  97. "$EventModifier::ALT\n\n"
  98. "@param mousePoint X/Y location of the mouse point\n"
  99. "@param mouseClickCount How many mouse clicks have occured for this event\n\n"
  100. "@tsexample\n"
  101. "// Mouse was moved in this control, causing the callback\n"
  102. "GuiMouseEventCtrl::onMouseMove(%this,%modifier,%mousePoint,%mouseClickCount)\n"
  103. "{\n"
  104. " // Code to call when a mouse event occurs.\n"
  105. "}\n"
  106. "@endtsexample\n\n"
  107. "@see GuiControl\n\n"
  108. );
  109. IMPLEMENT_CALLBACK( GuiMouseEventCtrl, onMouseDragged, void, ( S32 modifier, Point2I mousePoint, S32 mouseClickCount ),
  110. ( modifier, mousePoint, mouseClickCount ),
  111. "@brief Callback that occurs whenever the mouse is dragged while in this control.\n\n"
  112. "@param modifier Key that was pressed during this callback. Values are:\n\n"
  113. "$EventModifier::RSHIFT\n\n"
  114. "$EventModifier::SHIFT\n\n"
  115. "$EventModifier::LCTRL\n\n"
  116. "$EventModifier::RCTRL\n\n"
  117. "$EventModifier::CTRL\n\n"
  118. "$EventModifier::CTRL\n\n"
  119. "$EventModifier::RALT\n\n"
  120. "$EventModifier::ALT\n\n"
  121. "@param mousePoint X/Y location of the mouse point\n"
  122. "@param mouseClickCount How many mouse clicks have occured for this event\n\n"
  123. "@tsexample\n"
  124. "// Mouse was dragged in this control, causing the callback\n"
  125. "GuiMouseEventCtrl::onMouseDragged(%this,%modifier,%mousePoint,%mouseClickCount)\n"
  126. "{\n"
  127. " // Code to call when a mouse event occurs.\n"
  128. "}\n"
  129. "@endtsexample\n\n"
  130. "@see GuiControl\n\n"
  131. );
  132. IMPLEMENT_CALLBACK( GuiMouseEventCtrl, onMouseEnter, void, ( S32 modifier, Point2I mousePoint, S32 mouseClickCount ),
  133. ( modifier, mousePoint, mouseClickCount ),
  134. "@brief Callback that occurs whenever the mouse enters this control.\n\n"
  135. "@param modifier Key that was pressed during this callback. Values are:\n\n"
  136. "$EventModifier::RSHIFT\n\n"
  137. "$EventModifier::SHIFT\n\n"
  138. "$EventModifier::LCTRL\n\n"
  139. "$EventModifier::RCTRL\n\n"
  140. "$EventModifier::CTRL\n\n"
  141. "$EventModifier::CTRL\n\n"
  142. "$EventModifier::RALT\n\n"
  143. "$EventModifier::ALT\n\n"
  144. "@param mousePoint X/Y location of the mouse point\n"
  145. "@param mouseClickCount How many mouse clicks have occured for this event\n\n"
  146. "@tsexample\n"
  147. "// Mouse entered this control, causing the callback\n"
  148. "GuiMouseEventCtrl::onMouseEnter(%this,%modifier,%mousePoint,%mouseClickCount)\n"
  149. "{\n"
  150. " // Code to call when a mouse event occurs.\n"
  151. "}\n"
  152. "@endtsexample\n\n"
  153. "@see GuiControl\n\n"
  154. );
  155. IMPLEMENT_CALLBACK( GuiMouseEventCtrl, onMouseLeave, void, ( S32 modifier, Point2I mousePoint, S32 mouseClickCount ),
  156. ( modifier, mousePoint, mouseClickCount ),
  157. "@brief Callback that occurs whenever the mouse leaves this control.\n\n"
  158. "@param modifier Key that was pressed during this callback. Values are:\n\n"
  159. "$EventModifier::RSHIFT\n\n"
  160. "$EventModifier::SHIFT\n\n"
  161. "$EventModifier::LCTRL\n\n"
  162. "$EventModifier::RCTRL\n\n"
  163. "$EventModifier::CTRL\n\n"
  164. "$EventModifier::CTRL\n\n"
  165. "$EventModifier::RALT\n\n"
  166. "$EventModifier::ALT\n\n"
  167. "@param mousePoint X/Y location of the mouse point\n"
  168. "@param mouseClickCount How many mouse clicks have occured for this event\n\n"
  169. "@tsexample\n"
  170. "// Mouse left this control, causing the callback\n"
  171. "GuiMouseEventCtrl::onMouseLeave(%this,%modifier,%mousePoint,%mouseClickCount)\n"
  172. "{\n"
  173. " // Code to call when a mouse event occurs.\n"
  174. "}\n"
  175. "@endtsexample\n\n"
  176. "@see GuiControl\n\n"
  177. );
  178. IMPLEMENT_CALLBACK( GuiMouseEventCtrl, onRightMouseDown, void, ( S32 modifier, Point2I mousePoint, S32 mouseClickCount ),
  179. ( modifier, mousePoint, mouseClickCount ),
  180. "@brief Callback that occurs whenever the right mouse button is pressed while in this control.\n\n"
  181. "@param modifier Key that was pressed during this callback. Values are:\n\n"
  182. "$EventModifier::RSHIFT\n\n"
  183. "$EventModifier::SHIFT\n\n"
  184. "$EventModifier::LCTRL\n\n"
  185. "$EventModifier::RCTRL\n\n"
  186. "$EventModifier::CTRL\n\n"
  187. "$EventModifier::CTRL\n\n"
  188. "$EventModifier::RALT\n\n"
  189. "$EventModifier::ALT\n\n"
  190. "@param mousePoint X/Y location of the mouse point\n"
  191. "@param mouseClickCount How many mouse clicks have occured for this event\n\n"
  192. "@tsexample\n"
  193. "// Right mouse button was pressed in this control, causing the callback\n"
  194. "GuiMouseEventCtrl::onRightMouseDown(%this,%modifier,%mousePoint,%mouseClickCount)\n"
  195. "{\n"
  196. " // Code to call when a mouse event occurs.\n"
  197. "}\n"
  198. "@endtsexample\n\n"
  199. "@see GuiControl\n\n"
  200. );
  201. IMPLEMENT_CALLBACK( GuiMouseEventCtrl, onRightMouseUp, void, ( S32 modifier, Point2I mousePoint, S32 mouseClickCount ),
  202. ( modifier, mousePoint, mouseClickCount ),
  203. "@brief Callback that occurs whenever the right mouse button is released while in this control.\n\n"
  204. "@param modifier Key that was pressed during this callback. Values are:\n\n"
  205. "$EventModifier::RSHIFT\n\n"
  206. "$EventModifier::SHIFT\n\n"
  207. "$EventModifier::LCTRL\n\n"
  208. "$EventModifier::RCTRL\n\n"
  209. "$EventModifier::CTRL\n\n"
  210. "$EventModifier::CTRL\n\n"
  211. "$EventModifier::RALT\n\n"
  212. "$EventModifier::ALT\n\n"
  213. "@param mousePoint X/Y location of the mouse point\n"
  214. "@param mouseClickCount How many mouse clicks have occured for this event\n\n"
  215. "@tsexample\n"
  216. "// Right mouse button was released in this control, causing the callback\n"
  217. "GuiMouseEventCtrl::onRightMouseUp(%this,%modifier,%mousePoint,%mouseClickCount)\n"
  218. "{\n"
  219. " // Code to call when a mouse event occurs.\n"
  220. "}\n"
  221. "@endtsexample\n\n"
  222. "@see GuiControl\n\n"
  223. );
  224. IMPLEMENT_CALLBACK( GuiMouseEventCtrl, onRightMouseDragged, void, ( S32 modifier, Point2I mousePoint, S32 mouseClickCount ),
  225. ( modifier, mousePoint, mouseClickCount ),
  226. "@brief Callback that occurs whenever the mouse is dragged in this control while the right mouse button is pressed.\n\n"
  227. "@param modifier Key that was pressed during this callback. Values are:\n\n"
  228. "$EventModifier::RSHIFT\n\n"
  229. "$EventModifier::SHIFT\n\n"
  230. "$EventModifier::LCTRL\n\n"
  231. "$EventModifier::RCTRL\n\n"
  232. "$EventModifier::CTRL\n\n"
  233. "$EventModifier::CTRL\n\n"
  234. "$EventModifier::RALT\n\n"
  235. "$EventModifier::ALT\n\n"
  236. "@param mousePoint X/Y location of the mouse point\n"
  237. "@param mouseClickCount How many mouse clicks have occured for this event\n\n"
  238. "@tsexample\n"
  239. "// Right mouse button was dragged in this control, causing the callback\n"
  240. "GuiMouseEventCtrl::onRightMouseDragged(%this,%modifier,%mousePoint,%mouseClickCount)\n"
  241. "{\n"
  242. " // Code to call when a mouse event occurs.\n"
  243. "}\n"
  244. "@endtsexample\n\n"
  245. "@see GuiControl\n\n"
  246. );
  247. GuiMouseEventCtrl::GuiMouseEventCtrl()
  248. {
  249. mLockMouse = false;
  250. }
  251. //------------------------------------------------------------------------------
  252. void GuiMouseEventCtrl::sendMouseEvent(const char * name, const GuiEvent & event)
  253. {
  254. if(dStricmp(name,"onMouseDown") == 0)
  255. onMouseDown_callback(event.modifier, event.mousePoint, event.mouseClickCount);
  256. else if(dStricmp(name,"onMouseUp") == 0)
  257. onMouseUp_callback(event.modifier, event.mousePoint, event.mouseClickCount);
  258. else if(dStricmp(name,"onMouseMove") == 0)
  259. onMouseMove_callback(event.modifier, event.mousePoint, event.mouseClickCount);
  260. else if(dStricmp(name,"onMouseDragged") == 0)
  261. onMouseDragged_callback(event.modifier, event.mousePoint, event.mouseClickCount);
  262. else if(dStricmp(name,"onMouseEnter") == 0)
  263. onMouseEnter_callback(event.modifier, event.mousePoint, event.mouseClickCount);
  264. else if(dStricmp(name,"onMouseLeave") == 0)
  265. onMouseLeave_callback(event.modifier, event.mousePoint, event.mouseClickCount);
  266. else if(dStricmp(name,"onRightMouseDown") == 0)
  267. onRightMouseDown_callback(event.modifier, event.mousePoint, event.mouseClickCount);
  268. else if(dStricmp(name,"onRightMouseUp") == 0)
  269. onRightMouseUp_callback(event.modifier, event.mousePoint, event.mouseClickCount);
  270. else if(dStricmp(name,"onRightMouseDragged") == 0)
  271. onRightMouseDragged_callback(event.modifier, event.mousePoint, event.mouseClickCount);
  272. }
  273. //------------------------------------------------------------------------------
  274. void GuiMouseEventCtrl::initPersistFields()
  275. {
  276. addGroup( "Input" );
  277. addField("lockMouse", TypeBool, Offset(mLockMouse, GuiMouseEventCtrl),
  278. "Whether the control should lock the mouse between up and down button events." );
  279. endGroup( "Input" );
  280. Parent::initPersistFields();
  281. Con::setIntVariable("$EventModifier::LSHIFT", SI_LSHIFT);
  282. Con::setIntVariable("$EventModifier::RSHIFT", SI_RSHIFT);
  283. Con::setIntVariable("$EventModifier::SHIFT", SI_SHIFT);
  284. Con::setIntVariable("$EventModifier::LCTRL", SI_LCTRL);
  285. Con::setIntVariable("$EventModifier::RCTRL", SI_RCTRL);
  286. Con::setIntVariable("$EventModifier::CTRL", SI_CTRL);
  287. Con::setIntVariable("$EventModifier::LALT", SI_LALT);
  288. Con::setIntVariable("$EventModifier::RALT", SI_RALT);
  289. Con::setIntVariable("$EventModifier::ALT", SI_ALT);
  290. }
  291. //------------------------------------------------------------------------------
  292. void GuiMouseEventCtrl::onMouseDown(const GuiEvent & event)
  293. {
  294. if(mLockMouse)
  295. mouseLock();
  296. sendMouseEvent("onMouseDown", event);
  297. }
  298. void GuiMouseEventCtrl::onMouseUp(const GuiEvent & event)
  299. {
  300. if(mLockMouse)
  301. mouseUnlock();
  302. sendMouseEvent("onMouseUp", event);
  303. }
  304. void GuiMouseEventCtrl::onMouseMove(const GuiEvent & event)
  305. {
  306. sendMouseEvent("onMouseMove", event);
  307. }
  308. void GuiMouseEventCtrl::onMouseDragged(const GuiEvent & event)
  309. {
  310. sendMouseEvent("onMouseDragged", event);
  311. }
  312. void GuiMouseEventCtrl::onMouseEnter(const GuiEvent & event)
  313. {
  314. sendMouseEvent("onMouseEnter", event);
  315. }
  316. void GuiMouseEventCtrl::onMouseLeave(const GuiEvent & event)
  317. {
  318. sendMouseEvent("onMouseLeave", event);
  319. }
  320. void GuiMouseEventCtrl::onRightMouseDown(const GuiEvent & event)
  321. {
  322. if(mLockMouse)
  323. mouseLock();
  324. sendMouseEvent("onRightMouseDown", event);
  325. }
  326. void GuiMouseEventCtrl::onRightMouseUp(const GuiEvent & event)
  327. {
  328. if(mLockMouse)
  329. mouseUnlock();
  330. sendMouseEvent("onRightMouseUp", event);
  331. }
  332. void GuiMouseEventCtrl::onRightMouseDragged(const GuiEvent & event)
  333. {
  334. sendMouseEvent("onRightMouseDragged", event);
  335. }