LuckyWorldV2/Source/Luckyrobots/Private/Menus/MainScreenUserWidget.cpp
martinluckyrobots 4d4026d9ad Optimize code to unreal style
2025-04-07 11:32:45 +08:00

138 lines
3.5 KiB
C++

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