UIInput.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_widgets.h>
  23. using namespace tb;
  24. #include "../Core/Timer.h"
  25. #include "../Input/Input.h"
  26. #include "../Input/InputEvents.h"
  27. #include "UI.h"
  28. #include "UIEvents.h"
  29. namespace Atomic
  30. {
  31. static MODIFIER_KEYS GetModifierKeys(int qualifiers, bool superKey)
  32. {
  33. MODIFIER_KEYS code = TB_MODIFIER_NONE;
  34. if (qualifiers & QUAL_ALT) code |= TB_ALT;
  35. if (qualifiers & QUAL_CTRL) code |= TB_CTRL;
  36. if (qualifiers & QUAL_SHIFT) code |= TB_SHIFT;
  37. if (superKey) code |= TB_SUPER;
  38. return code;
  39. }
  40. // @return Return the upper case of a ascii charcter. Only for shortcut handling.
  41. static int toupr_ascii(int ascii)
  42. {
  43. if (ascii >= 'a' && ascii <= 'z')
  44. return ascii + 'A' - 'a';
  45. return ascii;
  46. }
  47. void UI::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  48. {
  49. if (inputDisabled_ || consoleVisible_)
  50. return;
  51. using namespace MouseButtonDown;
  52. unsigned button = eventData[P_BUTTON].GetUInt();
  53. IntVector2 pos;
  54. pos = GetSubsystem<Input>()->GetMousePosition();
  55. Input* input = GetSubsystem<Input>();
  56. int qualifiers = input->GetQualifiers();
  57. #ifdef ATOMIC_PLATFORM_WINDOWS
  58. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  59. #else
  60. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  61. #endif
  62. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  63. static double last_time = 0;
  64. static int counter = 1;
  65. Time* t = GetSubsystem<Time>();
  66. double time = t->GetElapsedTime() * 1000;
  67. if (time < last_time + 600)
  68. counter++;
  69. else
  70. counter = 1;
  71. last_time = time;
  72. if (button == MOUSEB_RIGHT)
  73. rootWidget_->InvokeRightPointerDown(pos.x_, pos.y_, counter, mod);
  74. else
  75. rootWidget_->InvokePointerDown(pos.x_, pos.y_, counter, mod, false);
  76. }
  77. void UI::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
  78. {
  79. if (inputDisabled_ || consoleVisible_)
  80. return;
  81. using namespace MouseButtonUp;
  82. unsigned button = eventData[P_BUTTON].GetUInt();
  83. IntVector2 pos;
  84. Input* input = GetSubsystem<Input>();
  85. pos = input->GetMousePosition();
  86. int qualifiers = input->GetQualifiers();
  87. #ifdef ATOMIC_PLATFORM_WINDOWS
  88. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  89. #else
  90. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  91. #endif
  92. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  93. if (button == MOUSEB_RIGHT)
  94. rootWidget_->InvokeRightPointerUp(pos.x_, pos.y_, mod);
  95. else
  96. rootWidget_->InvokePointerUp(pos.x_, pos.y_, mod, false);
  97. }
  98. void UI::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  99. {
  100. using namespace MouseMove;
  101. if (inputDisabled_ || consoleVisible_)
  102. return;
  103. int px = eventData[P_X].GetInt();
  104. int py = eventData[P_Y].GetInt();
  105. rootWidget_->InvokePointerMove(px, py, tb::TB_MODIFIER_NONE, false);
  106. }
  107. void UI::HandleMouseWheel(StringHash eventType, VariantMap& eventData)
  108. {
  109. if (inputDisabled_ || consoleVisible_)
  110. return;
  111. using namespace MouseWheel;
  112. int delta = eventData[P_WHEEL].GetInt();
  113. Input* input = GetSubsystem<Input>();
  114. rootWidget_->InvokeWheel(input->GetMousePosition().x_, input->GetMousePosition().y_, 0, delta > 0 ? -1 : 1, tb::TB_MODIFIER_NONE);
  115. }
  116. static bool InvokeShortcut(int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool down)
  117. {
  118. #ifdef __APPLE__
  119. bool shortcut_key = (modifierkeys & TB_SUPER) ? true : false;
  120. #else
  121. bool shortcut_key = (modifierkeys & TB_CTRL) ? true : false;
  122. #endif
  123. if (!TBWidget::focused_widget || !down || (!shortcut_key && special_key ==TB_KEY_UNDEFINED))
  124. return false;
  125. bool reverse_key = (modifierkeys & TB_SHIFT) ? true : false;
  126. int upper_key = toupr_ascii(key);
  127. TBID id;
  128. if (upper_key == 'X')
  129. id = TBIDC("cut");
  130. else if (upper_key == 'C' || special_key == TB_KEY_INSERT)
  131. id = TBIDC("copy");
  132. else if (upper_key == 'V' || (special_key == TB_KEY_INSERT && reverse_key))
  133. id = TBIDC("paste");
  134. else if (upper_key == 'A')
  135. id = TBIDC("selectall");
  136. else if (upper_key == 'Z' || upper_key == 'Y')
  137. {
  138. bool undo = upper_key == 'Z';
  139. if (reverse_key)
  140. undo = !undo;
  141. id = undo ? TBIDC("undo") : TBIDC("redo");
  142. }
  143. else if (upper_key == 'N')
  144. id = TBIDC("new");
  145. else if (upper_key == 'O')
  146. id = TBIDC("open");
  147. else if (upper_key == 'S')
  148. id = TBIDC("save");
  149. else if (upper_key == 'W')
  150. id = TBIDC("close");
  151. else if (upper_key == 'F')
  152. id = TBIDC("find");
  153. #ifdef ATOMIC_PLATFORM_OSX
  154. else if (upper_key == 'G' && (modifierkeys & TB_SHIFT))
  155. id = TBIDC("findprev");
  156. else if (upper_key == 'G')
  157. id = TBIDC("findnext");
  158. #else
  159. else if (special_key == TB_KEY_F3 && (modifierkeys & TB_SHIFT))
  160. id = TBIDC("findprev");
  161. else if (special_key == TB_KEY_F3)
  162. id = TBIDC("findnext");
  163. #endif
  164. else if (upper_key == 'P')
  165. id = TBIDC("play");
  166. else if (special_key == TB_KEY_PAGE_UP)
  167. id = TBIDC("prev_doc");
  168. else if (special_key == TB_KEY_PAGE_DOWN)
  169. id = TBIDC("next_doc");
  170. else
  171. return false;
  172. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  173. ev.modifierkeys = modifierkeys;
  174. ev.ref_id = id;
  175. TBWidget* eventWidget = TBWidget::focused_widget;
  176. if (id == TBIDC("save") || id == TBIDC("close")) {
  177. while (eventWidget && !eventWidget->GetDelegate()) {
  178. eventWidget = eventWidget->GetParent();
  179. }
  180. }
  181. if (!eventWidget)
  182. return false;
  183. return eventWidget->InvokeEvent(ev);
  184. }
  185. static bool InvokeKey(TBWidget* root, unsigned int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool keydown)
  186. {
  187. if (InvokeShortcut(key, special_key, modifierkeys, keydown))
  188. return true;
  189. root->InvokeKey(key, special_key, modifierkeys, keydown);
  190. return true;
  191. }
  192. void UI::HandleKey(bool keydown, int keycode, int scancode)
  193. {
  194. #ifdef ATOMIC_PLATFORM_WINDOWS
  195. if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
  196. return;
  197. #else
  198. if (keycode == KEY_LGUI || keycode == KEY_RGUI)
  199. return;
  200. #endif
  201. Input* input = GetSubsystem<Input>();
  202. int qualifiers = input->GetQualifiers();
  203. #ifdef ATOMIC_PLATFORM_WINDOWS
  204. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  205. #else
  206. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  207. #endif
  208. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  209. switch (keycode)
  210. {
  211. case KEY_RETURN:
  212. case KEY_RETURN2:
  213. case KEY_KP_ENTER:
  214. InvokeKey(rootWidget_, 0, TB_KEY_ENTER, mod, keydown);
  215. break;
  216. case KEY_F1:
  217. InvokeKey(rootWidget_, 0, TB_KEY_F1, mod, keydown);
  218. break;
  219. case KEY_F2:
  220. InvokeKey(rootWidget_, 0, TB_KEY_F2, mod, keydown);
  221. break;
  222. case KEY_F3:
  223. InvokeKey(rootWidget_, 0, TB_KEY_F3, mod, keydown);
  224. break;
  225. case KEY_F4:
  226. InvokeKey(rootWidget_, 0, TB_KEY_F4, mod, keydown);
  227. break;
  228. case KEY_F5:
  229. InvokeKey(rootWidget_, 0, TB_KEY_F5, mod, keydown);
  230. break;
  231. case KEY_F6:
  232. InvokeKey(rootWidget_, 0, TB_KEY_F6, mod, keydown);
  233. break;
  234. case KEY_F7:
  235. InvokeKey(rootWidget_, 0, TB_KEY_F7, mod, keydown);
  236. break;
  237. case KEY_F8:
  238. InvokeKey(rootWidget_, 0, TB_KEY_F8, mod, keydown);
  239. break;
  240. case KEY_F9:
  241. InvokeKey(rootWidget_, 0, TB_KEY_F9, mod, keydown);
  242. break;
  243. case KEY_F10:
  244. InvokeKey(rootWidget_, 0, TB_KEY_F10, mod, keydown);
  245. break;
  246. case KEY_F11:
  247. InvokeKey(rootWidget_, 0, TB_KEY_F11, mod, keydown);
  248. break;
  249. case KEY_F12:
  250. InvokeKey(rootWidget_, 0, TB_KEY_F12, mod, keydown);
  251. break;
  252. case KEY_LEFT:
  253. InvokeKey(rootWidget_, 0, TB_KEY_LEFT, mod, keydown);
  254. break;
  255. case KEY_UP:
  256. InvokeKey(rootWidget_, 0, TB_KEY_UP, mod, keydown);
  257. break;
  258. case KEY_RIGHT:
  259. InvokeKey(rootWidget_, 0, TB_KEY_RIGHT, mod, keydown);
  260. break;
  261. case KEY_DOWN:
  262. InvokeKey(rootWidget_, 0, TB_KEY_DOWN, mod, keydown);
  263. break;
  264. case KEY_PAGEUP:
  265. InvokeKey(rootWidget_, 0, TB_KEY_PAGE_UP, mod, keydown);
  266. break;
  267. case KEY_PAGEDOWN:
  268. InvokeKey(rootWidget_, 0, TB_KEY_PAGE_DOWN, mod, keydown);
  269. break;
  270. case KEY_HOME:
  271. InvokeKey(rootWidget_, 0, TB_KEY_HOME, mod, keydown);
  272. break;
  273. case KEY_END:
  274. InvokeKey(rootWidget_, 0, TB_KEY_END, mod, keydown);
  275. break;
  276. case KEY_INSERT:
  277. InvokeKey(rootWidget_, 0, TB_KEY_INSERT, mod, keydown);
  278. break;
  279. case KEY_TAB:
  280. InvokeKey(rootWidget_, 0, TB_KEY_TAB, mod, keydown);
  281. break;
  282. case KEY_DELETE:
  283. InvokeKey(rootWidget_, 0, TB_KEY_DELETE, mod, keydown);
  284. break;
  285. case KEY_BACKSPACE:
  286. InvokeKey(rootWidget_, 0, TB_KEY_BACKSPACE, mod, keydown);
  287. break;
  288. case KEY_ESC:
  289. InvokeKey(rootWidget_, 0, TB_KEY_ESC, mod, keydown);
  290. break;
  291. default:
  292. if (mod & TB_SUPER)
  293. {
  294. InvokeKey(rootWidget_, keycode, TB_KEY_UNDEFINED, mod, keydown);
  295. }
  296. }
  297. }
  298. void UI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  299. {
  300. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  301. return;
  302. using namespace KeyDown;
  303. int keycode = eventData[P_KEY].GetInt();
  304. int scancode = eventData[P_SCANCODE].GetInt();
  305. HandleKey(true, keycode, scancode);
  306. // Send Global Shortcut
  307. Input* input = GetSubsystem<Input>();
  308. #ifdef ATOMIC_PLATFORM_WINDOWS
  309. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  310. if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
  311. superdown = false;
  312. #else
  313. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  314. if (keycode == KEY_LGUI || keycode == KEY_RGUI)
  315. superdown = false;
  316. #endif
  317. if (!superdown)
  318. return;
  319. VariantMap shortcutData;
  320. shortcutData[UIShortcut::P_KEY] = keycode;
  321. shortcutData[UIShortcut::P_QUALIFIERS] = eventData[P_QUALIFIERS].GetInt();
  322. SendEvent(E_UISHORTCUT, shortcutData);
  323. }
  324. void UI::HandleKeyUp(StringHash eventType, VariantMap& eventData)
  325. {
  326. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  327. return;
  328. using namespace KeyUp;
  329. int keycode = eventData[P_KEY].GetInt();
  330. int scancode = eventData[P_SCANCODE].GetInt();
  331. HandleKey(false, keycode, scancode);
  332. }
  333. void UI::HandleTextInput(StringHash eventType, VariantMap& eventData)
  334. {
  335. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  336. return;
  337. using namespace TextInput;
  338. const String& text = eventData[P_TEXT].GetString();
  339. for (unsigned i = 0; i < text.Length(); i++)
  340. {
  341. InvokeKey(rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, true);
  342. InvokeKey(rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, false);
  343. }
  344. }
  345. }