1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-22 23:33:04 +01:00

Refactor guest list window to a class

This commit is contained in:
Ted John
2021-01-10 21:50:04 +00:00
parent b1e5a11bf3
commit 3e75f23970
7 changed files with 996 additions and 1046 deletions

View File

@@ -31,7 +31,6 @@ public:
void Init() override
{
ThemeManagerInitialise();
window_guest_list_init_vars();
window_new_ride_init_vars();
}

View File

@@ -86,10 +86,126 @@ static bool WindowFitsOnScreen(const ScreenCoordsXY& loc, int32_t width, int32_t
return WindowFitsBetweenOthers(loc, width, height);
}
rct_window* WindowCreate(
std::unique_ptr<rct_window>&& wp, rct_windowclass cls, const ScreenCoordsXY& pos, int32_t width, int32_t height,
uint16_t flags)
static ScreenCoordsXY ClampWindowToScreen(const ScreenCoordsXY& pos, const int32_t screenWidth, const int32_t width)
{
auto screenPos = pos;
if (screenPos.x < 0)
screenPos.x = 0;
if (screenPos.x + width > screenWidth)
screenPos.x = screenWidth - width;
return screenPos;
}
static ScreenCoordsXY GetAutoPositionForNewWindow(int32_t width, int32_t height)
{
auto uiContext = GetContext()->GetUiContext();
auto screenWidth = uiContext->GetWidth();
auto screenHeight = uiContext->GetHeight();
// Place window in an empty corner of the screen
const ScreenCoordsXY cornerPositions[] = {
{ 0, 30 }, // topLeft
{ screenWidth - width, 30 }, // topRight
{ 0, screenHeight - 34 - height }, // bottomLeft
{ screenWidth - width, screenHeight - 34 - height } // bottomRight
};
for (const auto& cornerPos : cornerPositions)
{
if (WindowFitsWithinSpace(cornerPos, width, height))
{
return ClampWindowToScreen(cornerPos, screenWidth, width);
}
}
// Place window next to another
for (auto& w : g_window_list)
{
if (w->flags & WF_STICK_TO_BACK)
continue;
const ScreenCoordsXY offsets[] = { { w->width + 2, 0 },
{ -w->width - 2, 0 },
{ 0, w->height + 2 },
{ 0, -w->height - 2 },
{ w->width + 2, -w->height - 2 },
{ -w->width - 2, -w->height - 2 },
{ w->width + 2, w->height + 2 },
{ -w->width - 2, w->height + 2 } };
for (const auto& offset : offsets)
{
auto screenPos = w->windowPos + offset;
if (WindowFitsWithinSpace(screenPos, width, height))
{
return ClampWindowToScreen(screenPos, screenWidth, width);
}
}
}
// Overlap
for (auto& w : g_window_list)
{
if (w->flags & WF_STICK_TO_BACK)
continue;
// clang-format off
const ScreenCoordsXY offsets[] = {
{ w->width + 2, 0 },
{ -w->width - 2, 0 },
{ 0, w->height + 2 },
{ 0, -w->height - 2 }
};
// clang-format on
for (const auto& offset : offsets)
{
auto screenPos = w->windowPos + offset;
if (WindowFitsOnScreen(screenPos, width, height))
{
return ClampWindowToScreen(screenPos, screenWidth, width);
}
}
}
// Cascade
auto screenPos = ScreenCoordsXY{ 0, 30 };
for (auto& w : g_window_list)
{
if (screenPos == w->windowPos)
{
screenPos.x += 5;
screenPos.y += 5;
}
}
return ClampWindowToScreen(screenPos, screenWidth, width);
}
static ScreenCoordsXY GetCentrePositionForNewWindow(int32_t width, int32_t height)
{
auto uiContext = GetContext()->GetUiContext();
auto screenWidth = uiContext->GetWidth();
auto screenHeight = uiContext->GetHeight();
return ScreenCoordsXY{ (screenWidth - width) / 2, std::max(TOP_TOOLBAR_HEIGHT + 1, (screenHeight - height) / 2) };
}
rct_window* WindowCreate(
std::unique_ptr<rct_window>&& wp, rct_windowclass cls, ScreenCoordsXY pos, int32_t width, int32_t height, uint32_t flags)
{
if (flags & WF_AUTO_POSITION)
{
if (flags & WF_CENTRE_SCREEN)
{
pos = GetCentrePositionForNewWindow(width, height);
}
else
{
pos = GetAutoPositionForNewWindow(width, height);
}
}
// Check if there are any window slots left
// include WINDOW_LIMIT_RESERVED for items such as the main viewport and toolbars to not appear to be counted.
if (g_window_list.size() >= static_cast<size_t>(gConfigGeneral.window_limit + WINDOW_LIMIT_RESERVED))
@@ -168,116 +284,25 @@ rct_window* WindowCreate(
rct_window* WindowCreate(
const ScreenCoordsXY& pos, int32_t width, int32_t height, rct_window_event_list* event_handlers, rct_windowclass cls,
uint16_t flags)
uint32_t flags)
{
auto w = std::make_unique<rct_window>();
w->event_handlers = event_handlers;
return WindowCreate(std::move(w), cls, pos, width, height, flags);
}
static ScreenCoordsXY ClampWindowToScreen(const ScreenCoordsXY& pos, const int32_t screenWidth, const int32_t width)
{
auto screenPos = pos;
if (screenPos.x < 0)
screenPos.x = 0;
if (screenPos.x + width > screenWidth)
screenPos.x = screenWidth - width;
return screenPos;
}
rct_window* WindowCreateAutoPos(
int32_t width, int32_t height, rct_window_event_list* event_handlers, rct_windowclass cls, uint16_t flags)
int32_t width, int32_t height, rct_window_event_list* event_handlers, rct_windowclass cls, uint32_t flags)
{
auto uiContext = GetContext()->GetUiContext();
auto screenWidth = uiContext->GetWidth();
auto screenHeight = uiContext->GetHeight();
// Place window in an empty corner of the screen
const ScreenCoordsXY cornerPositions[] = {
{ 0, 30 }, // topLeft
{ screenWidth - width, 30 }, // topRight
{ 0, screenHeight - 34 - height }, // bottomLeft
{ screenWidth - width, screenHeight - 34 - height } // bottomRight
};
for (const auto& cornerPos : cornerPositions)
{
if (WindowFitsWithinSpace(cornerPos, width, height))
return WindowCreate(ClampWindowToScreen(cornerPos, screenWidth, width), width, height, event_handlers, cls, flags);
}
// Place window next to another
for (auto& w : g_window_list)
{
if (w->flags & WF_STICK_TO_BACK)
continue;
const ScreenCoordsXY offsets[] = { { w->width + 2, 0 },
{ -w->width - 2, 0 },
{ 0, w->height + 2 },
{ 0, -w->height - 2 },
{ w->width + 2, -w->height - 2 },
{ -w->width - 2, -w->height - 2 },
{ w->width + 2, w->height + 2 },
{ -w->width - 2, w->height + 2 } };
for (const auto& offset : offsets)
{
auto screenPos = w->windowPos + offset;
if (WindowFitsWithinSpace(screenPos, width, height))
return WindowCreate(
ClampWindowToScreen(screenPos, screenWidth, width), width, height, event_handlers, cls, flags);
}
}
// Overlap
for (auto& w : g_window_list)
{
if (w->flags & WF_STICK_TO_BACK)
continue;
// clang-format off
const ScreenCoordsXY offsets[] = {
{ w->width + 2, 0 },
{ -w->width - 2, 0 },
{ 0, w->height + 2 },
{ 0, -w->height - 2 }
};
// clang-format on
for (const auto& offset : offsets)
{
auto screenPos = w->windowPos + offset;
if (WindowFitsOnScreen(screenPos, width, height))
return WindowCreate(
ClampWindowToScreen(screenPos, screenWidth, width), width, height, event_handlers, cls, flags);
}
}
// Cascade
auto screenPos = ScreenCoordsXY{ 0, 30 };
for (auto& w : g_window_list)
{
if (screenPos == w->windowPos)
{
screenPos.x += 5;
screenPos.y += 5;
}
}
return WindowCreate(ClampWindowToScreen(screenPos, screenWidth, width), width, height, event_handlers, cls, flags);
auto pos = GetAutoPositionForNewWindow(width, height);
return WindowCreate(pos, width, height, event_handlers, cls, flags);
}
rct_window* WindowCreateCentred(
int32_t width, int32_t height, rct_window_event_list* event_handlers, rct_windowclass cls, uint16_t flags)
int32_t width, int32_t height, rct_window_event_list* event_handlers, rct_windowclass cls, uint32_t flags)
{
auto uiContext = GetContext()->GetUiContext();
auto screenWidth = uiContext->GetWidth();
auto screenHeight = uiContext->GetHeight();
auto screenPos = ScreenCoordsXY{ (screenWidth - width) / 2, std::max(TOP_TOOLBAR_HEIGHT + 1, (screenHeight - height) / 2) };
return WindowCreate(screenPos, width, height, event_handlers, cls, flags);
auto pos = GetCentrePositionForNewWindow(width, height);
return WindowCreate(pos, width, height, event_handlers, cls, flags);
}
static int32_t WindowGetWidgetIndex(rct_window* w, rct_widget* widget)

