UIInput.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. TBWidget* eventWidget = TBWidget::focused_widget;
  156. if (id == TBIDC("save") || id == TBIDC("close")) {
  157. while (eventWidget && !eventWidget->GetDelegate()) {
  158. eventWidget = eventWidget->GetParent();
  159. }
  160. }
  161. if (!eventWidget)
  162. return false;
  163. return eventWidget->InvokeEvent(ev);
  164. }
  165. static bool InvokeKey(TBWidget* root, unsigned int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool keydown)
  166. {
  167. if (InvokeShortcut(key, special_key, modifierkeys, keydown))
  168. return true;
  169. root->InvokeKey(key, special_key, modifierkeys, keydown);
  170. return true;
  171. }
  172. void UI::HandleKey(bool keydown, int keycode, int scancode)
  173. {
  174. #ifdef ATOMIC_PLATFORM_WINDOWS
  175. if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
  176. return;
  177. #else
  178. if (keycode == KEY_LGUI || keycode == KEY_RGUI)
  179. return;
  180. #endif
  181. Input* input = GetSubsystem<Input>();
  182. int qualifiers = input->GetQualifiers();
  183. #ifdef ATOMIC_PLATFORM_WINDOWS
  184. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  185. #else
  186. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  187. #endif
  188. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  189. switch (keycode)
  190. {
  191. case KEY_RETURN:
  192. case KEY_RETURN2:
  193. case KEY_KP_ENTER:
  194. InvokeKey(rootWidget_, 0, TB_KEY_ENTER, mod, keydown);
  195. break;
  196. case KEY_F1:
  197. InvokeKey(rootWidget_, 0, TB_KEY_F1, mod, keydown);
  198. break;
  199. case KEY_F2:
  200. InvokeKey(rootWidget_, 0, TB_KEY_F2, mod, keydown);
  201. break;
  202. case KEY_F3:
  203. InvokeKey(rootWidget_, 0, TB_KEY_F3, mod, keydown);
  204. break;
  205. case KEY_F4:
  206. InvokeKey(rootWidget_, 0, TB_KEY_F4, mod, keydown);
  207. break;
  208. case KEY_F5:
  209. InvokeKey(rootWidget_, 0, TB_KEY_F5, mod, keydown);
  210. break;
  211. case KEY_F6:
  212. InvokeKey(rootWidget_, 0, TB_KEY_F6, mod, keydown);
  213. break;
  214. case KEY_F7:
  215. InvokeKey(rootWidget_, 0, TB_KEY_F7, mod, keydown);
  216. break;
  217. case KEY_F8:
  218. InvokeKey(rootWidget_, 0, TB_KEY_F8, mod, keydown);
  219. break;
  220. case KEY_F9:
  221. InvokeKey(rootWidget_, 0, TB_KEY_F9, mod, keydown);
  222. break;
  223. case KEY_F10:
  224. InvokeKey(rootWidget_, 0, TB_KEY_F10, mod, keydown);
  225. break;
  226. case KEY_F11:
  227. InvokeKey(rootWidget_, 0, TB_KEY_F11, mod, keydown);
  228. break;
  229. case KEY_F12:
  230. InvokeKey(rootWidget_, 0, TB_KEY_F12, mod, keydown);
  231. break;
  232. case KEY_LEFT:
  233. InvokeKey(rootWidget_, 0, TB_KEY_LEFT, mod, keydown);
  234. break;
  235. case KEY_UP:
  236. InvokeKey(rootWidget_, 0, TB_KEY_UP, mod, keydown);
  237. break;
  238. case KEY_RIGHT:
  239. InvokeKey(rootWidget_, 0, TB_KEY_RIGHT, mod, keydown);
  240. break;
  241. case KEY_DOWN:
  242. InvokeKey(rootWidget_, 0, TB_KEY_DOWN, mod, keydown);
  243. break;
  244. case KEY_PAGEUP:
  245. InvokeKey(rootWidget_, 0, TB_KEY_PAGE_UP, mod, keydown);
  246. break;
  247. case KEY_PAGEDOWN:
  248. InvokeKey(rootWidget_, 0, TB_KEY_PAGE_DOWN, mod, keydown);
  249. break;
  250. case KEY_HOME:
  251. InvokeKey(rootWidget_, 0, TB_KEY_HOME, mod, keydown);
  252. break;
  253. case KEY_END:
  254. InvokeKey(rootWidget_, 0, TB_KEY_END, mod, keydown);
  255. break;
  256. case KEY_INSERT:
  257. InvokeKey(rootWidget_, 0, TB_KEY_INSERT, mod, keydown);
  258. break;
  259. case KEY_TAB:
  260. InvokeKey(rootWidget_, 0, TB_KEY_TAB, mod, keydown);
  261. break;
  262. case KEY_DELETE:
  263. InvokeKey(rootWidget_, 0, TB_KEY_DELETE, mod, keydown);
  264. break;
  265. case KEY_BACKSPACE:
  266. InvokeKey(rootWidget_, 0, TB_KEY_BACKSPACE, mod, keydown);
  267. break;
  268. case KEY_ESC:
  269. InvokeKey(rootWidget_, 0, TB_KEY_ESC, mod, keydown);
  270. break;
  271. default:
  272. if (mod & TB_SUPER)
  273. {
  274. InvokeKey(rootWidget_, keycode, TB_KEY_UNDEFINED, mod, keydown);
  275. }
  276. }
  277. }
  278. void UI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  279. {
  280. if (inputDisabled_ || keyboardDisabled_)
  281. return;
  282. using namespace KeyDown;
  283. int keycode = eventData[P_KEY].GetInt();
  284. int scancode = eventData[P_SCANCODE].GetInt();
  285. HandleKey(true, keycode, scancode);
  286. }
  287. void UI::HandleKeyUp(StringHash eventType, VariantMap& eventData)
  288. {
  289. if (inputDisabled_ || keyboardDisabled_)
  290. return;
  291. using namespace KeyUp;
  292. int keycode = eventData[P_KEY].GetInt();
  293. int scancode = eventData[P_SCANCODE].GetInt();
  294. HandleKey(false, keycode, scancode);
  295. }
  296. void UI::HandleTextInput(StringHash eventType, VariantMap& eventData)
  297. {
  298. if (inputDisabled_ || keyboardDisabled_)
  299. return;
  300. using namespace TextInput;
  301. const String& text = eventData[P_TEXT].GetString();
  302. for (unsigned i = 0; i < text.Length(); i++)
  303. {
  304. InvokeKey(rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, true);
  305. InvokeKey(rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, false);
  306. }
  307. }
  308. }