Untitled

 avatar
unknown
c_cpp
a year ago
4.6 kB
16
Indexable
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "WindowSystem/WindowSystemDemoActor.generated.h"

class UWindowManagerComponent;

/**
 * Drop this actor into your level to spawn two example windows:
 *  - A UMG content window
 *  - A 3D Viewport window (optional LookAt target by tag)
 */
UCLASS()
class VIEWPORTWINDOWS_API AWindowSystemDemoActor : public AActor
{
    GENERATED_BODY()

public:
    AWindowSystemDemoActor();

protected:
    virtual void BeginPlay() override;

public: // Components
    /** Window manager component that builds the Desktop and spawns windows */
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="ViewportWindows")
    UWindowManagerComponent* WindowManager;

public: // Configurable demo settings
    /** Spawn demo windows on BeginPlay */
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="ViewportWindows|Demo")
    bool bOpenOnBeginPlay = true;

    /** Class for the UMG content window (leave null to use SimpleLabelWidget) */
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="ViewportWindows|Demo")
    TSubclassOf<UUserWidget> DemoContentClass;

    /** Optional tag to pick a LookAt actor for the 3D viewport window */
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="ViewportWindows|Demo")
    FName LookAtTag = TEXT("DemoCamTarget");

    /** ---- UMG Window params ---- */
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="ViewportWindows|Demo")
    FText UMGWindowTitle = FText::FromString(TEXT("UMG Window"));

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="ViewportWindows|Demo")
    FVector2D UMGWindowPosition = FVector2D(80.f, 80.f);

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="ViewportWindows|Demo")
    FVector2D UMGWindowSize = FVector2D(420.f, 280.f);

    /** Stable ID for persistence (pin to save/lock) */
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="ViewportWindows|Demo")
    FName UMGWindowId = TEXT("UMG_Main");

    /** ---- 3D Viewport Window params ---- */
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="ViewportWindows|Demo")
    FText ViewportWindowTitle = FText::FromString(TEXT("3D Viewport"));

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="ViewportWindows|Demo")
    FVector2D ViewportWindowPosition = FVector2D(540.f, 120.f);

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="ViewportWindows|Demo")
    FVector2D ViewportWindowSize = FVector2D(600.f, 380.f);

    /** Stable ID for persistence (pin to save/lock) */
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="ViewportWindows|Demo")
    FName ViewportWindowId = TEXT("Viewport_Main");
};


#include "WindowSystem/WindowSystemDemoActor.h"

#include "WindowSystem/WindowManagerComponent.h"
#include "WindowSystem/WindowWidget.h"
#include "WindowSystem/SimpleLabelWidget.h"

#include "Kismet/GameplayStatics.h"

AWindowSystemDemoActor::AWindowSystemDemoActor()
{
    PrimaryActorTick.bCanEverTick = false;

    WindowManager = CreateDefaultSubobject<UWindowManagerComponent>(TEXT("WindowManager"));
}

void AWindowSystemDemoActor::BeginPlay()
{
    Super::BeginPlay();

    if (!bOpenOnBeginPlay || !WindowManager)
    {
        return;
    }

    // Ensure the Desktop is created/added to viewport
    WindowManager->InitializeDesktop();

    // -------- UMG Window --------
    {
        TSubclassOf<UUserWidget> ContentClass =
            DemoContentClass ? DemoContentClass : USimpleLabelWidget::StaticClass();

        UWindowWidget* W1 = WindowManager->CreateUMGWindow(
            ContentClass,
            UMGWindowPosition,
            UMGWindowSize,
            UMGWindowTitle,
            UMGWindowId
        );

        // Optional: force anchored at first run (commented out)
        // if (W1) { W1->bCanClose = false; W1->SetAnchored(true); }
        // Click the 📌 button in play to persist the exact position/size.
    }

    // -------- 3D Viewport Window --------
    {
        AActor* LookAt = nullptr;
        if (!LookAtTag.IsNone())
        {
            TArray<AActor*> Found;
            UGameplayStatics::GetAllActorsWithTag(GetWorld(), LookAtTag, Found);
            if (Found.Num() > 0)
            {
                LookAt = Found[0];
            }
        }

        WindowManager->Create3DViewportWindow(
            LookAt,
            ViewportWindowPosition,
            ViewportWindowSize,
            ViewportWindowTitle,
            ViewportWindowId
        );
    }
}
Editor is loading...
Leave a Comment