UIInput.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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(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 (!TBWidget::focused_widget || !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)
  222. return false;
  223. return eventWidget->InvokeEvent(ev);
  224. }
  225. static bool InvokeKey(TBWidget* root, unsigned int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool keydown)
  226. {
  227. if (InvokeShortcut(key, special_key, modifierkeys, keydown))
  228. return true;
  229. root->InvokeKey(key, special_key, modifierkeys, keydown);
  230. return true;
  231. }
  232. void UI::HandleKey(bool keydown, int keycode, int scancode)
  233. {
  234. #ifdef ATOMIC_PLATFORM_WINDOWS
  235. if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
  236. return;
  237. #else
  238. if (keycode == KEY_LGUI || keycode == KEY_RGUI)
  239. return;
  240. #endif
  241. Input* input = GetSubsystem<Input>();
  242. int qualifiers = input->GetQualifiers();
  243. #ifdef ATOMIC_PLATFORM_WINDOWS
  244. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  245. #else
  246. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  247. #endif
  248. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  249. switch (keycode)
  250. {
  251. case KEY_RETURN:
  252. case KEY_RETURN2:
  253. case KEY_KP_ENTER:
  254. InvokeKey(rootWidget_, 0, TB_KEY_ENTER, mod, keydown);
  255. break;
  256. case KEY_F1:
  257. InvokeKey(rootWidget_, 0, TB_KEY_F1, mod, keydown);
  258. break;
  259. case KEY_F2:
  260. InvokeKey(rootWidget_, 0, TB_KEY_F2, mod, keydown);
  261. break;
  262. case KEY_F3:
  263. InvokeKey(rootWidget_, 0, TB_KEY_F3, mod, keydown);
  264. break;
  265. case KEY_F4:
  266. InvokeKey(rootWidget_, 0, TB_KEY_F4, mod, keydown);
  267. break;
  268. case KEY_F5:
  269. InvokeKey(rootWidget_, 0, TB_KEY_F5, mod, keydown);
  270. break;
  271. case KEY_F6:
  272. InvokeKey(rootWidget_, 0, TB_KEY_F6, mod, keydown);
  273. break;
  274. case KEY_F7:
  275. InvokeKey(rootWidget_, 0, TB_KEY_F7, mod, keydown);
  276. break;
  277. case KEY_F8:
  278. InvokeKey(rootWidget_, 0, TB_KEY_F8, mod, keydown);
  279. break;
  280. case KEY_F9:
  281. InvokeKey(rootWidget_, 0, TB_KEY_F9, mod, keydown);
  282. break;
  283. case KEY_F10:
  284. InvokeKey(rootWidget_, 0, TB_KEY_F10, mod, keydown);
  285. break;
  286. case KEY_F11:
  287. InvokeKey(rootWidget_, 0, TB_KEY_F11, mod, keydown);
  288. break;
  289. case KEY_F12:
  290. InvokeKey(rootWidget_, 0, TB_KEY_F12, mod, keydown);
  291. break;
  292. case KEY_LEFT:
  293. InvokeKey(rootWidget_, 0, TB_KEY_LEFT, mod, keydown);
  294. break;
  295. case KEY_UP:
  296. InvokeKey(rootWidget_, 0, TB_KEY_UP, mod, keydown);
  297. break;
  298. case KEY_RIGHT:
  299. InvokeKey(rootWidget_, 0, TB_KEY_RIGHT, mod, keydown);
  300. break;
  301. case KEY_DOWN:
  302. InvokeKey(rootWidget_, 0, TB_KEY_DOWN, mod, keydown);
  303. break;
  304. case KEY_PAGEUP:
  305. InvokeKey(rootWidget_, 0, TB_KEY_PAGE_UP, mod, keydown);
  306. break;
  307. case KEY_PAGEDOWN:
  308. InvokeKey(rootWidget_, 0, TB_KEY_PAGE_DOWN, mod, keydown);
  309. break;
  310. case KEY_HOME:
  311. InvokeKey(rootWidget_, 0, TB_KEY_HOME, mod, keydown);
  312. break;
  313. case KEY_END:
  314. InvokeKey(rootWidget_, 0, TB_KEY_END, mod, keydown);
  315. break;
  316. case KEY_INSERT:
  317. InvokeKey(rootWidget_, 0, TB_KEY_INSERT, mod, keydown);
  318. break;
  319. case KEY_TAB:
  320. InvokeKey(rootWidget_, 0, TB_KEY_TAB, mod, keydown);
  321. break;
  322. case KEY_DELETE:
  323. InvokeKey(rootWidget_, 0, TB_KEY_DELETE, mod, keydown);
  324. break;
  325. case KEY_BACKSPACE:
  326. InvokeKey(rootWidget_, 0, TB_KEY_BACKSPACE, mod, keydown);
  327. break;
  328. case KEY_ESC:
  329. InvokeKey(rootWidget_, 0, TB_KEY_ESC, mod, keydown);
  330. break;
  331. default:
  332. if (mod & TB_SUPER)
  333. {
  334. InvokeKey(rootWidget_, keycode, TB_KEY_UNDEFINED, mod, keydown);
  335. }
  336. }
  337. }
  338. void UI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  339. {
  340. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  341. return;
  342. using namespace KeyDown;
  343. int keycode = eventData[P_KEY].GetInt();
  344. int scancode = eventData[P_SCANCODE].GetInt();
  345. HandleKey(true, keycode, scancode);
  346. // Send Global Shortcut
  347. Input* input = GetSubsystem<Input>();
  348. #ifdef ATOMIC_PLATFORM_WINDOWS
  349. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  350. if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
  351. superdown = false;
  352. #else
  353. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  354. if (keycode == KEY_LGUI || keycode == KEY_RGUI)
  355. superdown = false;
  356. #endif
  357. if (!superdown)
  358. return;
  359. VariantMap shortcutData;
  360. shortcutData[UIShortcut::P_KEY] = keycode;
  361. shortcutData[UIShortcut::P_QUALIFIERS] = eventData[P_QUALIFIERS].GetInt();
  362. SendEvent(E_UISHORTCUT, shortcutData);
  363. }
  364. void UI::HandleKeyUp(StringHash eventType, VariantMap& eventData)
  365. {
  366. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  367. return;
  368. using namespace KeyUp;
  369. int keycode = eventData[P_KEY].GetInt();
  370. int scancode = eventData[P_SCANCODE].GetInt();
  371. HandleKey(false, keycode, scancode);
  372. }
  373. void UI::HandleTextInput(StringHash eventType, VariantMap& eventData)
  374. {
  375. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  376. return;
  377. using namespace TextInput;
  378. const String& text = eventData[P_TEXT].GetString();
  379. for (unsigned i = 0; i < text.Length(); i++)
  380. {
  381. InvokeKey(rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, true);
  382. InvokeKey(rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, false);
  383. }
  384. }
  385. }