LuckyWorldV2/Source/Luckyrobots/Private/Menus/MainScreenUserWidget.cpp

138 lines
3.5 KiB
C++
Raw Normal View History

2025-03-30 11:46:50 +08:00
// Fill out your copyright notice in the Description page of Project Settings.
2025-04-07 10:28:47 +08:00
#include "Menus/MainScreenUserWidget.h"
2025-03-30 11:46:50 +08:00
#include "Engine/DataTable.h"
2025-04-07 10:28:47 +08:00
#include "Core/LuckyRobotsGameInstance.h"
#include "FunctionLibraries/LuckyRobotsFunctionLibrary.h"
2025-03-30 11:46:50 +08:00
void UMainScreenUserWidget::NativeConstruct()
{
Super::NativeConstruct();
InitData();
}
void UMainScreenUserWidget::InitData()
{
InitRobotData();
InitLevelData();
2025-04-07 11:32:45 +08:00
if (ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance()))
2025-03-30 11:46:50 +08:00
{
2025-04-07 11:32:45 +08:00
CurrentQualityIndex = static_cast<int32>(GameInstance->CurrentSelectQuality);
2025-03-30 11:46:50 +08:00
}
}
void UMainScreenUserWidget::InitRobotData()
{
RobotDataList = ULuckyRobotsFunctionLibrary::GetActiveRobotDataList(this);
2025-04-07 11:32:45 +08:00
CurrentRobotIndex = 0;
2025-03-30 11:46:50 +08:00
UpdateSelectRobot();
}
void UMainScreenUserWidget::InitLevelData()
{
LevelDataList = ULuckyRobotsFunctionLibrary::GetActiveLevelDataList(this);
2025-03-30 11:46:50 +08:00
FRobotData CurrentRobotData = GetCurrentRobotData();
if (CurrentRobotData.Name != ERobotsName::None)
{
2025-04-07 11:32:45 +08:00
TArray<FLevelData> FilteredLevels;
for (const FLevelData& LevelData : LevelDataList)
2025-03-30 11:46:50 +08:00
{
2025-04-07 11:32:45 +08:00
if (LevelData.RobotTypeList.Contains(CurrentRobotData.RobotType))
2025-03-30 11:46:50 +08:00
{
2025-04-07 11:32:45 +08:00
FilteredLevels.Add(LevelData);
2025-03-30 11:46:50 +08:00
}
}
2025-04-07 11:32:45 +08:00
LevelDataList = FilteredLevels;
2025-03-30 11:46:50 +08:00
}
2025-04-07 11:32:45 +08:00
CurrentLevelIndex = 0;
2025-03-30 11:46:50 +08:00
UpdateSelectLevel();
}
2025-04-07 11:32:45 +08:00
FRobotData UMainScreenUserWidget::GetCurrentRobotData() const
2025-03-30 11:46:50 +08:00
{
2025-04-07 11:32:45 +08:00
if (RobotDataList.IsValidIndex(CurrentRobotIndex))
2025-03-30 11:46:50 +08:00
{
2025-04-07 11:32:45 +08:00
return RobotDataList[CurrentRobotIndex];
2025-03-30 11:46:50 +08:00
}
2025-04-07 11:32:45 +08:00
return FRobotData();
2025-03-30 11:46:50 +08:00
}
2025-04-07 11:32:45 +08:00
FLevelData UMainScreenUserWidget::GetCurrentLevelData() const
2025-03-30 11:46:50 +08:00
{
2025-04-07 11:32:45 +08:00
if (LevelDataList.IsValidIndex(CurrentLevelIndex))
2025-03-30 11:46:50 +08:00
{
2025-04-07 11:32:45 +08:00
return LevelDataList[CurrentLevelIndex];
2025-03-30 11:46:50 +08:00
}
2025-04-07 11:32:45 +08:00
return FLevelData();
2025-03-30 11:46:50 +08:00
}
void UMainScreenUserWidget::SelectNextRobot()
{
2025-04-07 11:32:45 +08:00
CurrentRobotIndex = FMath::Clamp(CurrentRobotIndex + 1, 0, RobotDataList.Num() - 1);
2025-03-30 11:46:50 +08:00
UpdateSelectRobot();
}
void UMainScreenUserWidget::SelectPreviousRobot()
{
2025-04-07 11:32:45 +08:00
CurrentRobotIndex = FMath::Clamp(CurrentRobotIndex - 1, 0, RobotDataList.Num() - 1);
2025-03-30 11:46:50 +08:00
UpdateSelectRobot();
}
void UMainScreenUserWidget::SelectNextLevel()
{
2025-04-07 11:32:45 +08:00
CurrentLevelIndex = FMath::Clamp(CurrentLevelIndex + 1, 0, LevelDataList.Num() - 1);
2025-03-30 11:46:50 +08:00
UpdateSelectLevel();
}
void UMainScreenUserWidget::SelectPreviousLevel()
{
2025-04-07 11:32:45 +08:00
CurrentLevelIndex = FMath::Clamp(CurrentLevelIndex - 1, 0, LevelDataList.Num() - 1);
2025-03-30 11:46:50 +08:00
UpdateSelectLevel();
}
void UMainScreenUserWidget::SelectNextQuality()
{
UEnum* QualityEnum = StaticEnum<EQualityEnum>();
2025-04-07 11:32:45 +08:00
int32 EnumCount = QualityEnum->NumEnums() - 1;
CurrentQualityIndex = FMath::Clamp(CurrentQualityIndex - 1, 0, EnumCount - 1);
2025-03-30 11:46:50 +08:00
UpdateSelectQuality();
}
void UMainScreenUserWidget::SelectPreviousQuality()
{
UEnum* QualityEnum = StaticEnum<EQualityEnum>();
2025-04-07 11:32:45 +08:00
int32 EnumCount = QualityEnum->NumEnums() - 1;
CurrentQualityIndex = FMath::Clamp(CurrentQualityIndex + 1, 0, EnumCount - 1);
2025-03-30 11:46:50 +08:00
UpdateSelectQuality();
}
void UMainScreenUserWidget::UpdateSelectRobot()
{
2025-04-07 11:32:45 +08:00
if (ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance()))
2025-03-30 11:46:50 +08:00
{
2025-04-07 11:32:45 +08:00
GameInstance->CurrentSelectRobot = GetCurrentRobotData().Name;
2025-03-30 11:46:50 +08:00
}
BPUpdateSelectRobot();
InitLevelData();
}
2025-04-07 11:32:45 +08:00
2025-03-30 11:46:50 +08:00
void UMainScreenUserWidget::UpdateSelectLevel()
{
2025-04-07 11:32:45 +08:00
if (ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance()))
2025-03-30 11:46:50 +08:00
{
2025-04-07 11:32:45 +08:00
GameInstance->CurrentSelectLevel = GetCurrentLevelData().LevelEnum;
2025-03-30 11:46:50 +08:00
}
BPUpdateSelectLevel();
}
void UMainScreenUserWidget::UpdateSelectQuality()
{
2025-04-07 11:32:45 +08:00
if (ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance()))
2025-03-30 11:46:50 +08:00
{
2025-04-07 11:32:45 +08:00
GameInstance->CurrentSelectQuality = static_cast<EQualityEnum>(CurrentQualityIndex);
2025-03-30 11:46:50 +08:00
}
BPUpdateSelectQuality();
2025-04-07 11:32:45 +08:00
}