OpenRCT2/src/openrct2-ui/windows/MapTooltip.cpp

152 lines
4.0 KiB
C++
Raw Normal View History

2014-10-17 03:01:58 +02:00
/*****************************************************************************
* Copyright (c) 2014-2023 OpenRCT2 developers
2014-10-17 03:01:58 +02:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2014-10-17 03:01:58 +02:00
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
2014-10-17 03:01:58 +02:00
*****************************************************************************/
2018-06-22 23:21:44 +02:00
#include "../interface/Theme.h"
2017-08-02 00:20:32 +02:00
2018-06-22 23:21:44 +02:00
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
2017-10-21 17:57:37 +02:00
#include <openrct2/Context.h>
2017-12-12 14:52:57 +01:00
#include <openrct2/Input.h>
2018-03-19 23:28:40 +01:00
#include <openrct2/drawing/Drawing.h>
2021-12-12 00:06:06 +01:00
#include <openrct2/localisation/Formatter.h>
2018-06-22 23:21:44 +02:00
#include <openrct2/localisation/Localisation.h>
2014-10-17 03:01:58 +02:00
// clang-format off
2022-12-24 16:50:29 +01:00
static Widget window_map_tooltip_widgets[] = {
MakeWidget({0, 0}, {200, 30}, WindowWidgetType::ImgBtn, WindowColour::Primary),
2021-09-26 11:11:42 +02:00
WIDGETS_END,
2014-10-17 03:01:58 +02:00
};
static void WindowMapTooltipUpdate(WindowBase *w);
static void WindowMapTooltipPaint(WindowBase *w, DrawPixelInfo *dpi);
static WindowEventList window_map_tooltip_events([](auto& events)
{
events.update = &WindowMapTooltipUpdate;
events.paint = &WindowMapTooltipPaint;
});
// clang-format on
2014-10-17 03:01:58 +02:00
2015-10-20 20:16:30 +02:00
#define MAP_TOOLTIP_ARGS
2014-10-17 03:01:58 +02:00
static ScreenCoordsXY _lastCursor;
static int32_t _cursorHoldDuration;
2014-10-17 03:01:58 +02:00
static void WindowMapTooltipOpen();
2014-10-17 03:01:58 +02:00
static Formatter _mapTooltipArgs;
void SetMapTooltip(Formatter& ft)
{
_mapTooltipArgs = ft;
}
const Formatter& GetMapTooltip()
{
return _mapTooltipArgs;
}
2014-10-17 03:01:58 +02:00
/**
*
* rct2: 0x006EE77A
*/
void WindowMapTooltipUpdateVisibility()
2014-10-17 03:01:58 +02:00
{
if (ThemeGetFlags() & UITHEME_FLAG_USE_FULL_BOTTOM_TOOLBAR)
{
// The map tooltip is drawn by the bottom toolbar
WindowInvalidateByClass(WindowClass::BottomToolbar);
return;
}
2022-11-06 21:49:07 +01:00
const CursorState* state = ContextGetCursorState();
auto cursor = state->position;
auto cursorChange = cursor - _lastCursor;
// Check for cursor movement
_cursorHoldDuration++;
if (abs(cursorChange.x) > 5 || abs(cursorChange.y) > 5 || (InputTestFlag(INPUT_FLAG_5))
|| InputGetState() == InputState::ViewportRight)
_cursorHoldDuration = 0;
_lastCursor = cursor;
// Show or hide tooltip
2022-07-31 14:22:58 +02:00
StringId stringId;
std::memcpy(&stringId, _mapTooltipArgs.Data(), sizeof(StringId));
2018-06-22 23:21:44 +02:00
if (_cursorHoldDuration < 25 || stringId == STR_NONE
|| InputTestPlaceObjectModifier(
2020-04-19 14:08:22 +02:00
static_cast<PLACE_OBJECT_MODIFIER>(PLACE_OBJECT_MODIFIER_COPY_Z | PLACE_OBJECT_MODIFIER_SHIFT_Z))
|| WindowFindByClass(WindowClass::Error) != nullptr)
2018-06-22 23:21:44 +02:00
{
WindowCloseByClass(WindowClass::MapTooltip);
2018-06-22 23:21:44 +02:00
}
else
{
WindowMapTooltipOpen();
}
2014-10-17 03:01:58 +02:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-10-17 03:01:58 +02:00
* rct2: 0x006A7C43
*/
static void WindowMapTooltipOpen()
2014-10-17 03:01:58 +02:00
{
WindowBase* w;
constexpr int32_t width = 200;
constexpr int32_t height = 44;
2022-11-06 21:49:07 +01:00
const CursorState* state = ContextGetCursorState();
ScreenCoordsXY pos = { state->position.x - (width / 2), state->position.y + 15 };
w = WindowFindByClass(WindowClass::MapTooltip);
2018-06-22 23:21:44 +02:00
if (w == nullptr)
{
w = WindowCreate(
pos, width, height, &window_map_tooltip_events, WindowClass::MapTooltip,
2018-06-22 23:21:44 +02:00
WF_STICK_TO_FRONT | WF_TRANSPARENT | WF_NO_BACKGROUND);
w->widgets = window_map_tooltip_widgets;
2018-06-22 23:21:44 +02:00
}
else
{
w->Invalidate();
2020-03-01 20:32:35 +01:00
w->windowPos = pos;
w->width = width;
w->height = height;
}
2014-10-17 03:01:58 +02:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-10-17 03:01:58 +02:00
* rct2: 0x006EE8CE
*/
static void WindowMapTooltipUpdate(WindowBase* w)
2014-10-17 03:01:58 +02:00
{
w->Invalidate();
2014-10-17 03:01:58 +02:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-10-17 03:01:58 +02:00
* rct2: 0x006EE894
*/
static void WindowMapTooltipPaint(WindowBase* w, DrawPixelInfo* dpi)
2014-10-17 03:01:58 +02:00
{
2022-07-31 14:22:58 +02:00
StringId stringId;
std::memcpy(&stringId, _mapTooltipArgs.Data(), sizeof(StringId));
2018-06-22 23:21:44 +02:00
if (stringId == STR_NONE)
{
return;
}
2014-10-17 03:01:58 +02:00
ScreenCoordsXY stringCoords(w->windowPos.x + (w->width / 2), w->windowPos.y + (w->height / 2));
DrawTextWrapped(dpi, stringCoords, w->width, STR_MAP_TOOLTIP_STRINGID, _mapTooltipArgs, { TextAlignment::CENTRE });
2015-12-21 05:03:37 +01:00
}