From 40cafdf455a7449d1a32da507ca6c2a1117ed331 Mon Sep 17 00:00:00 2001 From: Megumumpkin Date: Sat, 10 Jul 2021 18:02:35 +0700 Subject: [PATCH] GUI Sort Fix (#278) Co-authored-by: Megumumpkin --- WickedEngine/wiGUI.cpp | 8 +++++--- WickedEngine/wiWidget.cpp | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/WickedEngine/wiGUI.cpp b/WickedEngine/wiGUI.cpp index 88ab9f8de..cd5907784 100644 --- a/WickedEngine/wiGUI.cpp +++ b/WickedEngine/wiGUI.cpp @@ -44,9 +44,11 @@ void wiGUI::Update(const wiCanvas& canvas, float dt) } } - std::sort(widgets.begin(), widgets.end(), [](const wiWidget* a, const wiWidget* b) { - return a->priority < b->priority; - }); + if(priority > 0) //Sort only if there are priority changes + //Use std::stable_sort instead of std::sort to preserve UI element order with equal priorities + std::stable_sort(widgets.begin(), widgets.end(), [](const wiWidget* a, const wiWidget* b) { + return a->priority < b->priority; + }); } void wiGUI::Render(const wiCanvas& canvas, CommandList cmd) const diff --git a/WickedEngine/wiWidget.cpp b/WickedEngine/wiWidget.cpp index db729a580..424d3c598 100644 --- a/WickedEngine/wiWidget.cpp +++ b/WickedEngine/wiWidget.cpp @@ -1727,9 +1727,11 @@ void wiWindow::Update(const wiCanvas& canvas, float dt) } } - std::sort(widgets.begin(), widgets.end(), [](const wiWidget* a, const wiWidget* b) { - return a->priority < b->priority; - }); + if(priority > 0) //Sort only if there are priority changes + //Use std::stable_sort instead of std::sort to preserve UI element order with equal priorities + std::stable_sort(widgets.begin(), widgets.end(), [](const wiWidget* a, const wiWidget* b) { + return a->priority < b->priority; + }); if (IsMinimized()) {