| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- #include <TurboBadger/tb_widgets.h>
- using namespace tb;
- #include "../Core/Timer.h"
- #include "../Input/Input.h"
- #include "../Input/InputEvents.h"
- #include "UI.h"
- namespace Atomic
- {
- static MODIFIER_KEYS GetModifierKeys(int qualifiers, bool superKey)
- {
- MODIFIER_KEYS code = TB_MODIFIER_NONE;
- if (qualifiers & QUAL_ALT) code |= TB_ALT;
- if (qualifiers & QUAL_CTRL) code |= TB_CTRL;
- if (qualifiers & QUAL_SHIFT) code |= TB_SHIFT;
- if (superKey) code |= TB_SUPER;
- return code;
- }
- // @return Return the upper case of a ascii charcter. Only for shortcut handling.
- static int toupr_ascii(int ascii)
- {
- if (ascii >= 'a' && ascii <= 'z')
- return ascii + 'A' - 'a';
- return ascii;
- }
- void UI::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
- {
- if (inputDisabled_)
- return;
- using namespace MouseButtonDown;
- unsigned button = eventData[P_BUTTON].GetUInt();
- IntVector2 pos;
- pos = GetSubsystem<Input>()->GetMousePosition();
- Input* input = GetSubsystem<Input>();
- int qualifiers = input->GetQualifiers();
- #ifdef ATOMIC_PLATFORM_WINDOWS
- bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
- #else
- bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
- #endif
- MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
- static double last_time = 0;
- static int counter = 1;
- Time* t = GetSubsystem<Time>();
- double time = t->GetElapsedTime() * 1000;
- if (time < last_time + 600)
- counter++;
- else
- counter = 1;
- last_time = time;
- if (button == MOUSEB_RIGHT)
- rootWidget_->InvokeRightPointerDown(pos.x_, pos.y_, counter, mod);
- else
- rootWidget_->InvokePointerDown(pos.x_, pos.y_, counter, mod, false);
- }
- void UI::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
- {
- if (inputDisabled_)
- return;
- using namespace MouseButtonUp;
- unsigned button = eventData[P_BUTTON].GetUInt();
- IntVector2 pos;
- Input* input = GetSubsystem<Input>();
- pos = input->GetMousePosition();
- int qualifiers = input->GetQualifiers();
- #ifdef ATOMIC_PLATFORM_WINDOWS
- bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
- #else
- bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
- #endif
- MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
- if (button == MOUSEB_RIGHT)
- rootWidget_->InvokeRightPointerUp(pos.x_, pos.y_, mod);
- else
- rootWidget_->InvokePointerUp(pos.x_, pos.y_, mod, false);
- }
- void UI::HandleMouseMove(StringHash eventType, VariantMap& eventData)
- {
- using namespace MouseMove;
- if (inputDisabled_)
- return;
- int px = eventData[P_X].GetInt();
- int py = eventData[P_Y].GetInt();
- rootWidget_->InvokePointerMove(px, py, tb::TB_MODIFIER_NONE, false);
- }
- void UI::HandleMouseWheel(StringHash eventType, VariantMap& eventData)
- {
- if (inputDisabled_)
- return;
- using namespace MouseWheel;
- int delta = eventData[P_WHEEL].GetInt();
- Input* input = GetSubsystem<Input>();
- rootWidget_->InvokeWheel(input->GetMousePosition().x_, input->GetMousePosition().y_, 0, delta > 0 ? -1 : 1, tb::TB_MODIFIER_NONE);
- }
- static bool InvokeShortcut(int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool down)
- {
- #ifdef __APPLE__
- bool shortcut_key = (modifierkeys & TB_SUPER) ? true : false;
- #else
- bool shortcut_key = (modifierkeys & TB_CTRL) ? true : false;
- #endif
- if (!TBWidget::focused_widget || !down || (!shortcut_key && special_key ==TB_KEY_UNDEFINED))
- return false;
- bool reverse_key = (modifierkeys & TB_SHIFT) ? true : false;
- int upper_key = toupr_ascii(key);
- TBID id;
- if (upper_key == 'X')
- id = TBIDC("cut");
- else if (upper_key == 'C' || special_key == TB_KEY_INSERT)
- id = TBIDC("copy");
- else if (upper_key == 'V' || (special_key == TB_KEY_INSERT && reverse_key))
- id = TBIDC("paste");
- else if (upper_key == 'A')
- id = TBIDC("selectall");
- else if (upper_key == 'Z' || upper_key == 'Y')
- {
- bool undo = upper_key == 'Z';
- if (reverse_key)
- undo = !undo;
- id = undo ? TBIDC("undo") : TBIDC("redo");
- }
- else if (upper_key == 'N')
- id = TBIDC("new");
- else if (upper_key == 'O')
- id = TBIDC("open");
- else if (upper_key == 'S')
- id = TBIDC("save");
- else if (upper_key == 'W')
- id = TBIDC("close");
- else if (upper_key == 'F')
- id = TBIDC("find");
- #ifdef ATOMIC_PLATFORM_OSX
- else if (upper_key == 'G' && (modifierkeys & TB_SHIFT))
- id = TBIDC("findprev");
- else if (upper_key == 'G')
- id = TBIDC("findnext");
- #else
- else if (special_key == TB_KEY_F3 && (modifierkeys & TB_SHIFT))
- id = TBIDC("findprev");
- else if (special_key == TB_KEY_F3)
- id = TBIDC("findnext");
- #endif
- else if (upper_key == 'P')
- id = TBIDC("play");
- else if (upper_key == 'I')
- id = TBIDC("beautify");
- else if (special_key == TB_KEY_PAGE_UP)
- id = TBIDC("prev_doc");
- else if (special_key == TB_KEY_PAGE_DOWN)
- id = TBIDC("next_doc");
- else
- return false;
- TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
- ev.modifierkeys = modifierkeys;
- ev.ref_id = id;
- return TBWidget::focused_widget->InvokeEvent(ev);
- }
- static bool InvokeKey(TBWidget* root, unsigned int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool keydown)
- {
- if (InvokeShortcut(key, special_key, modifierkeys, keydown))
- return true;
- root->InvokeKey(key, special_key, modifierkeys, keydown);
- return true;
- }
- void UI::HandleKey(bool keydown, int keycode, int scancode)
- {
- #ifdef ATOMIC_PLATFORM_WINDOWS
- if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
- return;
- #else
- if (keycode == KEY_LGUI || keycode == KEY_RGUI)
- return;
- #endif
- Input* input = GetSubsystem<Input>();
- int qualifiers = input->GetQualifiers();
- #ifdef ATOMIC_PLATFORM_WINDOWS
- bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
- #else
- bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
- #endif
- MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
- switch (keycode)
- {
- case KEY_RETURN:
- case KEY_RETURN2:
- case KEY_KP_ENTER:
- InvokeKey(rootWidget_, 0, TB_KEY_ENTER, mod, keydown);
- break;
- case KEY_F1:
- InvokeKey(rootWidget_, 0, TB_KEY_F1, mod, keydown);
- break;
- case KEY_F2:
- InvokeKey(rootWidget_, 0, TB_KEY_F2, mod, keydown);
- break;
- case KEY_F3:
- InvokeKey(rootWidget_, 0, TB_KEY_F3, mod, keydown);
- break;
- case KEY_F4:
- InvokeKey(rootWidget_, 0, TB_KEY_F4, mod, keydown);
- break;
- case KEY_F5:
- InvokeKey(rootWidget_, 0, TB_KEY_F5, mod, keydown);
- break;
- case KEY_F6:
- InvokeKey(rootWidget_, 0, TB_KEY_F6, mod, keydown);
- break;
- case KEY_F7:
- InvokeKey(rootWidget_, 0, TB_KEY_F7, mod, keydown);
- break;
- case KEY_F8:
- InvokeKey(rootWidget_, 0, TB_KEY_F8, mod, keydown);
- break;
- case KEY_F9:
- InvokeKey(rootWidget_, 0, TB_KEY_F9, mod, keydown);
- break;
- case KEY_F10:
- InvokeKey(rootWidget_, 0, TB_KEY_F10, mod, keydown);
- break;
- case KEY_F11:
- InvokeKey(rootWidget_, 0, TB_KEY_F11, mod, keydown);
- break;
- case KEY_F12:
- InvokeKey(rootWidget_, 0, TB_KEY_F12, mod, keydown);
- break;
- case KEY_LEFT:
- InvokeKey(rootWidget_, 0, TB_KEY_LEFT, mod, keydown);
- break;
- case KEY_UP:
- InvokeKey(rootWidget_, 0, TB_KEY_UP, mod, keydown);
- break;
- case KEY_RIGHT:
- InvokeKey(rootWidget_, 0, TB_KEY_RIGHT, mod, keydown);
- break;
- case KEY_DOWN:
- InvokeKey(rootWidget_, 0, TB_KEY_DOWN, mod, keydown);
- break;
- case KEY_PAGEUP:
- InvokeKey(rootWidget_, 0, TB_KEY_PAGE_UP, mod, keydown);
- break;
- case KEY_PAGEDOWN:
- InvokeKey(rootWidget_, 0, TB_KEY_PAGE_DOWN, mod, keydown);
- break;
- case KEY_HOME:
- InvokeKey(rootWidget_, 0, TB_KEY_HOME, mod, keydown);
- break;
- case KEY_END:
- InvokeKey(rootWidget_, 0, TB_KEY_END, mod, keydown);
- break;
- case KEY_INSERT:
- InvokeKey(rootWidget_, 0, TB_KEY_INSERT, mod, keydown);
- break;
- case KEY_TAB:
- InvokeKey(rootWidget_, 0, TB_KEY_TAB, mod, keydown);
- break;
- case KEY_DELETE:
- InvokeKey(rootWidget_, 0, TB_KEY_DELETE, mod, keydown);
- break;
- case KEY_BACKSPACE:
- InvokeKey(rootWidget_, 0, TB_KEY_BACKSPACE, mod, keydown);
- break;
- case KEY_ESC:
- InvokeKey(rootWidget_, 0, TB_KEY_ESC, mod, keydown);
- break;
- default:
- if (mod & TB_SUPER)
- {
- InvokeKey(rootWidget_, keycode, TB_KEY_UNDEFINED, mod, keydown);
- }
- }
- }
- void UI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
- {
- if (inputDisabled_ || keyboardDisabled_)
- return;
- using namespace KeyDown;
- int keycode = eventData[P_KEY].GetInt();
- int scancode = eventData[P_SCANCODE].GetInt();
- HandleKey(true, keycode, scancode);
- }
- void UI::HandleKeyUp(StringHash eventType, VariantMap& eventData)
- {
- if (inputDisabled_ || keyboardDisabled_)
- return;
- using namespace KeyUp;
- int keycode = eventData[P_KEY].GetInt();
- int scancode = eventData[P_SCANCODE].GetInt();
- HandleKey(false, keycode, scancode);
- }
- void UI::HandleTextInput(StringHash eventType, VariantMap& eventData)
- {
- if (inputDisabled_ || keyboardDisabled_)
- return;
- using namespace TextInput;
- const String& text = eventData[P_TEXT].GetString();
- for (unsigned i = 0; i < text.Length(); i++)
- {
- InvokeKey(rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, true);
- InvokeKey(rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, false);
- }
- }
- }
|