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

1734 lines
58 KiB
C++
Raw Normal View History

2017-12-07 22:16:20 +01:00
/*****************************************************************************
* Copyright (c) 2014-2024 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>
#include <optional>
2017-12-07 22:16:20 +01:00
using namespace OpenRCT2::Ui::Windows;
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 std::optional<uint32_t> _ticksSinceDragStart;
2023-01-19 15:13:23 +00:00
static WidgetRef _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
2023-05-18 18:07:57 +03:00
uint32_t gTooltipCloseTimeout;
2023-01-19 15:13:23 +00:00
WidgetRef gTooltipWidget;
2020-10-17 09:10:08 +01:00
ScreenCoordsXY gTooltipCursor;
2017-12-07 22:16:20 +01:00
static std::optional<uint32_t> _clickRepeatTicks;
2017-12-07 22:16:20 +01:00
static MouseState GameGetNextInput(ScreenCoordsXY& screenCoords);
static void InputWidgetOver(const ScreenCoordsXY& screenCoords, WindowBase* w, WidgetIndex widgetIndex);
2022-08-21 18:49:23 +02:00
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);
static void InputWidgetLeft(const ScreenCoordsXY& screenCoords, WindowBase* w, WidgetIndex widgetIndex);
2020-11-02 06:43:35 -07:00
void InputStateWidgetPressed(
const ScreenCoordsXY& screenCoords, MouseState state, WidgetIndex widgetIndex, WindowBase* w, Widget* widget);
2020-11-02 06:43:35 -07:00
void SetCursor(CursorID cursor_id);
static void InputWindowPositionContinue(
WindowBase& w, const ScreenCoordsXY& lastScreenCoords, const ScreenCoordsXY& newScreenCoords);
static void InputWindowPositionEnd(WindowBase& w, const ScreenCoordsXY& screenCoords);
static void InputWindowResizeBegin(WindowBase& w, WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords);
static void InputWindowResizeContinue(WindowBase& w, const ScreenCoordsXY& screenCoords);
2020-11-02 06:43:35 -07:00
static void InputWindowResizeEnd();
static void InputViewportDragBegin(WindowBase& w);
2020-11-02 06:43:35 -07:00
static void InputViewportDragContinue();
static void InputViewportDragEnd();
static void InputScrollBegin(WindowBase& w, WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords);
static void InputScrollContinue(WindowBase& w, WidgetIndex widgetIndex, const ScreenCoordsXY& screenCoords);
2020-11-02 06:43:35 -07:00
static void InputScrollEnd();
static void InputScrollPartUpdateHThumb(WindowBase& w, WidgetIndex widgetIndex, int32_t x, int32_t scroll_id);
static void InputScrollPartUpdateHLeft(WindowBase& w, WidgetIndex widgetIndex, int32_t scroll_id);
static void InputScrollPartUpdateHRight(WindowBase& w, WidgetIndex widgetIndex, int32_t scroll_id);
static void InputScrollPartUpdateVThumb(WindowBase& w, WidgetIndex widgetIndex, int32_t y, int32_t scroll_id);
static void InputScrollPartUpdateVTop(WindowBase& w, WidgetIndex widgetIndex, int32_t scroll_id);
static void InputScrollPartUpdateVBottom(WindowBase& w, WidgetIndex widgetIndex, int32_t scroll_id);
static void InputUpdateTooltip(WindowBase* 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
{
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
*/
static void InputScrollDragBegin(const ScreenCoordsXY& screenCoords, WindowBase* 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 = gCurrentRealTimeTicks;
2017-12-07 22:16:20 +01:00
_dragScrollIndex = WindowGetScrollDataIndex(*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
*/
static void InputScrollDragContinue(const ScreenCoordsXY& screenCoords, WindowBase* 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;
if (differentialCoords.x == 0 && differentialCoords.y == 0)
return;
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);
WindowInvalidateByNumber(w->classification, w->number);
2017-12-07 22:16:20 +01:00
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
{
WindowBase* w = WindowFindByNumber(_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:
2019-10-20 14:10:30 -03:00
if (screenCoords.x != 0 || screenCoords.y != 0)
2018-06-22 23:20:31 +02:00
{
_ticksSinceDragStart = std::nullopt;
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
{
WindowBase* 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
w = WindowFindFromPoint(screenCoords);
widgetIndex = w == nullptr ? -1 : WindowFindWidgetFromPoint(*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:
WindowCloseByClass(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 = WindowBringToFront(*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:
w = WindowFindByNumber(_dragWidget.window_classification, _dragWidget.window_number);
2018-06-22 23:20:31 +02:00
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();
if (_ticksSinceDragStart.has_value() && gCurrentRealTimeTicks - _ticksSinceDragStart.value() < 500)
2018-06-22 23:20:31 +02:00
{
// 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:
w = WindowFindByNumber(_dragWidget.window_classification, _dragWidget.window_number);
2018-06-22 23:20:31 +02:00
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 (!InputTestFlag(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 = WindowFindByNumber(gCurrentToolWidget.window_classification, gCurrentToolWidget.window_number);
2018-06-22 23:20:31 +02:00
if (w == nullptr)
{
break;
}
w->OnToolDrag(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 = WindowFindByNumber(gCurrentToolWidget.window_classification, gCurrentToolWidget.window_number);
2018-06-22 23:20:31 +02:00
if (w != nullptr)
{
w->OnToolUp(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:
w = WindowFindByNumber(_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
void InputWindowPositionBegin(WindowBase& 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(
WindowBase& 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;
WindowMoveAndSnap(w, newScreenCoords - lastScreenCoords, snapProximity);
2017-12-07 22:16:20 +01:00
}
static void InputWindowPositionEnd(WindowBase& w, const ScreenCoordsXY& screenCoords)
2017-12-07 22:16:20 +01:00
{
_inputState = InputState::Normal;
2023-05-18 18:07:57 +03:00
gTooltipCloseTimeout = 0;
2017-12-07 22:16:20 +01:00
gTooltipWidget = _dragWidget;
w.OnMoved(screenCoords);
2017-12-07 22:16:20 +01:00
}
static void InputWindowResizeBegin(WindowBase& 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(WindowBase& 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
WindowResize(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;
2023-05-18 18:07:57 +03:00
gTooltipCloseTimeout = 0;
2017-12-07 22:16:20 +01:00
gTooltipWidget = _dragWidget;
}
#pragma endregion
#pragma region Viewport dragging
static void InputViewportDragBegin(WindowBase& 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;
_ticksSinceDragStart = gCurrentRealTimeTicks;
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
WindowUnfollowSprite(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
{
WindowBase* w;
Viewport* viewport;
2017-12-07 22:16:20 +01:00
2022-11-06 20:49:07 +00:00
auto newDragCoords = ContextGetCursorPosition();
2017-12-07 22:16:20 +01:00
auto differentialCoords = newDragCoords - gInputDragLast;
if (differentialCoords.x == 0 && differentialCoords.y == 0)
return;
w = WindowFindByNumber(_dragWidget.window_classification, _dragWidget.window_number);
2017-12-07 22:16:20 +01:00
// #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;
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 = std::nullopt;
2017-12-07 22:16:20 +01:00
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
}
}
}
const CursorState* cursorState = ContextGetCursorState();
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
static void InputScrollBegin(WindowBase& 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;
w.OnScrollSelect(scroll_id, scroll_area);
2017-12-15 23:48:40 +01:00
if (scroll_area == SCROLL_PART_VIEW)
{
w.OnScrollMouseDown(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 -= kScrollBarWidth + 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 -= kScrollBarWidth + 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);
WindowInvalidateByNumber(w.classification, w.number);
2017-12-07 22:16:20 +01:00
}
static void InputScrollContinue(WindowBase& 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:
w.OnScrollMouseDrag(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
*/
static void InputScrollPartUpdateHThumb(WindowBase& 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 (WindowFindByNumber(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 -= kScrollBarWidth + 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 -= kScrollBarWidth + 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);
WidgetInvalidateByNumber(w.classification, w.number, widgetIndex);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E99A9
*/
static void InputScrollPartUpdateVThumb(WindowBase& 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 (WindowFindByNumber(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 -= kScrollBarWidth + 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 -= kScrollBarWidth + 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);
WidgetInvalidateByNumber(w.classification, w.number, widgetIndex);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E9A60
*/
static void InputScrollPartUpdateHLeft(WindowBase& w, WidgetIndex widgetIndex, int32_t scroll_id)
2017-12-07 22:16:20 +01:00
{
if (WindowFindByNumber(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);
WidgetInvalidateByNumber(w.classification, w.number, widgetIndex);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E9ABF
*/
static void InputScrollPartUpdateHRight(WindowBase& w, WidgetIndex widgetIndex, int32_t scroll_id)
2017-12-07 22:16:20 +01:00
{
const auto& widget = w.widgets[widgetIndex];
if (WindowFindByNumber(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 -= kScrollBarWidth + 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);
WidgetInvalidateByNumber(w.classification, w.number, widgetIndex);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E9C37
*/
static void InputScrollPartUpdateVTop(WindowBase& w, WidgetIndex widgetIndex, int32_t scroll_id)
2017-12-07 22:16:20 +01:00
{
if (WindowFindByNumber(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);
WidgetInvalidateByNumber(w.classification, w.number, widgetIndex);
2017-12-07 22:16:20 +01:00
}
}
/**
*
* rct2: 0x006E9C96
*/
static void InputScrollPartUpdateVBottom(WindowBase& w, WidgetIndex widgetIndex, int32_t scroll_id)
2017-12-07 22:16:20 +01:00
{
const auto& widget = w.widgets[widgetIndex];
if (WindowFindByNumber(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 -= kScrollBarWidth + 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);
WidgetInvalidateByNumber(w.classification, w.number, widgetIndex);
2017-12-07 22:16:20 +01:00
}
}
#pragma endregion
#pragma region Widgets
/**
*
* rct2: 0x006E9253
*/
static void InputWidgetOver(const ScreenCoordsXY& screenCoords, WindowBase* 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
{
w->OnScrollMouseOver(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
{
WindowBase* w = WindowFindByNumber(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
{
w->OnPrepareDraw();
if (w->widgets[gHoverWidget.widget_index].type == WindowWidgetType::FlatBtn)
2017-12-15 23:48:40 +01:00
{
WidgetInvalidateByNumber(gHoverWidget.window_classification, gHoverWidget.window_number, gHoverWidget.widget_index);
2017-12-07 22:16:20 +01:00
}
}
}
/**
*
* rct2: 0x006E95F9
*/
static void InputWidgetLeft(const ScreenCoordsXY& screenCoords, WindowBase* 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;
}
WindowCloseByClass(WindowClass::Error);
WindowCloseByClass(WindowClass::Tooltip);
2017-12-07 22:16:20 +01:00
// Window might have changed position in the list, therefore find it again
w = WindowFindByNumber(windowClass, windowNumber);
2018-01-04 06:58:44 +01:00
if (w == nullptr)
2017-12-07 22:16:20 +01:00
return;
w = WindowBringToFront(*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
{
WindowCancelTextbox();
2017-12-07 22:16:20 +01:00
}
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 (WindowCanResize(*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
{
w = WindowFindByNumber(gCurrentToolWidget.window_classification, gCurrentToolWidget.window_number);
2018-06-22 23:20:31 +02:00
if (w != nullptr)
{
InputSetFlag(INPUT_FLAG_4, true);
w->OnToolDown(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;
_clickRepeatTicks = gCurrentRealTimeTicks;
2017-12-07 22:16:20 +01:00
WidgetInvalidateByNumber(windowClass, windowNumber, widgetIndex);
w->OnMouseDown(widgetIndex);
2018-06-22 23:20:31 +02:00
}
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
{
WindowBase* 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);
window = WindowFindFromPoint(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
{
WidgetIndex widgetId = WindowFindWidgetFromPoint(*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->OnCursor(widgetId, scrollCoords, CursorID::Arrow);
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:
cursorId = window->OnCursor(widgetId, screenCoords, CursorID::Arrow);
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)
{
WindowBase* w = WindowFindByNumber(gCurrentToolWidget.window_classification, gCurrentToolWidget.window_number);
2017-12-07 22:16:20 +01:00
if (w == nullptr)
ToolCancel();
else if (InputGetState() != InputState::ViewportRight)
w->OnToolUpdate(gCurrentToolWidget.widget_index, screenCoords);
2017-12-07 22:16:20 +01:00
}
}
2023-04-22 09:58:13 +01:00
const std::map<colour_t, StringId> kColourToTip = {
{ COLOUR_BLACK, STR_COLOUR_BLACK_TIP },
{ COLOUR_GREY, STR_COLOUR_GREY_TIP },
{ COLOUR_WHITE, STR_COLOUR_WHITE_TIP },
{ COLOUR_DARK_PURPLE, STR_COLOUR_DARK_PURPLE_TIP },
{ COLOUR_LIGHT_PURPLE, STR_COLOUR_LIGHT_PURPLE_TIP },
{ COLOUR_BRIGHT_PURPLE, STR_COLOUR_BRIGHT_PURPLE_TIP },
{ COLOUR_DARK_BLUE, STR_COLOUR_DARK_BLUE_TIP },
{ COLOUR_LIGHT_BLUE, STR_COLOUR_LIGHT_BLUE_TIP },
{ COLOUR_ICY_BLUE, STR_COLOUR_ICY_BLUE_TIP },
{ COLOUR_TEAL, STR_COLOUR_TEAL_TIP },
{ COLOUR_AQUAMARINE, STR_COLOUR_AQUAMARINE_TIP },
{ COLOUR_SATURATED_GREEN, STR_COLOUR_SATURATED_GREEN_TIP },
{ COLOUR_DARK_GREEN, STR_COLOUR_DARK_GREEN_TIP },
{ COLOUR_MOSS_GREEN, STR_COLOUR_MOSS_GREEN_TIP },
{ COLOUR_BRIGHT_GREEN, STR_COLOUR_BRIGHT_GREEN_TIP },
{ COLOUR_OLIVE_GREEN, STR_COLOUR_OLIVE_GREEN_TIP },
{ COLOUR_DARK_OLIVE_GREEN, STR_COLOUR_DARK_OLIVE_GREEN_TIP },
{ COLOUR_BRIGHT_YELLOW, STR_COLOUR_BRIGHT_YELLOW_TIP },
{ COLOUR_YELLOW, STR_COLOUR_YELLOW_TIP },
{ COLOUR_DARK_YELLOW, STR_COLOUR_DARK_YELLOW_TIP },
{ COLOUR_LIGHT_ORANGE, STR_COLOUR_LIGHT_ORANGE_TIP },
{ COLOUR_DARK_ORANGE, STR_COLOUR_DARK_ORANGE_TIP },
{ COLOUR_LIGHT_BROWN, STR_COLOUR_LIGHT_BROWN_TIP },
{ COLOUR_SATURATED_BROWN, STR_COLOUR_SATURATED_BROWN_TIP },
{ COLOUR_DARK_BROWN, STR_COLOUR_DARK_BROWN_TIP },
{ COLOUR_SALMON_PINK, STR_COLOUR_SALMON_PINK_TIP },
{ COLOUR_BORDEAUX_RED, STR_COLOUR_BORDEAUX_RED_TIP },
{ COLOUR_SATURATED_RED, STR_COLOUR_SATURATED_RED_TIP },
{ COLOUR_BRIGHT_RED, STR_COLOUR_BRIGHT_RED_TIP },
{ COLOUR_DARK_PINK, STR_COLOUR_DARK_PINK_TIP },
{ COLOUR_BRIGHT_PINK, STR_COLOUR_BRIGHT_PINK_TIP },
{ COLOUR_LIGHT_PINK, STR_COLOUR_LIGHT_PINK_TIP },
{ COLOUR_DARK_OLIVE_DARK, STR_COLOUR_DARK_OLIVE_DARK_TIP },
{ COLOUR_DARK_OLIVE_LIGHT, STR_COLOUR_DARK_OLIVE_LIGHT_TIP },
{ COLOUR_SATURATED_BROWN_LIGHT, STR_COLOUR_SATURATED_BROWN_LIGHT_TIP },
{ COLOUR_BORDEAUX_RED_DARK, STR_COLOUR_BORDEAUX_RED_DARK_TIP },
{ COLOUR_BORDEAUX_RED_LIGHT, STR_COLOUR_BORDEAUX_RED_LIGHT_TIP },
{ COLOUR_GRASS_GREEN_DARK, STR_COLOUR_GRASS_GREEN_DARK_TIP },
{ COLOUR_GRASS_GREEN_LIGHT, STR_COLOUR_GRASS_GREEN_LIGHT_TIP },
{ COLOUR_OLIVE_DARK, STR_COLOUR_OLIVE_DARK_TIP },
{ COLOUR_OLIVE_LIGHT, STR_COLOUR_OLIVE_LIGHT_TIP },
{ COLOUR_SATURATED_GREEN_LIGHT, STR_COLOUR_SATURATED_GREEN_LIGHT_TIP },
{ COLOUR_TAN_DARK, STR_COLOUR_TAN_DARK_TIP },
{ COLOUR_TAN_LIGHT, STR_COLOUR_TAN_LIGHT_TIP },
{ COLOUR_DULL_PURPLE_LIGHT, STR_COLOUR_DULL_PURPLE_LIGHT_TIP },
{ COLOUR_DULL_GREEN_DARK, STR_COLOUR_DULL_GREEN_DARK_TIP },
{ COLOUR_DULL_GREEN_LIGHT, STR_COLOUR_DULL_GREEN_LIGHT_TIP },
{ COLOUR_SATURATED_PURPLE_DARK, STR_COLOUR_SATURATED_PURPLE_DARK_TIP },
{ COLOUR_SATURATED_PURPLE_LIGHT, STR_COLOUR_SATURATED_PURPLE_LIGHT_TIP },
{ COLOUR_ORANGE_LIGHT, STR_COLOUR_ORANGE_LIGHT_TIP },
{ COLOUR_AQUA_DARK, STR_COLOUR_AQUA_DARK_TIP },
{ COLOUR_MAGENTA_LIGHT, STR_COLOUR_MAGENTA_LIGHT_TIP },
{ COLOUR_DULL_BROWN_DARK, STR_COLOUR_DULL_BROWN_DARK_TIP },
{ COLOUR_DULL_BROWN_LIGHT, STR_COLOUR_DULL_BROWN_LIGHT_TIP },
{ COLOUR_INVISIBLE, STR_COLOUR_INVISIBLE_TIP },
{ COLOUR_VOID, STR_COLOUR_VOID_TIP },
};
2017-12-07 22:16:20 +01:00
/**
*
* rct2: 0x006E8DA7
*/
2020-11-02 06:43:35 -07:00
void InputStateWidgetPressed(
const ScreenCoordsXY& screenCoords, MouseState state, WidgetIndex widgetIndex, WindowBase* 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
WindowBase* cursor_w = WindowFindByNumber(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
// If this variable is non-zero then its the last tick the mouse down event was fired.
if (_clickRepeatTicks.has_value())
2017-12-15 23:48:40 +01:00
{
// The initial amount of time in ticks to wait until the first click repeat.
constexpr auto ticksUntilRepeats = 16U;
2018-06-22 23:20:31 +02:00
// The amount of ticks between each click repeat.
constexpr auto eventDelayInTicks = 3U;
// The amount of ticks since the last click repeat.
const auto clickRepeatsDelta = gCurrentRealTimeTicks - _clickRepeatTicks.value();
// Handle click repeat, only start this when at least 16 ticks elapsed.
if (clickRepeatsDelta >= ticksUntilRepeats && (clickRepeatsDelta & eventDelayInTicks) == 0)
2017-12-15 23:48:40 +01:00
{
if (WidgetIsHoldable(*w, widgetIndex))
2018-06-22 23:20:31 +02:00
{
w->OnMouseDown(widgetIndex);
2018-06-22 23:20:31 +02:00
}
// Subtract initial delay from here on we want the event each third tick.
_clickRepeatTicks = gCurrentRealTimeTicks - ticksUntilRepeats;
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;
WindowInvalidateByClass(WindowClass::Dropdown);
2018-06-22 23:20:31 +02:00
}
return;
}
_inputFlags |= INPUT_FLAG_WIDGET_PRESSED;
WidgetInvalidateByNumber(cursor_w_class, cursor_w_number, widgetIndex);
2018-06-22 23:20:31 +02:00
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
WindowCloseByClass(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 = WindowFindByNumber(wClass, wNumber);
2018-06-22 23:20:31 +02:00
}
else
2018-06-22 23:20:31 +02:00
{
cursor_w = WindowFindByNumber(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;
WidgetInvalidateByNumber(cursor_w_class, cursor_w_number, cursor_widgetIndex);
2018-06-22 23:20:31 +02:00
}
_inputState = InputState::Normal;
2023-05-18 18:07:57 +03:00
gTooltipCloseTimeout = 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;
}
}
cursor_w->OnDropdown(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;
}
2023-05-18 18:07:57 +03:00
gTooltipCloseTimeout = 0;
2018-06-22 23:20:31 +02:00
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;
WidgetInvalidateByNumber(cursor_w_class, cursor_w_number, widgetIndex);
w->OnMouseUp(widgetIndex);
return;
2018-06-22 23:20:31 +02:00
default:
2017-12-07 22:16:20 +01:00
return;
}
_clickRepeatTicks = std::nullopt;
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;
WidgetInvalidateByNumber(cursor_w_class, cursor_w_number, cursor_widgetIndex);
2017-12-07 22:16:20 +01:00
}
return;
}
else if (gDropdownIsColour)
{
// This is ordinarily covered in InputWidgetOver but the dropdown with colours is a special case.
InputUpdateTooltip(w, widgetIndex, screenCoords);
}
2017-12-07 22:16:20 +01:00
gDropdownHighlightedIndex = -1;
WindowInvalidateByClass(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
2023-04-22 09:58:13 +01:00
WindowTooltipShow(OpenRCT2String{ kColourToTip.at(ColourDropDownIndexToColour(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;
WindowInvalidateByClass(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
}
}
static void InputUpdateTooltip(WindowBase* 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
{
2023-05-18 18:07:57 +03:00
if (gCurrentRealTimeTicks >= _tooltipNotShownTimeout && w != nullptr && WidgetIsVisible(*w, widgetIndex))
2017-12-15 23:48:40 +01:00
{
2023-05-18 18:07:57 +03:00
gTooltipCloseTimeout = gCurrentRealTimeTicks + 8000;
WindowTooltipOpen(w, widgetIndex, screenCoords);
2017-12-07 22:16:20 +01:00
}
}
2023-05-18 18:07:57 +03:00
else
{
ResetTooltipNotShown();
}
2023-05-18 18:07:57 +03:00
gTooltipCloseTimeout = gCurrentRealTimeTicks + 8000;
2020-10-17 09:10:08 +01:00
gTooltipCursor = screenCoords;
2017-12-15 23:48:40 +01:00
}
else
{
2023-05-18 18:07:57 +03:00
gTooltipCursor = screenCoords;
ResetTooltipNotShown();
2017-12-07 22:16:20 +01:00
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
}
2023-05-18 18:07:57 +03:00
if (gCurrentRealTimeTicks >= gTooltipCloseTimeout)
2017-12-15 23:48:40 +01:00
{
WindowCloseByClass(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
{
WindowBase* w = WindowFindByNumber(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;
WindowInvalidateByNumber(gPressedWidget.window_classification, gPressedWidget.window_number);
2017-12-07 22:16:20 +01:00
}
}
/**
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
{
WindowBase* mainWindow;
int32_t scrollX, scrollY;
2017-12-07 22:16:20 +01:00
mainWindow = WindowGetMain();
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
{
WindowBase* mainWindow = WindowGetMain();
if (mainWindow == nullptr)
return;
Viewport* viewport = mainWindow->viewport;
if (viewport == nullptr)
return;
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 = ViewportPosToMapPos({ x, y }, 0, viewport->rotation);
auto mapCoord_dy = ViewportPosToMapPos({ x, y_dy }, 0, viewport->rotation);
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;
if (mapCoord.x < kMapMinimumXY || mapCoord.y < kMapMinimumXY)
2017-12-07 22:16:20 +01:00
{
at_map_edge = 1;
}
if (mapCoord_dy.x < kMapMinimumXY || mapCoord_dy.y < kMapMinimumXY)
2017-12-07 22:16:20 +01:00
{
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;
}
}