Refactor window functions to take references 6

This commit is contained in:
Hielke Morsink 2022-07-31 16:55:21 +02:00
parent 755deeef8e
commit d05d675ed9
No known key found for this signature in database
GPG Key ID: FE0B343DF883E7F2
19 changed files with 100 additions and 104 deletions

View File

@ -115,7 +115,7 @@ void InputManager::HandleViewScrolling()
auto mainWindow = window_get_main();
if (mainWindow != nullptr && (_viewScroll.x != 0 || _viewScroll.y != 0))
{
window_unfollow_sprite(mainWindow);
window_unfollow_sprite(*mainWindow);
}
InputScrollViewport(_viewScroll);

View File

@ -297,7 +297,7 @@ static void GameHandleInputMouse(const ScreenCoordsXY& screenCoords, MouseState
if (w != nullptr)
{
w = window_bring_to_front(w);
w = window_bring_to_front(*w);
}
if (widgetIndex != -1)
@ -530,7 +530,7 @@ static void InputViewportDragBegin(rct_window& w)
gInputDragLast = cursorPosition;
context_hide_cursor();
window_unfollow_sprite(&w);
window_unfollow_sprite(w);
// gInputFlags |= INPUT_FLAG_5;
}
@ -1012,7 +1012,7 @@ static void InputWidgetLeft(const ScreenCoordsXY& screenCoords, rct_window* w, r
if (w == nullptr)
return;
w = window_bring_to_front(w);
w = window_bring_to_front(*w);
if (widgetIndex == -1)
return;

View File

@ -666,7 +666,7 @@ namespace OpenRCT2::Ui::Windows
else if (widgetDesc->Type == "textbox")
{
auto* text = const_cast<char*>(widgetDesc->Text.c_str());
window_start_textbox(this, widgetIndex, STR_STRING, text, widgetDesc->MaxLength + 1);
window_start_textbox(*this, widgetIndex, STR_STRING, text, widgetDesc->MaxLength + 1);
}
}
}

View File

@ -307,7 +307,7 @@ namespace OpenRCT2::Scripting
auto w = GetWindow();
if (w != nullptr)
{
window_bring_to_front(w);
window_bring_to_front(*w);
w->flags |= WF_WHITE_BORDER_MASK;
}
}

View File

@ -416,7 +416,7 @@ public:
break;
}
case WIDX_FILTER_TEXT_BOX:
window_start_textbox(this, widgetIndex, STR_STRING, _filter_string, sizeof(_filter_string));
window_start_textbox(*this, widgetIndex, STR_STRING, _filter_string, sizeof(_filter_string));
break;
case WIDX_FILTER_CLEAR_BUTTON:
std::fill_n(_filter_string, sizeof(_filter_string), 0x00);

View File

@ -697,7 +697,7 @@ private:
void GuestFollow()
{
rct_window* main = window_get_main();
window_follow_sprite(main, EntityId::FromUnderlying(number));
window_follow_sprite(*main, EntityId::FromUnderlying(number));
}
void OnViewportRotateOverview()

View File

@ -1983,7 +1983,7 @@ static void WindowRideMainFollowRide(rct_window* w)
{
auto headVehicleSpriteIndex = vehicle->sprite_index;
rct_window* w_main = window_get_main();
window_follow_sprite(w_main, headVehicleSpriteIndex);
window_follow_sprite(*w_main, headVehicleSpriteIndex);
}
}
}

View File

