| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- //
- // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include <TurboBadger/tb_widgets.h>
- using namespace tb;
- #include "../Core/Timer.h"
- #include "../Input/Input.h"
- #include "../Input/InputEvents.h"
- #include "UI.h"
- #include "UIEvents.h"
- #include "UIView.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;
- }
- bool UIView::FilterDefaultInput(bool keyEvent) const
- {
- if (!GetInputEnabled())
- return true;
- if (ui_.Null() || ui_->GetFocusedView() != this)
- return true;
- if (ui_->inputDisabled_ || ui_->consoleVisible_)
- return true;
- if (renderTexture_ && !keyEvent)
- return true;
- if (!keyEvent && !GetMouseEnabled())
- return true;
- if (keyEvent && !GetKeyboardEnabled() || ui_->keyboardDisabled_)
- return true;
- return false;
- }
- void UIView::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
- {
- if (FilterDefaultInput())
- 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_OSX
- bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
- #else
- bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
- #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)
- widget_->InvokeRightPointerDown(pos.x_, pos.y_, counter, mod);
- else
- widget_->InvokePointerDown(pos.x_, pos.y_, counter, mod, false);
- }
- void UIView::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
- {
- if (FilterDefaultInput())
- 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_OSX
- bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
- #else
- bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
- #endif
- MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
- if (button == MOUSEB_RIGHT)
- widget_->InvokeRightPointerUp(pos.x_, pos.y_, mod);
- else
- widget_->InvokePointerUp(pos.x_, pos.y_, mod, false);
- }
- void UIView::HandleMouseMove(StringHash eventType, VariantMap& eventData)
- {
- using namespace MouseMove;
- if (FilterDefaultInput())
- return;
- int px = eventData[P_X].GetInt();
- int py = eventData[P_Y].GetInt();
- widget_->InvokePointerMove(px, py, tb::TB_MODIFIER_NONE, false);
- ui_->tooltipHoverTime_ = 0;
- }
- void UIView::HandleMouseWheel(StringHash eventType, VariantMap& eventData)
- {
- if (FilterDefaultInput())
- return;
- using namespace MouseWheel;
- int delta = eventData[P_WHEEL].GetInt();
- Input* input = GetSubsystem<Input>();
- widget_->InvokeWheel(input->GetMousePosition().x_, input->GetMousePosition().y_, 0, -delta, tb::TB_MODIFIER_NONE);
- }
- //Touch Input
- void UIView::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
- {
- if (FilterDefaultInput())
- return;
- using namespace TouchBegin;
-
- int touchId = eventData[P_TOUCHID].GetInt();
- int px = eventData[P_X].GetInt();
- int py = eventData[P_Y].GetInt();
-
- 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;
-
- widget_->InvokePointerDown(px, py, counter, TB_MODIFIER_NONE, true, touchId);
- }
- void UIView::HandleTouchMove(StringHash eventType, VariantMap& eventData)
- {
- if (FilterDefaultInput())
- return;
-
- using namespace TouchMove;
-
- int touchId = eventData[P_TOUCHID].GetInt();
- int px = eventData[P_X].GetInt();
- int py = eventData[P_Y].GetInt();
- widget_->InvokePointerMove(px, py, TB_MODIFIER_NONE, true, touchId);
- }
- void UIView::HandleTouchEnd(StringHash eventType, VariantMap& eventData)
- {
- if (FilterDefaultInput())
- return;
- using namespace TouchEnd;
- int touchId = eventData[P_TOUCHID].GetInt();
- int px = eventData[P_X].GetInt();
- int py = eventData[P_Y].GetInt();
- widget_->InvokePointerUp(px, py, TB_MODIFIER_NONE, true, touchId);
- }
- static bool InvokeShortcut(UI* ui, 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 (!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 (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;
- TBWidget* eventWidget = TBWidget::focused_widget;
- if (id == TBIDC("save") || id == TBIDC("close")) {
- while (eventWidget && !eventWidget->GetDelegate()) {
- eventWidget = eventWidget->GetParent();
- }
- }
- if (!eventWidget || !eventWidget->InvokeEvent(ev))
- {
- VariantMap evData;
- evData[UIUnhandledShortcut::P_REFID] = id;
- ui->SendEvent(E_UIUNHANDLEDSHORTCUT, evData);
- return false;
- }
- return true;
- }
- static bool InvokeKey(UI* ui, TBWidget* root, unsigned int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool keydown)
- {
- if (InvokeShortcut(ui, key, special_key, modifierkeys, keydown))
- return true;
- root->InvokeKey(key, special_key, modifierkeys, keydown);
- return true;
- }
- void UIView::HandleKey(bool keydown, int keycode, int scancode)
- {
- if (keydown && (keycode == KEY_ESCAPE || keycode == KEY_RETURN || keycode == KEY_RETURN2 || keycode == KEY_KP_ENTER)
- && TBWidget::focused_widget)
- {
- SendEvent(E_UIWIDGETFOCUSESCAPED);
- }
- #ifndef ATOMIC_PLATFORM_OSX
- 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();
- #ifndef ATOMIC_PLATFORM_OSX
- 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);
- SPECIAL_KEY specialKey = TB_KEY_UNDEFINED;
- switch (keycode)
- {
- case KEY_RETURN:
- case KEY_RETURN2:
- case KEY_KP_ENTER:
- specialKey = TB_KEY_ENTER;
- break;
- case KEY_F1:
- specialKey = TB_KEY_F1;
- break;
- case KEY_F2:
- specialKey = TB_KEY_F2;
- break;
- case KEY_F3:
- specialKey = TB_KEY_F3;
- break;
- case KEY_F4:
- specialKey = TB_KEY_F4;
- break;
- case KEY_F5:
- specialKey = TB_KEY_F5;
- break;
- case KEY_F6:
- specialKey = TB_KEY_F6;
- break;
- case KEY_F7:
- specialKey = TB_KEY_F7;
- break;
- case KEY_F8:
- specialKey = TB_KEY_F8;
- break;
- case KEY_F9:
- specialKey = TB_KEY_F9;
- break;
- case KEY_F10:
- specialKey = TB_KEY_F10;
- break;
- case KEY_F11:
- specialKey = TB_KEY_F11;
- break;
- case KEY_F12:
- specialKey = TB_KEY_F12;
- break;
- case KEY_LEFT:
- specialKey = TB_KEY_LEFT;
- break;
- case KEY_UP:
- specialKey = TB_KEY_UP;
- break;
- case KEY_RIGHT:
- specialKey = TB_KEY_RIGHT;
- break;
- case KEY_DOWN:
- specialKey = TB_KEY_DOWN;
- break;
- case KEY_PAGEUP:
- specialKey = TB_KEY_PAGE_UP;
- break;
- case KEY_PAGEDOWN:
- specialKey = TB_KEY_PAGE_DOWN;
- break;
- case KEY_HOME:
- specialKey = TB_KEY_HOME;
- break;
- case KEY_END:
- specialKey = TB_KEY_END;
- break;
- case KEY_INSERT:
- specialKey = TB_KEY_INSERT;
- break;
- case KEY_TAB:
- specialKey = TB_KEY_TAB;
- break;
- case KEY_DELETE:
- specialKey = TB_KEY_DELETE;
- break;
- case KEY_BACKSPACE:
- specialKey = TB_KEY_BACKSPACE;
- break;
- case KEY_ESCAPE:
- specialKey = TB_KEY_ESC;
- break;
- }
- if (specialKey == TB_KEY_UNDEFINED)
- {
- if (mod & TB_SUPER)
- {
- InvokeKey(ui_, widget_, keycode, TB_KEY_UNDEFINED, mod, keydown);
- }
- }
- else
- {
- InvokeKey(ui_, widget_, 0, specialKey, mod, keydown);
- }
- }
- void UIView::HandleKeyDown(StringHash eventType, VariantMap& eventData)
- {
- if (FilterDefaultInput(true))
- return;
- using namespace KeyDown;
- int keycode = eventData[P_KEY].GetInt();
- int scancode = eventData[P_SCANCODE].GetInt();
- HandleKey(true, keycode, scancode);
- // Send Global Shortcut
- Input* input = GetSubsystem<Input>();
- #ifndef ATOMIC_PLATFORM_OSX
- bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
- if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
- superdown = false;
- #else
- bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
- if (keycode == KEY_LGUI || keycode == KEY_RGUI)
- superdown = false;
- #endif
- if (!superdown)
- return;
- VariantMap shortcutData;
- shortcutData[UIShortcut::P_KEY] = keycode;
- shortcutData[UIShortcut::P_QUALIFIERS] = eventData[P_QUALIFIERS].GetInt();
- SendEvent(E_UISHORTCUT, shortcutData);
- }
- void UIView::HandleKeyUp(StringHash eventType, VariantMap& eventData)
- {
- if (FilterDefaultInput(true))
- return;
- using namespace KeyUp;
- int keycode = eventData[P_KEY].GetInt();
- int scancode = eventData[P_SCANCODE].GetInt();
- HandleKey(false, keycode, scancode);
- }
- void UIView::HandleTextInput(StringHash eventType, VariantMap& eventData)
- {
- if (FilterDefaultInput(true))
- return;
- using namespace TextInput;
- const String& text = eventData[P_TEXT].GetString();
- for (unsigned i = 0; i < text.Length(); i++)
- {
- InvokeKey(ui_, widget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, true);
- InvokeKey(ui_, widget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, false);
- }
- }
- }
|