UIInput.cpp 13 KB

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