Files
OpenRCT2-Web/src/openrct2-ui/input/MouseInput.cpp
T

1683 lines
55 KiB
C++
Raw Normal View History

2017-12-07 22:16:20 +01:00
/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 OpenRCT2 developers
2017-12-07 22:16:20 +01:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2017-12-07 22:16:20 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2017-12-07 22:16:20 +01:00
*****************************************************************************/
2018-08-12 12:50:40 +01:00
#include <algorithm>
2018-03-14 15:25:34 +01:00
#include <cmath>
2018-11-21 23:16:04 +01:00
#include <iterator>
2017-12-15 23:48:40 +01:00
#include <openrct2-ui/interface/Dropdown.h>
2018-06-22 23:20:31 +02:00
#include <openrct2-ui/interface/Viewport.h>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/interface/Window.h>
2017-12-15 23:48:40 +01:00
#include <openrct2-ui/windows/Window.h>
2017-12-07 22:16:20 +01:00
#include <openrct2/Context.h>
#include <openrct2/Game.h>
#include <openrct2/Input.h>
2017-12-15 23:48:40 +01:00
#include <openrct2/OpenRCT2.h>
#include <openrct2/audio/audio.h>
#include <openrct2/config/Config.h>
2018-01-05 23:00:39 +00:00
#include <openrct2/interface/Chat.h>
2018-06-22 23:20:31 +02:00
#include <openrct2/interface/Cursors.h>
#include <openrct2/interface/InteractiveConsole.h>
2021-12-11 18:06:06 -05:00
#include <openrct2/localisation/Formatter.h>
2018-01-06 18:32:25 +01:00
#include <openrct2/localisation/Localisation.h>
2022-02-18 21:57:00 +01:00
#include <openrct2/platform/Platform.h>
2018-01-10 00:00:09 +01:00
#include <openrct2/ride/RideData.h>
2018-01-02 17:58:43 +00:00
#include <openrct2/scenario/Scenario.h>
2017-12-14 10:34:12 +01:00
#include <openrct2/world/Banner.h>
2018-01-03 14:56:08 +01:00
#include <openrct2/world/Map.h>
2018-01-11 10:59:26 +01:00
#include <openrct2/world/Scenery.h>
2017-12-07 22:16:20 +01:00
2020-11-02 06:43:35 -07:00
struct RCTMouseData
2017-12-15 23:48:40 +01:00
{
uint32_t x;
uint32_t y;
MouseState state;
2018-02-14 09:42:26 +01:00
};
2017-12-07 22:16:20 +01:00
2020-11-02 06:43:35 -07:00
static RCTMouseData _mouseInputQueue[64];
static uint8_t _mouseInputQueueReadIndex = 0;
static uint8_t _mouseInputQueueWriteIndex = 0;
2017-12-07 22:16:20 +01:00
static uint32_t _ticksSinceDragStart;
2017-12-07 22:16:20 +01:00
static widget_ref _dragWidget;
static uint8_t _dragScrollIndex;
static int32_t _originalWindowWidth;
static int32_t _originalWindowHeight;
2017-12-07 22:16:20 +01:00
static uint8_t _currentScrollIndex;
static uint8_t _currentScrollArea;
2017-12-07 22:16:20 +01:00
ScreenCoordsXY gInputDragLast;
2017-12-07 22:16:20 +01:00
uint16_t gTooltipTimeout;
2017-12-07 22:16:20 +01:00
widget_ref gTooltipWidget;
2020-10-17 09:10:08 +01:00
ScreenCoordsXY gTooltipCursor;
2017-12-07 22:16:20 +01:00
static int16_t _clickRepeatTicks;
2017-12-07 22:16:20 +01:00
static MouseState GameGetNextInput(ScreenCoordsXY& screenCoords);
2022-08-21 18:49:23 +02:00
static void InputWidgetOver(const ScreenCoordsXY& screenCoords, rct_window* w, WidgetIndex widgetIndex);
static void InputWidgetOverChangeCheck(WindowClass windowClass, rct_windownumber windowNumber, WidgetIndex widgetIndex);
2020-11-02 06:43:35 -07:00
static void InputWidgetOverFlatbuttonInvalidate();
void ProcessMouseOver(const ScreenCoordsXY& screenCoords);
void ProcessMouseTool(const ScreenCoordsXY& screenCoords);
void InvalidateScroll();
static RCTMouseData* GetMouseInput();
void TileElementRightClick(int32_t type, TileElement* tileElement, const ScreenCoordsXY& screenCoords);
static void GameHandleInputMouse(const ScreenCoordsXY& screenCoords, MouseState state);
2022-08-21 18:49:23 +02:00
static void InputWidgetLeft(const ScreenCoordsXY& screenCoords, rct_window* w, WidgetIndex widgetIndex);
2020-11-02 06:43:35 -07:00
void InputStateWidgetPressed(
2022-12-24 16:50:29 +01:00
const ScreenCoordsXY& screenCoords, MouseState state, WidgetIndex widgetIndex, rct_window* w, Widget* widget);
2020-11-02 06:43:35 -07:00
void SetCursor(CursorID cursor_id);
static void InputWindowPositionContinue(
rct_window& w, const ScreenCoordsXY& lastScreenCoords, const ScreenCoordsXY& newScreenCoords);
static void InputWindowPositionEnd(rct_window& w, const ScreenCoordsXY& screenCoords);
2022-08-21 18:49:23 +02:00
static void InputWindowResizeBegin(rct_window& w, WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords);
static void InputWindowResizeContinue(rct_window& w, const ScreenCoordsXY& screenCoords);
2020-11-02 06:43:35 -07:00
static void InputWindowResizeEnd();
static void InputViewportDragBegin(rct_window& w);
2020-11-02 06:43:35 -07:00
static void InputViewportDragContinue();
static void InputViewportDragEnd();
2022-08-21 18:49:23 +02:00
static void InputScrollBegin(rct_window& w, WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords);
static void InputScrollContinue(rct_window& w, WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords);
2020-11-02 06:43:35 -07:00
static void InputScrollEnd();
2022-08-21 18:49:23 +02:00
static void InputScrollPartUpdateHThumb(rct_window& w, WidgetIndex widgetIndex, int32_t x, int32_t scroll_id);
static void InputScrollPartUpdateHLeft(rct_window& w, WidgetIndex widgetIndex, int32_t scroll_id);
static void InputScrollPartUpdateHRight(rct_window& w, WidgetIndex widgetIndex, int32_t scroll_id);
static void InputScrollPartUpdateVThumb(rct_window& w, WidgetIndex widgetIndex, int32_t y, int32_t scroll_id);
static void InputScrollPartUpdateVTop(rct_window& w, WidgetIndex widgetIndex, int32_t scroll_id);
static void InputScrollPartUpdateVBottom(rct_window& w, WidgetIndex widgetIndex, int32_t scroll_id);
static void InputUpdateTooltip(rct_window* w, WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords);
2017-12-07 22:16:20 +01:00
#pragma region Mouse input
/**
*
* rct2: 0x006EA627
*/
2020-11-02 06:43:35 -07:00
void GameHandleInput()
2017-12-07 22:16:20 +01:00
{
window_visit_each([](rct_window* w) { window_event_periodic_update_call(w); });
2017-12-07 22:16:20 +01:00
InvalidateAllWindowsAfterInput();
2017-12-07 22:16:20 +01:00
MouseState state;
2019-10-20 14:10:30 -03:00
ScreenCoordsXY screenCoords;
while ((state = GameGetNextInput(screenCoords)) != MouseState::Released)
2017-12-15 23:48:40 +01:00
{
GameHandleInputMouse(screenCoords, state);
2017-12-07 22:16:20 +01:00
}
2017-12-15 23:48:40 +01:00
if (_inputFlags & INPUT_FLAG_5)
{
2020-11-02 06:43:35 -07:00
GameHandleInputMouse(screenCoords, state);
2017-12-15 23:48:40 +01:00
}
else
2017-12-15 23:48:40 +01:00
{
2022-11-06 20:49:07 +00:00
int32_t screenWidth = ContextGetWidth();
int32_t screenHeight = ContextGetHeight();
2019-10-20 14:10:30 -03:00
screenCoords.x = std::clamp(screenCoords.x, 0, screenWidth - 1);
screenCoords.y = std::clamp(screenCoords.y, 0, screenHeight - 1);
2017-12-07 22:16:20 +01:00
2020-11-02 06:43:35 -07:00
GameHandleInputMouse(screenCoords, state);
ProcessMouseOver(screenCoords);
ProcessMouseTool(screenCoords);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E83C7
2017-12-15 23:48:40 +01:00
*/
static MouseState GameGetNextInput(ScreenCoordsXY& screenCoords)
2017-12-07 22:16:20 +01:00
{
2020-11-02 06:43:35 -07:00
RCTMouseData* input = GetMouseInput();
2018-01-04 06:58:44 +01:00
if (input == nullptr)
2017-12-15 23:48:40 +01:00
{
2022-11-06 20:49:07 +00:00
const CursorState* cursorState = ContextGetCursorState();
screenCoords = cursorState->position;
return MouseState::Released;
2017-12-15 23:48:40 +01:00
}
2021-09-15 22:22:15 +02:00
screenCoords.x = input->x;
screenCoords.y = input->y;
return input->state;
2017-12-07 22:16:20 +01:00
}
/**
*
* rct2: 0x00407074
*/
2020-11-02 06:43:35 -07:00
static RCTMouseData* GetMouseInput()
2017-12-07 22:16:20 +01:00
{
// Check if that location has been written to yet
2017-12-15 23:48:40 +01:00
if (_mouseInputQueueReadIndex == _mouseInputQueueWriteIndex)
{
2018-01-04 06:58:44 +01:00
return nullptr;
2017-12-15 23:48:40 +01:00
}
2021-09-15 22:22:15 +02:00
RCTMouseData* result = &_mouseInputQueue[_mouseInputQueueReadIndex];
_mouseInputQueueReadIndex = (_mouseInputQueueReadIndex + 1) % std::size(_mouseInputQueue);
return result;
2017-12-07 22:16:20 +01:00
}
/**
*
* rct2: 0x006E957F
*/
2022-08-21 18:49:23 +02:00
static void InputScrollDragBegin(const ScreenCoordsXY& screenCoords, rct_window* w, WidgetIndex widgetIndex)
2017-12-07 22:16:20 +01:00
{
_inputState = InputState::ScrollRight;
gInputDragLast = screenCoords;
2017-12-07 22:16:20 +01:00
_dragWidget.window_classification = w->classification;
_dragWidget.window_number = w->number;
_dragWidget.widget_index = widgetIndex;
_ticksSinceDragStart = 0;
_dragScrollIndex = window_get_scroll_data_index(*w, widgetIndex);
2022-11-06 20:49:07 +00:00
ContextHideCursor();
2017-12-07 22:16:20 +01:00
}
/**
* Based on (heavily changed)
* rct2: 0x006E9E0E, 0x006E9ED0
*/
2020-11-02 06:43:35 -07:00
static void InputScrollDragContinue(const ScreenCoordsXY& screenCoords, rct_window* w)
2017-12-07 22:16:20 +01:00
{
2022-08-21 18:49:23 +02:00
WidgetIndex widgetIndex = _dragWidget.widget_index;
uint8_t scrollIndex = _dragScrollIndex;
2017-12-07 22:16:20 +01:00
const auto& widget = w->widgets[widgetIndex];
auto& scroll = w->scrolls[scrollIndex];
2017-12-07 22:16:20 +01:00
ScreenCoordsXY differentialCoords = screenCoords - gInputDragLast;
2017-12-07 22:16:20 +01:00
if (scroll.flags & HSCROLLBAR_VISIBLE)
2017-12-15 23:48:40 +01:00
{
int16_t size = widget.width() - 1;
if (scroll.flags & VSCROLLBAR_VISIBLE)
2017-12-07 22:16:20 +01:00
size -= 11;
size = std::max(0, scroll.h_right - size);
scroll.h_left = std::min<uint16_t>(std::max(0, scroll.h_left + differentialCoords.x), size);
2017-12-07 22:16:20 +01:00
}
if (scroll.flags & VSCROLLBAR_VISIBLE)
2017-12-15 23:48:40 +01:00
{
int16_t size = widget.height() - 1;
if (scroll.flags & HSCROLLBAR_VISIBLE)
2017-12-07 22:16:20 +01:00
size -= 11;
size = std::max(0, scroll.v_bottom - size);
scroll.v_top = std::min<uint16_t>(std::max(0, scroll.v_top + differentialCoords.y), size);
2017-12-07 22:16:20 +01:00
}
WidgetScrollUpdateThumbs(*w, widgetIndex);
2017-12-07 22:16:20 +01:00
window_invalidate_by_number(w->classification, w->number);
2022-10-16 17:27:01 +02:00
ScreenCoordsXY fixedCursorPosition = { static_cast<int32_t>(std::ceil(gInputDragLast.x * gConfigGeneral.WindowScale)),
static_cast<int32_t>(std::ceil(gInputDragLast.y * gConfigGeneral.WindowScale)) };
2017-12-07 22:16:20 +01:00
2022-11-06 20:49:07 +00:00
ContextSetCursorPosition(fixedCursorPosition);
2017-12-07 22:16:20 +01:00
}
/**
*
* rct2: 0x006E8ACB
*/
static void InputScrollRight(const ScreenCoordsXY& screenCoords, MouseState state)
2017-12-07 22:16:20 +01:00
{
2018-06-22 23:20:31 +02:00
rct_window* w = window_find_by_number(_dragWidget.window_classification, _dragWidget.window_number);
2018-01-04 06:58:44 +01:00
if (w == nullptr)
2017-12-15 23:48:40 +01:00
{
2022-11-06 20:49:07 +00:00
ContextShowCursor();
_inputState = InputState::Reset;
2017-12-07 22:16:20 +01:00
return;
}
2017-12-15 23:48:40 +01:00
switch (state)
{
case MouseState::Released:
_ticksSinceDragStart += gCurrentDeltaTime;
2019-10-20 14:10:30 -03:00
if (screenCoords.x != 0 || screenCoords.y != 0)
2018-06-22 23:20:31 +02:00
{
_ticksSinceDragStart = 1000;
2020-11-02 06:43:35 -07:00
InputScrollDragContinue(screenCoords, w);
2018-06-22 23:20:31 +02:00
}
break;
case MouseState::RightRelease:
_inputState = InputState::Reset;
2022-11-06 20:49:07 +00:00
ContextShowCursor();
2018-06-22 23:20:31 +02:00
break;
case MouseState::LeftPress:
case MouseState::LeftRelease:
case MouseState::RightPress:
// Function only handles right button, so it's the only one relevant
break;
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E8655
*/
static void GameHandleInputMouse(const ScreenCoordsXY& screenCoords, MouseState state)
2017-12-07 22:16:20 +01:00
{
2018-06-22 23:20:31 +02:00
rct_window* w;
2022-12-24 16:50:29 +01:00
Widget* widget;
2022-08-21 18:49:23 +02:00
WidgetIndex widgetIndex;
2017-12-07 22:16:20 +01:00
// Get window and widget under cursor position
2019-10-20 14:10:30 -03:00
w = window_find_from_point(screenCoords);
widgetIndex = w == nullptr ? -1 : window_find_widget_from_point(*w, screenCoords);
2018-01-04 06:58:44 +01:00
widget = widgetIndex == -1 ? nullptr : &w->widgets[widgetIndex];
2017-12-07 22:16:20 +01:00
2017-12-15 23:48:40 +01:00
switch (_inputState)
{
case InputState::Reset:
WindowTooltipReset(screenCoords);
2018-06-22 23:20:31 +02:00
// fall-through
case InputState::Normal:
2018-06-22 23:20:31 +02:00
switch (state)
2017-12-15 23:48:40 +01:00
{
case MouseState::Released:
2020-11-02 06:43:35 -07:00
InputWidgetOver(screenCoords, w, widgetIndex);
2018-06-22 23:20:31 +02:00
break;
case MouseState::LeftPress:
2020-11-02 06:43:35 -07:00
InputWidgetLeft(screenCoords, w, widgetIndex);
2018-06-22 23:20:31 +02:00
break;
case MouseState::RightPress:
window_close_by_class(WindowClass::Tooltip);
2017-12-07 22:16:20 +01:00
2018-06-22 23:20:31 +02:00
if (w != nullptr)
2017-12-15 23:48:40 +01:00
{
w = window_bring_to_front(*w);
2018-06-22 23:20:31 +02:00
}
if (widgetIndex != -1)
{
switch (widget->type)
{
case WindowWidgetType::Viewport:
2018-06-22 23:20:31 +02:00
if (!(gScreenFlags & (SCREEN_FLAGS_TRACK_MANAGER | SCREEN_FLAGS_TITLE_DEMO)))
{
InputViewportDragBegin(*w);
2018-06-22 23:20:31 +02:00
}
break;
case WindowWidgetType::Scroll:
2020-11-02 06:43:35 -07:00
InputScrollDragBegin(screenCoords, w, widgetIndex);
2018-06-22 23:20:31 +02:00
break;
default:
break;
2018-06-22 23:20:31 +02:00
}
2017-12-07 22:16:20 +01:00
}
break;
case MouseState::LeftRelease:
case MouseState::RightRelease:
// In this switch only button presses are relevant
break;
2018-06-22 23:20:31 +02:00
}
break;
case InputState::WidgetPressed:
2020-11-02 06:43:35 -07:00
InputStateWidgetPressed(screenCoords, state, widgetIndex, w, widget);
2018-06-22 23:20:31 +02:00
break;
case InputState::PositioningWindow:
2018-06-22 23:20:31 +02:00
w = window_find_by_number(_dragWidget.window_classification, _dragWidget.window_number);
if (w == nullptr)
{
_inputState = InputState::Reset;
2018-06-22 23:20:31 +02:00
}
else
{
InputWindowPositionContinue(*w, gInputDragLast, screenCoords);
if (state == MouseState::LeftRelease)
2018-06-22 23:20:31 +02:00
{
InputWindowPositionEnd(*w, screenCoords);
2017-12-07 22:16:20 +01:00
}
}
break;
case InputState::ViewportRight:
if (state == MouseState::Released)
2017-12-15 23:48:40 +01:00
{
2020-11-02 06:43:35 -07:00
InputViewportDragContinue();
2017-12-07 22:16:20 +01:00
}
else if (state == MouseState::RightRelease)
2017-12-15 23:48:40 +01:00
{
2020-11-02 06:43:35 -07:00
InputViewportDragEnd();
2018-06-22 23:20:31 +02:00
if (_ticksSinceDragStart < 500)
{
// If the user pressed the right mouse button for less than 500 ticks, interpret as right click
ViewportInteractionRightClick(screenCoords);
2018-06-22 23:20:31 +02:00
}
2017-12-07 22:16:20 +01:00
}
break;
case InputState::DropdownActive:
2020-11-02 06:43:35 -07:00
InputStateWidgetPressed(screenCoords, state, widgetIndex, w, widget);
2018-06-22 23:20:31 +02:00
break;
case InputState::ViewportLeft:
2018-06-22 23:20:31 +02:00
w = window_find_by_number(_dragWidget.window_classification, _dragWidget.window_number);
if (w == nullptr)
2017-12-15 23:48:40 +01:00
{
_inputState = InputState::Reset;
2017-12-07 22:16:20 +01:00
break;
}
2018-06-22 23:20:31 +02:00
switch (state)
2017-12-15 23:48:40 +01:00
{
case MouseState::Released:
2018-06-22 23:20:31 +02:00
if (w->viewport == nullptr)
{
_inputState = InputState::Reset;
2018-06-22 23:20:31 +02:00
break;
}
2017-12-07 22:16:20 +01:00
if (!input_test_flag(INPUT_FLAG_4))
break;
2018-06-22 23:20:31 +02:00
if (w->classification != _dragWidget.window_classification || w->number != _dragWidget.window_number
|| !(_inputFlags & INPUT_FLAG_TOOL_ACTIVE))
{
break;
}
w = window_find_by_number(gCurrentToolWidget.window_classification, gCurrentToolWidget.window_number);
if (w == nullptr)
{
break;
}
2019-10-20 14:10:30 -03:00
window_event_tool_drag_call(w, gCurrentToolWidget.widget_index, screenCoords);
2018-06-22 23:20:31 +02:00
break;
case MouseState::LeftRelease:
_inputState = InputState::Reset;
2018-06-22 23:20:31 +02:00
if (_dragWidget.window_number == w->number)
{
if ((_inputFlags & INPUT_FLAG_TOOL_ACTIVE))
{
w = window_find_by_number(
gCurrentToolWidget.window_classification, gCurrentToolWidget.window_number);
if (w != nullptr)
{
2019-10-20 14:10:30 -03:00
window_event_tool_up_call(w, gCurrentToolWidget.widget_index, screenCoords);
2018-06-22 23:20:31 +02:00
}
}
else if (!(_inputFlags & INPUT_FLAG_4))
{
ViewportInteractionLeftClick(screenCoords);
2018-06-22 23:20:31 +02:00
}
}
break;
case MouseState::LeftPress:
case MouseState::RightPress:
case MouseState::RightRelease:
// In this switch only left button release is relevant
break;
2018-06-22 23:20:31 +02:00
}
break;
case InputState::ScrollLeft:
2018-06-22 23:20:31 +02:00
switch (state)
{
case MouseState::Released:
InputScrollContinue(*w, widgetIndex, screenCoords);
2018-06-22 23:20:31 +02:00
break;
case MouseState::LeftRelease:
2020-11-02 06:43:35 -07:00
InputScrollEnd();
2018-06-22 23:20:31 +02:00
break;
case MouseState::LeftPress:
case MouseState::RightPress:
case MouseState::RightRelease:
// In this switch only left button release is relevant
break;
2018-06-22 23:20:31 +02:00
}
break;
case InputState::Resizing:
2018-06-22 23:20:31 +02:00
w = window_find_by_number(_dragWidget.window_classification, _dragWidget.window_number);
2018-01-04 06:58:44 +01:00
if (w == nullptr)
2017-12-15 23:48:40 +01:00
{
_inputState = InputState::Reset;
2017-12-07 22:16:20 +01:00
}
2018-06-22 23:20:31 +02:00
else
2017-12-15 23:48:40 +01:00
{
if (state == MouseState::LeftRelease)
2017-12-15 23:48:40 +01:00
{
2020-11-02 06:43:35 -07:00
InputWindowResizeEnd();
2017-12-15 23:48:40 +01:00
}
if (state == MouseState::Released || state == MouseState::LeftRelease)
2017-12-15 23:48:40 +01:00
{
InputWindowResizeContinue(*w, screenCoords);
2017-12-07 22:16:20 +01:00
}
}
break;
case InputState::ScrollRight:
2020-11-02 06:43:35 -07:00
InputScrollRight(screenCoords, state);
2017-12-07 22:16:20 +01:00
break;
}
}
#pragma region Window positioning / resizing
2022-08-21 18:49:23 +02:00
void InputWindowPositionBegin(rct_window& w, WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords)
2017-12-07 22:16:20 +01:00
{
_inputState = InputState::PositioningWindow;
gInputDragLast = screenCoords - w.windowPos;
_dragWidget.window_classification = w.classification;
_dragWidget.window_number = w.number;
2017-12-07 22:16:20 +01:00
_dragWidget.widget_index = widgetIndex;
}
2020-11-02 06:43:35 -07:00
static void InputWindowPositionContinue(
rct_window& w, const ScreenCoordsXY& lastScreenCoords, const ScreenCoordsXY& newScreenCoords)
2017-12-07 22:16:20 +01:00
{
int32_t snapProximity;
2017-12-07 22:16:20 +01:00
2022-10-16 17:27:01 +02:00
snapProximity = (w.flags & WF_NO_SNAPPING) ? 0 : gConfigGeneral.WindowSnapProximity;
window_move_and_snap(w, newScreenCoords - lastScreenCoords, snapProximity);
2017-12-07 22:16:20 +01:00
}
static void InputWindowPositionEnd(rct_window& w, const ScreenCoordsXY& screenCoords)
2017-12-07 22:16:20 +01:00
{
_inputState = InputState::Normal;
2017-12-07 22:16:20 +01:00
gTooltipTimeout = 0;
gTooltipWidget = _dragWidget;
window_event_moved_call(&w, screenCoords);
2017-12-07 22:16:20 +01:00
}
2022-08-21 18:49:23 +02:00
static void InputWindowResizeBegin(rct_window& w, WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords)
2017-12-07 22:16:20 +01:00
{
_inputState = InputState::Resizing;
gInputDragLast = screenCoords;
_dragWidget.window_classification = w.classification;
_dragWidget.window_number = w.number;
2017-12-07 22:16:20 +01:00
_dragWidget.widget_index = widgetIndex;
_originalWindowWidth = w.width;
_originalWindowHeight = w.height;
2017-12-07 22:16:20 +01:00
}
static void InputWindowResizeContinue(rct_window& w, const ScreenCoordsXY& screenCoords)
2017-12-07 22:16:20 +01:00
{
2022-11-06 20:49:07 +00:00
if (screenCoords.y < static_cast<int32_t>(ContextGetHeight()) - 2)
2017-12-15 23:48:40 +01:00
{
auto differentialCoords = screenCoords - gInputDragLast;
int32_t targetWidth = _originalWindowWidth + differentialCoords.x - w.width;
int32_t targetHeight = _originalWindowHeight + differentialCoords.y - w.height;
2017-12-07 22:16:20 +01:00
window_resize(w, targetWidth, targetHeight);
2017-12-07 22:16:20 +01:00
}
}
2020-11-02 06:43:35 -07:00
static void InputWindowResizeEnd()
2017-12-07 22:16:20 +01:00
{
_inputState = InputState::Normal;
2017-12-07 22:16:20 +01:00
gTooltipTimeout = 0;
gTooltipWidget = _dragWidget;
}
#pragma endregion
#pragma region Viewport dragging
static void InputViewportDragBegin(rct_window& w)
2017-12-07 22:16:20 +01:00
{
w.flags &= ~WF_SCROLLING_TO_LOCATION;
_inputState = InputState::ViewportRight;
_dragWidget.window_classification = w.classification;
_dragWidget.window_number = w.number;
2017-12-07 22:16:20 +01:00
_ticksSinceDragStart = 0;
2022-11-06 20:49:07 +00:00
auto cursorPosition = ContextGetCursorPosition();
gInputDragLast = cursorPosition;
if (!gConfigGeneral.InvertViewportDrag)
{
2022-11-06 20:49:07 +00:00
ContextHideCursor();
}
2017-12-07 22:16:20 +01:00
window_unfollow_sprite(w);
2017-12-07 22:16:20 +01:00
// gInputFlags |= INPUT_FLAG_5;
}
2020-11-02 06:43:35 -07:00
static void InputViewportDragContinue()
2017-12-07 22:16:20 +01:00
{
2018-06-22 23:20:31 +02:00
rct_window* w;
rct_viewport* viewport;
2017-12-07 22:16:20 +01:00
2022-11-06 20:49:07 +00:00
auto newDragCoords = ContextGetCursorPosition();
const CursorState* cursorState = ContextGetCursorState();
2017-12-07 22:16:20 +01:00
auto differentialCoords = newDragCoords - gInputDragLast;
2017-12-07 22:16:20 +01:00
w = window_find_by_number(_dragWidget.window_classification, _dragWidget.window_number);
// #3294: Window can be closed during a drag session, so just finish
// the session if the window no longer exists
2018-01-04 06:58:44 +01:00
if (w == nullptr)
2017-12-15 23:48:40 +01:00
{
2020-11-02 06:43:35 -07:00
InputViewportDragEnd();
2017-12-07 22:16:20 +01:00
return;
}
viewport = w->viewport;
_ticksSinceDragStart += gCurrentDeltaTime;
2018-01-04 06:58:44 +01:00
if (viewport == nullptr)
2017-12-15 23:48:40 +01:00
{
2022-11-06 20:49:07 +00:00
ContextShowCursor();
_inputState = InputState::Reset;
2017-12-15 23:48:40 +01:00
}
else if (differentialCoords.x != 0 || differentialCoords.y != 0)
2017-12-15 23:48:40 +01:00
{
if (!(w->flags & WF_NO_SCROLLING))
{
2017-12-07 22:16:20 +01:00
// User dragged a scrollable viewport
// If the drag time is less than 500 the "drag" is usually interpreted as a right click.
// As the user moved the mouse, don't interpret it as right click in any case.
_ticksSinceDragStart = 1000;
differentialCoords.x = (viewport->zoom + 1).ApplyTo(differentialCoords.x);
differentialCoords.y = (viewport->zoom + 1).ApplyTo(differentialCoords.y);
2022-10-16 17:27:01 +02:00
if (gConfigGeneral.InvertViewportDrag)
2017-12-15 23:48:40 +01:00
{
2020-03-01 16:32:35 -03:00
w->savedViewPos -= differentialCoords;
2017-12-15 23:48:40 +01:00
}
else
{
2020-03-01 16:32:35 -03:00
w->savedViewPos += differentialCoords;
2017-12-07 22:16:20 +01:00
}
}
}
if (cursorState->touch || gConfigGeneral.InvertViewportDrag)
{
gInputDragLast = newDragCoords;
}
else
{
2022-11-06 20:49:07 +00:00
ContextSetCursorPosition(gInputDragLast);
}
2017-12-07 22:16:20 +01:00
}
2020-11-02 06:43:35 -07:00
static void InputViewportDragEnd()
2017-12-07 22:16:20 +01:00
{
_inputState = InputState::Reset;
2022-11-06 20:49:07 +00:00
ContextShowCursor();
2017-12-07 22:16:20 +01:00
}
#pragma endregion
#pragma region Scroll bars
2022-08-21 18:49:23 +02:00
static void InputScrollBegin(rct_window& w, WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords)
2017-12-07 22:16:20 +01:00
{
const auto& widget = w.widgets[widgetIndex];
2017-12-07 22:16:20 +01:00
_inputState = InputState::ScrollLeft;
gPressedWidget.window_classification = w.classification;
gPressedWidget.window_number = w.number;
2017-12-07 22:16:20 +01:00
gPressedWidget.widget_index = widgetIndex;
2020-10-17 09:10:08 +01:00
gTooltipCursor = screenCoords;
2017-12-07 22:16:20 +01:00
int32_t scroll_area, scroll_id;
ScreenCoordsXY scrollCoords;
2017-12-07 22:16:20 +01:00
scroll_id = 0; // safety
WidgetScrollGetPart(w, &widget, screenCoords, scrollCoords, &scroll_area, &scroll_id);
2017-12-07 22:16:20 +01:00
_currentScrollArea = scroll_area;
_currentScrollIndex = scroll_id;
window_event_scroll_select_call(&w, scroll_id, scroll_area);
2017-12-15 23:48:40 +01:00
if (scroll_area == SCROLL_PART_VIEW)
{
window_event_scroll_mousedown_call(&w, scroll_id, scrollCoords);
2017-12-07 22:16:20 +01:00
return;
}
const auto& widg = w.widgets[widgetIndex];
auto& scroll = w.scrolls[scroll_id];
2017-12-07 22:16:20 +01:00
int32_t widget_width = widg.width() - 1;
if (scroll.flags & VSCROLLBAR_VISIBLE)
widget_width -= SCROLLBAR_WIDTH + 1;
int32_t widget_content_width = std::max(scroll.h_right - widget_width, 0);
2017-12-07 22:16:20 +01:00
int32_t widget_height = widg.bottom - widg.top - 1;
if (scroll.flags & HSCROLLBAR_VISIBLE)
widget_height -= SCROLLBAR_WIDTH + 1;
int32_t widget_content_height = std::max(scroll.v_bottom - widget_height, 0);
2017-12-07 22:16:20 +01:00
2017-12-15 23:48:40 +01:00
switch (scroll_area)
{
2018-06-22 23:20:31 +02:00
case SCROLL_PART_HSCROLLBAR_LEFT:
scroll.h_left = std::max(scroll.h_left - 3, 0);
2018-06-22 23:20:31 +02:00
break;
case SCROLL_PART_HSCROLLBAR_RIGHT:
scroll.h_left = std::min(scroll.h_left + 3, widget_content_width);
2018-06-22 23:20:31 +02:00
break;
case SCROLL_PART_HSCROLLBAR_LEFT_TROUGH:
scroll.h_left = std::max(scroll.h_left - widget_width, 0);
2018-06-22 23:20:31 +02:00
break;
case SCROLL_PART_HSCROLLBAR_RIGHT_TROUGH:
scroll.h_left = std::min(scroll.h_left + widget_width, widget_content_width);
2018-06-22 23:20:31 +02:00
break;
case SCROLL_PART_VSCROLLBAR_TOP:
scroll.v_top = std::max(scroll.v_top - 3, 0);
2018-06-22 23:20:31 +02:00
break;
case SCROLL_PART_VSCROLLBAR_BOTTOM:
scroll.v_top = std::min(scroll.v_top + 3, widget_content_height);
2018-06-22 23:20:31 +02:00
break;
case SCROLL_PART_VSCROLLBAR_TOP_TROUGH:
scroll.v_top = std::max(scroll.v_top - widget_height, 0);
2018-06-22 23:20:31 +02:00
break;
case SCROLL_PART_VSCROLLBAR_BOTTOM_TROUGH:
scroll.v_top = std::min(scroll.v_top + widget_height, widget_content_height);
2018-06-22 23:20:31 +02:00
break;
default:
break;
2017-12-07 22:16:20 +01:00
}
WidgetScrollUpdateThumbs(w, widgetIndex);
window_invalidate_by_number(w.classification, w.number);
2017-12-07 22:16:20 +01:00
}
2022-08-21 18:49:23 +02:00
static void InputScrollContinue(rct_window& w, WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords)
2017-12-07 22:16:20 +01:00
{
int32_t scroll_part, scroll_id;
2017-12-07 22:16:20 +01:00
const auto& widget = w.widgets[widgetIndex];
if (w.classification != gPressedWidget.window_classification || w.number != gPressedWidget.window_number
2018-06-22 23:20:31 +02:00
|| widgetIndex != gPressedWidget.widget_index)
2017-12-15 23:48:40 +01:00
{
2020-11-02 06:43:35 -07:00
InvalidateScroll();
2017-12-07 22:16:20 +01:00
return;
}
ScreenCoordsXY newScreenCoords;
WidgetScrollGetPart(w, &widget, screenCoords, newScreenCoords, &scroll_part, &scroll_id);
2017-12-07 22:16:20 +01:00
2017-12-15 23:48:40 +01:00
if (_currentScrollArea == SCROLL_PART_HSCROLLBAR_THUMB)
{
2020-10-17 09:10:08 +01:00
int32_t originalTooltipCursorX = gTooltipCursor.x;
gTooltipCursor.x = screenCoords.x;
2020-11-02 06:43:35 -07:00
InputScrollPartUpdateHThumb(w, widgetIndex, screenCoords.x - originalTooltipCursorX, scroll_id);
2017-12-07 22:16:20 +01:00
return;
}
2017-12-15 23:48:40 +01:00
if (_currentScrollArea == SCROLL_PART_VSCROLLBAR_THUMB)
{
2020-10-17 09:10:08 +01:00
int32_t originalTooltipCursorY = gTooltipCursor.y;
gTooltipCursor.y = screenCoords.y;
2020-11-02 06:43:35 -07:00
InputScrollPartUpdateVThumb(w, widgetIndex, screenCoords.y - originalTooltipCursorY, scroll_id);
2017-12-07 22:16:20 +01:00
return;
}
2017-12-15 23:48:40 +01:00
if (scroll_part != _currentScrollArea)
{
2020-11-02 06:43:35 -07:00
InvalidateScroll();
2017-12-07 22:16:20 +01:00
return;
}
2017-12-15 23:48:40 +01:00
switch (scroll_part)
{
2018-06-22 23:20:31 +02:00
case SCROLL_PART_VIEW:
window_event_scroll_mousedrag_call(&w, scroll_id, newScreenCoords);
2018-06-22 23:20:31 +02:00
break;
case SCROLL_PART_HSCROLLBAR_LEFT:
2020-11-02 06:43:35 -07:00
InputScrollPartUpdateHLeft(w, widgetIndex, scroll_id);
2018-06-22 23:20:31 +02:00
break;
case SCROLL_PART_HSCROLLBAR_RIGHT:
2020-11-02 06:43:35 -07:00
InputScrollPartUpdateHRight(w, widgetIndex, scroll_id);
2018-06-22 23:20:31 +02:00
break;
case SCROLL_PART_VSCROLLBAR_TOP:
2020-11-02 06:43:35 -07:00
InputScrollPartUpdateVTop(w, widgetIndex, scroll_id);
2018-06-22 23:20:31 +02:00
break;
case SCROLL_PART_VSCROLLBAR_BOTTOM:
2020-11-02 06:43:35 -07:00
InputScrollPartUpdateVBottom(w, widgetIndex, scroll_id);
2018-06-22 23:20:31 +02:00
break;
2017-12-07 22:16:20 +01:00
}
}
2020-11-02 06:43:35 -07:00
static void InputScrollEnd()
2017-12-07 22:16:20 +01:00
{
_inputState = InputState::Reset;
2020-11-02 06:43:35 -07:00
InvalidateScroll();
2017-12-07 22:16:20 +01:00
}
/**
*
* rct2: 0x006E98F2
*/
2022-08-21 18:49:23 +02:00
static void InputScrollPartUpdateHThumb(rct_window& w, WidgetIndex widgetIndex, int32_t x, int32_t scroll_id)
2017-12-07 22:16:20 +01:00
{
const auto& widget = w.widgets[widgetIndex];
auto& scroll = w.scrolls[scroll_id];
2017-12-07 22:16:20 +01:00
if (window_find_by_number(w.classification, w.number) != nullptr)
2017-12-15 23:48:40 +01:00
{
int32_t newLeft;
newLeft = scroll.h_right;
2017-12-07 22:16:20 +01:00
newLeft *= x;
x = widget.width() - 21;
if (scroll.flags & VSCROLLBAR_VISIBLE)
x -= SCROLLBAR_WIDTH + 1;
2017-12-07 22:16:20 +01:00
newLeft /= x;
x = newLeft;
scroll.flags |= HSCROLLBAR_THUMB_PRESSED;
newLeft = scroll.h_left;
2017-12-07 22:16:20 +01:00
newLeft += x;
if (newLeft < 0)
newLeft = 0;
x = widget.width() - 1;
if (scroll.flags & VSCROLLBAR_VISIBLE)
x -= SCROLLBAR_WIDTH + 1;
2017-12-07 22:16:20 +01:00
x *= -1;
x += scroll.h_right;
2017-12-07 22:16:20 +01:00
if (x < 0)
x = 0;
if (newLeft > x)
newLeft = x;
scroll.h_left = newLeft;
WidgetScrollUpdateThumbs(w, widgetIndex);
widget_invalidate_by_number(w.classification, w.number, widgetIndex);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E99A9
*/
2022-08-21 18:49:23 +02:00
static void InputScrollPartUpdateVThumb(rct_window& w, WidgetIndex widgetIndex, int32_t y, int32_t scroll_id)
2017-12-07 22:16:20 +01:00
{
const auto& widget = w.widgets[widgetIndex];
auto& scroll = w.scrolls[scroll_id];
2017-12-07 22:16:20 +01:00
if (window_find_by_number(w.classification, w.number) != nullptr)
2017-12-15 23:48:40 +01:00
{
int32_t newTop;
newTop = scroll.v_bottom;
2017-12-07 22:16:20 +01:00
newTop *= y;
y = widget.height() - 21;
if (scroll.flags & HSCROLLBAR_VISIBLE)
y -= SCROLLBAR_WIDTH + 1;
2017-12-07 22:16:20 +01:00
newTop /= y;
y = newTop;
scroll.flags |= VSCROLLBAR_THUMB_PRESSED;
newTop = scroll.v_top;
2017-12-07 22:16:20 +01:00
newTop += y;
if (newTop < 0)
newTop = 0;
y = widget.height() - 1;
if (scroll.flags & HSCROLLBAR_VISIBLE)
y -= SCROLLBAR_WIDTH + 1;
2017-12-07 22:16:20 +01:00
y *= -1;
y += scroll.v_bottom;
2017-12-07 22:16:20 +01:00
if (y < 0)
y = 0;
if (newTop > y)
newTop = y;
scroll.v_top = newTop;
WidgetScrollUpdateThumbs(w, widgetIndex);
widget_invalidate_by_number(w.classification, w.number, widgetIndex);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E9A60
*/
2022-08-21 18:49:23 +02:00
static void InputScrollPartUpdateHLeft(rct_window& w, WidgetIndex widgetIndex, int32_t scroll_id)
2017-12-07 22:16:20 +01:00
{
if (window_find_by_number(w.classification, w.number) != nullptr)
2017-12-15 23:48:40 +01:00
{
auto& scroll = w.scrolls[scroll_id];
scroll.flags |= HSCROLLBAR_LEFT_PRESSED;
if (scroll.h_left >= 3)
scroll.h_left -= 3;
WidgetScrollUpdateThumbs(w, widgetIndex);
widget_invalidate_by_number(w.classification, w.number, widgetIndex);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E9ABF
*/
2022-08-21 18:49:23 +02:00
static void InputScrollPartUpdateHRight(rct_window& w, WidgetIndex widgetIndex, int32_t scroll_id)
2017-12-07 22:16:20 +01:00
{
const auto& widget = w.widgets[widgetIndex];
if (window_find_by_number(w.classification, w.number) != nullptr)
2017-12-15 23:48:40 +01:00
{
auto& scroll = w.scrolls[scroll_id];
scroll.flags |= HSCROLLBAR_RIGHT_PRESSED;
scroll.h_left += 3;
int32_t newLeft = widget.width() - 1;
if (scroll.flags & VSCROLLBAR_VISIBLE)
newLeft -= SCROLLBAR_WIDTH + 1;
2017-12-07 22:16:20 +01:00
newLeft *= -1;
newLeft += scroll.h_right;
2017-12-07 22:16:20 +01:00
if (newLeft < 0)
newLeft = 0;
if (scroll.h_left > newLeft)
scroll.h_left = newLeft;
WidgetScrollUpdateThumbs(w, widgetIndex);
widget_invalidate_by_number(w.classification, w.number, widgetIndex);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E9C37
*/
2022-08-21 18:49:23 +02:00
static void InputScrollPartUpdateVTop(rct_window& w, WidgetIndex widgetIndex, int32_t scroll_id)
2017-12-07 22:16:20 +01:00
{
if (window_find_by_number(w.classification, w.number) != nullptr)
2017-12-15 23:48:40 +01:00
{
auto& scroll = w.scrolls[scroll_id];
scroll.flags |= VSCROLLBAR_UP_PRESSED;
if (scroll.v_top >= 3)
scroll.v_top -= 3;
WidgetScrollUpdateThumbs(w, widgetIndex);
widget_invalidate_by_number(w.classification, w.number, widgetIndex);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E9C96
*/
2022-08-21 18:49:23 +02:00
static void InputScrollPartUpdateVBottom(rct_window& w, WidgetIndex widgetIndex, int32_t scroll_id)
2017-12-07 22:16:20 +01:00
{
const auto& widget = w.widgets[widgetIndex];
if (window_find_by_number(w.classification, w.number) != nullptr)
2017-12-15 23:48:40 +01:00
{
auto& scroll = w.scrolls[scroll_id];
scroll.flags |= VSCROLLBAR_DOWN_PRESSED;
scroll.v_top += 3;
int32_t newTop = widget.height() - 1;
if (scroll.flags & HSCROLLBAR_VISIBLE)
newTop -= SCROLLBAR_WIDTH + 1;
2017-12-07 22:16:20 +01:00
newTop *= -1;
newTop += scroll.v_bottom;
2017-12-07 22:16:20 +01:00
if (newTop < 0)
newTop = 0;
if (scroll.v_top > newTop)
scroll.v_top = newTop;
WidgetScrollUpdateThumbs(w, widgetIndex);
widget_invalidate_by_number(w.classification, w.number, widgetIndex);
2017-12-07 22:16:20 +01:00
}
}
#pragma endregion
#pragma region Widgets
/**
*
* rct2: 0x006E9253
*/
2022-08-21 18:49:23 +02:00
static void InputWidgetOver(const ScreenCoordsXY& screenCoords, rct_window* w, WidgetIndex widgetIndex)
2017-12-07 22:16:20 +01:00
{
WindowClass windowClass = WindowClass::Null;
2017-12-07 22:16:20 +01:00
rct_windownumber windowNumber = 0;
2022-12-24 16:50:29 +01:00
Widget* widget = nullptr;
2017-12-07 22:16:20 +01:00
2018-01-04 06:58:44 +01:00
if (w != nullptr)
2017-12-15 23:48:40 +01:00
{
2017-12-07 22:16:20 +01:00
windowClass = w->classification;
windowNumber = w->number;
widget = &w->widgets[widgetIndex];
}
2020-11-02 06:43:35 -07:00
InputWidgetOverChangeCheck(windowClass, windowNumber, widgetIndex);
2017-12-07 22:16:20 +01:00
if (w != nullptr && widgetIndex != -1 && widget->type == WindowWidgetType::Scroll)
2017-12-15 23:48:40 +01:00
{
int32_t scroll_part, scrollId;
ScreenCoordsXY newScreenCoords;
WidgetScrollGetPart(*w, widget, screenCoords, newScreenCoords, &scroll_part, &scrollId);
2017-12-07 22:16:20 +01:00
if (scroll_part != SCROLL_PART_VIEW)
WindowTooltipClose();
2017-12-15 23:48:40 +01:00
else
{
window_event_scroll_mouseover_call(w, scrollId, newScreenCoords);
2020-11-02 06:43:35 -07:00
InputUpdateTooltip(w, widgetIndex, screenCoords);
2017-12-07 22:16:20 +01:00
}
}
2017-12-15 23:48:40 +01:00
else
{
2020-11-02 06:43:35 -07:00
InputUpdateTooltip(w, widgetIndex, screenCoords);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E9269
*/
2022-08-21 18:49:23 +02:00
static void InputWidgetOverChangeCheck(WindowClass windowClass, rct_windownumber windowNumber, WidgetIndex widgetIndex)
2017-12-07 22:16:20 +01:00
{
// Prevents invalid widgets being clicked source of bug is elsewhere
if (widgetIndex == -1)
return;
// Check if the widget that the cursor was over, has changed
2018-06-22 23:20:31 +02:00
if (windowClass != gHoverWidget.window_classification || windowNumber != gHoverWidget.window_number
|| widgetIndex != gHoverWidget.widget_index)
2017-12-15 23:48:40 +01:00
{
2017-12-07 22:16:20 +01:00
// Invalidate last widget cursor was on if widget is a flat button
2020-11-02 06:43:35 -07:00
InputWidgetOverFlatbuttonInvalidate();
2017-12-07 22:16:20 +01:00
// Set new cursor over widget
gHoverWidget.window_classification = windowClass;
gHoverWidget.window_number = windowNumber;
gHoverWidget.widget_index = widgetIndex;
// Invalidate new widget cursor is on if widget is a flat button
if (windowClass != WindowClass::Null)
2020-11-02 06:43:35 -07:00
InputWidgetOverFlatbuttonInvalidate();
2017-12-07 22:16:20 +01:00
}
}
/**
* Used to invalidate flat button widgets when the mouse leaves and enters them. This should be generalised so that all widgets
* can use this in the future.
*/
2020-11-02 06:43:35 -07:00
static void InputWidgetOverFlatbuttonInvalidate()
2017-12-07 22:16:20 +01:00
{
2018-06-22 23:20:31 +02:00
rct_window* w = window_find_by_number(gHoverWidget.window_classification, gHoverWidget.window_number);
2018-01-04 06:58:44 +01:00
if (w != nullptr)
2017-12-15 23:48:40 +01:00
{
2017-12-07 22:16:20 +01:00
window_event_invalidate_call(w);
if (w->widgets[gHoverWidget.widget_index].type == WindowWidgetType::FlatBtn)
2017-12-15 23:48:40 +01:00
{
2018-06-22 23:20:31 +02:00
widget_invalidate_by_number(
gHoverWidget.window_classification, gHoverWidget.window_number, gHoverWidget.widget_index);
2017-12-07 22:16:20 +01:00
}
}
}
/**
*
* rct2: 0x006E95F9
*/
2022-08-21 18:49:23 +02:00
static void InputWidgetLeft(const ScreenCoordsXY& screenCoords, rct_window* w, WidgetIndex widgetIndex)
2017-12-07 22:16:20 +01:00
{
WindowClass windowClass = WindowClass::Null;
2017-12-07 22:16:20 +01:00
rct_windownumber windowNumber = 0;
2018-01-04 06:58:44 +01:00
if (w != nullptr)
2017-12-15 23:48:40 +01:00
{
2017-12-07 22:16:20 +01:00
windowClass = w->classification;
windowNumber = w->number;
}
window_close_by_class(WindowClass::Error);
window_close_by_class(WindowClass::Tooltip);
2017-12-07 22:16:20 +01:00
// Window might have changed position in the list, therefore find it again
w = window_find_by_number(windowClass, windowNumber);
2018-01-04 06:58:44 +01:00
if (w == nullptr)
2017-12-07 22:16:20 +01:00
return;
w = window_bring_to_front(*w);
2017-12-07 22:16:20 +01:00
if (widgetIndex == -1)
return;
2018-06-22 23:20:31 +02:00
if (windowClass != gCurrentTextBox.window.classification || windowNumber != gCurrentTextBox.window.number
|| widgetIndex != gCurrentTextBox.widget_index)
2017-12-15 23:48:40 +01:00
{
2017-12-07 22:16:20 +01:00
window_cancel_textbox();
}
const auto& widget = w->widgets[widgetIndex];
2017-12-07 22:16:20 +01:00
switch (widget.type)
2017-12-15 23:48:40 +01:00
{
case WindowWidgetType::Frame:
case WindowWidgetType::Resize:
if (window_can_resize(*w)
2020-03-01 16:32:35 -03:00
&& (screenCoords.x >= w->windowPos.x + w->width - 19 && screenCoords.y >= w->windowPos.y + w->height - 19))
InputWindowResizeBegin(*w, widgetIndex, screenCoords);
2018-06-22 23:20:31 +02:00
break;
case WindowWidgetType::Viewport:
_inputState = InputState::ViewportLeft;
gInputDragLast = screenCoords;
2018-06-22 23:20:31 +02:00
_dragWidget.window_classification = windowClass;
_dragWidget.window_number = windowNumber;
if (_inputFlags & INPUT_FLAG_TOOL_ACTIVE)
2017-12-15 23:48:40 +01:00
{
2018-06-22 23:20:31 +02:00
w = window_find_by_number(gCurrentToolWidget.window_classification, gCurrentToolWidget.window_number);
if (w != nullptr)
{
input_set_flag(INPUT_FLAG_4, true);
2019-10-20 14:10:30 -03:00
window_event_tool_down_call(w, gCurrentToolWidget.widget_index, screenCoords);
2018-06-22 23:20:31 +02:00
}
2017-12-07 22:16:20 +01:00
}
2018-06-22 23:20:31 +02:00
break;
case WindowWidgetType::Caption:
InputWindowPositionBegin(*w, widgetIndex, screenCoords);
2018-06-22 23:20:31 +02:00
break;
case WindowWidgetType::Scroll:
InputScrollBegin(*w, widgetIndex, screenCoords);
2018-06-22 23:20:31 +02:00
break;
case WindowWidgetType::Empty:
case WindowWidgetType::LabelCentred:
case WindowWidgetType::Label:
case WindowWidgetType::Groupbox:
case WindowWidgetType::Placeholder:
case WindowWidgetType::Last:
// Non-interactive widget type
break;
case WindowWidgetType::ImgBtn:
case WindowWidgetType::ColourBtn:
case WindowWidgetType::TrnBtn:
case WindowWidgetType::Tab:
case WindowWidgetType::FlatBtn:
case WindowWidgetType::Button:
case WindowWidgetType::TableHeader:
case WindowWidgetType::Spinner:
case WindowWidgetType::DropdownMenu:
case WindowWidgetType::CloseBox:
case WindowWidgetType::Checkbox:
case WindowWidgetType::TextBox:
case WindowWidgetType::Custom:
if (!WidgetIsDisabled(*w, widgetIndex))
2018-06-22 23:20:31 +02:00
{
OpenRCT2::Audio::Play(OpenRCT2::Audio::SoundId::Click1, 0, w->windowPos.x + widget.midX());
2017-12-07 22:16:20 +01:00
2018-06-22 23:20:31 +02:00
// Set new cursor down widget
gPressedWidget.window_classification = windowClass;
gPressedWidget.window_number = windowNumber;
gPressedWidget.widget_index = widgetIndex;
_inputFlags |= INPUT_FLAG_WIDGET_PRESSED;
_inputState = InputState::WidgetPressed;
2018-06-22 23:20:31 +02:00
_clickRepeatTicks = 1;
2017-12-07 22:16:20 +01:00
2018-06-22 23:20:31 +02:00
widget_invalidate_by_number(windowClass, windowNumber, widgetIndex);
window_event_mouse_down_call(w, widgetIndex);
}
break;
2017-12-07 22:16:20 +01:00
}
}
#pragma endregion
/**
*
2017-12-15 23:48:40 +01:00
* rct2: 0x006ED833
2017-12-07 22:16:20 +01:00
*/
2020-11-02 06:43:35 -07:00
void ProcessMouseOver(const ScreenCoordsXY& screenCoords)
2017-12-07 22:16:20 +01:00
{
2018-06-22 23:20:31 +02:00
rct_window* window;
2017-12-07 22:16:20 +01:00
CursorID cursorId = CursorID::Arrow;
auto ft = Formatter();
2022-07-31 14:22:58 +02:00
ft.Add<StringId>(STR_NONE);
SetMapTooltip(ft);
2019-10-20 14:10:30 -03:00
window = window_find_from_point(screenCoords);
2017-12-07 22:16:20 +01:00
2018-01-04 06:58:44 +01:00
if (window != nullptr)
2017-12-15 23:48:40 +01:00
{
2022-08-21 18:49:23 +02:00
WidgetIndex widgetId = window_find_widget_from_point(*window, screenCoords);
2017-12-15 23:48:40 +01:00
if (widgetId != -1)
{
switch (window->widgets[widgetId].type)
{
case WindowWidgetType::Viewport:
2018-06-22 23:20:31 +02:00
if (!(_inputFlags & INPUT_FLAG_TOOL_ACTIVE))
2017-12-15 23:48:40 +01:00
{
if (ViewportInteractionLeftOver(screenCoords))
2018-06-22 23:20:31 +02:00
{
2020-11-02 06:43:35 -07:00
SetCursor(CursorID::HandPoint);
2018-06-22 23:20:31 +02:00
return;
}
break;
}
cursorId = static_cast<CursorID>(gCurrentToolId);
2017-12-07 22:16:20 +01:00
break;
2018-06-22 23:20:31 +02:00
case WindowWidgetType::Frame:
case WindowWidgetType::Resize:
2018-06-22 23:20:31 +02:00
if (!(window->flags & WF_RESIZABLE))
break;
if (window->min_width == window->max_width && window->min_height == window->max_height)
break;
2020-03-01 16:32:35 -03:00
if (screenCoords.x < window->windowPos.x + window->width - 0x13)
2018-06-22 23:20:31 +02:00
break;
2020-03-01 16:32:35 -03:00
if (screenCoords.y < window->windowPos.y + window->height - 0x13)
2018-06-22 23:20:31 +02:00
break;
cursorId = CursorID::DiagonalArrows;
2017-12-07 22:16:20 +01:00
break;
case WindowWidgetType::Scroll:
2017-12-07 22:16:20 +01:00
{
2018-06-22 23:20:31 +02:00
int32_t output_scroll_area, scroll_id;
ScreenCoordsXY scrollCoords;
2020-11-03 14:29:22 -07:00
WidgetScrollGetPart(
*window, &window->widgets[widgetId], screenCoords, scrollCoords, &output_scroll_area, &scroll_id);
2018-06-22 23:20:31 +02:00
if (output_scroll_area != SCROLL_PART_VIEW)
{
cursorId = CursorID::Arrow;
2018-06-22 23:20:31 +02:00
break;
}
// Same as default but with scroll_x/y
cursorId = window_event_cursor_call(window, widgetId, scrollCoords);
if (cursorId == CursorID::Undefined)
cursorId = CursorID::Arrow;
2017-12-07 22:16:20 +01:00
break;
}
2018-06-22 23:20:31 +02:00
default:
2019-10-20 14:10:30 -03:00
cursorId = window_event_cursor_call(window, widgetId, screenCoords);
if (cursorId == CursorID::Undefined)
cursorId = CursorID::Arrow;
2018-06-22 23:20:31 +02:00
break;
2017-12-07 22:16:20 +01:00
}
}
}
ViewportInteractionRightOver(screenCoords);
2020-11-02 06:43:35 -07:00
SetCursor(cursorId);
2017-12-07 22:16:20 +01:00
}
/**
*
* rct2: 0x006ED801
*/
2020-11-02 06:43:35 -07:00
void ProcessMouseTool(const ScreenCoordsXY& screenCoords)
2017-12-07 22:16:20 +01:00
{
if (_inputFlags & INPUT_FLAG_TOOL_ACTIVE)
{
2018-06-22 23:20:31 +02:00
rct_window* w = window_find_by_number(gCurrentToolWidget.window_classification, gCurrentToolWidget.window_number);
2017-12-07 22:16:20 +01:00
if (w == nullptr)
2017-12-07 22:16:20 +01:00
tool_cancel();
else if (input_get_state() != InputState::ViewportRight)
2019-10-20 14:10:30 -03:00
window_event_tool_update_call(w, gCurrentToolWidget.widget_index, screenCoords);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E8DA7
*/
2020-11-02 06:43:35 -07:00
void InputStateWidgetPressed(
2022-12-24 16:50:29 +01:00
const ScreenCoordsXY& screenCoords, MouseState state, WidgetIndex widgetIndex, rct_window* w, Widget* widget)
2017-12-07 22:16:20 +01:00
{
WindowClass cursor_w_class;
2017-12-07 22:16:20 +01:00
rct_windownumber cursor_w_number;
cursor_w_class = gPressedWidget.window_classification;
cursor_w_number = gPressedWidget.window_number;
2022-08-21 18:49:23 +02:00
WidgetIndex cursor_widgetIndex = gPressedWidget.widget_index;
2017-12-07 22:16:20 +01:00
2018-06-22 23:20:31 +02:00
rct_window* cursor_w = window_find_by_number(cursor_w_class, cursor_w_number);
2018-01-04 06:58:44 +01:00
if (cursor_w == nullptr)
2017-12-15 23:48:40 +01:00
{
_inputState = InputState::Reset;
2017-12-07 22:16:20 +01:00
return;
}
if (w != nullptr && state == MouseState::LeftRelease)
{
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)
{
gLastCloseModifier.window.number = w->number;
gLastCloseModifier.window.classification = w->classification;
gLastCloseModifier.modifier = CloseWindowModifier::Shift;
}
else if (gInputPlaceObjectModifier & PLACE_OBJECT_MODIFIER_COPY_Z)
{
gLastCloseModifier.window.number = w->number;
gLastCloseModifier.window.classification = w->classification;
gLastCloseModifier.modifier = CloseWindowModifier::Control;
}
}
}
2017-12-15 23:48:40 +01:00
switch (state)
{
case MouseState::Released:
if (w == nullptr || cursor_w_class != w->classification || cursor_w_number != w->number
|| widgetIndex != cursor_widgetIndex)
2018-06-22 23:20:31 +02:00
break;
2017-12-07 22:16:20 +01:00
if (WidgetIsDisabled(*w, widgetIndex))
2018-06-22 23:20:31 +02:00
break;
2017-12-07 22:16:20 +01:00
2018-06-22 23:20:31 +02:00
if (_clickRepeatTicks != 0)
2017-12-15 23:48:40 +01:00
{
2018-06-22 23:20:31 +02:00
_clickRepeatTicks++;
// Handle click repeat
if (_clickRepeatTicks >= 16 && (_clickRepeatTicks & 3) == 0)
2017-12-15 23:48:40 +01:00
{
if (WidgetIsHoldable(*w, widgetIndex))
2018-06-22 23:20:31 +02:00
{
window_event_mouse_down_call(w, widgetIndex);
}
2017-12-07 22:16:20 +01:00
}
}
2018-06-22 23:20:31 +02:00
if (_inputFlags & INPUT_FLAG_WIDGET_PRESSED)
{
if (_inputState == InputState::DropdownActive)
2018-06-22 23:20:31 +02:00
{
gDropdownHighlightedIndex = gDropdownDefaultIndex;
window_invalidate_by_class(WindowClass::Dropdown);
2018-06-22 23:20:31 +02:00
}
return;
}
_inputFlags |= INPUT_FLAG_WIDGET_PRESSED;
widget_invalidate_by_number(cursor_w_class, cursor_w_number, widgetIndex);
return;
case MouseState::LeftRelease:
case MouseState::RightPress:
if (_inputState == InputState::DropdownActive)
2017-12-15 23:48:40 +01:00
{
if (w != nullptr)
2017-12-15 23:48:40 +01:00
{
2018-06-22 23:20:31 +02:00
auto wClass = w->classification;
auto wNumber = w->number;
int32_t dropdown_index = 0;
bool dropdownCleanup = false;
2017-12-07 22:16:20 +01:00
if (w->classification == WindowClass::Dropdown)
2017-12-15 23:48:40 +01:00
{
2020-11-03 01:16:06 -07:00
dropdown_index = DropdownIndexFromPoint(screenCoords, w);
dropdownCleanup = dropdown_index == -1
2020-11-03 01:16:06 -07:00
|| (dropdown_index < Dropdown::ItemsMaxSize && Dropdown::IsDisabled(dropdown_index))
|| gDropdownItems[dropdown_index].IsSeparator();
w = nullptr; // To be closed right next
2017-12-07 22:16:20 +01:00
}
2018-06-22 23:20:31 +02:00
else
2017-12-15 23:48:40 +01:00
{
2018-06-22 23:20:31 +02:00
if (cursor_w_class != w->classification || cursor_w_number != w->number
|| widgetIndex != cursor_widgetIndex)
{
dropdownCleanup = true;
}
else
{
dropdown_index = -1;
if (_inputFlags & INPUT_FLAG_DROPDOWN_STAY_OPEN)
2018-06-22 23:20:31 +02:00
{
if (!(_inputFlags & INPUT_FLAG_DROPDOWN_MOUSE_UP))
{
_inputFlags |= INPUT_FLAG_DROPDOWN_MOUSE_UP;
return;
}
2018-06-22 23:20:31 +02:00
}
}
2017-12-07 22:16:20 +01:00
}
2018-06-22 23:20:31 +02:00
window_close_by_class(WindowClass::Dropdown);
if (dropdownCleanup)
2018-06-22 23:20:31 +02:00
{
// Update w as it will be invalid after closing the dropdown window
w = window_find_by_number(wClass, wNumber);
2018-06-22 23:20:31 +02:00
}
else
2018-06-22 23:20:31 +02:00
{
cursor_w = window_find_by_number(cursor_w_class, cursor_w_number);
if (_inputFlags & INPUT_FLAG_WIDGET_PRESSED)
2018-06-22 23:20:31 +02:00
{
_inputFlags &= ~INPUT_FLAG_WIDGET_PRESSED;
widget_invalidate_by_number(cursor_w_class, cursor_w_number, cursor_widgetIndex);
2018-06-22 23:20:31 +02:00
}
_inputState = InputState::Normal;
gTooltipTimeout = 0;
gTooltipWidget.widget_index = cursor_widgetIndex;
gTooltipWidget.window_classification = cursor_w_class;
gTooltipWidget.window_number = cursor_w_number;
if (dropdown_index == -1)
{
2020-11-03 01:16:06 -07:00
if (!Dropdown::IsDisabled(gDropdownDefaultIndex))
{
dropdown_index = gDropdownDefaultIndex;
}
}
window_event_dropdown_call(cursor_w, cursor_widgetIndex, dropdown_index);
2018-06-22 23:20:31 +02:00
}
2017-12-07 22:16:20 +01:00
}
}
2017-12-15 23:48:40 +01:00
_inputState = InputState::Normal;
2017-12-15 23:48:40 +01:00
if (state == MouseState::RightPress)
2018-06-22 23:20:31 +02:00
{
return;
}
gTooltipTimeout = 0;
gTooltipWidget.widget_index = cursor_widgetIndex;
if (w == nullptr)
2018-06-22 23:20:31 +02:00
break;
if (widget == nullptr)
2018-06-22 23:20:31 +02:00
break;
{
int32_t mid_point_x = widget->midX() + w->windowPos.x;
2020-10-07 00:34:42 +03:00
OpenRCT2::Audio::Play(OpenRCT2::Audio::SoundId::Click2, 0, mid_point_x);
2018-06-22 23:20:31 +02:00
}
if (cursor_w_class != w->classification || cursor_w_number != w->number || widgetIndex != cursor_widgetIndex)
break;
if (WidgetIsDisabled(*w, widgetIndex))
2018-06-22 23:20:31 +02:00
break;
widget_invalidate_by_number(cursor_w_class, cursor_w_number, widgetIndex);
window_event_mouse_up_call(w, widgetIndex);
return;
2018-06-22 23:20:31 +02:00
default:
2017-12-07 22:16:20 +01:00
return;
}
_clickRepeatTicks = 0;
if (_inputState != InputState::DropdownActive)
2017-12-15 23:48:40 +01:00
{
2017-12-07 22:16:20 +01:00
// Hold down widget and drag outside of area??
2017-12-15 23:48:40 +01:00
if (_inputFlags & INPUT_FLAG_WIDGET_PRESSED)
{
2017-12-07 22:16:20 +01:00
_inputFlags &= ~INPUT_FLAG_WIDGET_PRESSED;
widget_invalidate_by_number(cursor_w_class, cursor_w_number, cursor_widgetIndex);
}
return;
}
gDropdownHighlightedIndex = -1;
window_invalidate_by_class(WindowClass::Dropdown);
2018-01-04 06:58:44 +01:00
if (w == nullptr)
2017-12-15 23:48:40 +01:00
{
2017-12-07 22:16:20 +01:00
return;
}
if (w->classification == WindowClass::Dropdown)
2017-12-15 23:48:40 +01:00
{
2020-11-03 01:16:06 -07:00
int32_t dropdown_index = DropdownIndexFromPoint(screenCoords, w);
2017-12-15 23:48:40 +01:00
if (dropdown_index == -1)
{
2017-12-07 22:16:20 +01:00
return;
}
2017-12-15 23:48:40 +01:00
if (gDropdownIsColour && gDropdownLastColourHover != dropdown_index)
{
2017-12-07 22:16:20 +01:00
gDropdownLastColourHover = dropdown_index;
WindowTooltipClose();
2017-12-07 22:16:20 +01:00
2022-07-31 14:22:58 +02:00
static constexpr const StringId _colourTooltips[] = {
2017-12-07 22:16:20 +01:00
STR_COLOUR_BLACK_TIP,
STR_COLOUR_GREY_TIP,
STR_COLOUR_WHITE_TIP,
STR_COLOUR_DARK_PURPLE_TIP,
STR_COLOUR_LIGHT_PURPLE_TIP,
STR_COLOUR_BRIGHT_PURPLE_TIP,
STR_COLOUR_DARK_BLUE_TIP,
STR_COLOUR_LIGHT_BLUE_TIP,
STR_COLOUR_ICY_BLUE_TIP,
STR_COLOUR_TEAL_TIP,
STR_COLOUR_AQUAMARINE_TIP,
STR_COLOUR_SATURATED_GREEN_TIP,
STR_COLOUR_DARK_GREEN_TIP,
STR_COLOUR_MOSS_GREEN_TIP,
STR_COLOUR_BRIGHT_GREEN_TIP,
STR_COLOUR_OLIVE_GREEN_TIP,
STR_COLOUR_DARK_OLIVE_GREEN_TIP,
STR_COLOUR_BRIGHT_YELLOW_TIP,
STR_COLOUR_YELLOW_TIP,
STR_COLOUR_DARK_YELLOW_TIP,
STR_COLOUR_LIGHT_ORANGE_TIP,
STR_COLOUR_DARK_ORANGE_TIP,
STR_COLOUR_LIGHT_BROWN_TIP,
STR_COLOUR_SATURATED_BROWN_TIP,
STR_COLOUR_DARK_BROWN_TIP,
STR_COLOUR_SALMON_PINK_TIP,
STR_COLOUR_BORDEAUX_RED_TIP,
STR_COLOUR_SATURATED_RED_TIP,
STR_COLOUR_BRIGHT_RED_TIP,
STR_COLOUR_DARK_PINK_TIP,
STR_COLOUR_BRIGHT_PINK_TIP,
STR_COLOUR_LIGHT_PINK_TIP,
};
2021-11-24 17:08:41 +00:00
WindowTooltipShow(OpenRCT2String{ _colourTooltips[dropdown_index], {} }, screenCoords);
2017-12-07 22:16:20 +01:00
}
2020-11-03 01:16:06 -07:00
if (dropdown_index < Dropdown::ItemsMaxSize && Dropdown::IsDisabled(dropdown_index))
2017-12-15 23:48:40 +01:00
{
2017-12-07 22:16:20 +01:00
return;
}
if (gDropdownItems[dropdown_index].IsSeparator())
2017-12-15 23:48:40 +01:00
{
2017-12-07 22:16:20 +01:00
return;
}
gDropdownHighlightedIndex = dropdown_index;
window_invalidate_by_class(WindowClass::Dropdown);
2017-12-15 23:48:40 +01:00
}
else
{
2017-12-07 22:16:20 +01:00
gDropdownLastColourHover = -1;
WindowTooltipClose();
2017-12-07 22:16:20 +01:00
}
}
2022-08-21 18:49:23 +02:00
static void InputUpdateTooltip(rct_window* w, WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords)
2017-12-07 22:16:20 +01:00
{
if (gTooltipWidget.window_classification == WindowClass::Null)
2017-12-15 23:48:40 +01:00
{
2020-10-17 09:10:08 +01:00
if (gTooltipCursor == screenCoords)
2017-12-15 23:48:40 +01:00
{
2017-12-07 22:16:20 +01:00
_tooltipNotShownTicks++;
if (_tooltipNotShownTicks > 50 && w != nullptr && WidgetIsVisible(*w, widgetIndex))
2017-12-15 23:48:40 +01:00
{
2017-12-07 22:16:20 +01:00
gTooltipTimeout = 0;
WindowTooltipOpen(w, widgetIndex, screenCoords);
2017-12-07 22:16:20 +01:00
}
}
gTooltipTimeout = 0;
2020-10-17 09:10:08 +01:00
gTooltipCursor = screenCoords;
2017-12-15 23:48:40 +01:00
}
else
{
2017-12-07 22:16:20 +01:00
reset_tooltip_not_shown();
2018-06-22 23:20:31 +02:00
if (w == nullptr || gTooltipWidget.window_classification != w->classification
2021-01-23 12:21:18 +00:00
|| gTooltipWidget.window_number != w->number || gTooltipWidget.widget_index != widgetIndex
|| !WidgetIsVisible(*w, widgetIndex))
2017-12-15 23:48:40 +01:00
{
WindowTooltipClose();
2017-12-07 22:16:20 +01:00
}
gTooltipTimeout += gCurrentDeltaTime;
2017-12-15 23:48:40 +01:00
if (gTooltipTimeout >= 8000)
{
window_close_by_class(WindowClass::Tooltip);
2017-12-07 22:16:20 +01:00
}
}
}
#pragma endregion
#pragma region Keyboard input
/**
*
* rct2: 0x00406CD2
*/
2020-11-02 06:43:35 -07:00
int32_t GetNextKey()
2017-12-07 22:16:20 +01:00
{
2022-11-06 20:49:07 +00:00
uint8_t* keysPressed = const_cast<uint8_t*>(ContextGetKeysPressed());
for (int32_t i = 0; i < 221; i++)
2017-12-15 23:48:40 +01:00
{
if (keysPressed[i])
{
2017-12-07 22:16:20 +01:00
keysPressed[i] = 0;
return i;
}
}
return 0;
}
#pragma endregion
/**
2017-12-15 23:48:40 +01:00
*
* rct2: 0x006ED990
*/
2020-11-02 06:43:35 -07:00
void SetCursor(CursorID cursor_id)
2017-12-07 22:16:20 +01:00
{
assert(cursor_id != CursorID::Undefined);
if (_inputState == InputState::Resizing)
2017-12-15 23:48:40 +01:00
{
cursor_id = CursorID::DiagonalArrows;
2017-12-07 22:16:20 +01:00
}
2022-11-06 20:49:07 +00:00
ContextSetCurrentCursor(cursor_id);
2017-12-07 22:16:20 +01:00
}
/**
*
* rct2: 0x006E876D
*/
2020-11-02 06:43:35 -07:00
void InvalidateScroll()
2017-12-07 22:16:20 +01:00
{
2018-06-22 23:20:31 +02:00
rct_window* w = window_find_by_number(gPressedWidget.window_classification, gPressedWidget.window_number);
2018-01-04 06:58:44 +01:00
if (w != nullptr)
2017-12-15 23:48:40 +01:00
{
2017-12-07 22:16:20 +01:00
// Reset to basic scroll
w->scrolls[_currentScrollIndex].flags &= 0xFF11;
window_invalidate_by_number(gPressedWidget.window_classification, gPressedWidget.window_number);
}
}
/**
2017-12-15 23:48:40 +01:00
* rct2: 0x00406C96
*/
void StoreMouseInput(MouseState state, const ScreenCoordsXY& screenCoords)
2017-12-07 22:16:20 +01:00
{
uint32_t writeIndex = _mouseInputQueueWriteIndex;
2018-11-21 23:16:04 +01:00
uint32_t nextWriteIndex = (writeIndex + 1) % std::size(_mouseInputQueue);
2017-12-07 22:16:20 +01:00
// Check if the queue is full
2017-12-15 23:48:40 +01:00
if (nextWriteIndex != _mouseInputQueueReadIndex)
{
2020-11-02 06:43:35 -07:00
RCTMouseData* item = &_mouseInputQueue[writeIndex];
2019-10-20 14:10:30 -03:00
item->x = screenCoords.x;
item->y = screenCoords.y;
2017-12-07 22:16:20 +01:00
item->state = state;
_mouseInputQueueWriteIndex = nextWriteIndex;
}
}
2020-11-02 06:43:35 -07:00
void GameHandleEdgeScroll()
2017-12-07 22:16:20 +01:00
{
2018-06-22 23:20:31 +02:00
rct_window* mainWindow;
int32_t scrollX, scrollY;
2017-12-07 22:16:20 +01:00
mainWindow = window_get_main();
2018-01-04 06:58:44 +01:00
if (mainWindow == nullptr)
2017-12-07 22:16:20 +01:00
return;
if ((mainWindow->flags & WF_NO_SCROLLING) || (gScreenFlags & (SCREEN_FLAGS_TRACK_MANAGER | SCREEN_FLAGS_TITLE_DEMO)))
return;
2018-01-04 06:58:44 +01:00
if (mainWindow->viewport == nullptr)
2017-12-07 22:16:20 +01:00
return;
2022-11-06 20:49:07 +00:00
if (!ContextHasFocus())
2017-12-07 22:16:20 +01:00
return;
scrollX = 0;
scrollY = 0;
// Scroll left / right
2022-11-06 20:49:07 +00:00
const CursorState* cursorState = ContextGetCursorState();
if (cursorState->position.x == 0)
2017-12-07 22:16:20 +01:00
scrollX = -1;
2022-11-06 20:49:07 +00:00
else if (cursorState->position.x >= ContextGetWidth() - 1)
2017-12-07 22:16:20 +01:00
scrollX = 1;
// Scroll up / down
if (cursorState->position.y == 0)
2017-12-07 22:16:20 +01:00
scrollY = -1;
2022-11-06 20:49:07 +00:00
else if (cursorState->position.y >= ContextGetHeight() - 1)
2017-12-07 22:16:20 +01:00
scrollY = 1;
2020-11-02 06:43:35 -07:00
InputScrollViewport(ScreenCoordsXY(scrollX, scrollY));
2017-12-07 22:16:20 +01:00
}
2020-11-02 06:43:35 -07:00
bool InputTestPlaceObjectModifier(PLACE_OBJECT_MODIFIER modifier)
2017-12-07 22:16:20 +01:00
{
return gInputPlaceObjectModifier & modifier;
}
2020-11-02 06:43:35 -07:00
void InputScrollViewport(const ScreenCoordsXY& scrollScreenCoords)
2017-12-07 22:16:20 +01:00
{
2018-06-22 23:20:31 +02:00
rct_window* mainWindow = window_get_main();
rct_viewport* viewport = mainWindow->viewport;
2017-12-07 22:16:20 +01:00
2022-10-16 17:27:01 +02:00
const int32_t speed = gConfigGeneral.EdgeScrollingSpeed;
int32_t multiplier = viewport->zoom.ApplyTo(speed);
int32_t dx = scrollScreenCoords.x * multiplier;
int32_t dy = scrollScreenCoords.y * multiplier;
2017-12-07 22:16:20 +01:00
2019-10-20 14:10:30 -03:00
if (scrollScreenCoords.x != 0)
2017-12-07 22:16:20 +01:00
{
// Speed up scrolling horizontally when at the edge of the map
// so that the speed is consistent with vertical edge scrolling.
2020-03-01 16:32:35 -03:00
int32_t x = mainWindow->savedViewPos.x + viewport->view_width / 2 + dx;
int32_t y = mainWindow->savedViewPos.y + viewport->view_height / 2;
int32_t y_dy = mainWindow->savedViewPos.y + viewport->view_height / 2 + dy;
2017-12-07 22:16:20 +01:00
auto mapCoord = viewport_coord_to_map_coord({ x, y }, 0);
auto mapCoord_dy = viewport_coord_to_map_coord({ x, y_dy }, 0);
2017-12-07 22:16:20 +01:00
// Check if we're crossing the boundary
// Clamp to the map minimum value
int32_t at_map_edge = 0;
int32_t at_map_edge_dy = 0;
2017-12-07 22:16:20 +01:00
if (mapCoord.x < MAP_MINIMUM_X_Y || mapCoord.y < MAP_MINIMUM_X_Y)
{
at_map_edge = 1;
}
if (mapCoord_dy.x < MAP_MINIMUM_X_Y || mapCoord_dy.y < MAP_MINIMUM_X_Y)
{
at_map_edge_dy = 1;
}
// Clamp to the map maximum value (scenario specific)
auto mapSizeMinus2 = GetMapSizeMinus2();
if (mapCoord.x > mapSizeMinus2.x || mapCoord.y > mapSizeMinus2.y)
2017-12-07 22:16:20 +01:00
{
at_map_edge = 1;
}
if (mapCoord_dy.x > mapSizeMinus2.x || mapCoord_dy.y > mapSizeMinus2.y)
2017-12-07 22:16:20 +01:00
{
at_map_edge_dy = 1;
}
// If we crossed the boundary, multiply the distance by 2
if (at_map_edge && at_map_edge_dy)
{
dx *= 2;
}
2020-03-01 16:32:35 -03:00
mainWindow->savedViewPos.x += dx;
2017-12-07 22:16:20 +01:00
_inputFlags |= INPUT_FLAG_VIEWPORT_SCROLLING;
}
2019-10-20 14:10:30 -03:00
if (scrollScreenCoords.y != 0)
2017-12-07 22:16:20 +01:00
{
2020-03-01 16:32:35 -03:00
mainWindow->savedViewPos.y += dy;
2017-12-07 22:16:20 +01:00
_inputFlags |= INPUT_FLAG_VIEWPORT_SCROLLING;
}
}