UIInput.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 "../Graphics/Graphics.h"
  26. #include "../IO/Log.h"
  27. #include "../Input/Input.h"
  28. #include "../Input/InputEvents.h"
  29. #include "UI.h"
  30. #include "UIEvents.h"
  31. #include "UIOffscreenView.h"
  32. namespace Atomic
  33. {
  34. MODIFIER_KEYS GetModifierKeys(int qualifiers, bool superKey)
  35. {
  36. MODIFIER_KEYS code = TB_MODIFIER_NONE;
  37. if (qualifiers & QUAL_ALT) code |= TB_ALT;
  38. if (qualifiers & QUAL_CTRL) code |= TB_CTRL;
  39. if (qualifiers & QUAL_SHIFT) code |= TB_SHIFT;
  40. if (superKey) code |= TB_SUPER;
  41. return code;
  42. }
  43. SPECIAL_KEY GetSpecialKey(int keycode)
  44. {
  45. SPECIAL_KEY specialKey;
  46. switch (keycode)
  47. {
  48. case KEY_RETURN:
  49. case KEY_RETURN2:
  50. case KEY_KP_ENTER:
  51. specialKey = TB_KEY_ENTER;
  52. break;
  53. case KEY_F1:
  54. specialKey = TB_KEY_F1;
  55. break;
  56. case KEY_F2:
  57. specialKey = TB_KEY_F2;
  58. break;
  59. case KEY_F3:
  60. specialKey = TB_KEY_F3;
  61. break;
  62. case KEY_F4:
  63. specialKey = TB_KEY_F4;
  64. break;
  65. case KEY_F5:
  66. specialKey = TB_KEY_F5;
  67. break;
  68. case KEY_F6:
  69. specialKey = TB_KEY_F6;
  70. break;
  71. case KEY_F7:
  72. specialKey = TB_KEY_F7;
  73. break;
  74. case KEY_F8:
  75. specialKey = TB_KEY_F8;
  76. break;
  77. case KEY_F9:
  78. specialKey = TB_KEY_F9;
  79. break;
  80. case KEY_F10:
  81. specialKey = TB_KEY_F10;
  82. break;
  83. case KEY_F11:
  84. specialKey = TB_KEY_F11;
  85. break;
  86. case KEY_F12:
  87. specialKey = TB_KEY_F12;
  88. break;
  89. case KEY_LEFT:
  90. specialKey = TB_KEY_LEFT;
  91. break;
  92. case KEY_UP:
  93. specialKey = TB_KEY_UP;
  94. break;
  95. case KEY_RIGHT:
  96. specialKey = TB_KEY_RIGHT;
  97. break;
  98. case KEY_DOWN:
  99. specialKey = TB_KEY_DOWN;
  100. break;
  101. case KEY_PAGEUP:
  102. specialKey = TB_KEY_PAGE_UP;
  103. break;
  104. case KEY_PAGEDOWN:
  105. specialKey = TB_KEY_PAGE_DOWN;
  106. break;
  107. case KEY_HOME:
  108. specialKey = TB_KEY_HOME;
  109. break;
  110. case KEY_END:
  111. specialKey = TB_KEY_END;
  112. break;
  113. case KEY_INSERT:
  114. specialKey = TB_KEY_INSERT;
  115. break;
  116. case KEY_TAB:
  117. specialKey = TB_KEY_TAB;
  118. break;
  119. case KEY_DELETE:
  120. specialKey = TB_KEY_DELETE;
  121. break;
  122. case KEY_BACKSPACE:
  123. specialKey = TB_KEY_BACKSPACE;
  124. break;
  125. case KEY_ESCAPE:
  126. specialKey = TB_KEY_ESC;
  127. break;
  128. default:
  129. specialKey = TB_KEY_UNDEFINED;
  130. break;
  131. }
  132. return specialKey;
  133. }
  134. // @return Return the upper case of a ascii charcter. Only for shortcut handling.
  135. static int toupr_ascii(int ascii)
  136. {
  137. if (ascii >= 'a' && ascii <= 'z')
  138. return ascii + 'A' - 'a';
  139. return ascii;
  140. }
  141. UIOffscreenView* UI::GetOffscreenViewAtScreenPosition(const IntVector2& screenPos, IntVector2& viewPos)
  142. {
  143. for (HashSet<UIOffscreenView*>::Iterator it = offscreenViews_.Begin(); it != offscreenViews_.End(); ++it)
  144. {
  145. UIOffscreenView* osView = *it;
  146. IntRect rect = osView->inputRect_;
  147. Camera* camera = osView->inputCamera_;
  148. Octree* octree = osView->inputOctree_;
  149. Drawable* drawable = osView->inputDrawable_;
  150. bool rectIsDefault = rect == IntRect::ZERO;
  151. if (!camera || !octree || !drawable || (!rectIsDefault && !rect.IsInside(screenPos)))
  152. continue;
  153. Vector2 normPos(screenPos.x_ - rect.left_, screenPos.y_ - rect.top_);
  154. normPos /= rectIsDefault ? Vector2(graphics_->GetWidth(), graphics_->GetHeight()) : Vector2(rect.Width(), rect.Height());
  155. Ray ray(camera->GetScreenRay(normPos.x_, normPos.y_));
  156. PODVector<RayQueryResult> queryResultVector;
  157. RayOctreeQuery query(queryResultVector, ray, RAY_TRIANGLE_UV, M_INFINITY, DRAWABLE_GEOMETRY, DEFAULT_VIEWMASK);
  158. octree->RaycastSingle(query);
  159. if (queryResultVector.Empty())
  160. continue;
  161. RayQueryResult& queryResult(queryResultVector.Front());
  162. if (queryResult.drawable_ != drawable)
  163. continue;
  164. Vector2& uv = queryResult.textureUV_;
  165. viewPos = IntVector2(uv.x_ * osView->GetWidth(), uv.y_ * osView->GetHeight());
  166. return osView;
  167. }
  168. return nullptr;
  169. }
  170. tb::TBWidget* UI::GetInternalWidgetProjectedPosition(const IntVector2& screenPos, IntVector2& viewPos)
  171. {
  172. UIOffscreenView* osView = GetOffscreenViewAtScreenPosition(screenPos, viewPos);
  173. if (osView)
  174. return osView->GetInternalWidget();
  175. viewPos = screenPos;
  176. return rootWidget_;
  177. }
  178. void UI::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  179. {
  180. if (inputDisabled_ || consoleVisible_)
  181. return;
  182. using namespace MouseButtonDown;
  183. unsigned button = eventData[P_BUTTON].GetUInt();
  184. IntVector2 pos;
  185. pos = GetSubsystem<Input>()->GetMousePosition();
  186. Input* input = GetSubsystem<Input>();
  187. int qualifiers = input->GetQualifiers();
  188. #ifdef ATOMIC_PLATFORM_WINDOWS
  189. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  190. #else
  191. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  192. #endif
  193. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  194. static double last_time = 0;
  195. static int counter = 1;
  196. Time* t = GetSubsystem<Time>();
  197. double time = t->GetElapsedTime() * 1000;
  198. if (time < last_time + 600)
  199. counter++;
  200. else
  201. counter = 1;
  202. last_time = time;
  203. IntVector2 viewPos;
  204. tb::TBWidget* widget = UI::GetInternalWidgetProjectedPosition(pos, viewPos);
  205. if (button == MOUSEB_RIGHT)
  206. widget->InvokeRightPointerDown(viewPos.x_, viewPos.y_, counter, mod);
  207. else
  208. widget->InvokePointerDown(viewPos.x_, viewPos.y_, counter, mod, false);
  209. }
  210. void UI::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
  211. {
  212. if (inputDisabled_ || consoleVisible_)
  213. return;
  214. using namespace MouseButtonUp;
  215. unsigned button = eventData[P_BUTTON].GetUInt();
  216. IntVector2 pos;
  217. Input* input = GetSubsystem<Input>();
  218. pos = input->GetMousePosition();
  219. int qualifiers = input->GetQualifiers();
  220. #ifdef ATOMIC_PLATFORM_WINDOWS
  221. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  222. #else
  223. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  224. #endif
  225. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  226. IntVector2 viewPos;
  227. tb::TBWidget* widget = UI::GetInternalWidgetProjectedPosition(pos, viewPos);
  228. if (button == MOUSEB_RIGHT)
  229. widget->InvokeRightPointerUp(viewPos.x_, viewPos.y_, mod);
  230. else
  231. widget->InvokePointerUp(viewPos.x_, viewPos.y_, mod, false);
  232. // InvokePointerUp() seems to do the right thing no mater which root widget gets the call.
  233. }
  234. void UI::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  235. {
  236. using namespace MouseMove;
  237. if (inputDisabled_ || consoleVisible_)
  238. return;
  239. IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
  240. IntVector2 viewPos;
  241. tb::TBWidget* widget = UI::GetInternalWidgetProjectedPosition(pos, viewPos);
  242. widget->InvokePointerMove(viewPos.x_, viewPos.y_, tb::TB_MODIFIER_NONE, false);
  243. tooltipHoverTime_ = 0;
  244. }
  245. void UI::HandleMouseWheel(StringHash eventType, VariantMap& eventData)
  246. {
  247. if (inputDisabled_ || consoleVisible_)
  248. return;
  249. using namespace MouseWheel;
  250. int delta = eventData[P_WHEEL].GetInt();
  251. Input* input = GetSubsystem<Input>();
  252. IntVector2 pos(input->GetMousePosition().x_, input->GetMousePosition().y_);
  253. IntVector2 viewPos;
  254. tb::TBWidget* widget = UI::GetInternalWidgetProjectedPosition(pos, viewPos);
  255. widget->InvokeWheel(viewPos.x_, viewPos.y_, 0, -delta, tb::TB_MODIFIER_NONE);
  256. }
  257. //Touch Input
  258. void UI::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
  259. {
  260. if (inputDisabled_ || consoleVisible_)
  261. return;
  262. using namespace TouchBegin;
  263. int touchId = eventData[P_TOUCHID].GetInt();
  264. IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
  265. static double last_time = 0;
  266. static int counter = 1;
  267. Time* t = GetSubsystem<Time>();
  268. double time = t->GetElapsedTime() * 1000;
  269. if (time < last_time + 600)
  270. counter++;
  271. else
  272. counter = 1;
  273. last_time = time;
  274. IntVector2 viewPos;
  275. tb::TBWidget* widget = UI::GetInternalWidgetProjectedPosition(pos, viewPos);
  276. widget->InvokePointerDown(viewPos.x_, viewPos.y_, counter, TB_MODIFIER_NONE, true, touchId);
  277. }
  278. void UI::HandleTouchMove(StringHash eventType, VariantMap& eventData)
  279. {
  280. if (inputDisabled_ || consoleVisible_)
  281. return;
  282. using namespace TouchMove;
  283. int touchId = eventData[P_TOUCHID].GetInt();
  284. IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
  285. IntVector2 viewPos;
  286. tb::TBWidget* widget = UI::GetInternalWidgetProjectedPosition(pos, viewPos);
  287. widget->InvokePointerMove(viewPos.x_, viewPos.y_, TB_MODIFIER_NONE, true, touchId);
  288. }
  289. void UI::HandleTouchEnd(StringHash eventType, VariantMap& eventData)
  290. {
  291. if (inputDisabled_ || consoleVisible_)
  292. return;
  293. using namespace TouchEnd;
  294. int touchId = eventData[P_TOUCHID].GetInt();
  295. IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
  296. IntVector2 viewPos;
  297. tb::TBWidget* widget = UI::GetInternalWidgetProjectedPosition(pos, viewPos);
  298. widget->InvokePointerUp(viewPos.x_, viewPos.y_, TB_MODIFIER_NONE, true, touchId);
  299. }
  300. static bool InvokeShortcut(UI* ui, int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool down)
  301. {
  302. #ifdef __APPLE__
  303. bool shortcut_key = (modifierkeys & TB_SUPER) ? true : false;
  304. #else
  305. bool shortcut_key = (modifierkeys & TB_CTRL) ? true : false;
  306. #endif
  307. if (!down || (!shortcut_key && special_key ==TB_KEY_UNDEFINED))
  308. return false;
  309. bool reverse_key = (modifierkeys & TB_SHIFT) ? true : false;
  310. int upper_key = toupr_ascii(key);
  311. TBID id;
  312. if (upper_key == 'X')
  313. id = TBIDC("cut");
  314. else if (upper_key == 'C' || special_key == TB_KEY_INSERT)
  315. id = TBIDC("copy");
  316. else if (upper_key == 'V' || (special_key == TB_KEY_INSERT && reverse_key))
  317. id = TBIDC("paste");
  318. else if (upper_key == 'A')
  319. id = TBIDC("selectall");
  320. else if (upper_key == 'Z' || upper_key == 'Y')
  321. {
  322. bool undo = upper_key == 'Z';
  323. if (reverse_key)
  324. undo = !undo;
  325. id = undo ? TBIDC("undo") : TBIDC("redo");
  326. }
  327. else if (upper_key == 'N')
  328. id = TBIDC("new");
  329. else if (upper_key == 'O')
  330. id = TBIDC("open");
  331. else if (upper_key == 'S')
  332. id = TBIDC("save");
  333. else if (upper_key == 'W')
  334. id = TBIDC("close");
  335. else if (upper_key == 'F')
  336. id = TBIDC("find");
  337. #ifdef ATOMIC_PLATFORM_OSX
  338. else if (upper_key == 'G' && (modifierkeys & TB_SHIFT))
  339. id = TBIDC("findprev");
  340. else if (upper_key == 'G')
  341. id = TBIDC("findnext");
  342. #else
  343. else if (special_key == TB_KEY_F3 && (modifierkeys & TB_SHIFT))
  344. id = TBIDC("findprev");
  345. else if (special_key == TB_KEY_F3)
  346. id = TBIDC("findnext");
  347. #endif
  348. else if (upper_key == 'P')
  349. id = TBIDC("play");
  350. else if (special_key == TB_KEY_PAGE_UP)
  351. id = TBIDC("prev_doc");
  352. else if (special_key == TB_KEY_PAGE_DOWN)
  353. id = TBIDC("next_doc");
  354. else
  355. return false;
  356. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  357. ev.modifierkeys = modifierkeys;
  358. ev.ref_id = id;
  359. TBWidget* eventWidget = TBWidget::focused_widget;
  360. if (id == TBIDC("save") || id == TBIDC("close")) {
  361. while (eventWidget && !eventWidget->GetDelegate()) {
  362. eventWidget = eventWidget->GetParent();
  363. }
  364. }
  365. if (!eventWidget || !eventWidget->InvokeEvent(ev))
  366. {
  367. VariantMap evData;
  368. evData[UIUnhandledShortcut::P_REFID] = id;
  369. ui->SendEvent(E_UIUNHANDLEDSHORTCUT, evData);
  370. return false;
  371. }
  372. return true;
  373. }
  374. bool UI::InvokeKey(unsigned key, unsigned special_key, unsigned modifierkeys, bool keydown)
  375. {
  376. if (InvokeShortcut(this, key, SPECIAL_KEY(special_key), MODIFIER_KEYS(modifierkeys), keydown))
  377. return true;
  378. for (HashSet<UIOffscreenView*>::Iterator it = offscreenViews_.Begin(); it != offscreenViews_.End(); ++it)
  379. {
  380. UIOffscreenView* osView = *it;
  381. IntRect rect = osView->inputRect_;
  382. Camera* camera = osView->inputCamera_;
  383. Octree* octree = osView->inputOctree_;
  384. Drawable* drawable = osView->inputDrawable_;
  385. if (!camera || !octree || !drawable)
  386. continue;
  387. if (osView->GetInternalWidget()->InvokeKey(key, SPECIAL_KEY(special_key), MODIFIER_KEYS(modifierkeys), keydown))
  388. return true;
  389. }
  390. return rootWidget_->InvokeKey(key, SPECIAL_KEY(special_key), MODIFIER_KEYS(modifierkeys), keydown);
  391. }
  392. void UI::HandleKey(bool keydown, int keycode, int scancode)
  393. {
  394. if (keydown && (keycode == KEY_ESCAPE || keycode == KEY_RETURN || keycode == KEY_RETURN2 || keycode == KEY_KP_ENTER)
  395. && TBWidget::focused_widget)
  396. {
  397. SendEvent(E_UIWIDGETFOCUSESCAPED);
  398. }
  399. #ifdef ATOMIC_PLATFORM_WINDOWS
  400. if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
  401. return;
  402. #else
  403. if (keycode == KEY_LGUI || keycode == KEY_RGUI)
  404. return;
  405. #endif
  406. Input* input = GetSubsystem<Input>();
  407. int qualifiers = input->GetQualifiers();
  408. #ifdef ATOMIC_PLATFORM_WINDOWS
  409. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  410. #else
  411. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  412. #endif
  413. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  414. SPECIAL_KEY specialKey = GetSpecialKey(keycode);
  415. if (specialKey == TB_KEY_UNDEFINED)
  416. {
  417. if (mod & TB_SUPER)
  418. {
  419. InvokeKey(keycode, TB_KEY_UNDEFINED, mod, keydown);
  420. }
  421. }
  422. else
  423. {
  424. InvokeKey(0, specialKey, mod, keydown);
  425. }
  426. }
  427. void UI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  428. {
  429. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  430. return;
  431. using namespace KeyDown;
  432. int keycode = eventData[P_KEY].GetInt();
  433. int scancode = eventData[P_SCANCODE].GetInt();
  434. HandleKey(true, keycode, scancode);
  435. // Send Global Shortcut
  436. Input* input = GetSubsystem<Input>();
  437. #ifdef ATOMIC_PLATFORM_WINDOWS
  438. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  439. if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
  440. superdown = false;
  441. #else
  442. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  443. if (keycode == KEY_LGUI || keycode == KEY_RGUI)
  444. superdown = false;
  445. #endif
  446. if (!superdown)
  447. return;
  448. VariantMap shortcutData;
  449. shortcutData[UIShortcut::P_KEY] = keycode;
  450. shortcutData[UIShortcut::P_QUALIFIERS] = eventData[P_QUALIFIERS].GetInt();
  451. SendEvent(E_UISHORTCUT, shortcutData);
  452. }
  453. void UI::HandleKeyUp(StringHash eventType, VariantMap& eventData)
  454. {
  455. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  456. return;
  457. using namespace KeyUp;
  458. int keycode = eventData[P_KEY].GetInt();
  459. int scancode = eventData[P_SCANCODE].GetInt();
  460. HandleKey(false, keycode, scancode);
  461. }
  462. void UI::HandleTextInput(StringHash eventType, VariantMap& eventData)
  463. {
  464. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  465. return;
  466. using namespace TextInput;
  467. const String& text = eventData[P_TEXT].GetString();
  468. for (unsigned i = 0; i < text.Length(); i++)
  469. {
  470. InvokeKey(text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, true);
  471. InvokeKey(text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, false);
  472. }
  473. }
  474. }