Optimize WB_SelectGoalMenu

This commit is contained in:
martinluckyrobots 2025-04-17 13:30:44 +08:00
parent ced6e40acd
commit 500be7ee62
4 changed files with 189 additions and 2 deletions

View File

@ -4,6 +4,11 @@
#include "UI/Settings/SelectGoalUserWidget.h"
#include "SharedDef.h"
#include "Components/ComboBoxString.h"
#include "Components/SizeBox.h"
#include "Core/LuckyRobotsGameInstance.h"
#include "UI/Settings/PathfindingSelectorUserWidget.h"
#include "UI/GameUserWidget.h"
#include "Subsystem/UISubsystem.h"
void USelectGoalUserWidget::NativeConstruct()
{
@ -36,4 +41,160 @@ void USelectGoalUserWidget::NativeConstruct()
GetComboBoxString()->SetSelectedIndex(bInitAddOption);
}
}
void USelectGoalUserWidget::DoSelectionChanged(const FString& SelectedItem)
{
if (GetComboBoxString())
{
if (GetComboBoxString()->GetSelectedIndex() != 0)
{
if (AllGoalListDataTable)
{
FString ContextString;
TArray<FName> RowNames = AllGoalListDataTable->GetRowNames();
for (const FName& RowName : RowNames)
{
FAllGoalListData* AllGoalListData = AllGoalListDataTable->FindRow<FAllGoalListData>(RowName, ContextString);
if (AllGoalListData)
{
if (AllGoalListData->bIsActive)
{
UEnum* GoalTypeEnum = StaticEnum<EGoalType>();
FString GoalTypeString = GoalTypeEnum->GetDisplayNameTextByIndex(int(AllGoalListData->GoalType)).ToString();
if (GoalTypeString == SelectedItem)
{
if (AllGoalListData->Widget)
{
if (!CreatedUserWidget || (CreatedUserWidget && CreatedUserWidget->GetClass() != AllGoalListData->Widget))
{
if (CreatedUserWidget)
{
CreatedUserWidget->RemoveFromParent();
CreatedUserWidget = nullptr;
}
CreatedUserWidget = CreateWidget<UUserWidget>(this, AllGoalListData->Widget);
if (CreatedUserWidget)
{
if (GetSizeBox())
{
GetSizeBox()->AddChild(CreatedUserWidget);
}
}
}
}
else
{
if (CreatedUserWidget)
{
CreatedUserWidget->RemoveFromParent();
CreatedUserWidget = nullptr;
}
}
DoSendDetail(AllGoalListData->Example);
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
if (GameInstance)
{
GameInstance->bIsChanged = false;
}
NavigateSimpleEnvironments = Cast<UPathfindingSelectorUserWidget>(CreatedUserWidget);
break;
}
}
}
}
}
}
else
{
if (CreatedUserWidget)
{
CreatedUserWidget->RemoveFromParent();
CreatedUserWidget = nullptr;
}
}
}
}
void USelectGoalUserWidget::DoCloseWindow()
{
if (GetComboBoxString())
{
GetComboBoxString()->SetSelectedIndex(bInitAddOption);
}
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
if (GameInstance)
{
if (GameInstance->GameUserWidget)
{
GameInstance->GameUserWidget->bIsGoalMenuVis = false;
}
}
}
void USelectGoalUserWidget::DoAutoConfirm()
{
if (GetComboBoxString())
{
if (GetComboBoxString()->GetSelectedIndex() != 0)
{
auto UISubsystem = GetGameInstance()->GetSubsystem<UUISubsystem>();
if (UISubsystem)
{
UISubsystem->OnEndTracing.Broadcast();
}
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
if (GameInstance)
{
GameInstance->UpdateTargetSelector();
GameInstance->TempTask = FGoalsTaskData();
}
GetComboBoxString()->SetSelectedIndex(bInitAddOption);
DoCloseWindow();
}
}
}
void USelectGoalUserWidget::DoAddGoalAndStopTracing()
{
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
if (GameInstance)
{
if (GameInstance->bIsChanged)
{
if (GetComboBoxString())
{
if (GetComboBoxString()->GetSelectedIndex() != 0)
{
DoSaveGoalsBTNClick();
GetWorld()->GetTimerManager().ClearTimer(DelayAddGoalTimerHandle);
GetWorld()->GetTimerManager().SetTimer(DelayAddGoalTimerHandle, this, &USelectGoalUserWidget::DelayAddGoal, 0.2f, false);
}
}
}
}
}
void USelectGoalUserWidget::DelayAddGoal()
{
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
if (GameInstance)
{
GameInstance->TempTask = FGoalsTaskData();
}
if (NavigateSimpleEnvironments)
{
NavigateSimpleEnvironments->BPClickSelectObjectBtn();
}
}

View File

@ -8,6 +8,7 @@
class UComboBoxString;
class UPathfindingSelectorUserWidget;
class USizeBox;
/**
*
*/
@ -26,14 +27,39 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UPathfindingSelectorUserWidget* NavigateSimpleEnvironments;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UUserWidget* CreatedUserWidget;
public:
FTimerHandle DelayAddGoalTimerHandle;
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
bool bInitAddOption;
public:
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
UFUNCTION(BlueprintCallable)
void DoSelectionChanged(const FString& SelectedItem);
UFUNCTION(BlueprintCallable)
void DoCloseWindow();
UFUNCTION(BlueprintCallable)
void DoAutoConfirm();
UFUNCTION(BlueprintCallable)
void DoAddGoalAndStopTracing();
void DelayAddGoal();
public:
UFUNCTION(BlueprintPure, BlueprintImplementableEvent)
UComboBoxString* GetComboBoxString();
UFUNCTION(BlueprintPure, BlueprintImplementableEvent)
USizeBox* GetSizeBox();
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
void DoSendDetail(const FString& Explanationn);
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
void DoSaveGoalsBTNClick();
};