UIInput.cpp 16 KB

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