Files
OpenRCT2-Web/src/openrct2/interface/Window.cpp
T

1334 lines
38 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
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
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-06-22 22:59:46 +02:00
#include "Window.h"
#include "../Context.h"
2024-07-09 16:23:31 +02:00
#include "../Diagnostic.h"
2018-06-22 22:59:46 +02:00
#include "../Editor.h"
#include "../Game.h"
2024-03-01 20:26:20 +00:00
#include "../GameState.h"
2018-06-22 22:59:46 +02:00
#include "../Input.h"
#include "../OpenRCT2.h"
2014-10-06 17:36:58 +01:00
#include "../audio/audio.h"
#include "../config/Config.h"
2017-02-17 09:07:05 +01:00
#include "../core/Guard.hpp"
2018-01-05 21:17:33 +00:00
#include "../drawing/Drawing.h"
#include "../interface/Cursors.h"
#include "../localisation/Formatting.h"
2018-01-06 18:32:25 +01:00
#include "../localisation/StringIds.h"
2022-02-18 21:57:00 +01:00
#include "../platform/Platform.h"
2021-01-09 20:54:02 +00:00
#include "../ride/RideAudio.h"
2018-03-19 23:28:40 +01:00
#include "../scenario/Scenario.h"
#include "../ui/UiContext.h"
#include "../ui/WindowManager.h"
2018-06-22 22:59:46 +02:00
#include "../world/Map.h"
2018-01-05 23:45:53 +00:00
#include "Viewport.h"
2018-01-05 23:56:37 +00:00
#include "Widget.h"
2018-01-05 23:31:19 +01:00
#include "Window_internal.h"
2018-01-06 00:05:16 +00:00
2024-07-09 16:43:03 +02:00
#include <cassert>
2018-06-22 22:59:46 +02:00
#include <cmath>
#include <functional>
2018-11-21 23:16:04 +01:00
#include <iterator>
#include <list>
2018-06-22 22:59:46 +02:00
2024-03-01 20:26:20 +00:00
using namespace OpenRCT2;
std::list<std::shared_ptr<WindowBase>> g_window_list;
WindowBase* gWindowAudioExclusive;
WindowCloseModifier gLastCloseModifier = { { WindowClass::Null, 0 }, CloseWindowModifier::None };
2015-05-13 11:38:44 -04:00
uint32_t gWindowUpdateTicks;
2024-05-09 02:45:07 +02:00
colour_t gCurrentWindowColours[3];
2016-08-06 01:32:40 +01:00
Tool gCurrentToolId;
WidgetRef gCurrentToolWidget;
// converted from uint16_t values at 0x009A41EC - 0x009A4230
2017-07-01 00:11:28 +02:00
// these are percentage coordinates of the viewport to centre to, if a window is obscuring a location, the next is tried
// clang-format off
static constexpr float window_scroll_locations[][2] = {
{ 0.5f, 0.5f },
{ 0.75f, 0.5f },
{ 0.25f, 0.5f },
{ 0.5f, 0.75f },
{ 0.5f, 0.25f },
{ 0.75f, 0.75f },
{ 0.75f, 0.25f },
{ 0.25f, 0.75f },
{ 0.25f, 0.25f },
{ 0.125f, 0.5f },
{ 0.875f, 0.5f },
{ 0.5f, 0.125f },
{ 0.5f, 0.875f },
{ 0.875f, 0.125f },
{ 0.875f, 0.875f },
{ 0.125f, 0.875f },
{ 0.125f, 0.125f },
2014-09-22 02:04:10 -06:00
};
// clang-format on
2014-09-22 02:04:10 -06:00
namespace OpenRCT2::WindowCloseFlags
{
static constexpr uint32_t None = 0;
2023-06-27 03:50:29 +03:00
static constexpr uint32_t CloseSingle = (1 << 0);
} // namespace OpenRCT2::WindowCloseFlags
static void WindowDrawCore(DrawPixelInfo& dpi, WindowBase& w, int32_t left, int32_t top, int32_t right, int32_t bottom);
static void WindowDrawSingle(DrawPixelInfo& dpi, WindowBase& w, int32_t left, int32_t top, int32_t right, int32_t bottom);
2014-04-08 19:49:04 +01:00
std::list<std::shared_ptr<WindowBase>>::iterator WindowGetIterator(const WindowBase* w)
2018-06-16 02:14:59 +01:00
{
return std::find_if(g_window_list.begin(), g_window_list.end(), [w](const std::shared_ptr<WindowBase>& w2) -> bool {
return w == w2.get();
});
2018-06-16 02:14:59 +01:00
}
void WindowVisitEach(std::function<void(WindowBase*)> func)
{
for (auto& w : g_window_list)
{
2023-06-27 03:50:29 +03:00
if (w->flags & WF_DEAD)
continue;
func(w.get());
}
}
2024-07-15 01:17:57 +02:00
void WindowSetFlagForAllViewports(uint32_t viewportFlag, bool enabled)
{
WindowVisitEach([&](WindowBase* w) {
if (w->viewport != nullptr)
{
if (enabled)
w->viewport->flags |= viewportFlag;
else
w->viewport->flags &= ~viewportFlag;
}
});
}
2014-04-08 17:52:39 +01:00
/**
2015-09-22 19:18:57 +02:00
*
2014-04-08 17:52:39 +01:00
* rct2: 0x006ED7B0
*/
void WindowDispatchUpdateAll()
{
2017-06-06 23:24:18 +02:00
// gTooltipNotShownTicks++;
WindowVisitEach([&](WindowBase* w) { w->OnUpdate(); });
}
void WindowUpdateAllViewports()
{
WindowVisitEach([&](WindowBase* w) {
if (w->viewport != nullptr && WindowIsVisible(*w))
2018-06-16 02:14:59 +01:00
{
2023-01-16 21:14:50 +01:00
ViewportUpdatePosition(w);
2018-06-16 02:14:59 +01:00
}
});
}
2014-04-08 17:52:39 +01:00
/**
2015-09-22 19:18:57 +02:00
*
2014-04-08 17:52:39 +01:00
* rct2: 0x006E77A1
*/
void WindowUpdateAll()
2014-04-08 17:52:39 +01:00
{
// Remove all windows in g_window_list that have the WF_DEAD flag
g_window_list.remove_if([](auto&& w) -> bool { return w->flags & WF_DEAD; });
2023-06-27 03:50:29 +03:00
// Periodic update happens every second so 40 ticks.
if (gCurrentRealTimeTicks >= gWindowUpdateTicks)
2018-06-22 22:59:46 +02:00
{
gWindowUpdateTicks = gCurrentRealTimeTicks + kGameUpdateFPS;
WindowVisitEach([](WindowBase* w) { w->OnPeriodicUpdate(); });
2017-06-06 23:24:18 +02:00
}
2014-04-08 17:52:39 +01:00
2017-06-06 23:24:18 +02:00
// Border flash invalidation
WindowVisitEach([](WindowBase* w) {
if (w->flags & WF_WHITE_BORDER_MASK)
{
2017-06-06 23:24:18 +02:00
w->flags -= WF_WHITE_BORDER_ONE;
if (!(w->flags & WF_WHITE_BORDER_MASK))
{
w->Invalidate();
}
2017-06-06 23:24:18 +02:00
}
});
2014-04-08 17:52:39 +01:00
2024-03-01 20:26:20 +00:00
auto windowManager = GetContext()->GetUiContext()->GetWindowManager();
windowManager->UpdateMouseWheel();
2014-04-08 17:52:39 +01:00
}
void WindowNotifyLanguageChange()
{
WindowVisitEach([&](WindowBase* w) { w->OnLanguageChange(); });
}
2023-01-16 21:14:50 +01:00
static void WindowCloseSurplus(int32_t cap, WindowClass avoid_classification)
{
2018-06-22 22:59:46 +02:00
// find the amount of windows that are currently open
2020-04-22 17:09:29 +02:00
auto count = static_cast<int32_t>(g_window_list.size());
2018-06-22 22:59:46 +02:00
// difference between amount open and cap = amount to close
auto diff = count - kWindowLimitReserved - cap;
2018-06-16 02:14:59 +01:00
for (auto i = 0; i < diff; i++)
{
2018-06-22 22:59:46 +02:00
// iterates through the list until it finds the newest window, or a window that can be closed
WindowBase* foundW{};
2018-06-16 02:14:59 +01:00
for (auto& w : g_window_list)
{
2023-06-27 03:50:29 +03:00
if (w->flags & WF_DEAD)
continue;
if (!(w->flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT | WF_NO_AUTO_CLOSE)))
2018-06-16 02:14:59 +01:00
{
foundW = w.get();
2017-06-06 23:24:18 +02:00
break;
2018-06-16 02:14:59 +01:00
}
2017-06-06 23:24:18 +02:00
}
// skip window if window matches specified WindowClass (as user may be modifying via options)
if (avoid_classification != WindowClass::Null && foundW != nullptr && foundW->classification == avoid_classification)
2018-06-22 22:59:46 +02:00
{
2017-06-06 23:24:18 +02:00
continue;
}
WindowClose(*foundW);
2017-06-06 23:24:18 +02:00
}
}
/*
* Changes the maximum amount of windows allowed
*/
void WindowSetWindowLimit(int32_t value)
{
2024-05-10 12:10:21 +02:00
int32_t prev = Config::Get().general.WindowLimit;
2024-04-26 15:35:42 +00:00
int32_t val = std::clamp<int32_t>(value, kWindowLimitMin, kWindowLimitMax);
2024-05-10 12:10:21 +02:00
Config::Get().general.WindowLimit = val;
Config::Save();
2017-06-06 23:24:18 +02:00
// Checks if value decreases and then closes surplus
// windows if one sets a limit lower than the number of windows open
2018-06-22 22:59:46 +02:00
if (val < prev)
{
2023-01-16 21:14:50 +01:00
WindowCloseSurplus(val, WindowClass::Options);
2017-06-06 23:24:18 +02:00
}
}
/**
* Closes the specified window.
* rct2: 0x006ECD4C
*
* @param window The window to close (esi).
*/
void WindowClose(WindowBase& w)
{
w.OnClose();
2017-06-06 23:24:18 +02:00
// Remove viewport
2023-06-27 03:50:29 +03:00
w.RemoveViewport();
2017-06-06 23:24:18 +02:00
// Invalidate the window (area)
2023-06-27 03:50:29 +03:00
w.Invalidate();
2023-06-27 03:50:29 +03:00
w.flags |= WF_DEAD;
}
template<typename TPred>
static void WindowCloseByCondition(TPred pred, uint32_t flags = WindowCloseFlags::None)
{
2023-06-27 03:50:29 +03:00
for (auto it = g_window_list.rbegin(); it != g_window_list.rend(); ++it)
{
2023-06-27 03:50:29 +03:00
auto& wnd = *(*it);
if (wnd.flags & WF_DEAD)
continue;
2023-06-27 03:50:29 +03:00
if (pred(&wnd))
{
WindowClose(wnd);
if (flags & WindowCloseFlags::CloseSingle)
{
2023-06-27 03:50:29 +03:00
return;
}
2023-06-27 03:50:29 +03:00
}
}
}
2014-04-09 14:43:27 +01:00
/**
2014-10-16 02:02:43 +01:00
* Closes all windows with the specified window class.
2014-04-09 14:43:27 +01:00
* rct2: 0x006ECCF4
2014-10-16 02:02:43 +01:00
* @param cls (cl) with bit 15 set
2014-04-09 14:43:27 +01:00
*/
void WindowCloseByClass(WindowClass cls)
2014-04-09 14:43:27 +01:00
{
WindowCloseByCondition([&](WindowBase* w) -> bool { return w->classification == cls; });
2014-04-09 14:43:27 +01:00
}
/**
2014-10-16 02:02:43 +01:00
* Closes all windows with specified window class and number.
* rct2: 0x006ECCF4
* @param cls (cl) without bit 15 set
* @param number (dx)
*/
void WindowCloseByNumber(WindowClass cls, rct_windownumber number)
2014-10-16 02:02:43 +01:00
{
WindowCloseByCondition([cls, number](WindowBase* w) -> bool { return w->classification == cls && w->number == number; });
2014-10-16 02:02:43 +01:00
}
2022-02-12 23:31:06 +02:00
// TODO: Refactor this to use variant once the new window class is done.
void WindowCloseByNumber(WindowClass cls, EntityId number)
2022-02-12 23:31:06 +02:00
{
WindowCloseByNumber(cls, static_cast<rct_windownumber>(number.ToUnderlying()));
2022-02-12 23:31:06 +02:00
}
2014-05-09 12:54:11 +02:00
/**
2015-12-11 09:38:37 -06:00
* Closes the top-most window
2014-05-09 12:54:11 +02:00
*
* rct2: 0x006E403C
*/
void WindowCloseTop()
2014-05-12 01:45:45 +01:00
{
WindowCloseByClass(WindowClass::Dropdown);
2014-05-09 12:54:11 +02:00
2017-06-06 23:24:18 +02:00
if (gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR)
{
2024-03-01 20:26:20 +00:00
if (GetGameState().EditorStep != EditorStep::LandscapeEditor)
2017-06-06 23:24:18 +02:00
return;
}
auto pred = [](WindowBase* w) -> bool { return !(w->flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT)); };
2023-06-27 03:50:29 +03:00
WindowCloseByCondition(pred, WindowCloseFlags::CloseSingle);
2014-05-09 12:54:11 +02:00
}
/**
2015-12-11 09:38:37 -06:00
* Closes all open windows
2014-05-09 12:54:11 +02:00
*
* rct2: 0x006EE927
*/
void WindowCloseAll()
{
WindowCloseByClass(WindowClass::Dropdown);
WindowCloseByCondition([](WindowBase* w) -> bool { return !(w->flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT)); });
2014-05-09 12:54:11 +02:00
}
void WindowCloseAllExceptClass(WindowClass cls)
{
WindowCloseByClass(WindowClass::Dropdown);
WindowCloseByCondition([cls](WindowBase* w) -> bool {
return w->classification != cls && !(w->flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT));
});
2015-05-23 14:56:54 -04:00
}
2017-12-25 09:40:53 +01:00
/**
* Closes all windows, save for those having any of the passed flags.
*/
void WindowCloseAllExceptFlags(uint16_t flags)
2017-12-20 02:14:29 +01:00
{
WindowCloseByCondition([flags](WindowBase* w) -> bool { return !(w->flags & flags); });
2017-12-20 02:14:29 +01:00
}
/**
* Closes all windows except the specified window number and class.
* @param number (dx)
* @param cls (cl) without bit 15 set
*/
void WindowCloseAllExceptNumberAndClass(rct_windownumber number, WindowClass cls)
{
WindowCloseByClass(WindowClass::Dropdown);
WindowCloseByCondition([cls, number](WindowBase* w) -> bool {
return (!(w->number == number && w->classification == cls) && !(w->flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT)));
});
}
/**
* Invalidates the specified window.
* rct2: 0x006EB13A
*
* @param window The window to invalidate (esi).
*/
template<typename TPred>
static void WindowInvalidateByCondition(TPred pred)
{
WindowVisitEach([pred](WindowBase* w) {
if (pred(w))
{
w->Invalidate();
}
});
}
2014-04-04 21:46:26 +01:00
/**
2014-10-16 02:02:43 +01:00
* Invalidates all windows with the specified window class.
2014-04-04 21:46:26 +01:00
* rct2: 0x006EC3AC
2014-10-16 02:02:43 +01:00
* @param cls (al) with bit 14 set
2014-04-04 21:46:26 +01:00
*/
void WindowInvalidateByClass(WindowClass cls)
2014-04-04 21:46:26 +01:00
{
WindowInvalidateByCondition([cls](WindowBase* w) -> bool { return w->classification == cls; });
}
2014-10-16 02:02:43 +01:00
/**
* Invalidates all windows with the specified window class and number.
* rct2: 0x006EC3AC
*/
void WindowInvalidateByNumber(WindowClass cls, rct_windownumber number)
2014-04-14 21:25:20 +01:00
{
2023-01-16 21:14:50 +01:00
WindowInvalidateByCondition(
[cls, number](WindowBase* w) -> bool { return w->classification == cls && w->number == number; });
2014-10-16 02:02:43 +01:00
}
2022-02-12 23:31:06 +02:00
// TODO: Use variant for this once the window framework is done.
void WindowInvalidateByNumber(WindowClass cls, EntityId id)
2022-02-12 23:31:06 +02:00
{
WindowInvalidateByNumber(cls, static_cast<rct_windownumber>(id.ToUnderlying()));
2022-02-12 23:31:06 +02:00
}
/**
2015-12-11 09:38:37 -06:00
* Invalidates all windows.
*/
void WindowInvalidateAll()
{
WindowVisitEach([](WindowBase* w) { w->Invalidate(); });
}
2014-10-16 02:02:43 +01:00
/**
* Invalidates the specified widget of a window.
* rct2: 0x006EC402
*/
void WidgetInvalidate(WindowBase& w, WidgetIndex widgetIndex)
2014-10-16 02:02:43 +01:00
{
2017-02-07 21:01:07 +01:00
#ifdef DEBUG
2018-06-22 22:59:46 +02:00
for (int32_t i = 0; i <= widgetIndex; i++)
{
assert(w.widgets[i].type != WindowWidgetType::Last);
2017-06-06 23:24:18 +02:00
}
2016-08-21 15:20:30 +01:00
#endif
const auto& widget = w.widgets[widgetIndex];
if (widget.left == -2)
2017-06-06 23:24:18 +02:00
return;
2014-10-16 02:02:43 +01:00
GfxSetDirtyBlocks({ { w.windowPos + ScreenCoordsXY{ widget.left, widget.top } },
{ w.windowPos + ScreenCoordsXY{ widget.right + 1, widget.bottom + 1 } } });
2014-10-16 02:02:43 +01:00
}
template<typename TPred>
static void widget_invalidate_by_condition(TPred pred)
{
WindowVisitEach([pred](WindowBase* w) {
if (pred(w))
{
w->Invalidate();
}
});
}
2014-10-16 02:02:43 +01:00
/**
* Invalidates the specified widget of all windows that match the specified window class.
*/
void WidgetInvalidateByClass(WindowClass cls, WidgetIndex widgetIndex)
2014-10-16 02:02:43 +01:00
{
WindowVisitEach([cls, widgetIndex](WindowBase* w) {
if (w->classification == cls)
2018-06-16 02:14:59 +01:00
{
WidgetInvalidate(*w, widgetIndex);
2018-06-16 02:14:59 +01:00
}
});
2014-10-16 02:02:43 +01:00
}
/**
* Invalidates the specified widget of all windows that match the specified window class and number.
* rct2: 0x006EC3AC
*/
void WidgetInvalidateByNumber(WindowClass cls, rct_windownumber number, WidgetIndex widgetIndex)
2014-10-16 02:02:43 +01:00
{
WindowVisitEach([cls, number, widgetIndex](WindowBase* w) {
if (w->classification == cls && w->number == number)
2018-06-16 02:14:59 +01:00
{
WidgetInvalidate(*w, widgetIndex);
2018-06-16 02:14:59 +01:00
}
});
2014-04-14 21:25:20 +01:00
}
int32_t WindowGetScrollDataIndex(const WindowBase& w, WidgetIndex widget_index)
2014-04-03 19:49:00 +01:00
{
int32_t i, result;
2014-04-03 19:49:00 +01:00
2017-06-06 23:24:18 +02:00
result = 0;
2018-06-22 22:59:46 +02:00
for (i = 0; i < widget_index; i++)
{
const auto& widget = w.widgets[i];
if (widget.type == WindowWidgetType::Scroll)
2017-06-06 23:24:18 +02:00
result++;
}
return result;
2014-04-03 19:49:00 +01:00
}
/**
2015-09-22 19:18:57 +02:00
*
2014-04-03 19:49:00 +01:00
* rct2: 0x006ECDA4
*/
WindowBase* WindowBringToFront(WindowBase& w)
2014-04-03 19:49:00 +01:00
{
if (!(w.flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT)))
2018-06-16 02:14:59 +01:00
{
auto itSourcePos = WindowGetIterator(&w);
if (itSourcePos != g_window_list.end())
2018-06-16 02:14:59 +01:00
{
// Insert in front of the first non-stick-to-front window
auto itDestPos = g_window_list.begin();
for (auto it = g_window_list.rbegin(); it != g_window_list.rend(); it++)
2018-06-16 02:14:59 +01:00
{
auto& w2 = *it;
if (!(w2->flags & WF_STICK_TO_FRONT))
2018-06-16 02:14:59 +01:00
{
itDestPos = it.base();
2018-06-16 02:14:59 +01:00
break;
}
}
2014-04-03 19:49:00 +01:00
g_window_list.splice(itDestPos, g_window_list, itSourcePos);
w.Invalidate();
2014-04-03 19:49:00 +01:00
if (w.windowPos.x + w.width < 20)
2018-06-16 02:14:59 +01:00
{
int32_t i = 20 - w.windowPos.x;
w.windowPos.x += i;
if (w.viewport != nullptr)
w.viewport->pos.x += i;
w.Invalidate();
2018-06-16 02:14:59 +01:00
}
}
2017-06-06 23:24:18 +02:00
}
return &w;
2014-04-08 17:52:39 +01:00
}
WindowBase* WindowBringToFrontByClassWithFlags(WindowClass cls, uint16_t flags)
2014-10-16 02:02:43 +01:00
{
auto* windowMgr = GetContext()->GetUiContext()->GetWindowManager();
WindowBase* w = windowMgr->FindByClass(cls);
2018-06-22 22:59:46 +02:00
if (w != nullptr)
{
2017-06-06 23:24:18 +02:00
w->flags |= flags;
w->Invalidate();
w = WindowBringToFront(*w);
2017-06-06 23:24:18 +02:00
}
2014-10-16 02:02:43 +01:00
2017-06-06 23:24:18 +02:00
return w;
2014-10-16 02:02:43 +01:00
}
WindowBase* WindowBringToFrontByClass(WindowClass cls)
{
return WindowBringToFrontByClassWithFlags(cls, WF_WHITE_BORDER_MASK);
}
2014-10-16 02:02:43 +01:00
/**
2015-09-22 19:18:57 +02:00
*
2014-10-16 02:02:43 +01:00
* rct2: 0x006ED78A
* cls (cl)
* number (dx)
*/
WindowBase* WindowBringToFrontByNumber(WindowClass cls, rct_windownumber number)
2014-10-16 02:02:43 +01:00
{
auto* windowMgr = GetContext()->GetUiContext()->GetWindowManager();
WindowBase* w = windowMgr->FindByNumber(cls, number);
2018-06-22 22:59:46 +02:00
if (w != nullptr)
{
2017-06-06 23:24:18 +02:00
w->flags |= WF_WHITE_BORDER_MASK;
w->Invalidate();
w = WindowBringToFront(*w);
2017-06-06 23:24:18 +02:00
}
2014-10-16 02:02:43 +01:00
2017-06-06 23:24:18 +02:00
return w;
2014-10-16 02:02:43 +01:00
}
/**
*
2015-12-11 09:38:37 -06:00
* rct2: 0x006EE65A
*/
void WindowPushOthersRight(WindowBase& window)
{
WindowVisitEach([&window](WindowBase* w) {
if (w == &window)
return;
if (w->flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT))
return;
if (w->windowPos.x >= window.windowPos.x + window.width)
return;
if (w->windowPos.x + w->width <= window.windowPos.x)
return;
if (w->windowPos.y >= window.windowPos.y + window.height)
return;
if (w->windowPos.y + w->height <= window.windowPos.y)
return;
w->Invalidate();
2022-11-06 20:49:07 +00:00
if (window.windowPos.x + window.width + 13 >= ContextGetWidth())
return;
auto push_amount = window.windowPos.x + window.width - w->windowPos.x + 3;
2020-03-01 16:32:35 -03:00
w->windowPos.x += push_amount;
w->Invalidate();
if (w->viewport != nullptr)
2020-03-01 09:56:14 -03:00
w->viewport->pos.x += push_amount;
});
}
2014-04-13 22:23:56 +01:00
/**
2015-09-22 19:18:57 +02:00
*
2014-04-13 22:23:56 +01:00
* rct2: 0x006EE6EA
*/
void WindowPushOthersBelow(WindowBase& w1)
2014-04-13 22:23:56 +01:00
{
2017-06-06 23:24:18 +02:00
// Enumerate through all other windows
WindowVisitEach([&w1](WindowBase* w2) {
if (&w1 == w2)
return;
2017-06-06 23:24:18 +02:00
// ?
if (w2->flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT))
return;
2017-06-06 23:24:18 +02:00
// Check if w2 intersects with w1
if (w2->windowPos.x > (w1.windowPos.x + w1.width) || w2->windowPos.x + w2->width < w1.windowPos.x)
return;
if (w2->windowPos.y > (w1.windowPos.y + w1.height) || w2->windowPos.y + w2->height < w1.windowPos.y)
return;
2014-04-13 22:23:56 +01:00
2017-06-06 23:24:18 +02:00
// Check if there is room to push it down
2022-11-06 20:49:07 +00:00
if (w1.windowPos.y + w1.height + 80 >= ContextGetHeight())
return;
2014-04-13 22:23:56 +01:00
2017-06-06 23:24:18 +02:00
// Invalidate the window's current area
w2->Invalidate();
2014-04-13 22:23:56 +01:00
int32_t push_amount = w1.windowPos.y + w1.height - w2->windowPos.y + 3;
2020-03-01 16:32:35 -03:00
w2->windowPos.y += push_amount;
2014-04-13 22:23:56 +01:00
2017-06-06 23:24:18 +02:00
// Invalidate the window's new area
w2->Invalidate();
2014-04-13 22:23:56 +01:00
2017-06-06 23:24:18 +02:00
// Update viewport position if necessary
if (w2->viewport != nullptr)
2020-03-01 09:56:14 -03:00
w2->viewport->pos.y += push_amount;
});
2014-04-13 22:23:56 +01:00
}
2014-04-09 17:06:47 +01:00
/**
2015-09-22 19:18:57 +02:00
*
2014-04-09 17:06:47 +01:00
* rct2: 0x006EE2E4
*/
WindowBase* WindowGetMain()
2014-04-09 17:06:47 +01:00
{
2018-06-16 02:14:59 +01:00
for (auto& w : g_window_list)
{
2023-06-27 03:50:29 +03:00
if (w->flags & WF_DEAD)
continue;
if (w->classification == WindowClass::MainWindow)
2018-06-16 02:14:59 +01:00
{
return w.get();
2018-06-16 02:14:59 +01:00
}
}
2018-01-07 21:43:07 +01:00
return nullptr;
}
2014-04-11 17:38:15 +01:00
/**
2018-06-22 22:59:46 +02:00
*
* rct2: 0x006E7C9C
* @param w (esi)
* @param x (eax)
* @param y (ecx)
* @param z (edx)
*/
void WindowScrollToLocation(WindowBase& w, const CoordsXYZ& coords)
2014-04-11 17:38:15 +01:00
{
WindowUnfollowSprite(w);
if (w.viewport != nullptr)
2018-06-22 22:59:46 +02:00
{
2022-10-12 06:35:20 +01:00
int16_t height = TileElementHeight(coords);
if (coords.z < height - 16)
2018-06-22 22:59:46 +02:00
{
if (!(w.viewport->flags & VIEWPORT_FLAG_UNDERGROUND_INSIDE))
2018-06-22 22:59:46 +02:00
{
w.viewport->flags |= VIEWPORT_FLAG_UNDERGROUND_INSIDE;
w.Invalidate();
2017-06-06 23:24:18 +02:00
}
2018-06-22 22:59:46 +02:00
}
else
{
if (w.viewport->flags & VIEWPORT_FLAG_UNDERGROUND_INSIDE)
2018-06-22 22:59:46 +02:00
{
w.viewport->flags &= ~VIEWPORT_FLAG_UNDERGROUND_INSIDE;
w.Invalidate();
2017-06-06 23:24:18 +02:00
}
}
auto screenCoords = Translate3DTo2DWithZ(w.viewport->rotation, coords);
int32_t i = 0;
2018-06-16 02:14:59 +01:00
if (!(gScreenFlags & SCREEN_FLAGS_TITLE_DEMO))
{
bool found = false;
while (!found)
{
auto x2 = w.viewport->pos.x + static_cast<int32_t>(w.viewport->width * window_scroll_locations[i][0]);
auto y2 = w.viewport->pos.y + static_cast<int32_t>(w.viewport->height * window_scroll_locations[i][1]);
2018-06-16 02:14:59 +01:00
auto it = WindowGetIterator(&w);
for (; it != g_window_list.end(); it++)
{
auto w2 = (*it).get();
2021-08-16 20:51:16 +01:00
auto x1 = w2->windowPos.x - 10;
auto y1 = w2->windowPos.y - 10;
if (x2 >= x1 && x2 <= w2->width + x1 + 20)
2018-06-16 02:14:59 +01:00
{
if (y2 >= y1 && y2 <= w2->height + y1 + 20)
2018-06-16 02:14:59 +01:00
{
2017-06-06 23:24:18 +02:00
// window is covering this area, try the next one
i++;
2018-06-16 02:14:59 +01:00
found = false;
2017-06-06 23:24:18 +02:00
break;
}
}
}
if (it == g_window_list.end())
{
found = true;
}
2020-04-22 17:09:29 +02:00
if (i >= static_cast<int32_t>(std::size(window_scroll_locations)))
2018-06-16 02:14:59 +01:00
{
2017-06-06 23:24:18 +02:00
i = 0;
2018-06-16 02:14:59 +01:00
found = true;
2017-06-06 23:24:18 +02:00
}
}
}
// rct2: 0x006E7C76
if (w.viewport_target_sprite.IsNull())
2018-06-22 22:59:46 +02:00
{
if (!(w.flags & WF_NO_SCROLLING))
2018-06-22 22:59:46 +02:00
{
w.savedViewPos = screenCoords
- ScreenCoordsXY{ static_cast<int32_t>(w.viewport->ViewWidth() * window_scroll_locations[i][0]),
static_cast<int32_t>(w.viewport->ViewHeight() * window_scroll_locations[i][1]) };
w.flags |= WF_SCROLLING_TO_LOCATION;
2017-06-06 23:24:18 +02:00
}
}
}
2014-04-11 17:38:15 +01:00
}
void WindowViewportGetMapCoordsByCursor(
const WindowBase& w, int32_t* map_x, int32_t* map_y, int32_t* offset_x, int32_t* offset_y)
{
2017-06-06 23:24:18 +02:00
// Get mouse position to offset against.
2022-11-06 20:49:07 +00:00
auto mouseCoords = ContextGetCursorPositionScaled();
2017-06-06 23:24:18 +02:00
// Compute map coordinate by mouse position.
auto viewportPos = w.viewport->ScreenToViewportCoord(mouseCoords);
auto coordsXYZ = ViewportAdjustForMapHeight(viewportPos, w.viewport->rotation);
auto mapCoords = ViewportPosToMapPos(viewportPos, coordsXYZ.z, w.viewport->rotation);
*map_x = mapCoords.x;
*map_y = mapCoords.y;
2017-06-06 23:24:18 +02:00
// Get viewport coordinates centring around the tile.
2022-10-12 06:35:20 +01:00
int32_t z = TileElementHeight(mapCoords);
auto centreLoc = centre_2d_coordinates({ mapCoords.x, mapCoords.y, z }, w.viewport);
if (!centreLoc)
{
LOG_ERROR("Invalid location.");
return;
}
2017-06-06 23:24:18 +02:00
// Rebase mouse position onto centre of window, and compensate for zoom level.
int32_t rebased_x = w.viewport->zoom.ApplyTo(w.width / 2 - mouseCoords.x);
int32_t rebased_y = w.viewport->zoom.ApplyTo(w.height / 2 - mouseCoords.y);
2017-06-06 23:24:18 +02:00
// Compute cursor offset relative to tile.
*offset_x = w.viewport->zoom.ApplyTo(w.savedViewPos.x - (centreLoc->x + rebased_x));
*offset_y = w.viewport->zoom.ApplyTo(w.savedViewPos.y - (centreLoc->y + rebased_y));
}
void WindowViewportCentreTileAroundCursor(WindowBase& w, int32_t map_x, int32_t map_y, int32_t offset_x, int32_t offset_y)
{
2017-06-06 23:24:18 +02:00
// Get viewport coordinates centring around the tile.
2022-10-12 06:35:20 +01:00
int32_t z = TileElementHeight({ map_x, map_y });
auto centreLoc = centre_2d_coordinates({ map_x, map_y, z }, w.viewport);
2021-09-13 19:47:13 +03:00
if (!centreLoc.has_value())
{
LOG_ERROR("Invalid location.");
return;
}
2017-06-06 23:24:18 +02:00
// Get mouse position to offset against.
2022-11-06 20:49:07 +00:00
auto mouseCoords = ContextGetCursorPositionScaled();
2017-06-06 23:24:18 +02:00
// Rebase mouse position onto centre of window, and compensate for zoom level.
int32_t rebased_x = w.viewport->zoom.ApplyTo((w.width >> 1) - mouseCoords.x);
int32_t rebased_y = w.viewport->zoom.ApplyTo((w.height >> 1) - mouseCoords.y);
2016-07-22 10:47:07 +02:00
2017-06-06 23:24:18 +02:00
// Apply offset to the viewport.
w.savedViewPos = { centreLoc->x + rebased_x + w.viewport->zoom.ApplyInversedTo(offset_x),
centreLoc->y + rebased_y + w.viewport->zoom.ApplyInversedTo(offset_y) };
}
/**
* For all windows with viewports, ensure they do not have a zoom level less than the minimum.
*/
void WindowCheckAllValidZoom()
{
WindowVisitEach([](WindowBase* w) {
if (w->viewport != nullptr && w->viewport->zoom < ZoomLevel::min())
{
WindowZoomSet(*w, ZoomLevel::min(), false);
}
});
}
void WindowZoomSet(WindowBase& w, ZoomLevel zoomLevel, bool atCursor)
2014-04-26 15:50:01 +01:00
{
Viewport* v = w.viewport;
2020-02-15 17:09:41 +00:00
if (v == nullptr)
return;
zoomLevel = std::clamp(zoomLevel, ZoomLevel::min(), ZoomLevel::max());
2017-06-06 23:24:18 +02:00
if (v->zoom == zoomLevel)
return;
2017-06-06 23:24:18 +02:00
// Zooming to cursor? Remember where we're pointing at the moment.
2021-08-16 20:51:16 +01:00
int32_t saved_map_x = 0;
int32_t saved_map_y = 0;
int32_t offset_x = 0;
int32_t offset_y = 0;
2024-05-10 12:10:21 +02:00
if (Config::Get().general.ZoomToCursor && atCursor)
2018-06-22 22:59:46 +02:00
{
WindowViewportGetMapCoordsByCursor(w, &saved_map_x, &saved_map_y, &offset_x, &offset_y);
2017-06-06 23:24:18 +02:00
}
2017-06-06 23:24:18 +02:00
// Zoom in
2018-06-22 22:59:46 +02:00
while (v->zoom > zoomLevel)
{
2017-06-06 23:24:18 +02:00
v->zoom--;
w.savedViewPos.x += v->ViewWidth() / 2;
w.savedViewPos.y += v->ViewHeight() / 2;
2017-06-06 23:24:18 +02:00
}
2017-06-06 23:24:18 +02:00
// Zoom out
2018-06-22 22:59:46 +02:00
while (v->zoom < zoomLevel)
{
2017-06-06 23:24:18 +02:00
v->zoom++;
w.savedViewPos.x -= v->ViewWidth() / 4;
w.savedViewPos.y -= v->ViewHeight() / 4;
2017-06-06 23:24:18 +02:00
}
2017-06-06 23:24:18 +02:00
// Zooming to cursor? Centre around the tile we were hovering over just now.
2024-05-10 12:10:21 +02:00
if (Config::Get().general.ZoomToCursor && atCursor)
2018-06-22 22:59:46 +02:00
{
WindowViewportCentreTileAroundCursor(w, saved_map_x, saved_map_y, offset_x, offset_y);
2017-06-06 23:24:18 +02:00
}
2017-06-06 23:24:18 +02:00
// HACK: Prevents the redraw from failing when there is
// a window on top of the viewport.
WindowBringToFront(w);
w.Invalidate();
2014-04-26 15:50:01 +01:00
}
2014-04-08 17:52:39 +01:00
/**
2020-08-22 13:00:15 +01:00
* Splits a drawing of a window into regions that can be seen and are not hidden
* by other opaque overlapping windows.
2014-04-08 17:52:39 +01:00
*/
void WindowDraw(DrawPixelInfo& dpi, WindowBase& w, int32_t left, int32_t top, int32_t right, int32_t bottom)
2014-04-08 17:52:39 +01:00
{
if (!WindowIsVisible(w))
2018-06-22 22:59:46 +02:00
return;
2017-06-06 23:24:18 +02:00
// Divide the draws up for only the visible regions of the window recursively
auto itPos = WindowGetIterator(&w);
for (auto it = std::next(itPos); it != g_window_list.end(); it++)
2018-06-16 02:14:59 +01:00
{
2017-06-06 23:24:18 +02:00
// Check if this window overlaps w
auto topwindow = it->get();
2020-03-01 16:32:35 -03:00
if (topwindow->windowPos.x >= right || topwindow->windowPos.y >= bottom)
2017-06-06 23:24:18 +02:00
continue;
2020-03-01 16:32:35 -03:00
if (topwindow->windowPos.x + topwindow->width <= left || topwindow->windowPos.y + topwindow->height <= top)
2017-06-06 23:24:18 +02:00
continue;
if (topwindow->flags & WF_TRANSPARENT)
continue;
2014-04-08 19:49:04 +01:00
2017-06-06 23:24:18 +02:00
// A window overlaps w, split up the draw into two regions where the window starts to overlap
2020-03-01 16:32:35 -03:00
if (topwindow->windowPos.x > left)
2018-06-22 22:59:46 +02:00
{
2017-06-06 23:24:18 +02:00
// Split draw at topwindow.left
2023-01-16 21:14:50 +01:00
WindowDrawCore(dpi, w, left, top, topwindow->windowPos.x, bottom);
WindowDrawCore(dpi, w, topwindow->windowPos.x, top, right, bottom);
2018-06-22 22:59:46 +02:00
}
2020-03-01 16:32:35 -03:00
else if (topwindow->windowPos.x + topwindow->width < right)
2018-06-22 22:59:46 +02:00
{
2017-06-06 23:24:18 +02:00
// Split draw at topwindow.right
2023-01-16 21:14:50 +01:00
WindowDrawCore(dpi, w, left, top, topwindow->windowPos.x + topwindow->width, bottom);
WindowDrawCore(dpi, w, topwindow->windowPos.x + topwindow->width, top, right, bottom);
2018-06-22 22:59:46 +02:00
}
2020-03-01 16:32:35 -03:00
else if (topwindow->windowPos.y > top)
2018-06-22 22:59:46 +02:00
{
2017-06-06 23:24:18 +02:00
// Split draw at topwindow.top
2023-01-16 21:14:50 +01:00
WindowDrawCore(dpi, w, left, top, right, topwindow->windowPos.y);
WindowDrawCore(dpi, w, left, topwindow->windowPos.y, right, bottom);
2018-06-22 22:59:46 +02:00
}
2020-03-01 16:32:35 -03:00
else if (topwindow->windowPos.y + topwindow->height < bottom)
2018-06-22 22:59:46 +02:00
{
2017-06-06 23:24:18 +02:00
// Split draw at topwindow.bottom
2023-01-16 21:14:50 +01:00
WindowDrawCore(dpi, w, left, top, right, topwindow->windowPos.y + topwindow->height);
WindowDrawCore(dpi, w, left, topwindow->windowPos.y + topwindow->height, right, bottom);
2017-06-06 23:24:18 +02:00
}
2014-04-08 19:49:04 +01:00
2017-06-06 23:24:18 +02:00
// Drawing for this region should be done now, exit
2020-08-22 13:00:15 +01:00
return;
2017-06-06 23:24:18 +02:00
}
2014-04-08 19:49:04 +01:00
2017-06-06 23:24:18 +02:00
// No windows overlap
2023-01-16 21:14:50 +01:00
WindowDrawCore(dpi, w, left, top, right, bottom);
2020-08-22 13:00:15 +01:00
}
/**
* Draws the given window and any other overlapping transparent windows.
*/
static void WindowDrawCore(DrawPixelInfo& dpi, WindowBase& w, int32_t left, int32_t top, int32_t right, int32_t bottom)
2020-08-22 13:00:15 +01:00
{
// Clamp region
left = std::max<int32_t>(left, w.windowPos.x);
top = std::max<int32_t>(top, w.windowPos.y);
right = std::min<int32_t>(right, w.windowPos.x + w.width);
bottom = std::min<int32_t>(bottom, w.windowPos.y + w.height);
2020-08-22 13:00:15 +01:00
if (left >= right)
return;
if (top >= bottom)
return;
// Draw the window and any other overlapping transparent windows
for (auto it = WindowGetIterator(&w); it != g_window_list.end(); it++)
2020-08-22 13:00:15 +01:00
{
auto* v = (*it).get();
2023-06-27 03:50:29 +03:00
if (v->flags & WF_DEAD)
continue;
if ((&w == v || (v->flags & WF_TRANSPARENT)) && WindowIsVisible(*v))
2020-08-22 13:00:15 +01:00
{
WindowDrawSingle(dpi, *v, left, top, right, bottom);
2020-08-22 13:00:15 +01:00
}
}
2014-04-09 14:43:27 +01:00
}
static void WindowDrawSingle(DrawPixelInfo& dpi, WindowBase& w, int32_t left, int32_t top, int32_t right, int32_t bottom)
2016-06-05 15:11:12 +01:00
{
assert(dpi.zoom_level == ZoomLevel{ 0 });
2017-06-06 23:24:18 +02:00
// Copy dpi so we can crop it
DrawPixelInfo copy = dpi;
2016-06-05 15:11:12 +01:00
2017-06-06 23:24:18 +02:00
// Clamp left to 0
2024-09-27 01:46:46 +10:00
int32_t overflow = left - copy.x;
2018-06-22 22:59:46 +02:00
if (overflow > 0)
{
2024-09-27 01:46:46 +10:00
copy.x += overflow;
copy.width -= overflow;
if (copy.width <= 0)
2017-06-06 23:24:18 +02:00
return;
copy.pitch += overflow;
copy.bits += overflow;
2017-06-06 23:24:18 +02:00
}
2016-06-05 15:11:12 +01:00
2017-06-06 23:24:18 +02:00
// Clamp width to right
2024-09-27 01:46:46 +10:00
overflow = copy.x + copy.width - right;
2018-06-22 22:59:46 +02:00
if (overflow > 0)
{
2024-09-27 01:46:46 +10:00
copy.width -= overflow;
if (copy.width <= 0)
2017-06-06 23:24:18 +02:00
return;
copy.pitch += overflow;
2017-06-06 23:24:18 +02:00
}
2016-06-05 15:11:12 +01:00
2017-06-06 23:24:18 +02:00
// Clamp top to 0
2024-09-27 01:46:46 +10:00
overflow = top - copy.y;
2018-06-22 22:59:46 +02:00
if (overflow > 0)
{
2024-09-27 01:46:46 +10:00
copy.y += overflow;
copy.height -= overflow;
if (copy.height <= 0)
2017-06-06 23:24:18 +02:00
return;
copy.bits += copy.LineStride() * overflow;
2017-06-06 23:24:18 +02:00
}
2016-06-05 15:11:12 +01:00
2017-06-06 23:24:18 +02:00
// Clamp height to bottom
2024-09-27 01:46:46 +10:00
overflow = copy.y + copy.height - bottom;
2018-06-22 22:59:46 +02:00
if (overflow > 0)
{
2024-09-27 01:46:46 +10:00
copy.height -= overflow;
if (copy.height <= 0)
2017-06-06 23:24:18 +02:00
return;
}
2016-06-05 15:11:12 +01:00
2017-06-06 23:24:18 +02:00
// Invalidate modifies the window colours so first get the correct
// colour before setting the global variables for the string painting
w.OnPrepareDraw();
2016-06-05 15:11:12 +01:00
2017-06-06 23:24:18 +02:00
// Text colouring
2024-05-09 02:45:07 +02:00
gCurrentWindowColours[0] = w.colours[0].colour;
gCurrentWindowColours[1] = w.colours[1].colour;
gCurrentWindowColours[2] = w.colours[2].colour;
2016-06-05 15:11:12 +01:00
w.OnDraw(copy);
2016-06-05 15:11:12 +01:00
}
bool isToolActive(WindowClass cls)
{
return InputTestFlag(INPUT_FLAG_TOOL_ACTIVE) && gCurrentToolWidget.window_classification == cls;
}
bool isToolActive(WindowClass cls, rct_windownumber number)
{
return isToolActive(cls) && gCurrentToolWidget.window_number == number;
}
bool isToolActive(WindowClass cls, WidgetIndex widgetIndex)
{
return isToolActive(cls) && gCurrentToolWidget.widget_index == widgetIndex;
}
bool isToolActive(WindowClass cls, WidgetIndex widgetIndex, rct_windownumber number)
{
return isToolActive(cls, widgetIndex) && gCurrentToolWidget.window_number == number;
}
bool isToolActive(const WindowBase& w, WidgetIndex widgetIndex)
{
return isToolActive(w.classification, widgetIndex, w.number);
}
/**
2015-09-22 19:18:57 +02:00
*
* rct2: 0x006EE212
*
* @param tool (al)
* @param widgetIndex (dx)
* @param w (esi)
*/
bool ToolSet(const WindowBase& w, WidgetIndex widgetIndex, Tool tool)
{
if (InputTestFlag(INPUT_FLAG_TOOL_ACTIVE))
2018-06-22 22:59:46 +02:00
{
2022-07-28 03:22:22 +02:00
if (w.classification == gCurrentToolWidget.window_classification && w.number == gCurrentToolWidget.window_number
2018-06-22 22:59:46 +02:00
&& widgetIndex == gCurrentToolWidget.widget_index)
{
ToolCancel();
2017-06-06 23:24:18 +02:00
return true;
2018-06-22 22:59:46 +02:00
}
2021-09-15 22:22:15 +02:00
ToolCancel();
2017-06-06 23:24:18 +02:00
}
InputSetFlag(INPUT_FLAG_TOOL_ACTIVE, true);
InputSetFlag(INPUT_FLAG_4, false);
InputSetFlag(INPUT_FLAG_6, false);
2017-06-06 23:24:18 +02:00
gCurrentToolId = tool;
2022-07-28 03:22:22 +02:00
gCurrentToolWidget.window_classification = w.classification;
gCurrentToolWidget.window_number = w.number;
2017-06-06 23:24:18 +02:00
gCurrentToolWidget.widget_index = widgetIndex;
return false;
}
/**
2015-09-22 19:18:57 +02:00
*
* rct2: 0x006EE281
*/
void ToolCancel()
{
if (InputTestFlag(INPUT_FLAG_TOOL_ACTIVE))
2018-06-22 22:59:46 +02:00
{
InputSetFlag(INPUT_FLAG_TOOL_ACTIVE, false);
2022-10-12 06:35:20 +01:00
MapInvalidateSelectionRect();
MapInvalidateMapSelectionTiles();
2017-06-06 23:24:18 +02:00
// Reset map selection
gMapSelectFlags = 0;
2018-06-22 22:59:46 +02:00
if (gCurrentToolWidget.widget_index != -1)
{
2017-06-06 23:24:18 +02:00
// Invalidate tool widget
WidgetInvalidateByNumber(
2018-06-22 22:59:46 +02:00
gCurrentToolWidget.window_classification, gCurrentToolWidget.window_number, gCurrentToolWidget.widget_index);
2017-06-06 23:24:18 +02:00
// Abort tool event
auto* windowMgr = GetContext()->GetUiContext()->GetWindowManager();
WindowBase* w = windowMgr->FindByNumber(gCurrentToolWidget.window_classification, gCurrentToolWidget.window_number);
2018-01-07 21:43:07 +01:00
if (w != nullptr)
w->OnToolAbort(gCurrentToolWidget.widget_index);
2017-06-06 23:24:18 +02:00
}
}
2014-05-06 22:04:09 +01:00
}
2014-07-29 19:40:06 +01:00
/**
2018-06-22 22:59:46 +02:00
* rct2: 0x0066B905
*/
void WindowResizeGui(int32_t width, int32_t height)
2014-07-29 19:40:06 +01:00
{
WindowResizeGuiScenarioEditor(width, height);
2019-03-20 20:58:08 +01:00
if (gScreenFlags & SCREEN_FLAGS_EDITOR)
2017-06-06 23:24:18 +02:00
return;
2014-07-29 19:40:06 +01:00
auto* windowMgr = GetContext()->GetUiContext()->GetWindowManager();
WindowBase* titleWind = windowMgr->FindByClass(WindowClass::TitleMenu);
2018-06-22 22:59:46 +02:00
if (titleWind != nullptr)
{
2020-03-01 16:32:35 -03:00
titleWind->windowPos.x = (width - titleWind->width) / 2;
titleWind->windowPos.y = height - 182;
2017-06-06 23:24:18 +02:00
}
2014-07-29 19:40:06 +01:00
WindowBase* versionWind = windowMgr->FindByClass(WindowClass::TitleVersion);
if (versionWind != nullptr)
versionWind->windowPos.y = height - 30;
WindowBase* exitWind = windowMgr->FindByClass(WindowClass::TitleExit);
2018-06-22 22:59:46 +02:00
if (exitWind != nullptr)
{
2020-03-01 16:32:35 -03:00
exitWind->windowPos.x = width - 40;
exitWind->windowPos.y = height - 64;
2017-06-06 23:24:18 +02:00
}
WindowBase* optionsWind = windowMgr->FindByClass(WindowClass::TitleOptions);
2018-06-22 22:59:46 +02:00
if (optionsWind != nullptr)
{
2020-03-01 16:32:35 -03:00
optionsWind->windowPos.x = width - 80;
2017-06-06 23:24:18 +02:00
}
// Keep options window centred after a resize
WindowBase* optionsWindow = windowMgr->FindByClass(WindowClass::Options);
if (optionsWindow != nullptr)
{
optionsWindow->windowPos.x = (ContextGetWidth() - optionsWindow->width) / 2;
optionsWindow->windowPos.y = (ContextGetHeight() - optionsWindow->height) / 2;
}
// Keep progress bar window centred after a resize
WindowBase* ProgressWindow = windowMgr->FindByClass(WindowClass::ProgressWindow);
if (ProgressWindow != nullptr)
{
ProgressWindow->windowPos.x = (ContextGetWidth() - ProgressWindow->width) / 2;
ProgressWindow->windowPos.y = (ContextGetHeight() - ProgressWindow->height) / 2;
}
GfxInvalidateScreen();
}
/**
2018-06-22 22:59:46 +02:00
* rct2: 0x0066F0DD
*/
void WindowResizeGuiScenarioEditor(int32_t width, int32_t height)
{
auto* mainWind = WindowGetMain();
2018-06-22 22:59:46 +02:00
if (mainWind != nullptr)
{
Viewport* viewport = mainWind->viewport;
2017-06-06 23:24:18 +02:00
mainWind->width = width;
mainWind->height = height;
viewport->width = width;
viewport->height = height;
if (mainWind->widgets != nullptr && mainWind->widgets[WC_MAIN_WINDOW__0].type == WindowWidgetType::Viewport)
2018-06-22 22:59:46 +02:00
{
2017-06-06 23:24:18 +02:00
mainWind->widgets[WC_MAIN_WINDOW__0].right = width;
mainWind->widgets[WC_MAIN_WINDOW__0].bottom = height;
}
}
auto* windowMgr = GetContext()->GetUiContext()->GetWindowManager();
WindowBase* topWind = windowMgr->FindByClass(WindowClass::TopToolbar);
2018-06-22 22:59:46 +02:00
if (topWind != nullptr)
{
2018-01-06 00:05:16 +00:00
topWind->width = std::max(640, width);
2017-06-06 23:24:18 +02:00
}
WindowBase* bottomWind = windowMgr->FindByClass(WindowClass::BottomToolbar);
2018-06-22 22:59:46 +02:00
if (bottomWind != nullptr)
{
2020-03-01 16:32:35 -03:00
bottomWind->windowPos.y = height - 32;
2018-01-06 00:05:16 +00:00
bottomWind->width = std::max(640, width);
2017-06-06 23:24:18 +02:00
}
}
2014-08-24 18:45:47 +01:00
2014-09-27 00:41:24 +01:00
/**
*
* rct2: 0x006CBCC3
*/
void WindowCloseConstructionWindows()
2014-09-27 00:41:24 +01:00
{
WindowCloseByClass(WindowClass::RideConstruction);
WindowCloseByClass(WindowClass::Footpath);
WindowCloseByClass(WindowClass::TrackDesignList);
WindowCloseByClass(WindowClass::TrackDesignPlace);
}
2014-11-02 14:03:28 +00:00
/**
* Update zoom based volume attenuation for ride music and clear music list.
* rct2: 0x006BC348
*/
void WindowUpdateViewportRideMusic()
2014-11-02 14:03:28 +00:00
{
2024-03-01 20:26:20 +00:00
RideAudio::ClearAllViewportInstances();
2018-01-07 21:43:07 +01:00
g_music_tracking_viewport = nullptr;
2014-11-02 14:03:28 +00:00
2018-06-16 02:14:59 +01:00
for (auto it = g_window_list.rbegin(); it != g_window_list.rend(); it++)
{
auto w = it->get();
auto viewport = w->viewport;
2018-01-07 21:43:07 +01:00
if (viewport == nullptr || !(viewport->flags & VIEWPORT_FLAG_SOUND_ON))
2017-06-06 23:24:18 +02:00
continue;
2014-11-02 14:03:28 +00:00
2017-06-06 23:24:18 +02:00
g_music_tracking_viewport = viewport;
gWindowAudioExclusive = w;
2014-11-02 14:03:28 +00:00
2021-09-29 20:04:56 +02:00
if (viewport->zoom <= ZoomLevel{ 0 })
2024-03-01 20:26:20 +00:00
Audio::gVolumeAdjustZoom = 0;
2021-09-29 20:04:56 +02:00
else if (viewport->zoom == ZoomLevel{ 1 })
2024-03-01 20:26:20 +00:00
Audio::gVolumeAdjustZoom = 30;
else
2024-03-01 20:26:20 +00:00
Audio::gVolumeAdjustZoom = 60;
2017-06-06 23:24:18 +02:00
break;
}
2014-11-09 17:18:51 +00:00
}
/**
*
* rct2: 0x006EE3C3
*/
void TextinputCancel()
{
WindowCloseByClass(WindowClass::Textinput);
2015-06-06 17:04:53 -04:00
}
bool WindowIsVisible(WindowBase& w)
2016-07-05 12:54:28 -04:00
{
2017-06-06 23:24:18 +02:00
// w->visibility is used to prevent repeat calculations within an iteration by caching the result
if (w.visibility == VisibilityCache::Visible)
2018-06-22 22:59:46 +02:00
return true;
if (w.visibility == VisibilityCache::Covered)
2018-06-22 22:59:46 +02:00
return false;
2017-06-06 23:24:18 +02:00
// only consider viewports, consider the main window always visible
if (w.viewport == nullptr || w.classification == WindowClass::MainWindow)
2017-06-06 23:24:18 +02:00
{
// default to previous behaviour
w.visibility = VisibilityCache::Visible;
2017-06-06 23:24:18 +02:00
return true;
}
2016-07-05 12:54:28 -04:00
2017-06-06 23:24:18 +02:00
// start from the window above the current
auto itPos = WindowGetIterator(&w);
for (auto it = std::next(itPos); it != g_window_list.end(); it++)
2017-06-06 23:24:18 +02:00
{
auto& w_other = *(*it);
2023-06-27 03:50:29 +03:00
if (w_other.flags & WF_DEAD)
continue;
2018-06-16 02:14:59 +01:00
2017-06-06 23:24:18 +02:00
// if covered by a higher window, no rendering needed
if (w_other.windowPos.x <= w.windowPos.x && w_other.windowPos.y <= w.windowPos.y
&& w_other.windowPos.x + w_other.width >= w.windowPos.x + w.width
&& w_other.windowPos.y + w_other.height >= w.windowPos.y + w.height)
2017-06-06 23:24:18 +02:00
{
w.visibility = VisibilityCache::Covered;
w.viewport->visibility = VisibilityCache::Covered;
2017-06-06 23:24:18 +02:00
return false;
}
}
2016-07-05 12:54:28 -04:00
2017-06-06 23:24:18 +02:00
// default to previous behaviour
w.visibility = VisibilityCache::Visible;
w.viewport->visibility = VisibilityCache::Visible;
2017-06-06 23:24:18 +02:00
return true;
2016-07-05 12:54:28 -04:00
}
2017-12-20 02:14:29 +01:00
/**
*
* rct2: 0x006E7499
* left (ax)
* top (bx)
* right (dx)
* bottom (bp)
*/
void WindowDrawAll(DrawPixelInfo& dpi, int32_t left, int32_t top, int32_t right, int32_t bottom)
2017-12-20 02:14:29 +01:00
{
auto windowDPI = dpi.Crop({ left, top }, { right - left, bottom - top });
WindowVisitEach([&windowDPI, left, top, right, bottom](WindowBase* w) {
2018-06-22 22:59:46 +02:00
if (w->flags & WF_TRANSPARENT)
return;
2020-03-01 16:32:35 -03:00
if (right <= w->windowPos.x || bottom <= w->windowPos.y)
return;
2020-03-01 16:32:35 -03:00
if (left >= w->windowPos.x + w->width || top >= w->windowPos.y + w->height)
return;
WindowDraw(windowDPI, *w, left, top, right, bottom);
});
2017-12-20 02:14:29 +01:00
}
Viewport* WindowGetPreviousViewport(Viewport* current)
2017-12-20 02:14:29 +01:00
{
2018-01-07 21:43:07 +01:00
bool foundPrevious = (current == nullptr);
2018-06-16 02:14:59 +01:00
for (auto it = g_window_list.rbegin(); it != g_window_list.rend(); it++)
2017-12-20 02:14:29 +01:00
{
auto& w = **it;
2023-06-27 03:50:29 +03:00
if (w.flags & WF_DEAD)
continue;
2018-06-16 02:14:59 +01:00
if (w.viewport != nullptr)
2017-12-20 02:14:29 +01:00
{
2018-06-16 02:14:59 +01:00
if (foundPrevious)
{
return w.viewport;
}
if (w.viewport == current)
{
foundPrevious = true;
}
2017-12-20 02:14:29 +01:00
}
}
2018-01-07 21:43:07 +01:00
return nullptr;
2017-12-20 02:14:29 +01:00
}
2017-12-20 12:20:17 +01:00
void WindowResetVisibilities()
2017-12-20 12:20:17 +01:00
{
// reset window visibility status to unknown
WindowVisitEach([](WindowBase* w) {
w->visibility = VisibilityCache::Unknown;
if (w->viewport != nullptr)
2017-12-20 12:20:17 +01:00
{
w->viewport->visibility = VisibilityCache::Unknown;
2017-12-20 12:20:17 +01:00
}
});
2017-12-20 12:20:17 +01:00
}
void WindowInitAll()
2017-12-20 12:20:17 +01:00
{
WindowCloseAllExceptFlags(0);
2017-12-20 12:20:17 +01:00
}
2017-11-02 20:16:30 -04:00
void WindowFollowSprite(WindowBase& w, EntityId spriteIndex)
2017-11-02 20:16:30 -04:00
{
2022-02-12 23:31:06 +02:00
if (spriteIndex.ToUnderlying() < MAX_ENTITIES || spriteIndex.IsNull())
2017-11-02 20:16:30 -04:00
{
w.viewport_smart_follow_sprite = spriteIndex;
2017-11-02 20:16:30 -04:00
}
}
void WindowUnfollowSprite(WindowBase& w)
2017-11-02 20:16:30 -04:00
{
w.viewport_smart_follow_sprite = EntityId::GetNull();
w.viewport_target_sprite = EntityId::GetNull();
2017-11-02 20:16:30 -04:00
}
2018-01-06 00:05:16 +00:00
Viewport* WindowGetViewport(WindowBase* w)
2018-01-05 23:31:56 +01:00
{
if (w == nullptr)
{
return nullptr;
}
return w->viewport;
}