From f96f11395162a8d57f5fdcef18c7a64f7acf76f8 Mon Sep 17 00:00:00 2001 From: frosch Date: Sun, 9 May 2021 17:10:07 +0200 Subject: [PATCH] Codechange: use IterateFromBack/Front only if the order is important. Use Iterate if the order does not matter. --- src/misc_gui.cpp | 2 +- src/sound.cpp | 1 + src/viewport.cpp | 6 ++-- src/widgets/dropdown.cpp | 2 +- src/window.cpp | 68 ++++++++++++++++++++-------------------- src/window_gui.h | 1 + 6 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index a40bf50139..3094d0eade 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -1289,7 +1289,7 @@ void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallback { if (parent == nullptr) parent = FindWindowById(WC_MAIN_WINDOW, 0); - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue; const QueryWindow *qw = (const QueryWindow *)w; diff --git a/src/sound.cpp b/src/sound.cpp index 413c70af2a..c22e044372 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -245,6 +245,7 @@ static void SndPlayScreenCoordFx(SoundID sound, int left, int right, int top, in { if (_settings_client.music.effect_vol == 0) return; + /* Iterate from back, so that main viewport is checked first */ for (const Window *w : Window::IterateFromBack()) { const Viewport *vp = w->viewport; diff --git a/src/viewport.cpp b/src/viewport.cpp index 3e4c2d273f..4344eac2a3 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -1480,7 +1480,7 @@ void ViewportSign::MarkDirty(ZoomLevel maxzoom) const zoomlevels[zoom].bottom = this->top + ScaleByZoom(VPSM_TOP + FONT_HEIGHT_NORMAL + VPSM_BOTTOM + 1, zoom); } - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { Viewport *vp = w->viewport; if (vp != nullptr && vp->zoom <= maxzoom) { assert(vp->width != 0); @@ -1953,7 +1953,7 @@ bool MarkAllViewportsDirty(int left, int top, int right, int bottom) { bool dirty = false; - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { Viewport *vp = w->viewport; if (vp != nullptr) { assert(vp->width != 0); @@ -1966,7 +1966,7 @@ bool MarkAllViewportsDirty(int left, int top, int right, int bottom) void ConstrainAllViewportsZoom() { - for (Window *w : Window::IterateFromFront()) { + for (Window *w : Window::Iterate()) { if (w->viewport == nullptr) continue; ZoomLevel zoom = static_cast(Clamp(w->viewport->zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max)); diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp index b6f7de3c18..a4bd002593 100644 --- a/src/widgets/dropdown.cpp +++ b/src/widgets/dropdown.cpp @@ -501,7 +501,7 @@ void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int butt */ int HideDropDownMenu(Window *pw) { - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { if (w->window_class != WC_DROPDOWN_MENU) continue; DropdownWindow *dw = dynamic_cast(w); diff --git a/src/window.cpp b/src/window.cpp index 9b51eafd8a..54671f90bc 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1055,7 +1055,7 @@ void Window::SetShaded(bool make_shaded) */ static Window *FindChildWindow(const Window *w, WindowClass wc) { - for (Window *v : Window::IterateFromBack()) { + for (Window *v : Window::Iterate()) { if ((wc == WC_INVALID || wc == v->window_class) && v->parent == w) return v; } @@ -1129,7 +1129,7 @@ Window::~Window() */ Window *FindWindowById(WindowClass cls, WindowNumber number) { - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { if (w->window_class == cls && w->window_number == number) return w; } @@ -1144,7 +1144,7 @@ Window *FindWindowById(WindowClass cls, WindowNumber number) */ Window *FindWindowByClass(WindowClass cls) { - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { if (w->window_class == cls) return w; } @@ -1172,7 +1172,7 @@ void DeleteWindowById(WindowClass cls, WindowNumber number, bool force) void DeleteWindowByClass(WindowClass cls) { /* Note: the container remains stable, even when deleting windows. */ - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { if (w->window_class == cls) { delete w; } @@ -1188,7 +1188,7 @@ void DeleteWindowByClass(WindowClass cls) void DeleteCompanyWindows(CompanyID id) { /* Note: the container remains stable, even when deleting windows. */ - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { if (w->owner == id) { delete w; } @@ -1207,7 +1207,7 @@ void DeleteCompanyWindows(CompanyID id) */ void ChangeWindowOwner(Owner old_owner, Owner new_owner) { - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { if (w->owner != old_owner) continue; switch (w->window_class) { @@ -1576,7 +1576,7 @@ static bool IsGoodAutoPlace1(int left, int top, int width, int height, int toolb if (left < 0 || top < toolbar_y || right > _screen.width || bottom > _screen.height) return false; /* Make sure it is not obscured by any window. */ - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->window_class == WC_MAIN_WINDOW) continue; if (right > w->left && @@ -1621,7 +1621,7 @@ static bool IsGoodAutoPlace2(int left, int top, int width, int height, int toolb if (top < toolbar_y || top > _screen.height - (height >> 2)) return false; /* Make sure it is not obscured by any window. */ - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->window_class == WC_MAIN_WINDOW) continue; if (left + width > w->left && @@ -1658,7 +1658,7 @@ static Point GetAutoPlacePosition(int width, int height) * The new window must be entirely on-screen, and not overlap with an existing window. * Eight starting points are tried, two at each corner. */ - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->window_class == WC_MAIN_WINDOW) continue; if (IsGoodAutoPlace1(w->left + w->width, w->top, width, height, toolbar_y, pt)) return pt; @@ -1675,7 +1675,7 @@ static Point GetAutoPlacePosition(int width, int height) * The new window may be partly off-screen, and must not overlap with an existing window. * Only four starting points are tried. */ - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->window_class == WC_MAIN_WINDOW) continue; if (IsGoodAutoPlace2(w->left + w->width, w->top, width, height, toolbar_y, pt)) return pt; @@ -1692,7 +1692,7 @@ static Point GetAutoPlacePosition(int width, int height) int offset_y = std::max(NWidgetLeaf::closebox_dimension.height, FONT_HEIGHT_NORMAL + WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM); restart: - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->left == left && w->top == top) { left += offset_x; top += offset_y; @@ -1896,7 +1896,7 @@ void UnInitWindowSystem() { UnshowCriticalError(); - for (Window *w : Window::IterateFromFront()) delete w; + for (Window *w : Window::Iterate()) delete w; for (Window *w = _z_front_window; w != nullptr; /* nothing */) { Window *to_del = w; @@ -1925,7 +1925,7 @@ static void DecreaseWindowCounters() if (_scroller_click_timeout != 0) _scroller_click_timeout--; if (hundredth_tick_timeout != 0) hundredth_tick_timeout--; - for (Window *w : Window::IterateFromFront()) { + for (Window *w : Window::Iterate()) { if (!_network_dedicated && hundredth_tick_timeout == 0) w->OnHundredthTick(); if (_scroller_click_timeout == 0) { @@ -1951,7 +1951,7 @@ static void DecreaseWindowCounters() w->OnMouseLoop(); } - for (Window *w : Window::IterateFromFront()) { + for (Window *w : Window::Iterate()) { if ((w->flags & WF_TIMEOUT) && --w->timeout_timer == 0) { CLRBITS(w->flags, WF_TIMEOUT); @@ -2190,7 +2190,7 @@ static EventState HandleWindowDragging() if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED; /* Otherwise find the window... */ - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { if (w->flags & WF_DRAGGING) { /* Stop the dragging if the left mouse button was released */ if (!_left_button_down) { @@ -2210,7 +2210,7 @@ static EventState HandleWindowDragging() int vsnap = _settings_client.gui.window_snap_radius; int delta; - for (const Window *v : Window::IterateFromBack()) { + for (const Window *v : Window::Iterate()) { if (v == w) continue; // Don't snap at yourself if (y + w->height > v->top && y < v->top + v->height) { @@ -2423,7 +2423,7 @@ static void HandleScrollbarScrolling(Window *w) */ static EventState HandleActiveWidget() { - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { if (w->mouse_capture_widget >= 0) { /* Abort if no button is clicked any more. */ if (!_left_button_down) { @@ -3095,7 +3095,7 @@ void InputLoop() */ void CallWindowRealtimeTickEvent(uint delta_ms) { - for (Window *w : Window::IterateFromFront()) { + for (Window *w : Window::Iterate()) { w->OnRealtimeTick(delta_ms); } } @@ -3124,7 +3124,7 @@ void UpdateWindows() } /* Process invalidations before anything else. */ - for (Window *w : Window::IterateFromFront()) { + for (Window *w : Window::Iterate()) { w->ProcessScheduledInvalidations(); w->ProcessHighlightedInvalidations(); } @@ -3157,7 +3157,7 @@ void UpdateWindows() if (window_timer.HasElapsed()) { window_timer.SetInterval(MILLISECONDS_PER_TICK); - for (Window *w : Window::IterateFromFront()) { + for (Window *w : Window::Iterate()) { if ((w->flags & WF_WHITE_BORDER) && --w->white_border_timer == 0) { CLRBITS(w->flags, WF_WHITE_BORDER); w->SetDirty(); @@ -3167,7 +3167,7 @@ void UpdateWindows() DrawDirtyBlocks(); - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { /* Update viewport only if window is not shaded. */ if (w->viewport != nullptr && !w->IsShaded()) UpdateViewportPosition(w); } @@ -3183,7 +3183,7 @@ void UpdateWindows() */ void SetWindowDirty(WindowClass cls, WindowNumber number) { - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->window_class == cls && w->window_number == number) w->SetDirty(); } } @@ -3196,7 +3196,7 @@ void SetWindowDirty(WindowClass cls, WindowNumber number) */ void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index) { - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->window_class == cls && w->window_number == number) { w->SetWidgetDirty(widget_index); } @@ -3209,7 +3209,7 @@ void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_inde */ void SetWindowClassesDirty(WindowClass cls) { - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->window_class == cls) w->SetDirty(); } } @@ -3281,7 +3281,7 @@ void Window::ProcessHighlightedInvalidations() */ void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope) { - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { if (w->window_class == cls && w->window_number == number) { w->InvalidateData(data, gui_scope); } @@ -3298,7 +3298,7 @@ void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool g */ void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope) { - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { if (w->window_class == cls) { w->InvalidateData(data, gui_scope); } @@ -3310,7 +3310,7 @@ void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope) */ void CallWindowGameTickEvent() { - for (Window *w : Window::IterateFromFront()) { + for (Window *w : Window::Iterate()) { w->OnGameTick(); } } @@ -3324,7 +3324,7 @@ void CallWindowGameTickEvent() void DeleteNonVitalWindows() { /* Note: the container remains stable, even when deleting windows. */ - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->window_class != WC_MAIN_WINDOW && w->window_class != WC_SELECT_GAME && w->window_class != WC_MAIN_TOOLBAR && @@ -3350,7 +3350,7 @@ void DeleteAllNonVitalWindows() DeleteNonVitalWindows(); /* Note: the container remains stable, even when deleting windows. */ - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->flags & WF_STICKY) { delete w; } @@ -3375,13 +3375,13 @@ void DeleteAllMessages() void DeleteConstructionWindows() { /* Note: the container remains stable, even when deleting windows. */ - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->window_desc->flags & WDF_CONSTRUCTION) { delete w; } } - for (const Window *w : Window::IterateFromBack()) w->SetDirty(); + for (const Window *w : Window::Iterate()) w->SetDirty(); } /** Delete all always on-top windows to get an empty screen */ @@ -3400,7 +3400,7 @@ void ReInitAllWindows(bool zoom_changed) extern void InitDepotWindowBlockSizes(); InitDepotWindowBlockSizes(); - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { if (zoom_changed) w->nested_root->AdjustPaddingForZoom(); w->ReInit(); } @@ -3490,7 +3490,7 @@ int PositionNetworkChatWindow(Window *w) */ void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index) { - for (const Window *w : Window::IterateFromBack()) { + for (const Window *w : Window::Iterate()) { if (w->viewport != nullptr && w->viewport->follow_vehicle == from_index) { w->viewport->follow_vehicle = to_index; w->SetDirty(); @@ -3508,7 +3508,7 @@ void RelocateAllWindows(int neww, int newh) { DeleteWindowById(WC_DROPDOWN_MENU, 0); - for (Window *w : Window::IterateFromBack()) { + for (Window *w : Window::Iterate()) { int left, top; /* XXX - this probably needs something more sane. For example specifying * in a 'backup'-desc that the window should always be centered. */ diff --git a/src/window_gui.h b/src/window_gui.h index 7f7ab2667e..2b7c693674 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -853,6 +853,7 @@ public: WindowIterator begin() { return WindowIterator(Tfront ? _z_front_window : _z_back_window); } WindowIterator end() { return WindowIterator(nullptr); } }; + using Iterate = AllWindows; //!< Iterate all windows in whatever order is easiest. using IterateFromBack = AllWindows; //!< Iterate all windows in Z order from back to front. using IterateFromFront = AllWindows; //!< Iterate all windows in Z order from front to back. };