UIInput.cpp 9.9 KB

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