From 18c2f2c5d54650491c7ea8a06553ecd43241edc5 Mon Sep 17 00:00:00 2001 From: Steve Streeting Date: Fri, 7 Jan 2022 16:10:48 +0000 Subject: [PATCH] Oops, missed added files --- .../Public/StevesUiHelpers.cpp | 44 +++++++++++++++++++ .../StevesUEHelpers/Public/StevesUiHelpers.h | 17 +++++++ 2 files changed, 61 insertions(+) create mode 100644 Source/StevesUEHelpers/Public/StevesUiHelpers.cpp create mode 100644 Source/StevesUEHelpers/Public/StevesUiHelpers.h diff --git a/Source/StevesUEHelpers/Public/StevesUiHelpers.cpp b/Source/StevesUEHelpers/Public/StevesUiHelpers.cpp new file mode 100644 index 0000000..1d96d56 --- /dev/null +++ b/Source/StevesUEHelpers/Public/StevesUiHelpers.cpp @@ -0,0 +1,44 @@ +#include "StevesUiHelpers.h" + +#include "Components/PanelWidget.h" + +void StevesUiHelpers::InsertChildWidgetAt(UPanelWidget* Parent, UWidget* Child, int AtIndex) +{ + checkf(AtIndex <= Parent->GetChildrenCount(), TEXT("Insertion index %d is greater than child count"), AtIndex); + + // Short-circuit for simple case of insert at end + const int OrigCount = Parent->GetChildrenCount(); + if (OrigCount == AtIndex) + { + Parent->AddChild(Child); + } + else + { + // Note: cannot use InsertChildAt, that's editor-only + // There are a few options with the runtime API + // 1. Remove all children and re-add them in the right order + // 2. Remove all children on or after the insert point and re-add those after adding the new item + // 3. Use ReplaceChildAt to insert a new item and move everything down, with AddChild at the end + // + // I'm going with 2. because that seems to blend best performance with the most well defined behaviour + // I'm not sure ReplaceChildAt is designed to move children around (note: ShiftChild exists but again, editor-only) + + TArray WidgetsToReplaceReversed; + WidgetsToReplaceReversed.Reserve(OrigCount - AtIndex); + // Go backwards so we can remove as we go and not remove from the middle + for (int i = OrigCount - 1; i >= AtIndex; --i) + { + WidgetsToReplaceReversed.Add(Parent->GetChildAt(i)); + Parent->RemoveChildAt(i); + } + // insert item + Parent->AddChild(Child); + // add back previous, reverse order + for (int i = WidgetsToReplaceReversed.Num() - 1; i >= 0; --i) + { + Parent->AddChild(WidgetsToReplaceReversed[i]); + } + + } + +} diff --git a/Source/StevesUEHelpers/Public/StevesUiHelpers.h b/Source/StevesUEHelpers/Public/StevesUiHelpers.h new file mode 100644 index 0000000..aa9ab4b --- /dev/null +++ b/Source/StevesUEHelpers/Public/StevesUiHelpers.h @@ -0,0 +1,17 @@ +#pragma once +#include "CoreMinimal.h" +#include "Components/Widget.h" + +/// Helper Ui routines that UE4 is missing, all static +/// Exposed to BP in in StevesBPL +class STEVESUEHELPERS_API StevesUiHelpers +{ +public: + /** + * Insert a child widget at a specific index + * @param Parent The container widget + * @param Child The child widget to add + * @param AtIndex The index at which the new child should exist + */ + static void InsertChildWidgetAt(UPanelWidget* Parent, UWidget* Child, int AtIndex = 0); +};