UIInput.cpp 14 KB

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