1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-06 06:32:56 +01:00

Rework modifier key state into strong enum class (#22558)

* Rework modifier key state into strong enum class

* Move modifier key state into InputManager
This commit is contained in:
Aaron van Geffen
2024-08-14 16:36:05 +02:00
committed by GitHub
parent 0c97e44be0
commit 2a99b2f1f9
12 changed files with 74 additions and 65 deletions

View File

@@ -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())

View File

@@ -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<uint8_t> _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();

View File

@@ -10,13 +10,15 @@
#include "MouseInput.h"
#include "../UiStringIds.h"
#include "../interface/ViewportInteraction.h"
#include <cassert>
#include <cmath>
#include <iterator>
#include <openrct2-ui/UiContext.h>
#include <openrct2-ui/input/InputManager.h>
#include <openrct2-ui/interface/Dropdown.h>
#include <openrct2-ui/interface/Viewport.h>
#include <openrct2-ui/interface/ViewportInteraction.h>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/interface/Window.h>
#include <openrct2-ui/windows/Window.h>
@@ -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();

View File

@@ -7,11 +7,12 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "../interface/Viewport.h"
#include <openrct2-ui/UiContext.h>
#include <openrct2-ui/input/InputManager.h>
#include <openrct2-ui/input/MouseInput.h>
#include <openrct2-ui/interface/Dropdown.h>
#include <openrct2-ui/interface/LandTool.h>
#include <openrct2-ui/interface/Viewport.h>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Context.h>
@@ -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();

View File

@@ -7,8 +7,9 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "../interface/Theme.h"
#include <openrct2-ui/UiContext.h>
#include <openrct2-ui/input/InputManager.h>
#include <openrct2-ui/interface/Theme.h>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Context.h>
@@ -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>(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);
}

View File

@@ -7,13 +7,14 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "../interface/ViewportInteraction.h"
#include "../ride/Construction.h"
#include <limits>
#include <openrct2-ui/UiContext.h>
#include <openrct2-ui/input/InputManager.h>
#include <openrct2-ui/interface/Dropdown.h>
#include <openrct2-ui/interface/Viewport.h>
#include <openrct2-ui/interface/ViewportInteraction.h>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/ride/Construction.h>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Cheats.h>
#include <openrct2/Context.h>
@@ -2921,10 +2922,11 @@ static Widget _rideConstructionWidgets[] = {
static std::optional<CoordsXY> 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<decltype(TileElement::BaseHeight)>::max() - 32);

View File

@@ -6,11 +6,13 @@
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "../interface/ViewportInteraction.h"
#include <deque>
#include <openrct2-ui/UiContext.h>
#include <openrct2-ui/input/InputManager.h>
#include <openrct2-ui/interface/Dropdown.h>
#include <openrct2-ui/interface/Viewport.h>
#include <openrct2-ui/interface/ViewportInteraction.h>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Context.h>
@@ -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);

View File

@@ -7,11 +7,12 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "../interface/ViewportQuery.h"
#include <limits>
#include <openrct2-ui/UiContext.h>
#include <openrct2-ui/input/InputManager.h>
#include <openrct2-ui/interface/Dropdown.h>
#include <openrct2-ui/interface/Viewport.h>
#include <openrct2-ui/interface/ViewportQuery.h>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Context.h>
@@ -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;
}

View File

@@ -7,11 +7,12 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "../UiStringIds.h"
#include "../interface/Viewport.h"
#include <iterator>
#include <openrct2-ui/UiContext.h>
#include <openrct2-ui/UiStringIds.h>
#include <openrct2-ui/input/InputManager.h>
#include <openrct2-ui/interface/Dropdown.h>
#include <openrct2-ui/interface/Viewport.h>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Game.h>
@@ -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;

View File

@@ -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();

View File

@@ -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;
}

View File

@@ -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();