From 7a86fc2cb50307f9d0b65247adf64604c00e10ce Mon Sep 17 00:00:00 2001 From: Ted John Date: Sun, 11 Jun 2017 16:31:54 +0100 Subject: [PATCH] Link back shortcut functions Some of this will be temporary until input and windows move over to libopenrct2ui. --- src/openrct2-ui/WindowManager.cpp | 17 ++ src/openrct2-ui/input/KeyboardShortcuts.cpp | 55 ++++++- src/openrct2-ui/input/KeyboardShortcuts.h | 4 +- src/openrct2-ui/libopenrct2ui.vcxproj | 2 + src/openrct2/Context.cpp | 12 ++ src/openrct2/Context.h | 2 + src/openrct2/input.c | 162 +++++--------------- src/openrct2/ui/DummyWindowManager.cpp | 2 + src/openrct2/ui/WindowManager.h | 3 + 9 files changed, 130 insertions(+), 129 deletions(-) diff --git a/src/openrct2-ui/WindowManager.cpp b/src/openrct2-ui/WindowManager.cpp index c62d264eb0..041502635e 100644 --- a/src/openrct2-ui/WindowManager.cpp +++ b/src/openrct2-ui/WindowManager.cpp @@ -32,6 +32,23 @@ public: return nullptr; } } + + void HandleKeyboardShortcut(sint32 key) override + { + rct_window * w = window_find_by_class(WC_CHANGE_KEYBOARD_SHORTCUT); + if (w != NULL) { + keyboard_shortcuts_set(key); + window_close_by_class(WC_CHANGE_KEYBOARD_SHORTCUT); + window_invalidate_by_class(WC_KEYBOARD_SHORTCUT_LIST); + } else { + keyboard_shortcut_handle(key); + } + } + + void GetKeyboardMapScroll(const uint8 * keysState, sint32 * x, sint32 * y) override + { + get_keyboard_map_scroll(keysState, x, y); + } }; IWindowManager * OpenRCT2::Ui::CreateWindowManager() diff --git a/src/openrct2-ui/input/KeyboardShortcuts.cpp b/src/openrct2-ui/input/KeyboardShortcuts.cpp index 4ae54c6597..817d90e38d 100644 --- a/src/openrct2-ui/input/KeyboardShortcuts.cpp +++ b/src/openrct2-ui/input/KeyboardShortcuts.cpp @@ -109,8 +109,6 @@ void KeyboardShortcuts::Set(sint32 key) // Map shortcut to this key _keys[gKeyboardShortcutChangeId] = key; - // window_close_by_class(WC_CHANGE_KEYBOARD_SHORTCUT); - // window_invalidate_by_class(WC_KEYBOARD_SHORTCUT_LIST); Save(); } @@ -160,6 +158,54 @@ std::string KeyboardShortcuts::GetShortcutString(sint32 shortcut) const return std::string(buffer); } +void KeyboardShortcuts::GetKeyboardMapScroll(const uint8 * keysState, sint32 * x, sint32 * y) const +{ + for (sint32 shortcutId = SHORTCUT_SCROLL_MAP_UP; shortcutId <= SHORTCUT_SCROLL_MAP_RIGHT; shortcutId++) + { + uint16 shortcutKey = _keys[shortcutId]; + uint8 scancode = shortcutKey & 0xFF; + + if (shortcutKey == 0xFFFF) continue; + if (!keysState[scancode]) continue; + + if (shortcutKey & SHIFT) { + if (!keysState[SDL_SCANCODE_LSHIFT] && !keysState[SDL_SCANCODE_RSHIFT]) continue; + } + if (shortcutKey & CTRL) { + if (!keysState[SDL_SCANCODE_LCTRL] && !keysState[SDL_SCANCODE_RCTRL]) continue; + } + if (shortcutKey & ALT) { + if (!keysState[SDL_SCANCODE_LALT] && !keysState[SDL_SCANCODE_RALT]) continue; + } +#ifdef __MACOSX__ + if (shortcutKey & CMD) { + if (!keysState[SDL_SCANCODE_LGUI] && !keysState[SDL_SCANCODE_RGUI]) continue; + } +#endif + switch (shortcutId) { + case SHORTCUT_SCROLL_MAP_UP: + *x = 0; + *y = -1; + break; + case SHORTCUT_SCROLL_MAP_LEFT: + *x = -1; + *y = 0; + break; + case SHORTCUT_SCROLL_MAP_DOWN: + *x = 0; + *y = 1; + break; + case SHORTCUT_SCROLL_MAP_RIGHT: + *x = 1; + *y = 0; + break; + default: + *x = 0; + *y = 0; + } + } +} + extern "C" { void keyboard_shortcuts_reset() @@ -192,6 +238,11 @@ extern "C" auto str = _instance->GetShortcutString(shortcut); String::Set(buffer, bufferSize, str.c_str()); } + + void get_keyboard_map_scroll(const uint8 * keysState, sint32 * x, sint32 * y) + { + _instance->GetKeyboardMapScroll(keysState, x, y); + } } // Default keyboard shortcuts diff --git a/src/openrct2-ui/input/KeyboardShortcuts.h b/src/openrct2-ui/input/KeyboardShortcuts.h index cc7dd93a83..7e0f58e5f4 100644 --- a/src/openrct2-ui/input/KeyboardShortcuts.h +++ b/src/openrct2-ui/input/KeyboardShortcuts.h @@ -130,6 +130,7 @@ namespace OpenRCT2 void Set(sint32 key); sint32 GetFromKey(sint32 key); + void GetKeyboardMapScroll(const uint8 * keysState, sint32 * x, sint32 * y) const; }; } } @@ -155,10 +156,11 @@ extern "C" rct_window * window_shortcut_keys_open(); rct_window * window_shortcut_change_open(sint32 selected_key); - void keyboard_shortcut_set(sint32 key); void keyboard_shortcut_handle(sint32 key); void keyboard_shortcut_handle_command(sint32 shortcutIndex); void keyboard_shortcut_format_string(char *buffer, size_t size, uint16 shortcutKey); + + void get_keyboard_map_scroll(const uint8 * keysState, sint32 * x, sint32 * y); #ifdef __cplusplus } #endif diff --git a/src/openrct2-ui/libopenrct2ui.vcxproj b/src/openrct2-ui/libopenrct2ui.vcxproj index 36efcad5b6..391cbe7109 100644 --- a/src/openrct2-ui/libopenrct2ui.vcxproj +++ b/src/openrct2-ui/libopenrct2ui.vcxproj @@ -48,6 +48,7 @@ + @@ -72,6 +73,7 @@ + {8DD8AB7D-2EA6-44E3-8265-BAF08E832951} diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index 659e6a67ef..7766a9df51 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -659,6 +659,18 @@ extern "C" return windowManager->OpenWindow(wc); } + void context_handle_keyboard_shortcut(sint32 key) + { + auto windowManager = GetContext()->GetUiContext()->GetWindowManager(); + windowManager->HandleKeyboardShortcut(key); + } + + void context_get_keyboard_map_scroll(const uint8 * keysState, sint32 * x, sint32 * y) + { + auto windowManager = GetContext()->GetUiContext()->GetWindowManager(); + windowManager->GetKeyboardMapScroll(keysState, x, y); + } + bool platform_open_common_file_dialog(utf8 * outFilename, file_dialog_desc * desc, size_t outSize) { try diff --git a/src/openrct2/Context.h b/src/openrct2/Context.h index 8b027da6fc..45c52eca91 100644 --- a/src/openrct2/Context.h +++ b/src/openrct2/Context.h @@ -128,6 +128,8 @@ extern "C" bool context_has_focus(); void context_set_cursor_trap(bool value); rct_window * context_open_window(rct_windowclass wc); + void context_handle_keyboard_shortcut(sint32 key); + void context_get_keyboard_map_scroll(const uint8 * keysState, sint32 * x, sint32 * y); #ifdef __cplusplus } #endif diff --git a/src/openrct2/input.c b/src/openrct2/input.c index 490bca269e..94d21de70d 100644 --- a/src/openrct2/input.c +++ b/src/openrct2/input.c @@ -1434,87 +1434,19 @@ static void input_handle_chat(sint32 key) } } -/** - * - * rct2: 0x006E3B43 - */ -void title_handle_keyboard_input() +static void input_handle_keyboard(bool isTitle) { - rct_window *w; - sint32 key; - if (gOpenRCT2Headless) { return; } if (!gConsoleOpen) { - // Handle modifier keys and key scrolling - gInputPlaceObjectModifier = PLACE_OBJECT_MODIFIER_NONE; - const uint8 * keysState = context_get_keys_state(); - if (keysState[SDL_SCANCODE_LSHIFT] || keysState[SDL_SCANCODE_RSHIFT]) - gInputPlaceObjectModifier |= PLACE_OBJECT_MODIFIER_SHIFT_Z; - if (keysState[SDL_SCANCODE_LCTRL] || keysState[SDL_SCANCODE_RCTRL]) - gInputPlaceObjectModifier |= PLACE_OBJECT_MODIFIER_COPY_Z; - if (keysState[SDL_SCANCODE_LALT] || keysState[SDL_SCANCODE_RALT]) - gInputPlaceObjectModifier |= 4; -#ifdef __MACOSX__ - if (keysState[SDL_SCANCODE_LGUI] || keysState[SDL_SCANCODE_RGUI]) { - gInputPlaceObjectModifier |= 8; - } -#endif - } - - while ((key = get_next_key()) != 0) { - if (key == 255) - continue; - - // Reserve backtick for console - if (key == SDL_SCANCODE_GRAVE) { - if ((gConfigGeneral.debugging_tools && !context_is_input_active()) || gConsoleOpen) { - window_cancel_textbox(); - console_toggle(); - } - continue; - } else if (gConsoleOpen) { - input_handle_console(key); - continue; - } - - key |= gInputPlaceObjectModifier << 8; - - w = window_find_by_class(WC_CHANGE_KEYBOARD_SHORTCUT); - if (w != NULL) { - // keyboard_shortcut_set(key); - } else { - w = window_find_by_class(WC_TEXTINPUT); - if (w != NULL) { - window_text_input_key(w, key); - } - else if (!gUsingWidgetTextBox) { - // keyboard_shortcut_handle(key); - } - } - } -} - -/** - * - * rct2: 0x006E3B43 - */ -void game_handle_keyboard_input() -{ - rct_window *w; - sint32 key; - - if (gOpenRCT2Headless) { - return; - } - - if (!gConsoleOpen) { - // Handle mouse scrolling - if (_inputState == INPUT_STATE_NORMAL && gConfigGeneral.edge_scrolling) { - if (!(gInputPlaceObjectModifier & (PLACE_OBJECT_MODIFIER_SHIFT_Z | PLACE_OBJECT_MODIFIER_COPY_Z))) { - game_handle_edge_scroll(); + if (!isTitle) { + // Handle mouse scrolling + if (_inputState == INPUT_STATE_NORMAL && gConfigGeneral.edge_scrolling) { + if (!(gInputPlaceObjectModifier & (PLACE_OBJECT_MODIFIER_SHIFT_Z | PLACE_OBJECT_MODIFIER_COPY_Z))) { + game_handle_edge_scroll(); + } } } @@ -1535,11 +1467,13 @@ void game_handle_keyboard_input() gInputPlaceObjectModifier |= 8; } #endif - game_handle_key_scroll(); + if (!isTitle) { + game_handle_key_scroll(); + } } - // Handle key input + sint32 key; while (!gOpenRCT2Headless && (key = get_next_key()) != 0) { if (key == 255) continue; @@ -1554,27 +1488,40 @@ void game_handle_keyboard_input() } else if (gConsoleOpen) { input_handle_console(key); continue; - } else if (gChatOpen) { + } else if (!isTitle && gChatOpen) { input_handle_chat(key); continue; } key |= gInputPlaceObjectModifier << 8; - w = window_find_by_class(WC_CHANGE_KEYBOARD_SHORTCUT); + rct_window * w = window_find_by_class(WC_TEXTINPUT); if (w != NULL) { - // keyboard_shortcut_set(key); - } else { - w = window_find_by_class(WC_TEXTINPUT); - if (w != NULL) { - window_text_input_key(w, key); - } else if (!gUsingWidgetTextBox) { - // keyboard_shortcut_handle(key); - } + window_text_input_key(w, key); + } else if (!gUsingWidgetTextBox) { + context_handle_keyboard_shortcut(key); } } } +/** + * + * rct2: 0x006E3B43 + */ +void title_handle_keyboard_input() +{ + input_handle_keyboard(true); +} + +/** + * + * rct2: 0x006E3B43 + */ +void game_handle_keyboard_input() +{ + input_handle_keyboard(false); +} + /** * * rct2: 0x00406CD2 @@ -1704,45 +1651,8 @@ void game_handle_key_scroll() scrollX = 0; scrollY = 0; - -// const uint8 * keysState = context_get_keys_state(); -// for (sint32 shortcutId = SHORTCUT_SCROLL_MAP_UP; shortcutId <= SHORTCUT_SCROLL_MAP_RIGHT; shortcutId++) { -// uint16 shortcutKey = gShortcutKeys[shortcutId]; -// uint8 scancode = shortcutKey & 0xFF; -// -// if (shortcutKey == 0xFFFF) continue; -// if (!keysState[scancode]) continue; -// -// if (shortcutKey & SHIFT) { -// if (!keysState[SDL_SCANCODE_LSHIFT] && !keysState[SDL_SCANCODE_RSHIFT]) continue; -// } -// if (shortcutKey & CTRL) { -// if (!keysState[SDL_SCANCODE_LCTRL] && !keysState[SDL_SCANCODE_RCTRL]) continue; -// } -// if (shortcutKey & ALT) { -// if (!keysState[SDL_SCANCODE_LALT] && !keysState[SDL_SCANCODE_RALT]) continue; -// } -// #ifdef __MACOSX__ -// if (shortcutKey & CMD) { -// if (!keysState[SDL_SCANCODE_LGUI] && !keysState[SDL_SCANCODE_RGUI]) continue; -// } -// #endif -// -// switch (shortcutId) { -// case SHORTCUT_SCROLL_MAP_UP: -// scrollY = -1; -// break; -// case SHORTCUT_SCROLL_MAP_LEFT: -// scrollX = -1; -// break; -// case SHORTCUT_SCROLL_MAP_DOWN: -// scrollY = 1; -// break; -// case SHORTCUT_SCROLL_MAP_RIGHT: -// scrollX = 1; -// break; -// } -// } + const uint8 * keysState = context_get_keys_state(); + context_get_keyboard_map_scroll(keysState, &scrollX, &scrollY); // Scroll viewport if (scrollX != 0) { diff --git a/src/openrct2/ui/DummyWindowManager.cpp b/src/openrct2/ui/DummyWindowManager.cpp index 5ffdc361e9..8d3102fc54 100644 --- a/src/openrct2/ui/DummyWindowManager.cpp +++ b/src/openrct2/ui/DummyWindowManager.cpp @@ -21,6 +21,8 @@ namespace OpenRCT2 { namespace Ui class DummyWindowManager final : public IWindowManager { rct_window * OpenWindow(rct_windowclass wc) override { return nullptr; } + void HandleKeyboardShortcut(sint32 key) override { } + void GetKeyboardMapScroll(const uint8 * keysState, sint32 * x, sint32 * y) override { } }; IWindowManager * CreateDummyWindowManager() diff --git a/src/openrct2/ui/WindowManager.h b/src/openrct2/ui/WindowManager.h index 96db0dae17..12bbb6aef9 100644 --- a/src/openrct2/ui/WindowManager.h +++ b/src/openrct2/ui/WindowManager.h @@ -34,6 +34,9 @@ namespace OpenRCT2 { virtual ~IWindowManager() = default; virtual rct_window * OpenWindow(rct_windowclass wc) abstract; + + virtual void HandleKeyboardShortcut(sint32 key) abstract; + virtual void GetKeyboardMapScroll(const uint8 * keysState, sint32 * x, sint32 * y) abstract; }; IWindowManager * CreateDummyWindowManager();