Untitled

 avatar
unknown
c_cpp
a year ago
4.2 kB
12
Indexable
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "WindowSystem/WindowLayoutSave.h"
#include "WindowManagerComponent.generated.h"

class UDesktopWidget;
class UWindowWidget;
class UViewport3DWidget;

UCLASS(ClassGroup=(UI), meta=(BlueprintSpawnableComponent))
class VIEWPORTWINDOWS_API UWindowManagerComponent : public UActorComponent
{
    GENERATED_BODY()
public:
    UWindowManagerComponent();

    UFUNCTION(BlueprintCallable, Category="Windows")
    void InitializeDesktop();

    // Single, non-overloaded UFUNCTIONs (WindowId is optional)
    UFUNCTION(BlueprintCallable, Category="Windows", meta=(AdvancedDisplay="WindowId"))
    UWindowWidget* CreateUMGWindow(
        TSubclassOf<UUserWidget> ContentClass,
        const FVector2D& Position,
        const FVector2D& Size,
        const FText& Title,
        FName WindowId = NAME_None);

    UFUNCTION(BlueprintCallable, Category="Windows", meta=(AdvancedDisplay="WindowId"))
    UWindowWidget* Create3DViewportWindow(
        AActor* LookAtActor,
        const FVector2D& Position,
        const FVector2D& Size,
        const FText& Title,
        FName WindowId = NAME_None);

    UFUNCTION(BlueprintCallable, Category="Windows")
    UDesktopWidget* GetDesktop() const { return Desktop; }

    // Persistence API
    UFUNCTION(BlueprintCallable, Category="Windows|Layout")
    void ApplySavedLayout(UWindowWidget* Window);

    UFUNCTION(BlueprintCallable, Category="Windows|Layout")
    bool SaveLayoutForWindow(UWindowWidget* Window, bool bAnchorAlso = true);

    UFUNCTION(BlueprintCallable, Category="Windows|Layout")
    bool ClearLayoutForWindow(UWindowWidget* Window);

    UFUNCTION(BlueprintCallable, Category="Windows|Layout")
    void ClearAllLayouts();

private:
    UPROPERTY()
    UDesktopWidget* Desktop = nullptr;

    UPROPERTY()
    TSubclassOf<UDesktopWidget> DesktopClass = nullptr;
    UPROPERTY()
    TSubclassOf<UWindowWidget>  WindowClass = nullptr;
    UPROPERTY()
    TSubclassOf<UViewport3DWidget> Viewport3DClass = nullptr;

    FString SaveSlotName = TEXT("WindowLayouts");
    uint32  SaveUserIndex = 0;
    int32   NextZOrder = 1;

    UWindowLayoutSave* LoadLayouts();
    bool WriteLayouts(UWindowLayoutSave* Data);
    FVector2D GetViewport() const;
    void BringToFront(UWindowWidget* Window);

public:
    virtual void BeginPlay() override;
};

UWindowWidget* UWindowManagerComponent::CreateUMGWindow(
    TSubclassOf<UUserWidget> ContentClass,
    const FVector2D& Position,
    const FVector2D& Size,
    const FText& Title,
    FName WindowId /*=NAME_None*/)
{
    if (!Desktop || !ContentClass) return nullptr;

    APlayerController* PC = GetWorld()->GetFirstPlayerController();
    if (!PC) return nullptr;

    UWindowWidget* Window = CreateWidget<UWindowWidget>(PC, WindowClass);
    Window->Title = Title;
    Window->WindowId = WindowId;

    UUserWidget* Content = CreateWidget<UUserWidget>(PC, ContentClass);
    Window->SetContent(Content);

    if (UCanvasPanelSlot* Slot = Desktop->AddWindowToDesktop(Window, Position, Size))
    {
        BringToFront(Window);
    }

    if (!WindowId.IsNone())
    {
        ApplySavedLayout(Window);
    }

    return Window;
}

UWindowWidget* UWindowManagerComponent::Create3DViewportWindow(
    AActor* LookAtActor,
    const FVector2D& Position,
    const FVector2D& Size,
    const FText& Title,
    FName WindowId /*=NAME_None*/)
{
    if (!Desktop) return nullptr;

    APlayerController* PC = GetWorld()->GetFirstPlayerController();
    if (!PC) return nullptr;

    UWindowWidget* Window = CreateWidget<UWindowWidget>(PC, WindowClass);
    Window->Title = Title;
    Window->WindowId = WindowId;

    UViewport3DWidget* View = CreateWidget<UViewport3DWidget>(PC, Viewport3DClass);
    if (LookAtActor) View->SetLookAt(LookAtActor);
    Window->SetContent(View);

    if (UCanvasPanelSlot* Slot = Desktop->AddWindowToDesktop(Window, Position, Size))
    {
        BringToFront(Window);
    }

    if (!WindowId.IsNone())
    {
        ApplySavedLayout(Window);
    }

    return Window;
}
Editor is loading...
Leave a Comment