Files
integration/Source/Integration/RootCanvas.cpp
2026-04-22 07:22:55 -04:00

127 lines
3.2 KiB
C++

////////////////////////////////////////////////////////////
//
// RootCanvas.cpp
//
////////////////////////////////////////////////////////////
#include "RootCanvas.h"
#include "Common.h"
#include "Blueprint/UserWidget.h"
UlxRootCanvasSlot::UlxRootCanvasSlot(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
// Children of the root canvas default to filling the parent,
// matching the viewport's "stretch" behavior. Individual widgets
// can still override anchors/offsets after being added.
SetAnchors(FAnchors(0.0f, 0.0f, 1.0f, 1.0f));
SetOffsets(FMargin(0.0f, 0.0f, 0.0f, 0.0f));
}
void UlxRootCanvasSlot::SetZOrderReliable(int32 Order)
{
if (SlotUnderConstruction)
{
PRAGMA_DISABLE_DEPRECATION_WARNINGS
ZOrder = Order;
PRAGMA_ENABLE_DEPRECATION_WARNINGS
}
else
{
SetZOrder(Order);
}
}
int32 UlxRootCanvasSlot::GetZOrderReliable()
{
if (SlotUnderConstruction)
{
PRAGMA_DISABLE_DEPRECATION_WARNINGS
return ZOrder;
PRAGMA_ENABLE_DEPRECATION_WARNINGS
}
else
{
return GetZOrder();
}
}
UClass* UlxRootCanvasPanel::GetSlotClass() const
{
return UlxRootCanvasSlot::StaticClass();
}
UlxRootCanvasSlot *UlxRootCanvasPanel::GetRootCanvasSlot(UUserWidget *Widget, ElxSuccessOrWrongType &Result)
{
if (IsValid(Widget))
{
if (UlxRootCanvasSlot *Slot = Cast<UlxRootCanvasSlot>(Widget->Slot))
{
Result = ElxSuccessOrWrongType::Success;
return Slot;
}
}
Result = ElxSuccessOrWrongType::WrongType;
return nullptr;
}
UlxRootCanvasSlot* UlxRootCanvasPanel::AddChildToRootCanvas(UWidget* Content)
{
return Cast<UlxRootCanvasSlot>(Super::AddChild(Content));
}
int32 UlxRootCanvasPanel::GetMaxZOrder() const
{
int32 MaxZOrder = 0;
for (UPanelSlot *PanelSlot : Slots)
{
UlxRootCanvasSlot *TypedSlot = Cast<UlxRootCanvasSlot>(PanelSlot);
check(TypedSlot);
MaxZOrder = FMath::Max(MaxZOrder, TypedSlot->GetZOrderReliable());
}
return MaxZOrder;
}
TArray<UlxRootCanvasSlot*> UlxRootCanvasPanel::GetSortedUserWidgets()
{
TArray<UlxRootCanvasSlot*> Result;
Result.Reserve(Slots.Num());
for (UPanelSlot *PanelSlot : Slots)
{
UlxRootCanvasSlot *TypedSlot = Cast<UlxRootCanvasSlot>(PanelSlot);
check(TypedSlot);
if (Cast<UUserWidget>(TypedSlot->Content) == nullptr) continue;
Result.Add(TypedSlot);
}
Result.StableSort([](const UlxRootCanvasSlot &A, const UlxRootCanvasSlot &B)
{
return A.GetZOrder() > B.GetZOrder();
});
return Result;
}
void UlxRootCanvasPanel::SetWidgetWindowManagement(class UUserWidget *Widget,
bool ShowPointer, bool BlockInput, bool EnableEnhancedInput, bool BringToFront, UWidget *DesiredFocusWidget)
{
if (!IsValid(Widget))
{
UE_LOG(LogLuprexIntegration, Error, TEXT("ManageRootWidget called with an invalid widget."));
return;
}
UlxRootCanvasSlot *Slot = Cast<UlxRootCanvasSlot>(Widget->Slot);
if (Slot == nullptr)
{
UE_LOG(LogLuprexIntegration, Error, TEXT("Widget is not yet a root widget, use 'AddWidgetToRoot' first"));
return;
}
UlxRootCanvasPanel *Panel = Cast<UlxRootCanvasPanel>(Slot->Parent);
check(Panel);
Slot->ShowPointer = ShowPointer;
Slot->BlockInput = BlockInput;
Slot->EnableEnhancedInput = EnableEnhancedInput;
Widget->SetDesiredFocusWidget(DesiredFocusWidget);
if (BringToFront) Slot->SetZOrderReliable(Panel->GetMaxZOrder() + 1);
}