From d0c7d0f6fb786e82f166d4820c2e393c20b7b852 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Thu, 13 Apr 2023 14:16:14 -0300 Subject: [PATCH] Close #18318: Refactor MapTooltip window to class (#19914) --- src/openrct2-ui/windows/MapTooltip.cpp | 85 ++++++++++---------------- 1 file changed, 32 insertions(+), 53 deletions(-) diff --git a/src/openrct2-ui/windows/MapTooltip.cpp b/src/openrct2-ui/windows/MapTooltip.cpp index 3706bc3770..956edaf039 100644 --- a/src/openrct2-ui/windows/MapTooltip.cpp +++ b/src/openrct2-ui/windows/MapTooltip.cpp @@ -23,16 +23,7 @@ static Widget window_map_tooltip_widgets[] = { WIDGETS_END, }; -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 - #define MAP_TOOLTIP_ARGS static ScreenCoordsXY _lastCursor; @@ -42,6 +33,33 @@ static void WindowMapTooltipOpen(); static Formatter _mapTooltipArgs; +class MapTooltip final : public Window +{ +public: + MapTooltip() + { + widgets = window_map_tooltip_widgets; + } + + void OnUpdate() override + { + Invalidate(); + } + + void OnDraw(DrawPixelInfo& dpi) override + { + StringId stringId; + std::memcpy(&stringId, _mapTooltipArgs.Data(), sizeof(StringId)); + if (stringId == STR_NONE) + { + return; + } + + auto stringCoords = windowPos + ScreenCoordsXY{ width / 2, height / 2 }; + DrawTextWrapped(dpi, stringCoords, width, STR_MAP_TOOLTIP_STRINGID, _mapTooltipArgs, { TextAlignment::CENTRE }); + } +}; + void SetMapTooltip(Formatter& ft) { _mapTooltipArgs = ft; @@ -52,10 +70,6 @@ const Formatter& GetMapTooltip() return _mapTooltipArgs; } -/** - * - * rct2: 0x006EE77A - */ void WindowMapTooltipUpdateVisibility() { if (ThemeGetFlags() & UITHEME_FLAG_USE_FULL_BOTTOM_TOOLBAR) @@ -94,58 +108,23 @@ void WindowMapTooltipUpdateVisibility() } } -/** - * - * rct2: 0x006A7C43 - */ static void WindowMapTooltipOpen() { - WindowBase* w; - constexpr int32_t width = 200; constexpr int32_t height = 44; const CursorState* state = ContextGetCursorState(); - ScreenCoordsXY pos = { state->position.x - (width / 2), state->position.y + 15 }; + auto pos = state->position + ScreenCoordsXY{ -width / 2, 15 }; - w = WindowFindByClass(WindowClass::MapTooltip); - if (w == nullptr) - { - w = WindowCreate( - pos, width, height, &window_map_tooltip_events, WindowClass::MapTooltip, - WF_STICK_TO_FRONT | WF_TRANSPARENT | WF_NO_BACKGROUND); - w->widgets = window_map_tooltip_widgets; - } - else + if (auto w = WindowFindByClass(WindowClass::MapTooltip)) { w->Invalidate(); w->windowPos = pos; w->width = width; w->height = height; } -} - -/** - * - * rct2: 0x006EE8CE - */ -static void WindowMapTooltipUpdate(WindowBase* w) -{ - w->Invalidate(); -} - -/** - * - * rct2: 0x006EE894 - */ -static void WindowMapTooltipPaint(WindowBase* w, DrawPixelInfo& dpi) -{ - StringId stringId; - std::memcpy(&stringId, _mapTooltipArgs.Data(), sizeof(StringId)); - if (stringId == STR_NONE) + else { - return; + w = WindowCreate( + WindowClass::MapTooltip, pos, width, height, WF_STICK_TO_FRONT | WF_TRANSPARENT | WF_NO_BACKGROUND); } - - 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 }); }