diff --git a/src/openrct2-ui/input/InputManager.cpp b/src/openrct2-ui/input/InputManager.cpp index 38f03d0e4f..5c4bb40572 100644 --- a/src/openrct2-ui/input/InputManager.cpp +++ b/src/openrct2-ui/input/InputManager.cpp @@ -27,6 +27,11 @@ using namespace OpenRCT2::Ui; +InputManager::InputManager() +{ + _modifierKeyState = EnumValue(ModifierKey::none); +} + void InputManager::QueueInputEvent(const SDL_Event& e) { switch (e.type) @@ -126,7 +131,7 @@ void InputManager::HandleViewScrolling() if (InputGetState() != InputState::Normal) return; - if (gInputPlaceObjectModifier & (PLACE_OBJECT_MODIFIER_SHIFT_Z | PLACE_OBJECT_MODIFIER_COPY_Z)) + if (IsModifierKeyPressed(ModifierKey::shift) || IsModifierKeyPressed(ModifierKey::ctrl)) return; GameHandleEdgeScroll(); @@ -135,36 +140,42 @@ void InputManager::HandleViewScrolling() void InputManager::HandleModifiers() { + _modifierKeyState = EnumValue(ModifierKey::none); + auto modifiers = SDL_GetModState(); - gInputPlaceObjectModifier = PLACE_OBJECT_MODIFIER_NONE; if (modifiers & KMOD_SHIFT) { - gInputPlaceObjectModifier |= PLACE_OBJECT_MODIFIER_SHIFT_Z; + _modifierKeyState |= EnumValue(ModifierKey::shift); } if (modifiers & KMOD_CTRL) { - gInputPlaceObjectModifier |= PLACE_OBJECT_MODIFIER_COPY_Z; + _modifierKeyState |= EnumValue(ModifierKey::ctrl); } if (modifiers & KMOD_ALT) { - gInputPlaceObjectModifier |= 4; + _modifierKeyState |= EnumValue(ModifierKey::alt); } #ifdef __MACOSX__ if (modifiers & KMOD_GUI) { - gInputPlaceObjectModifier |= 8; + _modifierKeyState |= EnumValue(ModifierKey::cmd); } #endif if (Config::Get().general.VirtualFloorStyle != VirtualFloorStyles::Off) { - if (gInputPlaceObjectModifier & (PLACE_OBJECT_MODIFIER_COPY_Z | PLACE_OBJECT_MODIFIER_SHIFT_Z)) + if (IsModifierKeyPressed(ModifierKey::ctrl) || IsModifierKeyPressed(ModifierKey::shift)) VirtualFloorEnable(); else VirtualFloorDisable(); } } +bool InputManager::IsModifierKeyPressed(ModifierKey modifier) const +{ + return _modifierKeyState & EnumValue(modifier); +} + void InputManager::ProcessEvents() { while (!_events.empty()) diff --git a/src/openrct2-ui/input/InputManager.h b/src/openrct2-ui/input/InputManager.h index 4937dad5dc..4cd8fef1eb 100644 --- a/src/openrct2-ui/input/InputManager.h +++ b/src/openrct2-ui/input/InputManager.h @@ -43,6 +43,15 @@ namespace OpenRCT2::Ui InputEventState State; }; + enum class ModifierKey : uint8_t + { + none = 0, + shift = 1 << 0, + ctrl = 1 << 1, + alt = 1 << 2, + cmd = 1 << 3, + }; + class InputManager { private: @@ -52,6 +61,7 @@ namespace OpenRCT2::Ui ScreenCoordsXY _viewScroll; uint32_t _mouseState{}; std::vector _keyboardState; + uint8_t _modifierKeyState; void CheckJoysticks(); @@ -70,6 +80,9 @@ namespace OpenRCT2::Ui bool HasTextInputFocus() const; public: + InputManager(); + + bool IsModifierKeyPressed(ModifierKey modifier) const; void QueueInputEvent(const SDL_Event& e); void QueueInputEvent(InputEvent&& e); void Process(); diff --git a/src/openrct2-ui/input/MouseInput.cpp b/src/openrct2-ui/input/MouseInput.cpp index 612addf5fe..5ea6805e5b 100644 --- a/src/openrct2-ui/input/MouseInput.cpp +++ b/src/openrct2-ui/input/MouseInput.cpp @@ -10,13 +10,15 @@ #include "MouseInput.h" #include "../UiStringIds.h" -#include "../interface/ViewportInteraction.h" #include #include #include +#include +#include #include #include +#include #include #include #include @@ -1290,13 +1292,14 @@ void InputStateWidgetPressed( if (w->widgets[widgetIndex].type == WindowWidgetType::CloseBox && cursor_w_class == w->classification && cursor_w_number == w->number && widgetIndex == cursor_widgetIndex) { - if (gInputPlaceObjectModifier & PLACE_OBJECT_MODIFIER_SHIFT_Z) + auto& im = GetInputManager(); + if (im.IsModifierKeyPressed(ModifierKey::shift)) { gLastCloseModifier.window.number = w->number; gLastCloseModifier.window.classification = w->classification; gLastCloseModifier.modifier = CloseWindowModifier::Shift; } - else if (gInputPlaceObjectModifier & PLACE_OBJECT_MODIFIER_COPY_Z) + else if (im.IsModifierKeyPressed(ModifierKey::ctrl)) { gLastCloseModifier.window.number = w->number; gLastCloseModifier.window.classification = w->classification; @@ -1645,11 +1648,6 @@ void GameHandleEdgeScroll() InputScrollViewport(ScreenCoordsXY(scrollX, scrollY)); } -bool InputTestPlaceObjectModifier(PLACE_OBJECT_MODIFIER modifier) -{ - return gInputPlaceObjectModifier & modifier; -} - void InputScrollViewport(const ScreenCoordsXY& scrollScreenCoords) { WindowBase* mainWindow = WindowGetMain(); diff --git a/src/openrct2-ui/windows/Land.cpp b/src/openrct2-ui/windows/Land.cpp index 830b66a25b..84152b69c9 100644 --- a/src/openrct2-ui/windows/Land.cpp +++ b/src/openrct2-ui/windows/Land.cpp @@ -7,11 +7,12 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ -#include "../interface/Viewport.h" - +#include +#include #include #include #include +#include #include #include #include @@ -601,7 +602,7 @@ static Widget window_land_widgets[] = { */ void ToolUpdateLand(const ScreenCoordsXY& screenPos) { - const bool mapCtrlPressed = InputTestPlaceObjectModifier(PLACE_OBJECT_MODIFIER_COPY_Z); + const bool mapCtrlPressed = GetInputManager().IsModifierKeyPressed(ModifierKey::ctrl); MapInvalidateSelectionRect(); diff --git a/src/openrct2-ui/windows/MapTooltip.cpp b/src/openrct2-ui/windows/MapTooltip.cpp index a7fd5b1dbc..41a3a95938 100644 --- a/src/openrct2-ui/windows/MapTooltip.cpp +++ b/src/openrct2-ui/windows/MapTooltip.cpp @@ -7,8 +7,9 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ -#include "../interface/Theme.h" - +#include +#include +#include #include #include #include @@ -96,10 +97,9 @@ static Widget window_map_tooltip_widgets[] = { StringId stringId; std::memcpy(&stringId, _mapTooltipArgs.Data(), sizeof(StringId)); - if (_cursorHoldDuration < 25 || stringId == STR_NONE - || InputTestPlaceObjectModifier( - static_cast(PLACE_OBJECT_MODIFIER_COPY_Z | PLACE_OBJECT_MODIFIER_SHIFT_Z)) - || WindowFindByClass(WindowClass::Error) != nullptr) + auto& im = GetInputManager(); + if (_cursorHoldDuration < 25 || stringId == STR_NONE || im.IsModifierKeyPressed(ModifierKey::ctrl) + || im.IsModifierKeyPressed(ModifierKey::shift) || WindowFindByClass(WindowClass::Error) != nullptr) { WindowCloseByClass(WindowClass::MapTooltip); } diff --git a/src/openrct2-ui/windows/RideConstruction.cpp b/src/openrct2-ui/windows/RideConstruction.cpp index a4674ac97f..6e36ccf071 100644 --- a/src/openrct2-ui/windows/RideConstruction.cpp +++ b/src/openrct2-ui/windows/RideConstruction.cpp @@ -7,13 +7,14 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ -#include "../interface/ViewportInteraction.h" -#include "../ride/Construction.h" - #include +#include +#include #include #include +#include #include +#include #include #include #include @@ -2921,10 +2922,11 @@ static Widget _rideConstructionWidgets[] = { static std::optional RideGetPlacePositionFromScreenPosition(ScreenCoordsXY screenCoords) { CoordsXY mapCoords; + auto& im = GetInputManager(); if (!_trackPlaceCtrlState) { - if (gInputPlaceObjectModifier & PLACE_OBJECT_MODIFIER_COPY_Z) + if (im.IsModifierKeyPressed(ModifierKey::ctrl)) { auto info = GetMapCoordinatesFromPos(screenCoords, 0xFCCA); if (info.SpriteType != ViewportInteractionItem::None) @@ -2936,7 +2938,7 @@ static Widget _rideConstructionWidgets[] = { } else { - if (!(gInputPlaceObjectModifier & PLACE_OBJECT_MODIFIER_COPY_Z)) + if (!(im.IsModifierKeyPressed(ModifierKey::ctrl))) { _trackPlaceCtrlState = false; } @@ -2944,7 +2946,7 @@ static Widget _rideConstructionWidgets[] = { if (!_trackPlaceShiftState) { - if (gInputPlaceObjectModifier & PLACE_OBJECT_MODIFIER_SHIFT_Z) + if (im.IsModifierKeyPressed(ModifierKey::shift)) { _trackPlaceShiftState = true; _trackPlaceShiftStart = screenCoords; @@ -2953,7 +2955,7 @@ static Widget _rideConstructionWidgets[] = { } else { - if (gInputPlaceObjectModifier & PLACE_OBJECT_MODIFIER_SHIFT_Z) + if (im.IsModifierKeyPressed(ModifierKey::shift)) { uint16_t maxHeight = ZoomLevel::max().ApplyTo( std::numeric_limits::max() - 32); diff --git a/src/openrct2-ui/windows/Scenery.cpp b/src/openrct2-ui/windows/Scenery.cpp index 90678c0439..62dde13ad6 100644 --- a/src/openrct2-ui/windows/Scenery.cpp +++ b/src/openrct2-ui/windows/Scenery.cpp @@ -6,11 +6,13 @@ * * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ -#include "../interface/ViewportInteraction.h" #include +#include +#include #include #include +#include #include #include #include @@ -2359,9 +2361,10 @@ static Widget WindowSceneryBaseWidgets[] = { } else { + auto& im = GetInputManager(); if (!gSceneryCtrlPressed) { - if (InputTestPlaceObjectModifier(PLACE_OBJECT_MODIFIER_COPY_Z)) + if (im.IsModifierKeyPressed(ModifierKey::ctrl)) { // CTRL pressed constexpr auto flag = EnumsToFlags( @@ -2379,7 +2382,7 @@ static Widget WindowSceneryBaseWidgets[] = { } else { - if (!(InputTestPlaceObjectModifier(PLACE_OBJECT_MODIFIER_COPY_Z))) + if (!(im.IsModifierKeyPressed(ModifierKey::ctrl))) { // CTRL not pressed gSceneryCtrlPressed = false; @@ -2388,7 +2391,7 @@ static Widget WindowSceneryBaseWidgets[] = { if (!gSceneryShiftPressed) { - if (InputTestPlaceObjectModifier(PLACE_OBJECT_MODIFIER_SHIFT_Z)) + if (im.IsModifierKeyPressed(ModifierKey::shift)) { // SHIFT pressed gSceneryShiftPressed = true; @@ -2399,7 +2402,7 @@ static Widget WindowSceneryBaseWidgets[] = { } else { - if (InputTestPlaceObjectModifier(PLACE_OBJECT_MODIFIER_SHIFT_Z)) + if (im.IsModifierKeyPressed(ModifierKey::shift)) { // SHIFT pressed gSceneryShiftPressZOffset = (gSceneryShiftPressY - screenPos.y + 4); diff --git a/src/openrct2-ui/windows/StaffList.cpp b/src/openrct2-ui/windows/StaffList.cpp index fd3f1511b1..7744b3edd7 100644 --- a/src/openrct2-ui/windows/StaffList.cpp +++ b/src/openrct2-ui/windows/StaffList.cpp @@ -7,11 +7,12 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ -#include "../interface/ViewportQuery.h" - #include +#include +#include #include #include +#include #include #include #include @@ -526,7 +527,7 @@ static Widget _staffListWidgets[] = { void HireNewMember(StaffType staffType, EntertainerCostume entertainerType) { bool autoPosition = Config::Get().general.AutoStaffPlacement; - if (gInputPlaceObjectModifier & PLACE_OBJECT_MODIFIER_SHIFT_Z) + if (GetInputManager().IsModifierKeyPressed(ModifierKey::shift)) { autoPosition = autoPosition ^ 1; } diff --git a/src/openrct2-ui/windows/TileInspector.cpp b/src/openrct2-ui/windows/TileInspector.cpp index d3fc4a4504..5daf9b1a00 100644 --- a/src/openrct2-ui/windows/TileInspector.cpp +++ b/src/openrct2-ui/windows/TileInspector.cpp @@ -7,11 +7,12 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ -#include "../UiStringIds.h" -#include "../interface/Viewport.h" - #include +#include +#include +#include #include +#include #include #include #include @@ -958,7 +959,7 @@ static uint64_t PageDisabledWidgets[] = { CoordsXY mapCoords; TileElement* clickedElement = nullptr; bool mouseOnViewport = false; - if (InputTestPlaceObjectModifier(PLACE_OBJECT_MODIFIER_COPY_Z)) + if (GetInputManager().IsModifierKeyPressed(ModifierKey::ctrl)) { auto info = GetMapCoordinatesFromPos(screenCoords, ViewportInteractionFlags); clickedElement = info.Element; @@ -1772,7 +1773,7 @@ static uint64_t PageDisabledWidgets[] = { void UpdateSelectedTile(const ScreenCoordsXY& screenCoords) { - const bool ctrlIsHeldDown = InputTestPlaceObjectModifier(PLACE_OBJECT_MODIFIER_COPY_Z); + const bool ctrlIsHeldDown = GetInputManager().IsModifierKeyPressed(ModifierKey::ctrl); // Mouse hasn't moved if (screenCoords.x == _toolMouseX && screenCoords.y == _toolMouseY && _toolCtrlDown == ctrlIsHeldDown) return; diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index a59dcad317..6078d210fd 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -19,7 +19,6 @@ #include "Game.h" #include "GameState.h" #include "GameStateSnapshots.h" -#include "Input.h" #include "OpenRCT2.h" #include "ParkImporter.h" #include "PlatformEnvironment.h" @@ -525,7 +524,6 @@ namespace OpenRCT2 LightFXInit(); } - InputResetPlaceObjModifier(); ViewportInitAll(); ContextInit(); diff --git a/src/openrct2/Input.cpp b/src/openrct2/Input.cpp index 32336ef4ea..4e26a5972b 100644 --- a/src/openrct2/Input.cpp +++ b/src/openrct2/Input.cpp @@ -14,7 +14,6 @@ InputState _inputState; uint8_t _inputFlags; -uint8_t gInputPlaceObjectModifier; WidgetRef gHoverWidget; WidgetRef gPressedWidget; @@ -75,8 +74,3 @@ void ResetTooltipNotShown() { _tooltipNotShownTimeout = gCurrentRealTimeTicks + 50; } - -void InputResetPlaceObjModifier() -{ - gInputPlaceObjectModifier = PLACE_OBJECT_MODIFIER_NONE; -} diff --git a/src/openrct2/Input.h b/src/openrct2/Input.h index 8be8c268ee..453abb8ef9 100644 --- a/src/openrct2/Input.h +++ b/src/openrct2/Input.h @@ -50,15 +50,6 @@ enum class InputState ScrollRight }; -enum PLACE_OBJECT_MODIFIER -{ - PLACE_OBJECT_MODIFIER_NONE = 0, - PLACE_OBJECT_MODIFIER_SHIFT_Z = (1 << 0), - PLACE_OBJECT_MODIFIER_COPY_Z = (1 << 1), -}; - -extern uint8_t gInputPlaceObjectModifier; - extern WidgetRef gHoverWidget; extern WidgetRef gPressedWidget; @@ -78,11 +69,7 @@ void InputSetFlag(INPUT_FLAGS flag, bool on); bool InputTestFlag(INPUT_FLAGS flag); void InputResetFlags(); -bool InputTestPlaceObjectModifier(PLACE_OBJECT_MODIFIER modifier); - void InputSetState(InputState state); InputState InputGetState(); void ResetTooltipNotShown(); - -void InputResetPlaceObjModifier();