UIInput.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. //Touch Input
  117. void UI::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
  118. {
  119. if (inputDisabled_ || consoleVisible_)
  120. return;
  121. using namespace TouchBegin;
  122. int touchId = eventData[P_TOUCHID].GetInt();
  123. int px = eventData[P_X].GetInt();
  124. int py = eventData[P_Y].GetInt();
  125. static double last_time = 0;
  126. static int counter = 1;
  127. Time* t = GetSubsystem<Time>();
  128. double time = t->GetElapsedTime() * 1000;
  129. if (time < last_time + 600)
  130. counter++;
  131. else
  132. counter = 1;
  133. last_time = time;
  134. rootWidget_->InvokePointerDown(px, py, counter, TB_MODIFIER_NONE, true, touchId);
  135. }
  136. void UI::HandleTouchMove(StringHash eventType, VariantMap& eventData)
  137. {
  138. if (inputDisabled_ || consoleVisible_)
  139. return;
  140. using namespace TouchMove;
  141. int touchId = eventData[P_TOUCHID].GetInt();
  142. int px = eventData[P_X].GetInt();
  143. int py = eventData[P_Y].GetInt();
  144. rootWidget_->InvokePointerMove(px, py, TB_MODIFIER_NONE, true, touchId);
  145. }
  146. void UI::HandleTouchEnd(StringHash eventType, VariantMap& eventData)
  147. {
  148. if (inputDisabled_ || consoleVisible_)
  149. return;
  150. using namespace TouchEnd;
  151. int touchId = eventData[P_TOUCHID].GetInt();
  152. int px = eventData[P_X].GetInt();
  153. int py = eventData[P_Y].GetInt();
  154. rootWidget_->InvokePointerUp(px, py, TB_MODIFIER_NONE, true, touchId);
  155. }
  156. static bool InvokeShortcut(UI* ui, int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool down)
  157. {
  158. #ifdef __APPLE__
  159. bool shortcut_key = (modifierkeys & TB_SUPER) ? true : false;
  160. #else
  161. bool shortcut_key = (modifierkeys & TB_CTRL) ? true : false;
  162. #endif
  163. if (!down || (!shortcut_key && special_key ==TB_KEY_UNDEFINED))
  164. return false;
  165. bool reverse_key = (modifierkeys & TB_SHIFT) ? true : false;
  166. int upper_key = toupr_ascii(key);
  167. TBID id;
  168. if (upper_key == 'X')
  169. id = TBIDC("cut");
  170. else if (upper_key == 'C' || special_key == TB_KEY_INSERT)
  171. id = TBIDC("copy");
  172. else if (upper_key == 'V' || (special_key == TB_KEY_INSERT && reverse_key))
  173. id = TBIDC("paste");
  174. else if (upper_key == 'A')
  175. id = TBIDC("selectall");
  176. else if (upper_key == 'Z' || upper_key == 'Y')
  177. {
  178. bool undo = upper_key == 'Z';
  179. if (reverse_key)
  180. undo = !undo;
  181. id = undo ? TBIDC("undo") : TBIDC("redo");
  182. }
  183. else if (upper_key == 'N')
  184. id = TBIDC("new");
  185. else if (upper_key == 'O')
  186. id = TBIDC("open");
  187. else if (upper_key == 'S')
  188. id = TBIDC("save");
  189. else if (upper_key == 'W')
  190. id = TBIDC("close");
  191. else if (upper_key == 'F')
  192. id = TBIDC("find");
  193. #ifdef ATOMIC_PLATFORM_OSX
  194. else if (upper_key == 'G' && (modifierkeys & TB_SHIFT))
  195. id = TBIDC("findprev");
  196. else if (upper_key == 'G')
  197. id = TBIDC("findnext");
  198. #else
  199. else if (special_key == TB_KEY_F3 && (modifierkeys & TB_SHIFT))
  200. id = TBIDC("findprev");
  201. else if (special_key == TB_KEY_F3)
  202. id = TBIDC("findnext");
  203. #endif
  204. else if (upper_key == 'P')
  205. id = TBIDC("play");
  206. else if (special_key == TB_KEY_PAGE_UP)
  207. id = TBIDC("prev_doc");
  208. else if (special_key == TB_KEY_PAGE_DOWN)
  209. id = TBIDC("next_doc");
  210. else
  211. return false;
  212. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  213. ev.modifierkeys = modifierkeys;
  214. ev.ref_id = id;
  215. TBWidget* eventWidget = TBWidget::focused_widget;
  216. if (id == TBIDC("save") || id == TBIDC("close")) {
  217. while (eventWidget && !eventWidget->GetDelegate()) {
  218. eventWidget = eventWidget->GetParent();
  219. }
  220. }
  221. if (!eventWidget || !eventWidget->InvokeEvent(ev))
  222. {
  223. VariantMap evData;
  224. evData[UIUnhandledShortcut::P_REFID] = id;
  225. ui->SendEvent(E_UIUNHANDLEDSHORTCUT, evData);
  226. return false;
  227. }
  228. return true;
  229. }
  230. static bool InvokeKey(UI* ui, TBWidget* root, unsigned int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool keydown)
  231. {
  232. if (InvokeShortcut(ui, key, special_key, modifierkeys, keydown))
  233. return true;
  234. root->InvokeKey(key, special_key, modifierkeys, keydown);
  235. return true;
  236. }
  237. void UI::HandleKey(bool keydown, int keycode, int scancode)
  238. {
  239. if (keydown && (keycode == KEY_ESC || keycode == KEY_RETURN || keycode == KEY_RETURN2 || keycode == KEY_KP_ENTER)
  240. && TBWidget::focused_widget)
  241. {
  242. SendEvent(E_UIWIDGETFOCUSESCAPED);
  243. }
  244. #ifdef ATOMIC_PLATFORM_WINDOWS
  245. if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
  246. return;
  247. #else
  248. if (keycode == KEY_LGUI || keycode == KEY_RGUI)
  249. return;
  250. #endif
  251. Input* input = GetSubsystem<Input>();
  252. int qualifiers = input->GetQualifiers();
  253. #ifdef ATOMIC_PLATFORM_WINDOWS
  254. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  255. #else
  256. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  257. #endif
  258. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  259. SPECIAL_KEY specialKey = TB_KEY_UNDEFINED;
  260. switch (keycode)
  261. {
  262. case KEY_RETURN:
  263. case KEY_RETURN2:
  264. case KEY_KP_ENTER:
  265. specialKey = TB_KEY_ENTER;
  266. break;
  267. case KEY_F1:
  268. specialKey = TB_KEY_F1;
  269. break;
  270. case KEY_F2:
  271. specialKey = TB_KEY_F2;
  272. break;
  273. case KEY_F3:
  274. specialKey = TB_KEY_F3;
  275. break;
  276. case KEY_F4:
  277. specialKey = TB_KEY_F4;
  278. break;
  279. case KEY_F5:
  280. specialKey = TB_KEY_F5;
  281. break;
  282. case KEY_F6:
  283. specialKey = TB_KEY_F6;
  284. break;
  285. case KEY_F7:
  286. specialKey = TB_KEY_F7;
  287. break;
  288. case KEY_F8:
  289. specialKey = TB_KEY_F8;
  290. break;
  291. case KEY_F9:
  292. specialKey = TB_KEY_F9;
  293. break;
  294. case KEY_F10:
  295. specialKey = TB_KEY_F10;
  296. break;
  297. case KEY_F11:
  298. specialKey = TB_KEY_F11;
  299. break;
  300. case KEY_F12:
  301. specialKey = TB_KEY_F12;
  302. break;
  303. case KEY_LEFT:
  304. specialKey = TB_KEY_LEFT;
  305. break;
  306. case KEY_UP:
  307. specialKey = TB_KEY_UP;
  308. break;
  309. case KEY_RIGHT:
  310. specialKey = TB_KEY_RIGHT;
  311. break;
  312. case KEY_DOWN:
  313. specialKey = TB_KEY_DOWN;
  314. break;
  315. case KEY_PAGEUP:
  316. specialKey = TB_KEY_PAGE_UP;
  317. break;
  318. case KEY_PAGEDOWN:
  319. specialKey = TB_KEY_PAGE_DOWN;
  320. break;
  321. case KEY_HOME:
  322. specialKey = TB_KEY_HOME;
  323. break;
  324. case KEY_END:
  325. specialKey = TB_KEY_END;
  326. break;
  327. case KEY_INSERT:
  328. specialKey = TB_KEY_INSERT;
  329. break;
  330. case KEY_TAB:
  331. specialKey = TB_KEY_TAB;
  332. break;
  333. case KEY_DELETE:
  334. specialKey = TB_KEY_DELETE;
  335. break;
  336. case KEY_BACKSPACE:
  337. specialKey = TB_KEY_BACKSPACE;
  338. break;
  339. case KEY_ESC:
  340. specialKey = TB_KEY_ESC;
  341. break;
  342. }
  343. if (specialKey == TB_KEY_UNDEFINED)
  344. {
  345. if (mod & TB_SUPER)
  346. {
  347. InvokeKey(this, rootWidget_, keycode, TB_KEY_UNDEFINED, mod, keydown);
  348. }
  349. }
  350. else
  351. {
  352. InvokeKey(this, rootWidget_, 0, specialKey, mod, keydown);
  353. }
  354. }
  355. void UI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  356. {
  357. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  358. return;
  359. using namespace KeyDown;
  360. int keycode = eventData[P_KEY].GetInt();
  361. int scancode = eventData[P_SCANCODE].GetInt();
  362. HandleKey(true, keycode, scancode);
  363. // Send Global Shortcut
  364. Input* input = GetSubsystem<Input>();
  365. #ifdef ATOMIC_PLATFORM_WINDOWS
  366. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  367. if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
  368. superdown = false;
  369. #else
  370. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  371. if (keycode == KEY_LGUI || keycode == KEY_RGUI)
  372. superdown = false;
  373. #endif
  374. if (!superdown)
  375. return;
  376. VariantMap shortcutData;
  377. shortcutData[UIShortcut::P_KEY] = keycode;
  378. shortcutData[UIShortcut::P_QUALIFIERS] = eventData[P_QUALIFIERS].GetInt();
  379. SendEvent(E_UISHORTCUT, shortcutData);
  380. }
  381. void UI::HandleKeyUp(StringHash eventType, VariantMap& eventData)
  382. {
  383. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  384. return;
  385. using namespace KeyUp;
  386. int keycode = eventData[P_KEY].GetInt();
  387. int scancode = eventData[P_SCANCODE].GetInt();
  388. HandleKey(false, keycode, scancode);
  389. }
  390. void UI::HandleTextInput(StringHash eventType, VariantMap& eventData)
  391. {
  392. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  393. return;
  394. using namespace TextInput;
  395. const String& text = eventData[P_TEXT].GetString();
  396. for (unsigned i = 0; i < text.Length(); i++)
  397. {
  398. InvokeKey(this, rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, true);
  399. InvokeKey(this, rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, false);
  400. }
  401. }
  402. }