From 2cd3add37346ad5ab116773e060fbe0f4df00bf3 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Sat, 14 Dec 2019 23:30:55 -0300 Subject: [PATCH 1/3] Make CursorState store a ScreenCoordsXY object --- src/openrct2-ui/UiContext.cpp | 8 +++--- src/openrct2-ui/input/MouseInput.cpp | 11 ++++---- src/openrct2-ui/interface/Window.cpp | 6 ++--- .../windows/EditorObjectSelection.cpp | 2 +- src/openrct2-ui/windows/Error.cpp | 4 +-- src/openrct2-ui/windows/MapTooltip.cpp | 27 +++++++------------ src/openrct2-ui/windows/RideConstruction.cpp | 4 +-- src/openrct2-ui/windows/Scenery.cpp | 9 +++---- src/openrct2/Context.h | 2 +- 9 files changed, 32 insertions(+), 41 deletions(-) diff --git a/src/openrct2-ui/UiContext.cpp b/src/openrct2-ui/UiContext.cpp index cc3ee8e5f5..5b7b90fdbd 100644 --- a/src/openrct2-ui/UiContext.cpp +++ b/src/openrct2-ui/UiContext.cpp @@ -367,8 +367,8 @@ public: } break; case SDL_MOUSEMOTION: - _cursorState.x = (int32_t)(e.motion.x / gConfigGeneral.window_scale); - _cursorState.y = (int32_t)(e.motion.y / gConfigGeneral.window_scale); + _cursorState.position = { static_cast(e.motion.x / gConfigGeneral.window_scale), + static_cast(e.motion.y / gConfigGeneral.window_scale) }; break; case SDL_MOUSEWHEEL: if (_inGameConsole.IsOpen()) @@ -435,8 +435,8 @@ public: // Apple sends touchscreen events for trackpads, so ignore these events on macOS #ifndef __MACOSX__ case SDL_FINGERMOTION: - _cursorState.x = (int32_t)(e.tfinger.x * _width); - _cursorState.y = (int32_t)(e.tfinger.y * _height); + _cursorState.position = { static_cast(e.tfinger.x * _width), + static_cast(e.tfinger.y * _height) }; break; case SDL_FINGERDOWN: { diff --git a/src/openrct2-ui/input/MouseInput.cpp b/src/openrct2-ui/input/MouseInput.cpp index d4676baefc..832acf6316 100644 --- a/src/openrct2-ui/input/MouseInput.cpp +++ b/src/openrct2-ui/input/MouseInput.cpp @@ -145,8 +145,7 @@ static int32_t game_get_next_input(ScreenCoordsXY& screenCoords) if (input == nullptr) { const CursorState* cursorState = context_get_cursor_state(); - screenCoords.x = cursorState->x; - screenCoords.y = cursorState->y; + screenCoords = cursorState->position; return 0; } else @@ -1578,15 +1577,15 @@ void game_handle_edge_scroll() // Scroll left / right const CursorState* cursorState = context_get_cursor_state(); - if (cursorState->x == 0) + if (cursorState->position.x == 0) scrollX = -1; - else if (cursorState->x >= context_get_width() - 1) + else if (cursorState->position.x >= context_get_width() - 1) scrollX = 1; // Scroll up / down - if (cursorState->y == 0) + if (cursorState->position.y == 0) scrollY = -1; - else if (cursorState->y >= context_get_height() - 1) + else if (cursorState->position.y >= context_get_height() - 1) scrollY = 1; input_scroll_viewport(ScreenCoordsXY(scrollX, scrollY)); diff --git a/src/openrct2-ui/interface/Window.cpp b/src/openrct2-ui/interface/Window.cpp index 1069f7d771..8340fb5c92 100644 --- a/src/openrct2-ui/interface/Window.cpp +++ b/src/openrct2-ui/interface/Window.cpp @@ -533,7 +533,7 @@ static bool window_other_wheel_input(rct_window* w, rct_widgetindex widgetIndex, void window_all_wheel_input() { // Get wheel value - CursorState* cursorState = (CursorState*)context_get_cursor_state(); + auto cursorState = context_get_cursor_state(); int32_t absolute_wheel = cursorState->wheel; int32_t relative_wheel = absolute_wheel - _previousAbsoluteWheel; int32_t pixel_scroll = relative_wheel * WINDOW_SCROLL_PIXELS; @@ -545,7 +545,7 @@ void window_all_wheel_input() // Check window cursor is over if (!(input_test_flag(INPUT_FLAG_5))) { - rct_window* w = window_find_from_point(ScreenCoordsXY(cursorState->x, cursorState->y)); + rct_window* w = window_find_from_point(cursorState->position); if (w != nullptr) { // Check if main window @@ -556,7 +556,7 @@ void window_all_wheel_input() } // Check scroll view, cursor is over - rct_widgetindex widgetIndex = window_find_widget_from_point(w, ScreenCoordsXY(cursorState->x, cursorState->y)); + rct_widgetindex widgetIndex = window_find_widget_from_point(w, cursorState->position); if (widgetIndex != -1) { rct_widget* widget = &w->widgets[widgetIndex]; diff --git a/src/openrct2-ui/windows/EditorObjectSelection.cpp b/src/openrct2-ui/windows/EditorObjectSelection.cpp index 069dc10eaf..f0b44db126 100644 --- a/src/openrct2-ui/windows/EditorObjectSelection.cpp +++ b/src/openrct2-ui/windows/EditorObjectSelection.cpp @@ -698,7 +698,7 @@ static void window_editor_object_selection_scroll_mousedown(rct_window* w, int32 w->Invalidate(); const CursorState* state = context_get_cursor_state(); - audio_play_sound(SoundId::Click1, 0, state->x); + audio_play_sound(SoundId::Click1, 0, state->position.x); if (gScreenFlags & SCREEN_FLAGS_TRACK_MANAGER) { diff --git a/src/openrct2-ui/windows/Error.cpp b/src/openrct2-ui/windows/Error.cpp index 2f20f5dd7c..2ccb0aeb49 100644 --- a/src/openrct2-ui/windows/Error.cpp +++ b/src/openrct2-ui/windows/Error.cpp @@ -126,10 +126,10 @@ rct_window* window_error_open(rct_string_id title, rct_string_id message) int32_t screenWidth = context_get_width(); int32_t screenHeight = context_get_height(); const CursorState* state = context_get_cursor_state(); - x = state->x - (width / 2); + x = state->position.x - (width / 2); x = std::clamp(x, 0, screenWidth); - y = state->y + 26; + y = state->position.y + 26; y = std::max(22, y); maxY = screenHeight - height; if (y > maxY) diff --git a/src/openrct2-ui/windows/MapTooltip.cpp b/src/openrct2-ui/windows/MapTooltip.cpp index 74695e7764..20a24e98b3 100644 --- a/src/openrct2-ui/windows/MapTooltip.cpp +++ b/src/openrct2-ui/windows/MapTooltip.cpp @@ -59,8 +59,7 @@ static rct_window_event_list window_map_tooltip_events = { #define MAP_TOOLTIP_ARGS -static int32_t _lastCursorX; -static int32_t _lastCursorY; +static ScreenCoordsXY _lastCursor; static int32_t _cursorHoldDuration; static void window_map_tooltip_open(); @@ -78,19 +77,15 @@ void window_map_tooltip_update_visibility() return; } - int32_t cursorX, cursorY; - const CursorState* state = context_get_cursor_state(); - cursorX = state->x; - cursorY = state->y; + auto cursor = state->position; // Check for cursor movement _cursorHoldDuration++; - if (abs(cursorX - _lastCursorX) > 5 || abs(cursorY - _lastCursorY) > 5 || (input_test_flag(INPUT_FLAG_5))) + if (abs(cursor.x - _lastCursor.x) > 5 || abs(cursor.y - _lastCursor.y) > 5 || (input_test_flag(INPUT_FLAG_5))) _cursorHoldDuration = 0; - _lastCursorX = cursorX; - _lastCursorY = cursorY; + _lastCursor = cursor; // Show or hide tooltip rct_string_id stringId; @@ -116,27 +111,25 @@ void window_map_tooltip_update_visibility() static void window_map_tooltip_open() { rct_window* w; - int32_t x, y, width, height; - width = 200; - height = 44; + constexpr int32_t width = 200; + constexpr int32_t height = 44; const CursorState* state = context_get_cursor_state(); - x = state->x - (width / 2); - y = state->y + 15; + ScreenCoordsXY pos = { state->position.x - (width / 2), state->position.y + 15 }; w = window_find_by_class(WC_MAP_TOOLTIP); if (w == nullptr) { w = window_create( - ScreenCoordsXY(x, y), width, height, &window_map_tooltip_events, WC_MAP_TOOLTIP, + pos, width, height, &window_map_tooltip_events, WC_MAP_TOOLTIP, WF_STICK_TO_FRONT | WF_TRANSPARENT | WF_NO_BACKGROUND); w->widgets = window_map_tooltip_widgets; } else { w->Invalidate(); - w->x = x; - w->y = y; + w->x = pos.x; + w->y = pos.y; w->width = width; w->height = height; } diff --git a/src/openrct2-ui/windows/RideConstruction.cpp b/src/openrct2-ui/windows/RideConstruction.cpp index 90feaf0f35..060e1b7c8e 100644 --- a/src/openrct2-ui/windows/RideConstruction.cpp +++ b/src/openrct2-ui/windows/RideConstruction.cpp @@ -3841,7 +3841,7 @@ void ride_construction_tooldown_construct(ScreenCoordsXY screenCoords) || errorText == STR_CAN_ONLY_BUILD_THIS_ABOVE_GROUND || errorText == STR_TOO_HIGH_FOR_SUPPORTS || zAttempts == 0 || z < 0) { - audio_play_sound(SoundId::Error, 0, state->x); + audio_play_sound(SoundId::Error, 0, state->position.x); w = window_find_by_class(WC_RIDE_CONSTRUCTION); if (w != nullptr) { @@ -3914,7 +3914,7 @@ void ride_construction_tooldown_construct(ScreenCoordsXY screenCoords) _currentTrackAlternative = saveCurrentTrackAlternative; _currentTrackLiftHill = saveCurrentTrackLiftHill; - audio_play_sound(SoundId::Error, 0, state->x); + audio_play_sound(SoundId::Error, 0, state->position.x); break; } else if (zAttempts >= 0) diff --git a/src/openrct2-ui/windows/Scenery.cpp b/src/openrct2-ui/windows/Scenery.cpp index 57b673690a..708997c7db 100644 --- a/src/openrct2-ui/windows/Scenery.cpp +++ b/src/openrct2-ui/windows/Scenery.cpp @@ -748,15 +748,14 @@ static void window_scenery_periodic_update(rct_window* w) static void window_scenery_update(rct_window* w) { const CursorState* state = context_get_cursor_state(); - rct_window* other = window_find_from_point(ScreenCoordsXY(state->x, state->y)); + rct_window* other = window_find_from_point(state->position); if (other == w) { - int32_t window_x = state->x - w->x + 26; - int32_t window_y = state->y - w->y; + ScreenCoordsXY window = state->position - ScreenCoordsXY{ w->x + 26, w->y }; - if (window_y < 44 || window_x <= w->width) + if (window.y < 44 || window.x <= w->width) { - rct_widgetindex widgetIndex = window_find_widget_from_point(w, ScreenCoordsXY(state->x, state->y)); + rct_widgetindex widgetIndex = window_find_widget_from_point(w, state->position); if (widgetIndex >= WIDX_SCENERY_TAB_CONTENT_PANEL) { w->scenery.hover_counter++; diff --git a/src/openrct2/Context.h b/src/openrct2/Context.h index 44767bfbcf..490eb8b959 100644 --- a/src/openrct2/Context.h +++ b/src/openrct2/Context.h @@ -28,7 +28,7 @@ using rct_windowclass = uint8_t; struct CursorState { - int32_t x, y; + ScreenCoordsXY position; uint8_t left, middle, right, any; int32_t wheel; int32_t old; From 972a8735e893b20d20c3d31396dec7c3540d1780 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Tue, 17 Dec 2019 23:32:34 -0300 Subject: [PATCH 2/3] Fix arithmetic error in Scenery::window_scenery_update() --- src/openrct2-ui/windows/Scenery.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openrct2-ui/windows/Scenery.cpp b/src/openrct2-ui/windows/Scenery.cpp index 708997c7db..faa3f152ae 100644 --- a/src/openrct2-ui/windows/Scenery.cpp +++ b/src/openrct2-ui/windows/Scenery.cpp @@ -751,7 +751,7 @@ static void window_scenery_update(rct_window* w) rct_window* other = window_find_from_point(state->position); if (other == w) { - ScreenCoordsXY window = state->position - ScreenCoordsXY{ w->x + 26, w->y }; + ScreenCoordsXY window = state->position - ScreenCoordsXY{ w->x - 26, w->y }; if (window.y < 44 || window.x <= w->width) { From 8613edee4a6e18678b97bec2e80c8794c4ea8c86 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Tue, 17 Dec 2019 23:33:07 -0300 Subject: [PATCH 3/3] Use ScreenCoordsXY::operator- to improve readability --- src/openrct2-ui/windows/Error.cpp | 17 +++++++---------- src/openrct2-ui/windows/MapTooltip.cpp | 3 ++- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/openrct2-ui/windows/Error.cpp b/src/openrct2-ui/windows/Error.cpp index 2ccb0aeb49..7938027846 100644 --- a/src/openrct2-ui/windows/Error.cpp +++ b/src/openrct2-ui/windows/Error.cpp @@ -75,7 +75,7 @@ static uint16_t _window_error_num_lines; rct_window* window_error_open(rct_string_id title, rct_string_id message) { utf8* dst; - int32_t numLines, fontHeight, x, y, width, height, maxY; + int32_t numLines, fontHeight, width, height, maxY; rct_window* w; window_close_by_class(WC_ERROR); @@ -126,20 +126,17 @@ rct_window* window_error_open(rct_string_id title, rct_string_id message) int32_t screenWidth = context_get_width(); int32_t screenHeight = context_get_height(); const CursorState* state = context_get_cursor_state(); - x = state->position.x - (width / 2); - x = std::clamp(x, 0, screenWidth); - - y = state->position.y + 26; - y = std::max(22, y); + ScreenCoordsXY windowPosition = state->position - ScreenCoordsXY(width / 2, -26); + windowPosition.x = std::clamp(windowPosition.x, 0, screenWidth); + windowPosition.y = std::max(22, windowPosition.y); maxY = screenHeight - height; - if (y > maxY) + if (windowPosition.y > maxY) { - y = y - height - 40; - y = std::min(y, maxY); + windowPosition.y = std::min(windowPosition.y - height - 40, maxY); } w = window_create( - ScreenCoordsXY(x, y), width, height, &window_error_events, WC_ERROR, WF_STICK_TO_FRONT | WF_TRANSPARENT | WF_RESIZABLE); + windowPosition, width, height, &window_error_events, WC_ERROR, WF_STICK_TO_FRONT | WF_TRANSPARENT | WF_RESIZABLE); w->widgets = window_error_widgets; w->error.var_480 = 0; if (!gDisableErrorWindowSound) diff --git a/src/openrct2-ui/windows/MapTooltip.cpp b/src/openrct2-ui/windows/MapTooltip.cpp index 20a24e98b3..35e60e857a 100644 --- a/src/openrct2-ui/windows/MapTooltip.cpp +++ b/src/openrct2-ui/windows/MapTooltip.cpp @@ -79,10 +79,11 @@ void window_map_tooltip_update_visibility() const CursorState* state = context_get_cursor_state(); auto cursor = state->position; + auto cursorChange = cursor - _lastCursor; // Check for cursor movement _cursorHoldDuration++; - if (abs(cursor.x - _lastCursor.x) > 5 || abs(cursor.y - _lastCursor.y) > 5 || (input_test_flag(INPUT_FLAG_5))) + if (abs(cursorChange.x) > 5 || abs(cursorChange.y) > 5 || (input_test_flag(INPUT_FLAG_5))) _cursorHoldDuration = 0; _lastCursor = cursor;