mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-15 11:03:00 +01:00
Refactor and fix shortcut
This commit is contained in:
@@ -9,7 +9,6 @@
|
||||
|
||||
#include "WindowManager.h"
|
||||
|
||||
#include "input/Input.h"
|
||||
#include "input/KeyboardShortcuts.h"
|
||||
#include "interface/Theme.h"
|
||||
#include "windows/Window.h"
|
||||
@@ -517,8 +516,6 @@ public:
|
||||
|
||||
void HandleKeyboard(bool isTitle) override
|
||||
{
|
||||
InputHandleKeyboard(isTitle);
|
||||
|
||||
auto& inputManager = GetInputManager();
|
||||
inputManager.Process();
|
||||
}
|
||||
|
||||
@@ -1,126 +0,0 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2020 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#include "Input.h"
|
||||
|
||||
#include "../UiContext.h"
|
||||
#include "../interface/InGameConsole.h"
|
||||
#include "KeyboardShortcuts.h"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <cctype>
|
||||
#include <openrct2-ui/windows/Window.h>
|
||||
#include <openrct2/Context.h>
|
||||
#include <openrct2/Input.h>
|
||||
#include <openrct2/OpenRCT2.h>
|
||||
#include <openrct2/common.h>
|
||||
#include <openrct2/config/Config.h>
|
||||
#include <openrct2/interface/Chat.h>
|
||||
#include <openrct2/interface/InteractiveConsole.h>
|
||||
#include <openrct2/paint/VirtualFloor.h>
|
||||
|
||||
using namespace OpenRCT2::Ui;
|
||||
|
||||
static void InputHandleConsole(int32_t key)
|
||||
{
|
||||
ConsoleInput input = ConsoleInput::None;
|
||||
switch (key)
|
||||
{
|
||||
case SDL_SCANCODE_ESCAPE:
|
||||
input = ConsoleInput::LineClear;
|
||||
break;
|
||||
case SDL_SCANCODE_RETURN:
|
||||
case SDL_SCANCODE_KP_ENTER:
|
||||
input = ConsoleInput::LineExecute;
|
||||
break;
|
||||
case SDL_SCANCODE_UP:
|
||||
input = ConsoleInput::HistoryPrevious;
|
||||
break;
|
||||
case SDL_SCANCODE_DOWN:
|
||||
input = ConsoleInput::HistoryNext;
|
||||
break;
|
||||
case SDL_SCANCODE_PAGEUP:
|
||||
input = ConsoleInput::ScrollPrevious;
|
||||
break;
|
||||
case SDL_SCANCODE_PAGEDOWN:
|
||||
input = ConsoleInput::ScrollNext;
|
||||
break;
|
||||
}
|
||||
if (input != ConsoleInput::None)
|
||||
{
|
||||
auto& console = GetInGameConsole();
|
||||
console.Input(input);
|
||||
}
|
||||
}
|
||||
|
||||
static void InputHandleChat(int32_t key)
|
||||
{
|
||||
ChatInput input = ChatInput::None;
|
||||
switch (key)
|
||||
{
|
||||
case SDL_SCANCODE_ESCAPE:
|
||||
input = ChatInput::Close;
|
||||
break;
|
||||
case SDL_SCANCODE_RETURN:
|
||||
case SDL_SCANCODE_KP_ENTER:
|
||||
input = ChatInput::Send;
|
||||
break;
|
||||
}
|
||||
if (input != ChatInput::None)
|
||||
{
|
||||
chat_input(input);
|
||||
}
|
||||
}
|
||||
|
||||
static void GameHandleKeyScroll()
|
||||
{
|
||||
rct_window* mainWindow;
|
||||
|
||||
mainWindow = window_get_main();
|
||||
if (mainWindow == nullptr)
|
||||
return;
|
||||
if ((mainWindow->flags & WF_NO_SCROLLING) || (gScreenFlags & (SCREEN_FLAGS_TRACK_MANAGER | SCREEN_FLAGS_TITLE_DEMO)))
|
||||
return;
|
||||
if (mainWindow->viewport == nullptr)
|
||||
return;
|
||||
|
||||
rct_window* textWindow;
|
||||
|
||||
textWindow = window_find_by_class(WC_TEXTINPUT);
|
||||
if (textWindow || gUsingWidgetTextBox)
|
||||
return;
|
||||
if (gChatOpen)
|
||||
return;
|
||||
|
||||
const uint8_t* keysState = context_get_keys_state();
|
||||
auto scrollCoords = GetKeyboardMapScroll(keysState);
|
||||
|
||||
if (scrollCoords.x != 0 || scrollCoords.y != 0)
|
||||
{
|
||||
window_unfollow_sprite(mainWindow);
|
||||
}
|
||||
InputScrollViewport(scrollCoords);
|
||||
}
|
||||
|
||||
static int32_t InputScancodeToRCTKeycode(int32_t sdl_key)
|
||||
{
|
||||
char keycode = static_cast<char>(SDL_GetKeyFromScancode(static_cast<SDL_Scancode>(sdl_key)));
|
||||
|
||||
// Until we reshuffle the text files to use the new positions
|
||||
// this will suffice to move the majority to the correct positions.
|
||||
// Note any special buttons PgUp PgDwn are mapped wrong.
|
||||
if (keycode >= 'a' && keycode <= 'z')
|
||||
keycode = toupper(keycode);
|
||||
|
||||
return keycode;
|
||||
}
|
||||
|
||||
void InputHandleKeyboard(bool isTitle)
|
||||
{
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2020 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
void InputHandleKeyboard(bool isTitle);
|
||||
@@ -31,15 +31,12 @@ void InputManager::QueueInputEvent(InputEvent&& e)
|
||||
void InputManager::Process()
|
||||
{
|
||||
HandleModifiers();
|
||||
HandleMouseEdgeScrolling();
|
||||
ProcessEvents();
|
||||
HandleViewScrolling();
|
||||
}
|
||||
|
||||
void InputManager::HandleMouseEdgeScrolling()
|
||||
void InputManager::HandleViewScrolling()
|
||||
{
|
||||
if (!gConfigGeneral.edge_scrolling)
|
||||
return;
|
||||
|
||||
if (gScreenFlags & SCREEN_FLAGS_TITLE_DEMO)
|
||||
return;
|
||||
|
||||
@@ -47,13 +44,25 @@ void InputManager::HandleMouseEdgeScrolling()
|
||||
if (console.IsOpen())
|
||||
return;
|
||||
|
||||
if (input_get_state() != InputState::Normal)
|
||||
return;
|
||||
// Shortcut scrolling
|
||||
auto mainWindow = window_get_main();
|
||||
if (mainWindow != nullptr && _viewScroll.x != 0 || _viewScroll.y != 0)
|
||||
{
|
||||
window_unfollow_sprite(mainWindow);
|
||||
}
|
||||
InputScrollViewport(_viewScroll);
|
||||
|
||||
if (gInputPlaceObjectModifier & (PLACE_OBJECT_MODIFIER_SHIFT_Z | PLACE_OBJECT_MODIFIER_COPY_Z))
|
||||
return;
|
||||
// Mouse edge scrolling
|
||||
if (gConfigGeneral.edge_scrolling)
|
||||
{
|
||||
if (input_get_state() != InputState::Normal)
|
||||
return;
|
||||
|
||||
GameHandleEdgeScroll();
|
||||
if (gInputPlaceObjectModifier & (PLACE_OBJECT_MODIFIER_SHIFT_Z | PLACE_OBJECT_MODIFIER_COPY_Z))
|
||||
return;
|
||||
|
||||
GameHandleEdgeScroll();
|
||||
}
|
||||
}
|
||||
|
||||
void InputManager::HandleModifiers()
|
||||
@@ -101,38 +110,43 @@ void InputManager::ProcessEvents()
|
||||
void InputManager::Process(const InputEvent& e)
|
||||
{
|
||||
auto& shortcutManager = GetShortcutManager();
|
||||
auto& console = GetInGameConsole();
|
||||
if (console.IsOpen())
|
||||
if (e.DeviceKind == InputDeviceKind::Keyboard)
|
||||
{
|
||||
if (!shortcutManager.ProcessEventForSpecificShortcut(e, SHORTCUT_ID_DEBUG_CONSOLE))
|
||||
auto& console = GetInGameConsole();
|
||||
if (console.IsOpen())
|
||||
{
|
||||
ProcessInGameConsole(e);
|
||||
}
|
||||
}
|
||||
else if (gChatOpen)
|
||||
{
|
||||
ProcessChat(e);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (e.DeviceKind == InputDeviceKind::Keyboard)
|
||||
{
|
||||
auto w = window_find_by_class(WC_TEXTINPUT);
|
||||
if (w != nullptr)
|
||||
if (!shortcutManager.ProcessEventForSpecificShortcut(e, SHORTCUT_ID_DEBUG_CONSOLE))
|
||||
{
|
||||
if (e.State == InputEventState::Release)
|
||||
ProcessInGameConsole(e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if (gChatOpen)
|
||||
{
|
||||
ProcessChat(e);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (e.DeviceKind == InputDeviceKind::Keyboard)
|
||||
{
|
||||
auto w = window_find_by_class(WC_TEXTINPUT);
|
||||
if (w != nullptr)
|
||||
{
|
||||
window_text_input_key(w, e.Button);
|
||||
if (e.State == InputEventState::Release)
|
||||
{
|
||||
window_text_input_key(w, e.Button);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if (gUsingWidgetTextBox)
|
||||
{
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if (!gUsingWidgetTextBox)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
shortcutManager.ProcessEvent(e);
|
||||
}
|
||||
shortcutManager.ProcessEvent(e);
|
||||
}
|
||||
|
||||
void InputManager::ProcessInGameConsole(const InputEvent& e)
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <openrct2/world/Location.hpp>
|
||||
#include <queue>
|
||||
|
||||
namespace OpenRCT2::Ui
|
||||
@@ -38,8 +39,9 @@ namespace OpenRCT2::Ui
|
||||
{
|
||||
private:
|
||||
std::queue<InputEvent> _events;
|
||||
ScreenCoordsXY _viewScroll;
|
||||
|
||||
void HandleMouseEdgeScrolling();
|
||||
void HandleViewScrolling();
|
||||
void HandleModifiers();
|
||||
void ProcessEvents();
|
||||
void Process(const InputEvent& e);
|
||||
|
||||
@@ -707,7 +707,10 @@ void ShortcutManager::RegisterDefaultShortcuts()
|
||||
RegisterShortcut("multiplayer.chat", STR_SEND_MESSAGE, "C", []() {
|
||||
if (!(gScreenFlags & SCREEN_FLAGS_TITLE_DEMO))
|
||||
{
|
||||
chat_toggle();
|
||||
if (chat_available())
|
||||
{
|
||||
chat_toggle();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ ShortcutInput::ShortcutInput(const std::string_view& value)
|
||||
auto sepIndex = FindPlus(value, index);
|
||||
while (sepIndex != std::string::npos)
|
||||
{
|
||||
auto text = value.substr(index, sepIndex);
|
||||
auto text = value.substr(index, sepIndex - index);
|
||||
auto mod = ParseModifier(text);
|
||||
modifiers |= mod;
|
||||
index = sepIndex + 1;
|
||||
@@ -283,22 +283,52 @@ bool ShortcutInput::AppendModifier(std::string& s, const std::string_view& text,
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ShortcutInput::Matches(const InputEvent& e) const
|
||||
static bool HasModifier(uint32_t shortcut, uint32_t actual, uint32_t left, uint32_t right)
|
||||
{
|
||||
auto modifiers = e.Modifiers & UsefulModifiers;
|
||||
if (e.DeviceKind == InputDeviceKind::Mouse)
|
||||
if (shortcut & (left | right))
|
||||
{
|
||||
if (Kind == ShortcutInputKind::Mouse && Modifiers == modifiers && Key == e.Button)
|
||||
if ((shortcut & left) && (actual & left))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (e.DeviceKind == InputDeviceKind::Keyboard)
|
||||
{
|
||||
if (Kind == ShortcutInputKind::Keyboard && Modifiers == modifiers && Key == e.Button)
|
||||
if ((shortcut & right) && (actual & right))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
else if (actual & (left | right))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool CompareModifiers(uint32_t shortcut, uint32_t actual)
|
||||
{
|
||||
shortcut &= UsefulModifiers;
|
||||
return HasModifier(shortcut, actual, KMOD_LCTRL, KMOD_RCTRL) && HasModifier(shortcut, actual, KMOD_LSHIFT, KMOD_RSHIFT)
|
||||
&& HasModifier(shortcut, actual, KMOD_LALT, KMOD_RALT) && HasModifier(shortcut, actual, KMOD_LGUI, KMOD_RGUI);
|
||||
}
|
||||
|
||||
bool ShortcutInput::Matches(const InputEvent& e) const
|
||||
{
|
||||
if (CompareModifiers(Modifiers, e.Modifiers))
|
||||
{
|
||||
if (e.DeviceKind == InputDeviceKind::Mouse)
|
||||
{
|
||||
if (Kind == ShortcutInputKind::Mouse && Key == e.Button)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (e.DeviceKind == InputDeviceKind::Keyboard)
|
||||
{
|
||||
if (Kind == ShortcutInputKind::Keyboard && Key == e.Button)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,6 @@
|
||||
<ClInclude Include="drawing\engines\opengl\SwapFramebuffer.h" />
|
||||
<ClInclude Include="drawing\engines\opengl\TextureCache.h" />
|
||||
<ClInclude Include="drawing\engines\opengl\TransparencyDepth.h" />
|
||||
<ClInclude Include="input\Input.h" />
|
||||
<ClInclude Include="input\InputManager.h" />
|
||||
<ClInclude Include="input\KeyboardShortcuts.h" />
|
||||
<ClInclude Include="input\ShortcutManager.h" />
|
||||
@@ -92,7 +91,6 @@
|
||||
<ClCompile Include="drawing\engines\opengl\TextureCache.cpp" />
|
||||
<ClCompile Include="drawing\engines\opengl\TransparencyDepth.cpp" />
|
||||
<ClCompile Include="drawing\engines\SoftwareDrawingEngine.cpp" />
|
||||
<ClCompile Include="input\Input.cpp" />
|
||||
<ClCompile Include="input\InputManager.cpp" />
|
||||
<ClCompile Include="input\KeyboardShortcut.cpp" />
|
||||
<ClCompile Include="input\KeyboardShortcuts.cpp" />
|
||||
|
||||
Reference in New Issue
Block a user