From af0fc89cf411644ae9df226f3a99cecd597bdc3f Mon Sep 17 00:00:00 2001 From: frutiemax Date: Mon, 27 Jul 2020 18:51:10 -0400 Subject: [PATCH] Close #12456: Refactor INPUT_STATE to use strong enum (#12484) --- src/openrct2-ui/input/Input.cpp | 2 +- src/openrct2-ui/input/MouseInput.cpp | 72 ++++++++++++++-------------- src/openrct2-ui/interface/Widget.cpp | 2 +- src/openrct2-ui/windows/Dropdown.cpp | 4 +- src/openrct2-ui/windows/Scenery.cpp | 4 +- src/openrct2-ui/windows/Tooltip.cpp | 2 +- src/openrct2/GameState.cpp | 2 +- src/openrct2/Input.cpp | 6 +-- src/openrct2/Input.h | 28 +++++------ src/openrct2/interface/Viewport.cpp | 2 +- 10 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/openrct2-ui/input/Input.cpp b/src/openrct2-ui/input/Input.cpp index 18048505bb..d44a214ecc 100644 --- a/src/openrct2-ui/input/Input.cpp +++ b/src/openrct2-ui/input/Input.cpp @@ -134,7 +134,7 @@ void input_handle_keyboard(bool isTitle) if (!isTitle) { // Handle mouse scrolling - if (input_get_state() == INPUT_STATE_NORMAL && gConfigGeneral.edge_scrolling) + if (input_get_state() == InputState::Normal && gConfigGeneral.edge_scrolling) { if (!(gInputPlaceObjectModifier & (PLACE_OBJECT_MODIFIER_SHIFT_Z | PLACE_OBJECT_MODIFIER_COPY_Z))) { diff --git a/src/openrct2-ui/input/MouseInput.cpp b/src/openrct2-ui/input/MouseInput.cpp index eb552c904a..c0d3e4abc5 100644 --- a/src/openrct2-ui/input/MouseInput.cpp +++ b/src/openrct2-ui/input/MouseInput.cpp @@ -181,7 +181,7 @@ static rct_mouse_data* get_mouse_input() */ static void input_scroll_drag_begin(const ScreenCoordsXY& screenCoords, rct_window* w, rct_widgetindex widgetIndex) { - _inputState = INPUT_STATE_SCROLL_RIGHT; + _inputState = InputState::ScrollRight; gInputDragLast = screenCoords; _dragWidget.window_classification = w->classification; _dragWidget.window_number = w->number; @@ -243,7 +243,7 @@ static void input_scroll_right(const ScreenCoordsXY& screenCoords, int32_t state if (w == nullptr) { context_show_cursor(); - _inputState = INPUT_STATE_RESET; + _inputState = InputState::Reset; return; } @@ -258,7 +258,7 @@ static void input_scroll_right(const ScreenCoordsXY& screenCoords, int32_t state } break; case MOUSE_STATE_RIGHT_RELEASE: - _inputState = INPUT_STATE_RESET; + _inputState = InputState::Reset; context_show_cursor(); break; } @@ -281,10 +281,10 @@ static void game_handle_input_mouse(const ScreenCoordsXY& screenCoords, int32_t switch (_inputState) { - case INPUT_STATE_RESET: + case InputState::Reset: window_tooltip_reset(screenCoords); // fall-through - case INPUT_STATE_NORMAL: + case InputState::Normal: switch (state) { case MOUSE_STATE_RELEASED: @@ -319,14 +319,14 @@ static void game_handle_input_mouse(const ScreenCoordsXY& screenCoords, int32_t break; } break; - case INPUT_STATE_WIDGET_PRESSED: + case InputState::WidgetPressed: input_state_widget_pressed(screenCoords, state, widgetIndex, w, widget); break; - case INPUT_STATE_POSITIONING_WINDOW: + case InputState::PositioningWindow: w = window_find_by_number(_dragWidget.window_classification, _dragWidget.window_number); if (w == nullptr) { - _inputState = INPUT_STATE_RESET; + _inputState = InputState::Reset; } else { @@ -337,7 +337,7 @@ static void game_handle_input_mouse(const ScreenCoordsXY& screenCoords, int32_t } } break; - case INPUT_STATE_VIEWPORT_RIGHT: + case InputState::ViewportRight: if (state == MOUSE_STATE_RELEASED) { input_viewport_drag_continue(); @@ -352,14 +352,14 @@ static void game_handle_input_mouse(const ScreenCoordsXY& screenCoords, int32_t } } break; - case INPUT_STATE_DROPDOWN_ACTIVE: + case InputState::DropdownActive: input_state_widget_pressed(screenCoords, state, widgetIndex, w, widget); break; - case INPUT_STATE_VIEWPORT_LEFT: + case InputState::ViewportLeft: w = window_find_by_number(_dragWidget.window_classification, _dragWidget.window_number); if (w == nullptr) { - _inputState = INPUT_STATE_RESET; + _inputState = InputState::Reset; break; } @@ -368,7 +368,7 @@ static void game_handle_input_mouse(const ScreenCoordsXY& screenCoords, int32_t case MOUSE_STATE_RELEASED: if (w->viewport == nullptr) { - _inputState = INPUT_STATE_RESET; + _inputState = InputState::Reset; break; } @@ -387,7 +387,7 @@ static void game_handle_input_mouse(const ScreenCoordsXY& screenCoords, int32_t window_event_tool_drag_call(w, gCurrentToolWidget.widget_index, screenCoords); break; case MOUSE_STATE_LEFT_RELEASE: - _inputState = INPUT_STATE_RESET; + _inputState = InputState::Reset; if (_dragWidget.window_number == w->number) { if ((_inputFlags & INPUT_FLAG_TOOL_ACTIVE)) @@ -407,7 +407,7 @@ static void game_handle_input_mouse(const ScreenCoordsXY& screenCoords, int32_t break; } break; - case INPUT_STATE_SCROLL_LEFT: + case InputState::ScrollLeft: switch (state) { case MOUSE_STATE_RELEASED: @@ -418,11 +418,11 @@ static void game_handle_input_mouse(const ScreenCoordsXY& screenCoords, int32_t break; } break; - case INPUT_STATE_RESIZING: + case InputState::Resizing: w = window_find_by_number(_dragWidget.window_classification, _dragWidget.window_number); if (w == nullptr) { - _inputState = INPUT_STATE_RESET; + _inputState = InputState::Reset; } else { @@ -436,7 +436,7 @@ static void game_handle_input_mouse(const ScreenCoordsXY& screenCoords, int32_t } } break; - case INPUT_STATE_SCROLL_RIGHT: + case InputState::ScrollRight: input_scroll_right(screenCoords, state); break; } @@ -446,7 +446,7 @@ static void game_handle_input_mouse(const ScreenCoordsXY& screenCoords, int32_t void input_window_position_begin(rct_window* w, rct_widgetindex widgetIndex, const ScreenCoordsXY& screenCoords) { - _inputState = INPUT_STATE_POSITIONING_WINDOW; + _inputState = InputState::PositioningWindow; gInputDragLast = screenCoords - w->windowPos; _dragWidget.window_classification = w->classification; _dragWidget.window_number = w->number; @@ -464,7 +464,7 @@ static void input_window_position_continue( static void input_window_position_end(rct_window* w, const ScreenCoordsXY& screenCoords) { - _inputState = INPUT_STATE_NORMAL; + _inputState = InputState::Normal; gTooltipTimeout = 0; gTooltipWidget = _dragWidget; window_event_moved_call(w, screenCoords); @@ -472,7 +472,7 @@ static void input_window_position_end(rct_window* w, const ScreenCoordsXY& scree static void input_window_resize_begin(rct_window* w, rct_widgetindex widgetIndex, const ScreenCoordsXY& screenCoords) { - _inputState = INPUT_STATE_RESIZING; + _inputState = InputState::Resizing; gInputDragLast = screenCoords; _dragWidget.window_classification = w->classification; _dragWidget.window_number = w->number; @@ -495,7 +495,7 @@ static void input_window_resize_continue(rct_window* w, const ScreenCoordsXY& sc static void input_window_resize_end() { - _inputState = INPUT_STATE_NORMAL; + _inputState = InputState::Normal; gTooltipTimeout = 0; gTooltipWidget = _dragWidget; } @@ -507,7 +507,7 @@ static void input_window_resize_end() static void input_viewport_drag_begin(rct_window* w) { w->flags &= ~WF_SCROLLING_TO_LOCATION; - _inputState = INPUT_STATE_VIEWPORT_RIGHT; + _inputState = InputState::ViewportRight; _dragWidget.window_classification = w->classification; _dragWidget.window_number = w->number; _ticksSinceDragStart = 0; @@ -543,7 +543,7 @@ static void input_viewport_drag_continue() if (viewport == nullptr) { context_show_cursor(); - _inputState = INPUT_STATE_RESET; + _inputState = InputState::Reset; } else if (differentialCoords.x != 0 || differentialCoords.y != 0) { @@ -580,7 +580,7 @@ static void input_viewport_drag_continue() static void input_viewport_drag_end() { - _inputState = INPUT_STATE_RESET; + _inputState = InputState::Reset; context_show_cursor(); } @@ -594,7 +594,7 @@ static void input_scroll_begin(rct_window* w, rct_widgetindex widgetIndex, const widget = &w->widgets[widgetIndex]; - _inputState = INPUT_STATE_SCROLL_LEFT; + _inputState = InputState::ScrollLeft; gPressedWidget.window_classification = w->classification; gPressedWidget.window_number = w->number; gPressedWidget.widget_index = widgetIndex; @@ -723,7 +723,7 @@ static void input_scroll_continue(rct_window* w, rct_widgetindex widgetIndex, co static void input_scroll_end() { - _inputState = INPUT_STATE_RESET; + _inputState = InputState::Reset; invalidate_scroll(); } @@ -1029,7 +1029,7 @@ static void input_widget_left(const ScreenCoordsXY& screenCoords, rct_window* w, input_window_resize_begin(w, widgetIndex, screenCoords); break; case WWT_VIEWPORT: - _inputState = INPUT_STATE_VIEWPORT_LEFT; + _inputState = InputState::ViewportLeft; gInputDragLast = screenCoords; _dragWidget.window_classification = windowClass; _dragWidget.window_number = windowNumber; @@ -1059,7 +1059,7 @@ static void input_widget_left(const ScreenCoordsXY& screenCoords, rct_window* w, gPressedWidget.window_number = windowNumber; gPressedWidget.widget_index = widgetIndex; _inputFlags |= INPUT_FLAG_WIDGET_PRESSED; - _inputState = INPUT_STATE_WIDGET_PRESSED; + _inputState = InputState::WidgetPressed; _clickRepeatTicks = 1; widget_invalidate_by_number(windowClass, windowNumber, widgetIndex); @@ -1204,7 +1204,7 @@ void input_state_widget_pressed( rct_window* cursor_w = window_find_by_number(cursor_w_class, cursor_w_number); if (cursor_w == nullptr) { - _inputState = INPUT_STATE_RESET; + _inputState = InputState::Reset; return; } @@ -1233,7 +1233,7 @@ void input_state_widget_pressed( if (_inputFlags & INPUT_FLAG_WIDGET_PRESSED) { - if (_inputState == INPUT_STATE_DROPDOWN_ACTIVE) + if (_inputState == InputState::DropdownActive) { gDropdownHighlightedIndex = gDropdownDefaultIndex; window_invalidate_by_class(WC_DROPDOWN); @@ -1246,7 +1246,7 @@ void input_state_widget_pressed( return; case MOUSE_STATE_LEFT_RELEASE: case MOUSE_STATE_RIGHT_PRESS: - if (_inputState == INPUT_STATE_DROPDOWN_ACTIVE) + if (_inputState == InputState::DropdownActive) { if (w) { @@ -1300,7 +1300,7 @@ void input_state_widget_pressed( widget_invalidate_by_number(cursor_w_class, cursor_w_number, cursor_widgetIndex); } - _inputState = INPUT_STATE_NORMAL; + _inputState = InputState::Normal; gTooltipTimeout = 0; gTooltipWidget.widget_index = cursor_widgetIndex; gTooltipWidget.window_classification = cursor_w_class; @@ -1318,7 +1318,7 @@ void input_state_widget_pressed( } } - _inputState = INPUT_STATE_NORMAL; + _inputState = InputState::Normal; if (state == MOUSE_STATE_RIGHT_PRESS) { @@ -1353,7 +1353,7 @@ void input_state_widget_pressed( } _clickRepeatTicks = 0; - if (_inputState != INPUT_STATE_DROPDOWN_ACTIVE) + if (_inputState != InputState::DropdownActive) { // Hold down widget and drag outside of area?? if (_inputFlags & INPUT_FLAG_WIDGET_PRESSED) @@ -1505,7 +1505,7 @@ int32_t get_next_key() */ void set_cursor(uint8_t cursor_id) { - if (_inputState == INPUT_STATE_RESIZING) + if (_inputState == InputState::Resizing) { cursor_id = CURSOR_DIAGONAL_ARROWS; } diff --git a/src/openrct2-ui/interface/Widget.cpp b/src/openrct2-ui/interface/Widget.cpp index 4e58a5d557..3d304b3f54 100644 --- a/src/openrct2-ui/interface/Widget.cpp +++ b/src/openrct2-ui/interface/Widget.cpp @@ -840,7 +840,7 @@ bool widget_is_pressed(rct_window* w, rct_widgetindex widgetIndex) { return true; } - if (input_get_state() == INPUT_STATE_WIDGET_PRESSED || input_get_state() == INPUT_STATE_DROPDOWN_ACTIVE) + if (input_get_state() == InputState::WidgetPressed || input_get_state() == InputState::DropdownActive) { if (!(input_test_flag(INPUT_FLAG_WIDGET_PRESSED))) return false; diff --git a/src/openrct2-ui/windows/Dropdown.cpp b/src/openrct2-ui/windows/Dropdown.cpp index 77d9d32158..673e927e9c 100644 --- a/src/openrct2-ui/windows/Dropdown.cpp +++ b/src/openrct2-ui/windows/Dropdown.cpp @@ -223,7 +223,7 @@ void window_dropdown_show_text_custom_width( _dropdownItemsChecked.reset(); gDropdownIsColour = false; gDropdownDefaultIndex = -1; - input_set_state(INPUT_STATE_DROPDOWN_ACTIVE); + input_set_state(InputState::DropdownActive); } /** @@ -303,7 +303,7 @@ void window_dropdown_show_image( _dropdownItemsChecked.reset(); gDropdownIsColour = false; gDropdownDefaultIndex = -1; - input_set_state(INPUT_STATE_DROPDOWN_ACTIVE); + input_set_state(InputState::DropdownActive); } void window_dropdown_close() diff --git a/src/openrct2-ui/windows/Scenery.cpp b/src/openrct2-ui/windows/Scenery.cpp index 76acf22d43..61f16433fc 100644 --- a/src/openrct2-ui/windows/Scenery.cpp +++ b/src/openrct2-ui/windows/Scenery.cpp @@ -766,7 +766,7 @@ static void window_scenery_update(rct_window* w) w->scenery.hover_counter++; if (w->scenery.hover_counter < 8) { - if (input_get_state() != INPUT_STATE_SCROLL_LEFT) + if (input_get_state() != InputState::ScrollLeft) { w->min_width = WINDOW_SCENERY_WIDTH; w->max_width = WINDOW_SCENERY_WIDTH; @@ -792,7 +792,7 @@ static void window_scenery_update(rct_window* w) else { w->scenery.hover_counter = 0; - if (input_get_state() != INPUT_STATE_SCROLL_LEFT) + if (input_get_state() != InputState::ScrollLeft) { w->min_width = WINDOW_SCENERY_WIDTH; w->max_width = WINDOW_SCENERY_WIDTH; diff --git a/src/openrct2-ui/windows/Tooltip.cpp b/src/openrct2-ui/windows/Tooltip.cpp index 8af18fd840..6b168b614c 100644 --- a/src/openrct2-ui/windows/Tooltip.cpp +++ b/src/openrct2-ui/windows/Tooltip.cpp @@ -69,7 +69,7 @@ void window_tooltip_reset(const ScreenCoordsXY& screenCoords) gTooltipCursorY = screenCoords.y; gTooltipTimeout = 0; gTooltipWidget.window_classification = 255; - input_set_state(INPUT_STATE_NORMAL); + input_set_state(InputState::Normal); input_set_flag(INPUT_FLAG_4, false); } diff --git a/src/openrct2/GameState.cpp b/src/openrct2/GameState.cpp index 7981126eb2..d6bce9d87c 100644 --- a/src/openrct2/GameState.cpp +++ b/src/openrct2/GameState.cpp @@ -165,7 +165,7 @@ void GameState::Update() UpdateLogic(); if (gGameSpeed == 1) { - if (input_get_state() == INPUT_STATE_RESET || input_get_state() == INPUT_STATE_NORMAL) + if (input_get_state() == InputState::Reset || input_get_state() == InputState::Normal) { if (input_test_flag(INPUT_FLAG_VIEWPORT_SCROLLING)) { diff --git a/src/openrct2/Input.cpp b/src/openrct2/Input.cpp index 76a0dee4ae..48e4a17179 100644 --- a/src/openrct2/Input.cpp +++ b/src/openrct2/Input.cpp @@ -11,7 +11,7 @@ #include "Context.h" -INPUT_STATE _inputState; +InputState _inputState; uint8_t _inputFlags; uint8_t gInputPlaceObjectModifier; @@ -63,12 +63,12 @@ void input_reset_flags() _inputFlags = 0; } -void input_set_state(INPUT_STATE state) +void input_set_state(InputState state) { _inputState = state; } -INPUT_STATE input_get_state() +InputState input_get_state() { return _inputState; } diff --git a/src/openrct2/Input.h b/src/openrct2/Input.h index c0ff594fbb..20022d5756 100644 --- a/src/openrct2/Input.h +++ b/src/openrct2/Input.h @@ -46,18 +46,18 @@ enum MOUSE_STATE MOUSE_STATE_RIGHT_RELEASE }; -enum INPUT_STATE +enum class InputState { - INPUT_STATE_RESET, - INPUT_STATE_NORMAL, - INPUT_STATE_WIDGET_PRESSED, - INPUT_STATE_POSITIONING_WINDOW, - INPUT_STATE_VIEWPORT_RIGHT, - INPUT_STATE_DROPDOWN_ACTIVE, - INPUT_STATE_VIEWPORT_LEFT, - INPUT_STATE_SCROLL_LEFT, - INPUT_STATE_RESIZING, - INPUT_STATE_SCROLL_RIGHT + Reset, + Normal, + WidgetPressed, + PositioningWindow, + ViewportRight, + DropdownActive, + ViewportLeft, + ScrollLeft, + Resizing, + ScrollRight }; enum PLACE_OBJECT_MODIFIER @@ -90,7 +90,7 @@ extern TOOL_IDX gCurrentToolId; extern widget_ref gCurrentToolWidget; // TODO: Move to openrct2-ui and make static again -extern INPUT_STATE _inputState; +extern InputState _inputState; extern uint8_t _inputFlags; extern uint16_t _tooltipNotShownTicks; @@ -110,8 +110,8 @@ void input_reset_flags(); bool input_test_place_object_modifier(PLACE_OBJECT_MODIFIER modifier); -void input_set_state(INPUT_STATE state); -INPUT_STATE input_get_state(); +void input_set_state(InputState state); +InputState input_get_state(); void reset_tooltip_not_shown(); diff --git a/src/openrct2/interface/Viewport.cpp b/src/openrct2/interface/Viewport.cpp index f10aa62679..8c112829bc 100644 --- a/src/openrct2/interface/Viewport.cpp +++ b/src/openrct2/interface/Viewport.cpp @@ -83,7 +83,7 @@ void viewport_init_all() // ? input_reset_flags(); - input_set_state(INPUT_STATE_RESET); + input_set_state(InputState::Reset); gPressedWidget.window_classification = 255; gPickupPeepImage = UINT32_MAX; reset_tooltip_not_shown();