Oops, missed added files

This commit is contained in:
Steve Streeting 2022-01-07 16:10:48 +00:00
parent ee07df6172
commit 18c2f2c5d5
2 changed files with 61 additions and 0 deletions

View File

@ -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<UWidget*> 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]);
}
}
}

View File

@ -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);
};