// Fill out your copyright notice in the Description page of Project Settings. #include "WidgetManagerComponent.h" #include "Blueprint/UserWidget.h" #include "ProjectT/System/Core/Common/GlobalUtilsLibrary.h" UWidgetManagerComponent::UWidgetManagerComponent() { PrimaryComponentTick.bCanEverTick = true; } // Called when the game starts void UWidgetManagerComponent::BeginPlay() { Super::BeginPlay(); /*OnClosedAllWidget.AddLambda([this] { ActiveWidget = nullptr; ToggleGameUIInput(false); });*/ } // Called every frame void UWidgetManagerComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); } void UWidgetManagerComponent::AsyncOpenWidget(const TSoftClassPtr& InWidgetClass) { UGlobalUtilsLibrary::AsyncLoadSubClass(InWidgetClass, [this](const TSubclassOf& LoadedWidgetClass) { if(LoadedWidgetClass) { OpenWidget(LoadedWidgetClass); if(OnCompletedCreateWidget.IsBound()) { OnCompletedCreateWidget.Execute(); } } }); } void UWidgetManagerComponent::AsyncOpenWidgetWithCallback(const TSoftClassPtr& InWidgetClass, FOnCompletedCreateWidget InCallback) { UGlobalUtilsLibrary::AsyncLoadSubClass(InWidgetClass, [this, InCallback](const TSubclassOf& LoadedWidgetClass) { if(LoadedWidgetClass) { OnCompletedCreateWidget = InCallback; OpenWidget(LoadedWidgetClass); OnCompletedCreateWidget.Execute(); } }); } void UWidgetManagerComponent::OpenWidget(const TSubclassOf& InWidgetClass) { if(!WidgetStack.IsEmpty()) { ActiveWidget->SetVisibility(ESlateVisibility::Collapsed); } ActiveWidget = CreateWidget(GetWorld(), InWidgetClass); ActiveWidget->AddToViewport(); ActiveWidget->SetVisibility(ESlateVisibility::Visible); if(WidgetStack.IsEmpty()) { ToggleGameUIInput(); } WidgetStack.Emplace(ActiveWidget); } void UWidgetManagerComponent::CloseWidget() { if(WidgetStack.IsEmpty()) return; WidgetStack.Last()->RemoveFromParent(); WidgetStack.Pop(); if(!WidgetStack.IsEmpty()) { ActiveWidget = WidgetStack.Last(); ActiveWidget->SetVisibility(ESlateVisibility::Visible); } else { if(OnClosedAllWidget.IsBound()) { OnClosedAllWidget.Execute(); } } } void UWidgetManagerComponent::FlushAllWidget(FOnClosedAllWidget InCallBack) { for(UUserWidget* Widget : WidgetStack) { Widget->RemoveFromParent(); } WidgetStack.Empty(); OnClosedAllWidget = InCallBack; if(OnClosedAllWidget.IsBound()) { OnClosedAllWidget.Execute(); } } void UWidgetManagerComponent::FlushAllWidget() { for(UUserWidget* Widget : WidgetStack) { Widget->RemoveFromParent(); } WidgetStack.Empty(); } void UWidgetManagerComponent::ToggleGameUIInput(const bool InbGameAndUI/* = true*/) const { APlayerController* PC = GetWorld()->GetFirstPlayerController(); if(!NMT_ENSURE(PC)) return; InbGameAndUI ? PC->SetInputMode(FInputModeGameAndUI()) : PC->SetInputMode(FInputModeGameOnly()); }