File diff suppressed because it is too large Load Diff

View File

@@ -98,7 +98,6 @@ rct_window* window_player_open(uint8_t id);
rct_window* window_new_campaign_open(int16_t campaignType);
rct_window* window_install_track_open(const utf8* path);
void window_guest_list_init_vars();
void window_guest_list_refresh_list();
rct_window* window_guest_list_open();
rct_window* window_guest_list_open_with_filter(GuestListFilterType type, int32_t index);

View File

@@ -1419,9 +1419,16 @@ void window_event_mouse_down_call(rct_window* w, rct_widgetindex widgetIndex)
void window_event_dropdown_call(rct_window* w, rct_widgetindex widgetIndex, int32_t dropdownIndex)
{
if (w->event_handlers == nullptr)
{
if (dropdownIndex != -1)
{
w->OnDropdown(widgetIndex, dropdownIndex);
}
}
else if (w->event_handlers->dropdown != nullptr)
{
w->event_handlers->dropdown(w, widgetIndex, dropdownIndex);
}
}
void window_event_unknown_05_call(rct_window* w)
@@ -1497,15 +1504,25 @@ void window_event_unknown_0E_call(rct_window* w)
void window_get_scroll_size(rct_window* w, int32_t scrollIndex, int32_t* width, int32_t* height)
{
if (w->event_handlers != nullptr)
if (w->event_handlers->get_scroll_size != nullptr)
if (w->event_handlers == nullptr)
{
auto size = w->OnScrollGetSize(scrollIndex);
if (width != nullptr)
*width = size.width;
if (height != nullptr)
*height = size.height;
}
else if (w->event_handlers->get_scroll_size != nullptr)
{
w->event_handlers->get_scroll_size(w, scrollIndex, width, height);
}
}
void window_event_scroll_mousedown_call(rct_window* w, int32_t scrollIndex, const ScreenCoordsXY& screenCoords)
{
if (w->event_handlers != nullptr)
if (w->event_handlers->scroll_mousedown != nullptr)
if (w->event_handlers == nullptr)
w->OnScrollMouseDown(scrollIndex, screenCoords);
else if (w->event_handlers->scroll_mousedown != nullptr)
w->event_handlers->scroll_mousedown(w, scrollIndex, screenCoords);
}
@@ -1518,8 +1535,9 @@ void window_event_scroll_mousedrag_call(rct_window* w, int32_t scrollIndex, cons
void window_event_scroll_mouseover_call(rct_window* w, int32_t scrollIndex, const ScreenCoordsXY& screenCoords)
{
if (w->event_handlers != nullptr)
if (w->event_handlers->scroll_mouseover != nullptr)
if (w->event_handlers == nullptr)
w->OnScrollMouseOver(scrollIndex, screenCoords);
else if (w->event_handlers->scroll_mouseover != nullptr)
w->event_handlers->scroll_mouseover(w, scrollIndex, screenCoords);
}
@@ -1602,8 +1620,9 @@ void window_event_paint_call(rct_window* w, rct_drawpixelinfo* dpi)
void window_event_scroll_paint_call(rct_window* w, rct_drawpixelinfo* dpi, int32_t scrollIndex)
{
if (w->event_handlers != nullptr)
if (w->event_handlers->scroll_paint != nullptr)
if (w->event_handlers == nullptr)
w->OnScrollDraw(scrollIndex, *dpi);
else if (w->event_handlers->scroll_paint != nullptr)
w->event_handlers->scroll_paint(w, dpi, scrollIndex);
}

View File

@@ -364,7 +364,11 @@ enum WINDOW_FLAGS
WF_WHITE_BORDER_ONE = (1 << 12),
WF_WHITE_BORDER_MASK = (1 << 12) | (1 << 13),
WF_NO_SNAPPING = (1 << 15)
WF_NO_SNAPPING = (1 << 15),
// Create only flags
WF_AUTO_POSITION = (1 << 16),
WF_CENTRE_SCREEN = (1 << 17),
};
enum SCROLL_FLAGS
@@ -673,21 +677,25 @@ void window_update_all();
void window_set_window_limit(int32_t value);
rct_window* WindowCreate(
std::unique_ptr<rct_window>&& w, rct_windowclass cls, const ScreenCoordsXY& pos, int32_t width, int32_t height,
uint16_t flags);
std::unique_ptr<rct_window>&& w, rct_windowclass cls, ScreenCoordsXY pos, int32_t width, int32_t height, uint32_t flags);
template<typename T, typename std::enable_if<std::is_base_of<rct_window, T>::value>::type* = nullptr>
T* WindowCreate(rct_windowclass cls, const ScreenCoordsXY& pos, int32_t width, int32_t height, uint16_t flags = 0)
T* WindowCreate(rct_windowclass cls, const ScreenCoordsXY& pos, int32_t width, int32_t height, uint32_t flags = 0)
{
return static_cast<T*>(WindowCreate(std::make_unique<T>(), cls, pos, width, height, flags));
}
template<typename T, typename std::enable_if<std::is_base_of<rct_window, T>::value>::type* = nullptr>
T* WindowCreate(rct_windowclass cls, int32_t width, int32_t height, uint32_t flags = 0)
{
return static_cast<T*>(WindowCreate(std::make_unique<T>(), cls, {}, width, height, flags | WF_AUTO_POSITION));
}
rct_window* WindowCreate(
const ScreenCoordsXY& pos, int32_t width, int32_t height, rct_window_event_list* event_handlers, rct_windowclass cls,
uint16_t flags);
uint32_t flags);
rct_window* WindowCreateAutoPos(
int32_t width, int32_t height, rct_window_event_list* event_handlers, rct_windowclass cls, uint16_t flags);
int32_t width, int32_t height, rct_window_event_list* event_handlers, rct_windowclass cls, uint32_t flags);
rct_window* WindowCreateCentred(
int32_t width, int32_t height, rct_window_event_list* event_handlers, rct_windowclass cls, uint16_t flags);
int32_t width, int32_t height, rct_window_event_list* event_handlers, rct_windowclass cls, uint32_t flags);
void window_close(rct_window* window);
void window_close_by_class(rct_windowclass cls);

View File

@@ -119,6 +119,9 @@ struct rct_window
virtual void OnClose()
{
}
virtual void OnResize()
{
}
virtual void OnUpdate()
{
}
@@ -144,6 +147,19 @@ struct rct_window
virtual void OnTextInput(rct_widgetindex widgetIndex, std::string_view text)
{
}
virtual ScreenSize OnScrollGetSize(int32_t scrollIndex)
{
return {};
}
virtual void OnScrollMouseOver(int32_t scrollIndex, const ScreenCoordsXY& screenCoords)
{
}
virtual void OnScrollMouseDown(int32_t scrollIndex, const ScreenCoordsXY& screenCoords)
{
}
virtual void OnScrollDraw(int32_t scrollIndex, rct_drawpixelinfo& dpi)
{
}
};
#ifdef __WARN_SUGGEST_FINAL_METHODS__