@ -167,7 +167,7 @@ static void WindowServerListMouseup(rct_window* w, rct_widgetindex widgetIndex)
window_close(*w);
break;
case WIDX_PLAYER_NAME_INPUT:
window_start_textbox(w, widgetIndex, STR_STRING, _playerName, 63);
window_start_textbox(*w, widgetIndex, STR_STRING, _playerName, 63);
break;
case WIDX_LIST:
{

View File

@ -97,19 +97,19 @@ public:
Close();
break;
case WIDX_PORT_INPUT:
window_start_textbox(this, widgetIndex, STR_STRING, _port, 6);
window_start_textbox(*this, widgetIndex, STR_STRING, _port, 6);
break;
case WIDX_NAME_INPUT:
window_start_textbox(this, widgetIndex, STR_STRING, _name, 64);
window_start_textbox(*this, widgetIndex, STR_STRING, _name, 64);
break;
case WIDX_DESCRIPTION_INPUT:
window_start_textbox(this, widgetIndex, STR_STRING, _description, MAX_SERVER_DESCRIPTION_LENGTH);
window_start_textbox(*this, widgetIndex, STR_STRING, _description, MAX_SERVER_DESCRIPTION_LENGTH);
break;
case WIDX_GREETING_INPUT:
window_start_textbox(this, widgetIndex, STR_STRING, _greeting, CHAT_INPUT_SIZE);
window_start_textbox(*this, widgetIndex, STR_STRING, _greeting, CHAT_INPUT_SIZE);
break;
case WIDX_PASSWORD_INPUT:
window_start_textbox(this, widgetIndex, STR_STRING, _password, 32);
window_start_textbox(*this, widgetIndex, STR_STRING, _password, 32);
break;
case WIDX_MAXPLAYERS_INCREASE:
if (gConfigNetwork.maxplayers < 255)

View File

@ -1200,7 +1200,7 @@ private:
void FollowPeep()
{
rct_window* main = window_get_main();
window_follow_sprite(main, EntityId::FromUnderlying(number));
window_follow_sprite(*main, EntityId::FromUnderlying(number));
}
void DrawTabImages(rct_drawpixelinfo* dpi)

View File

@ -133,7 +133,7 @@ static void WindowTitleMenuMouseup(rct_window* w, rct_widgetindex widgetIndex)
windowToOpen = window_find_by_class(WC_SCENARIO_SELECT);
if (windowToOpen != nullptr)
{
window_bring_to_front(windowToOpen);
window_bring_to_front(*windowToOpen);
}
else
{
@ -146,7 +146,7 @@ static void WindowTitleMenuMouseup(rct_window* w, rct_widgetindex widgetIndex)
windowToOpen = window_find_by_class(WC_LOADSAVE);
if (windowToOpen != nullptr)
{
window_bring_to_front(windowToOpen);
window_bring_to_front(*windowToOpen);
}
else
{
@ -160,7 +160,7 @@ static void WindowTitleMenuMouseup(rct_window* w, rct_widgetindex widgetIndex)
windowToOpen = window_find_by_class(WC_SERVER_LIST);
if (windowToOpen != nullptr)
{
window_bring_to_front(windowToOpen);
window_bring_to_front(*windowToOpen);
}
else
{

View File

@ -279,7 +279,7 @@ public:
}
break;
case WIDX_FILTER_STRING:
window_start_textbox(this, widgetIndex, STR_STRING, _filterString, sizeof(_filterString)); // TODO check this
window_start_textbox(*this, widgetIndex, STR_STRING, _filterString, sizeof(_filterString)); // TODO check this
// out
break;
case WIDX_FILTER_CLEAR:

View File

@ -468,7 +468,7 @@ void game_load_init()
else
{
auto* mainWindow = window_get_main();
window_unfollow_sprite(mainWindow);
window_unfollow_sprite(*mainWindow);
}
auto windowManager = GetContext()->GetUiContext()->GetWindowManager();

View File

@ -1919,7 +1919,7 @@ void viewport_invalidate(const rct_viewport* viewport, const ScreenRect& screenR
if (owner != nullptr && owner->classification != WC_MAIN_WINDOW)
{
// note, window_is_visible will update viewport->visibility, so this should have a low hit count
if (!window_is_visible(owner))
if (!window_is_visible(*owner))
{
return;
}

View File

@ -83,8 +83,8 @@ namespace WindowCloseFlags
static constexpr uint32_t CloseSingle = (1 << 1);
} // namespace WindowCloseFlags
static void window_draw_core(rct_drawpixelinfo* dpi, rct_window* w, int32_t left, int32_t top, int32_t right, int32_t bottom);
static void window_draw_single(rct_drawpixelinfo* dpi, rct_window* w, int32_t left, int32_t top, int32_t right, int32_t bottom);
static void window_draw_core(rct_drawpixelinfo* dpi, rct_window& w, int32_t left, int32_t top, int32_t right, int32_t bottom);
static void window_draw_single(rct_drawpixelinfo* dpi, rct_window& w, int32_t left, int32_t top, int32_t right, int32_t bottom);
std::list<std::shared_ptr<rct_window>>::iterator window_get_iterator(const rct_window* w)
{
@ -115,7 +115,7 @@ void window_dispatch_update_all()
void window_update_all_viewports()
{
window_visit_each([&](rct_window* w) {
if (w->viewport != nullptr && window_is_visible(w))
if (w->viewport != nullptr && window_is_visible(*w))
{
viewport_update_position(w);
}
@ -649,11 +649,11 @@ int32_t window_get_scroll_data_index(rct_window& w, rct_widgetindex widget_index
*
* rct2: 0x006ECDA4
*/
rct_window* window_bring_to_front(rct_window* w)
rct_window* window_bring_to_front(rct_window& w)
{
if (!(w->flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT)))
if (!(w.flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT)))
{
auto itSourcePos = window_get_iterator(w);
auto itSourcePos = window_get_iterator(&w);
if (itSourcePos != g_window_list.end())
{
// Insert in front of the first non-stick-to-front window
@ -669,31 +669,29 @@ rct_window* window_bring_to_front(rct_window* w)
}
g_window_list.splice(itDestPos, g_window_list, itSourcePos);
w->Invalidate();
w.Invalidate();
if (w->windowPos.x + w->width < 20)
if (w.windowPos.x + w.width < 20)
{
int32_t i = 20 - w->windowPos.x;
w->windowPos.x += i;
if (w->viewport != nullptr)
w->viewport->pos.x += i;
w->Invalidate();
int32_t i = 20 - w.windowPos.x;
w.windowPos.x += i;
if (w.viewport != nullptr)
w.viewport->pos.x += i;
w.Invalidate();
}
}
}
return w;
return &w;
}
rct_window* window_bring_to_front_by_class_with_flags(rct_windowclass cls, uint16_t flags)
{
rct_window* w;
w = window_find_by_class(cls);
rct_window* w = window_find_by_class(cls);
if (w != nullptr)
{
w->flags |= flags;
w->Invalidate();
w = window_bring_to_front(w);
w = window_bring_to_front(*w);
}
return w;
@ -719,7 +717,7 @@ rct_window* window_bring_to_front_by_number(rct_windowclass cls, rct_windownumbe
{
w->flags |= WF_WHITE_BORDER_MASK;
w->Invalidate();
w = window_bring_to_front(w);
w = window_bring_to_front(*w);
}
return w;
@ -820,7 +818,7 @@ rct_window* window_get_main()
*/
void window_scroll_to_location(rct_window& w, const CoordsXYZ& coords)
{
window_unfollow_sprite(&w);
window_unfollow_sprite(w);
if (w.viewport != nullptr)
{
int16_t height = tile_element_height(coords);
@ -1071,7 +1069,7 @@ void window_zoom_set(rct_window& w, ZoomLevel zoomLevel, bool atCursor)
// HACK: Prevents the redraw from failing when there is
// a window on top of the viewport.
window_bring_to_front(&w);
window_bring_to_front(w);
w.Invalidate();
}
@ -1120,7 +1118,7 @@ void main_window_zoom(bool zoomIn, bool atCursor)
*/
void window_draw(rct_drawpixelinfo* dpi, rct_window& w, int32_t left, int32_t top, int32_t right, int32_t bottom)
{
if (!window_is_visible(&w))
if (!window_is_visible(w))
return;
// Divide the draws up for only the visible regions of the window recursively
@ -1140,26 +1138,26 @@ void window_draw(rct_drawpixelinfo* dpi, rct_window& w, int32_t left, int32_t to
if (topwindow->windowPos.x > left)
{
// Split draw at topwindow.left
window_draw_core(dpi, &w, left, top, topwindow->windowPos.x, bottom);
window_draw_core(dpi, &w, topwindow->windowPos.x, top, right, bottom);
window_draw_core(dpi, w, left, top, topwindow->windowPos.x, bottom);
window_draw_core(dpi, w, topwindow->windowPos.x, top, right, bottom);
}
else if (topwindow->windowPos.x + topwindow->width < right)
{
// Split draw at topwindow.right
window_draw_core(dpi, &w, left, top, topwindow->windowPos.x + topwindow->width, bottom);
window_draw_core(dpi, &w, topwindow->windowPos.x + topwindow->width, top, right, bottom);
window_draw_core(dpi, w, left, top, topwindow->windowPos.x + topwindow->width, bottom);
window_draw_core(dpi, w, topwindow->windowPos.x + topwindow->width, top, right, bottom);
}
else if (topwindow->windowPos.y > top)
{
// Split draw at topwindow.top
window_draw_core(dpi, &w, left, top, right, topwindow->windowPos.y);
window_draw_core(dpi, &w, left, topwindow->windowPos.y, right, bottom);
window_draw_core(dpi, w, left, top, right, topwindow->windowPos.y);
window_draw_core(dpi, w, left, topwindow->windowPos.y, right, bottom);
}
else if (topwindow->windowPos.y + topwindow->height < bottom)
{
// Split draw at topwindow.bottom
window_draw_core(dpi, &w, left, top, right, topwindow->windowPos.y + topwindow->height);
window_draw_core(dpi, &w, left, topwindow->windowPos.y + topwindow->height, right, bottom);
window_draw_core(dpi, w, left, top, right, topwindow->windowPos.y + topwindow->height);
window_draw_core(dpi, w, left, topwindow->windowPos.y + topwindow->height, right, bottom);
}
// Drawing for this region should be done now, exit
@ -1167,36 +1165,36 @@ void window_draw(rct_drawpixelinfo* dpi, rct_window& w, int32_t left, int32_t to
}
// No windows overlap
window_draw_core(dpi, &w, left, top, right, bottom);
window_draw_core(dpi, w, left, top, right, bottom);
}
/**
* Draws the given window and any other overlapping transparent windows.
*/
static void window_draw_core(rct_drawpixelinfo* dpi, rct_window* w, int32_t left, int32_t top, int32_t right, int32_t bottom)
static void window_draw_core(rct_drawpixelinfo* dpi, rct_window& w, int32_t left, int32_t top, int32_t right, int32_t bottom)
{
// 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);
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);
if (left >= right)
return;
if (top >= bottom)
return;
// Draw the window and any other overlapping transparent windows
for (auto it = window_get_iterator(w); it != g_window_list.end(); it++)
for (auto it = window_get_iterator(&w); it != g_window_list.end(); it++)
{
auto v = (*it).get();
if ((w == v || (v->flags & WF_TRANSPARENT)) && window_is_visible(v))
auto* v = (*it).get();
if ((&w == v || (v->flags & WF_TRANSPARENT)) && window_is_visible(*v))
{
window_draw_single(dpi, v, left, top, right, bottom);
window_draw_single(dpi, *v, left, top, right, bottom);
}
}
}
static void window_draw_single(rct_drawpixelinfo* dpi, rct_window* w, int32_t left, int32_t top, int32_t right, int32_t bottom)
static void window_draw_single(rct_drawpixelinfo* dpi, rct_window& w, int32_t left, int32_t top, int32_t right, int32_t bottom)
{
// Copy dpi so we can crop it
rct_drawpixelinfo copy = *dpi;
@ -1246,15 +1244,15 @@ static void window_draw_single(rct_drawpixelinfo* dpi, rct_window* w, int32_t le
// Invalidate modifies the window colours so first get the correct
// colour before setting the global variables for the string painting
window_event_invalidate_call(w);
window_event_invalidate_call(&w);
// Text colouring
gCurrentWindowColours[0] = NOT_TRANSLUCENT(w->colours[0]);
gCurrentWindowColours[1] = NOT_TRANSLUCENT(w->colours[1]);
gCurrentWindowColours[2] = NOT_TRANSLUCENT(w->colours[2]);
gCurrentWindowColours[3] = NOT_TRANSLUCENT(w->colours[3]);
gCurrentWindowColours[0] = NOT_TRANSLUCENT(w.colours[0]);
gCurrentWindowColours[1] = NOT_TRANSLUCENT(w.colours[1]);
gCurrentWindowColours[2] = NOT_TRANSLUCENT(w.colours[2]);
gCurrentWindowColours[3] = NOT_TRANSLUCENT(w.colours[3]);
window_event_paint_call(w, dpi);
window_event_paint_call(&w, dpi);
}
/**
@ -1970,14 +1968,14 @@ void textinput_cancel()
}
void window_start_textbox(
rct_window* call_w, rct_widgetindex call_widget, rct_string_id existing_text, char* existing_args, int32_t maxLength)
rct_window& call_w, rct_widgetindex call_widget, rct_string_id existing_text, char* existing_args, int32_t maxLength)
{
if (gUsingWidgetTextBox)
window_cancel_textbox();
gUsingWidgetTextBox = true;
gCurrentTextBox.window.classification = call_w->classification;
gCurrentTextBox.window.number = call_w->number;
gCurrentTextBox.window.classification = call_w.classification;
gCurrentTextBox.window.number = call_w.number;
gCurrentTextBox.widget_index = call_widget;
gTextBoxFrameNo = 0;
@ -2039,45 +2037,43 @@ void window_update_textbox()
}
}
bool window_is_visible(rct_window* w)
bool window_is_visible(rct_window& w)
{
// w->visibility is used to prevent repeat calculations within an iteration by caching the result
if (w == nullptr)
return false;
if (w->visibility == VisibilityCache::Visible)
if (w.visibility == VisibilityCache::Visible)
return true;
if (w->visibility == VisibilityCache::Covered)
if (w.visibility == VisibilityCache::Covered)
return false;
// only consider viewports, consider the main window always visible
if (w->viewport == nullptr || w->classification == WC_MAIN_WINDOW)
if (w.viewport == nullptr || w.classification == WC_MAIN_WINDOW)
{
// default to previous behaviour
w->visibility = VisibilityCache::Visible;
w.visibility = VisibilityCache::Visible;
return true;
}
// start from the window above the current
auto itPos = window_get_iterator(w);
auto itPos = window_get_iterator(&w);
for (auto it = std::next(itPos); it != g_window_list.end(); it++)
{
auto& w_other = *(*it);
// 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)
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)
{
w->visibility = VisibilityCache::Covered;
w->viewport->visibility = VisibilityCache::Covered;
w.visibility = VisibilityCache::Covered;
w.viewport->visibility = VisibilityCache::Covered;
return false;
}
}
// default to previous behaviour
w->visibility = VisibilityCache::Visible;
w->viewport->visibility = VisibilityCache::Visible;
w.visibility = VisibilityCache::Visible;
w.viewport->visibility = VisibilityCache::Visible;
return true;
}
@ -2141,18 +2137,18 @@ void window_init_all()
window_close_all_except_flags(0);
}
void window_follow_sprite(rct_window* w, EntityId spriteIndex)
void window_follow_sprite(rct_window& w, EntityId spriteIndex)
{
if (spriteIndex.ToUnderlying() < MAX_ENTITIES || spriteIndex.IsNull())
{
w->viewport_smart_follow_sprite = spriteIndex;
w.viewport_smart_follow_sprite = spriteIndex;
}
}
void window_unfollow_sprite(rct_window* w)
void window_unfollow_sprite(rct_window& w)
{
w->viewport_smart_follow_sprite = EntityId::GetNull();
w->viewport_target_sprite = EntityId::GetNull();
w.viewport_smart_follow_sprite = EntityId::GetNull();
w.viewport_target_sprite = EntityId::GetNull();
}
rct_viewport* window_get_viewport(rct_window* w)
@ -2182,9 +2178,9 @@ rct_window* window_get_listening()
return nullptr;
}
rct_windowclass window_get_classification(rct_window* window)
rct_windowclass window_get_classification(rct_window& window)
{
return window->classification;
return window.classification;
}
/**

View File

@ -645,7 +645,7 @@ void window_update_all();
void window_set_window_limit(int32_t value);
rct_window* window_bring_to_front(rct_window* w);
rct_window* window_bring_to_front(rct_window& w);
rct_window* window_bring_to_front_by_class(rct_windowclass cls);
rct_window* window_bring_to_front_by_class_with_flags(rct_windowclass cls, uint16_t flags);
rct_window* window_bring_to_front_by_number(rct_windowclass cls, rct_windownumber number);
@ -799,12 +799,12 @@ void window_move_and_snap(rct_window& w, ScreenCoordsXY newWindowCoords, int32_t
int32_t window_can_resize(rct_window& w);
void window_start_textbox(
rct_window* call_w, rct_widgetindex call_widget, rct_string_id existing_text, char* existing_args, int32_t maxLength);
rct_window& call_w, rct_widgetindex call_widget, rct_string_id existing_text, char* existing_args, int32_t maxLength);
void window_cancel_textbox();
void window_update_textbox_caret();
void window_update_textbox();
bool window_is_visible(rct_window* w);
bool window_is_visible(rct_window& w);
bool scenery_tool_is_active();
@ -835,8 +835,8 @@ void window_footpath_keyboard_shortcut_slope_up();
void window_footpath_keyboard_shortcut_build_current();
void window_footpath_keyboard_shortcut_demolish_current();
void window_follow_sprite(rct_window* w, EntityId spriteIndex);
void window_unfollow_sprite(rct_window* w);
void window_follow_sprite(rct_window& w, EntityId spriteIndex);
void window_unfollow_sprite(rct_window& w);
bool window_ride_construction_update_state(
int32_t* trackType, int32_t* trackDirection, RideId* rideIndex, int32_t* _liftHillAndAlternativeState, CoordsXYZ* trackPos,
@ -848,4 +848,4 @@ money32 place_provisional_track_piece(
extern RideConstructionState _rideConstructionState2;
rct_window* window_get_listening();
rct_windowclass window_get_classification(rct_window* window);
rct_windowclass window_get_classification(rct_window& window);

View File

@ -804,7 +804,7 @@ bool Vehicle::SoundCanPlay() const
auto left = g_music_tracking_viewport->viewPos.x;
auto bottom = g_music_tracking_viewport->viewPos.y;
if (window_get_classification(gWindowAudioExclusive) == WC_MAIN_WINDOW)
if (window_get_classification(*gWindowAudioExclusive) == WC_MAIN_WINDOW)
{
left -= quarter_w;
bottom -= quarter_h;
@ -816,7 +816,7 @@ bool Vehicle::SoundCanPlay() const
auto right = g_music_tracking_viewport->view_width + left;
auto top = g_music_tracking_viewport->view_height + bottom;
if (window_get_classification(gWindowAudioExclusive) == WC_MAIN_WINDOW)
if (window_get_classification(*gWindowAudioExclusive) == WC_MAIN_WINDOW)
{
right += quarter_w + quarter_w;
top += quarter_h + quarter_h;

View File

@ -18,7 +18,7 @@ namespace OpenRCT2::Title
auto* w = window_get_main();
if (w != nullptr)
{
window_follow_sprite(w, Follow.SpriteIndex);
window_follow_sprite(*w, Follow.SpriteIndex);
}
return 0;

View File

@ -89,7 +89,7 @@ void TitleScreen::StopPreviewingSequence()
rct_window* mainWindow = window_get_main();
if (mainWindow != nullptr)
{
window_unfollow_sprite(mainWindow);
window_unfollow_sprite(*mainWindow);
}
_previewingSequence = false;
_currentSequence = title_get_config_sequence();