UIInput.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #include <TurboBadger/tb_widgets.h>
  2. using namespace tb;
  3. #include "../Core/Timer.h"
  4. #include "../Input/Input.h"
  5. #include "../Input/InputEvents.h"
  6. #include "UI.h"
  7. #include "UIEvents.h"
  8. namespace Atomic
  9. {
  10. static MODIFIER_KEYS GetModifierKeys(int qualifiers, bool superKey)
  11. {
  12. MODIFIER_KEYS code = TB_MODIFIER_NONE;
  13. if (qualifiers & QUAL_ALT) code |= TB_ALT;
  14. if (qualifiers & QUAL_CTRL) code |= TB_CTRL;
  15. if (qualifiers & QUAL_SHIFT) code |= TB_SHIFT;
  16. if (superKey) code |= TB_SUPER;
  17. return code;
  18. }
  19. // @return Return the upper case of a ascii charcter. Only for shortcut handling.
  20. static int toupr_ascii(int ascii)
  21. {
  22. if (ascii >= 'a' && ascii <= 'z')
  23. return ascii + 'A' - 'a';
  24. return ascii;
  25. }
  26. void UI::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  27. {
  28. if (inputDisabled_ || consoleVisible_)
  29. return;
  30. using namespace MouseButtonDown;
  31. unsigned button = eventData[P_BUTTON].GetUInt();
  32. IntVector2 pos;
  33. pos = GetSubsystem<Input>()->GetMousePosition();
  34. Input* input = GetSubsystem<Input>();
  35. int qualifiers = input->GetQualifiers();
  36. #ifdef ATOMIC_PLATFORM_WINDOWS
  37. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  38. #else
  39. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  40. #endif
  41. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  42. static double last_time = 0;
  43. static int counter = 1;
  44. Time* t = GetSubsystem<Time>();
  45. double time = t->GetElapsedTime() * 1000;
  46. if (time < last_time + 600)
  47. counter++;
  48. else
  49. counter = 1;
  50. last_time = time;
  51. if (button == MOUSEB_RIGHT)
  52. rootWidget_->InvokeRightPointerDown(pos.x_, pos.y_, counter, mod);
  53. else
  54. rootWidget_->InvokePointerDown(pos.x_, pos.y_, counter, mod, false);
  55. }
  56. void UI::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
  57. {
  58. if (inputDisabled_ || consoleVisible_)
  59. return;
  60. using namespace MouseButtonUp;
  61. unsigned button = eventData[P_BUTTON].GetUInt();
  62. IntVector2 pos;
  63. Input* input = GetSubsystem<Input>();
  64. pos = input->GetMousePosition();
  65. int qualifiers = input->GetQualifiers();
  66. #ifdef ATOMIC_PLATFORM_WINDOWS
  67. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  68. #else
  69. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  70. #endif
  71. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  72. if (button == MOUSEB_RIGHT)
  73. rootWidget_->InvokeRightPointerUp(pos.x_, pos.y_, mod);
  74. else
  75. rootWidget_->InvokePointerUp(pos.x_, pos.y_, mod, false);
  76. }
  77. void UI::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  78. {
  79. using namespace MouseMove;
  80. if (inputDisabled_ || consoleVisible_)
  81. return;
  82. int px = eventData[P_X].GetInt();
  83. int py = eventData[P_Y].GetInt();
  84. rootWidget_->InvokePointerMove(px, py, tb::TB_MODIFIER_NONE, false);
  85. }
  86. void UI::HandleMouseWheel(StringHash eventType, VariantMap& eventData)
  87. {
  88. if (inputDisabled_ || consoleVisible_)
  89. return;
  90. using namespace MouseWheel;
  91. int delta = eventData[P_WHEEL].GetInt();
  92. Input* input = GetSubsystem<Input>();
  93. rootWidget_->InvokeWheel(input->GetMousePosition().x_, input->GetMousePosition().y_, 0, delta > 0 ? -1 : 1, tb::TB_MODIFIER_NONE);
  94. }
  95. static bool InvokeShortcut(int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool down)
  96. {
  97. #ifdef __APPLE__
  98. bool shortcut_key = (modifierkeys & TB_SUPER) ? true : false;
  99. #else
  100. bool shortcut_key = (modifierkeys & TB_CTRL) ? true : false;
  101. #endif
  102. if (!TBWidget::focused_widget || !down || (!shortcut_key && special_key ==TB_KEY_UNDEFINED))
  103. return false;
  104. bool reverse_key = (modifierkeys & TB_SHIFT) ? true : false;
  105. int upper_key = toupr_ascii(key);
  106. TBID id;
  107. if (upper_key == 'X')
  108. id = TBIDC("cut");
  109. else if (upper_key == 'C' || special_key == TB_KEY_INSERT)
  110. id = TBIDC("copy");
  111. else if (upper_key == 'V' || (special_key == TB_KEY_INSERT && reverse_key))
  112. id = TBIDC("paste");
  113. else if (upper_key == 'A')
  114. id = TBIDC("selectall");
  115. else if (upper_key == 'Z' || upper_key == 'Y')
  116. {
  117. bool undo = upper_key == 'Z';
  118. if (reverse_key)
  119. undo = !undo;
  120. id = undo ? TBIDC("undo") : TBIDC("redo");
  121. }
  122. else if (upper_key == 'N')
  123. id = TBIDC("new");
  124. else if (upper_key == 'O')
  125. id = TBIDC("open");
  126. else if (upper_key == 'S')
  127. id = TBIDC("save");
  128. else if (upper_key == 'W')
  129. id = TBIDC("close");
  130. else if (upper_key == 'F')
  131. id = TBIDC("find");
  132. #ifdef ATOMIC_PLATFORM_OSX
  133. else if (upper_key == 'G' && (modifierkeys & TB_SHIFT))
  134. id = TBIDC("findprev");
  135. else if (upper_key == 'G')
  136. id = TBIDC("findnext");
  137. #else
  138. else if (special_key == TB_KEY_F3 && (modifierkeys & TB_SHIFT))
  139. id = TBIDC("findprev");
  140. else if (special_key == TB_KEY_F3)
  141. id = TBIDC("findnext");
  142. #endif
  143. else if (upper_key == 'P')
  144. id = TBIDC("play");
  145. else if (special_key == TB_KEY_PAGE_UP)
  146. id = TBIDC("prev_doc");
  147. else if (special_key == TB_KEY_PAGE_DOWN)
  148. id = TBIDC("next_doc");
  149. else
  150. return false;
  151. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  152. ev.modifierkeys = modifierkeys;
  153. ev.ref_id = id;
  154. TBWidget* eventWidget = TBWidget::focused_widget;
  155. if (id == TBIDC("save") || id == TBIDC("close")) {
  156. while (eventWidget && !eventWidget->GetDelegate()) {
  157. eventWidget = eventWidget->GetParent();
  158. }
  159. }
  160. if (!eventWidget)
  161. return false;
  162. return eventWidget->InvokeEvent(ev);
  163. }
  164. static bool InvokeKey(TBWidget* root, unsigned int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool keydown)
  165. {
  166. if (InvokeShortcut(key, special_key, modifierkeys, keydown))
  167. return true;
  168. root->InvokeKey(key, special_key, modifierkeys, keydown);
  169. return true;
  170. }
  171. void UI::HandleKey(bool keydown, int keycode, int scancode)
  172. {
  173. #ifdef ATOMIC_PLATFORM_WINDOWS
  174. if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
  175. return;
  176. #else
  177. if (keycode == KEY_LGUI || keycode == KEY_RGUI)
  178. return;
  179. #endif
  180. Input* input = GetSubsystem<Input>();
  181. int qualifiers = input->GetQualifiers();
  182. #ifdef ATOMIC_PLATFORM_WINDOWS
  183. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  184. #else
  185. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  186. #endif
  187. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  188. switch (keycode)
  189. {
  190. case KEY_RETURN:
  191. case KEY_RETURN2:
  192. case KEY_KP_ENTER:
  193. InvokeKey(rootWidget_, 0, TB_KEY_ENTER, mod, keydown);
  194. break;
  195. case KEY_F1:
  196. InvokeKey(rootWidget_, 0, TB_KEY_F1, mod, keydown);
  197. break;
  198. case KEY_F2:
  199. InvokeKey(rootWidget_, 0, TB_KEY_F2, mod, keydown);
  200. break;
  201. case KEY_F3:
  202. InvokeKey(rootWidget_, 0, TB_KEY_F3, mod, keydown);
  203. break;
  204. case KEY_F4:
  205. InvokeKey(rootWidget_, 0, TB_KEY_F4, mod, keydown);
  206. break;
  207. case KEY_F5:
  208. InvokeKey(rootWidget_, 0, TB_KEY_F5, mod, keydown);
  209. break;
  210. case KEY_F6:
  211. InvokeKey(rootWidget_, 0, TB_KEY_F6, mod, keydown);
  212. break;
  213. case KEY_F7:
  214. InvokeKey(rootWidget_, 0, TB_KEY_F7, mod, keydown);
  215. break;
  216. case KEY_F8:
  217. InvokeKey(rootWidget_, 0, TB_KEY_F8, mod, keydown);
  218. break;
  219. case KEY_F9:
  220. InvokeKey(rootWidget_, 0, TB_KEY_F9, mod, keydown);
  221. break;
  222. case KEY_F10:
  223. InvokeKey(rootWidget_, 0, TB_KEY_F10, mod, keydown);
  224. break;
  225. case KEY_F11:
  226. InvokeKey(rootWidget_, 0, TB_KEY_F11, mod, keydown);
  227. break;
  228. case KEY_F12:
  229. InvokeKey(rootWidget_, 0, TB_KEY_F12, mod, keydown);
  230. break;
  231. case KEY_LEFT:
  232. InvokeKey(rootWidget_, 0, TB_KEY_LEFT, mod, keydown);
  233. break;
  234. case KEY_UP:
  235. InvokeKey(rootWidget_, 0, TB_KEY_UP, mod, keydown);
  236. break;
  237. case KEY_RIGHT:
  238. InvokeKey(rootWidget_, 0, TB_KEY_RIGHT, mod, keydown);
  239. break;
  240. case KEY_DOWN:
  241. InvokeKey(rootWidget_, 0, TB_KEY_DOWN, mod, keydown);
  242. break;
  243. case KEY_PAGEUP:
  244. InvokeKey(rootWidget_, 0, TB_KEY_PAGE_UP, mod, keydown);
  245. break;
  246. case KEY_PAGEDOWN:
  247. InvokeKey(rootWidget_, 0, TB_KEY_PAGE_DOWN, mod, keydown);
  248. break;
  249. case KEY_HOME:
  250. InvokeKey(rootWidget_, 0, TB_KEY_HOME, mod, keydown);
  251. break;
  252. case KEY_END:
  253. InvokeKey(rootWidget_, 0, TB_KEY_END, mod, keydown);
  254. break;
  255. case KEY_INSERT:
  256. InvokeKey(rootWidget_, 0, TB_KEY_INSERT, mod, keydown);
  257. break;
  258. case KEY_TAB:
  259. InvokeKey(rootWidget_, 0, TB_KEY_TAB, mod, keydown);
  260. break;
  261. case KEY_DELETE:
  262. InvokeKey(rootWidget_, 0, TB_KEY_DELETE, mod, keydown);
  263. break;
  264. case KEY_BACKSPACE:
  265. InvokeKey(rootWidget_, 0, TB_KEY_BACKSPACE, mod, keydown);
  266. break;
  267. case KEY_ESC:
  268. InvokeKey(rootWidget_, 0, TB_KEY_ESC, mod, keydown);
  269. break;
  270. default:
  271. if (mod & TB_SUPER)
  272. {
  273. InvokeKey(rootWidget_, keycode, TB_KEY_UNDEFINED, mod, keydown);
  274. }
  275. }
  276. }
  277. void UI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  278. {
  279. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  280. return;
  281. using namespace KeyDown;
  282. int keycode = eventData[P_KEY].GetInt();
  283. int scancode = eventData[P_SCANCODE].GetInt();
  284. HandleKey(true, keycode, scancode);
  285. // Send Global Shortcut
  286. Input* input = GetSubsystem<Input>();
  287. #ifdef ATOMIC_PLATFORM_WINDOWS
  288. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  289. if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
  290. superdown = false;
  291. #else
  292. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  293. if (keycode == KEY_LGUI || keycode == KEY_RGUI)
  294. superdown = false;
  295. #endif
  296. if (!superdown)
  297. return;
  298. VariantMap shortcutData;
  299. shortcutData[UIShortcut::P_KEY] = keycode;
  300. shortcutData[UIShortcut::P_QUALIFIERS] = eventData[P_QUALIFIERS].GetInt();
  301. SendEvent(E_UISHORTCUT, shortcutData);
  302. }
  303. void UI::HandleKeyUp(StringHash eventType, VariantMap& eventData)
  304. {
  305. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  306. return;
  307. using namespace KeyUp;
  308. int keycode = eventData[P_KEY].GetInt();
  309. int scancode = eventData[P_SCANCODE].GetInt();
  310. HandleKey(false, keycode, scancode);
  311. }
  312. void UI::HandleTextInput(StringHash eventType, VariantMap& eventData)
  313. {
  314. if (inputDisabled_ || keyboardDisabled_ || consoleVisible_)
  315. return;
  316. using namespace TextInput;
  317. const String& text = eventData[P_TEXT].GetString();
  318. for (unsigned i = 0; i < text.Length(); i++)
  319. {
  320. InvokeKey(rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, true);
  321. InvokeKey(rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, false);
  322. }
  323. }
  324. }