// Fill out your copyright notice in the Description page of Project Settings. #include "MainScreenUserWidget.h" #include "Engine/DataTable.h" #include "LuckyRobotsGameInstance.h" #include "LuckyRobotsFunctionLibrary.h" void UMainScreenUserWidget::NativeConstruct() { Super::NativeConstruct(); InitData(); } void UMainScreenUserWidget::InitData() { InitRobotData(); InitLevelData(); ULuckyRobotsGameInstance* LuckyRobotsGameInstance = Cast(GetGameInstance()); if (LuckyRobotsGameInstance) { iCurrentSelectQuality = int(LuckyRobotsGameInstance->CurrentSelectQuality); } } void UMainScreenUserWidget::InitRobotData() { RobotDataList = ULuckyRobotsFunctionLibrary::GetActiveRobotDataList(this); iCurrentSelectRobot = 0; UpdateSelectRobot(); } void UMainScreenUserWidget::InitLevelData() { LevelDataList = ULuckyRobotsFunctionLibrary::GetActiveLevelDataList(this); FRobotData CurrentRobotData = GetCurrentRobotData(); if (CurrentRobotData.Name != ERobotsName::None) { TArray ActiveLevelDataList = LevelDataList; LevelDataList.Empty(); for (auto ActiveLevelData : ActiveLevelDataList) { if (ActiveLevelData.RobotTypeList.Find(CurrentRobotData.RobotType) >= 0) { LevelDataList.Add(ActiveLevelData); } } } iCurrentSelectLevel = 0; UpdateSelectLevel(); } FRobotData UMainScreenUserWidget::GetCurrentRobotData() { FRobotData CurrentRobotData; if (RobotDataList.IsValidIndex(iCurrentSelectRobot)) { CurrentRobotData = RobotDataList[iCurrentSelectRobot]; } return CurrentRobotData; } FLevelData UMainScreenUserWidget::GetCurrentLevelData() { FLevelData CurrentLevelData; if (LevelDataList.IsValidIndex(iCurrentSelectLevel)) { CurrentLevelData = LevelDataList[iCurrentSelectLevel]; } return CurrentLevelData; } void UMainScreenUserWidget::SelectNextRobot() { iCurrentSelectRobot = FMath::Clamp(iCurrentSelectRobot + 1, 0, RobotDataList.Num() - 1); UpdateSelectRobot(); } void UMainScreenUserWidget::SelectPreviousRobot() { iCurrentSelectRobot = FMath::Clamp(iCurrentSelectRobot - 1, 0, RobotDataList.Num() - 1); UpdateSelectRobot(); } void UMainScreenUserWidget::SelectNextLevel() { iCurrentSelectLevel = FMath::Clamp(iCurrentSelectLevel + 1, 0, LevelDataList.Num() - 1); UpdateSelectLevel(); } void UMainScreenUserWidget::SelectPreviousLevel() { iCurrentSelectLevel = FMath::Clamp(iCurrentSelectLevel - 1, 0, LevelDataList.Num() - 1); UpdateSelectLevel(); } void UMainScreenUserWidget::SelectNextQuality() { UEnum* QualityEnum = StaticEnum(); int QualityEnumNum = QualityEnum->NumEnums() - 1; iCurrentSelectQuality = FMath::Clamp(iCurrentSelectQuality - 1, 0, QualityEnumNum - 1); UpdateSelectQuality(); } void UMainScreenUserWidget::SelectPreviousQuality() { UEnum* QualityEnum = StaticEnum(); int QualityEnumNum = QualityEnum->NumEnums() - 1; iCurrentSelectQuality = FMath::Clamp(iCurrentSelectQuality + 1, 0, QualityEnumNum - 1); UpdateSelectQuality(); } void UMainScreenUserWidget::UpdateSelectRobot() { ULuckyRobotsGameInstance* LuckyRobotsGameInstance = Cast(GetGameInstance()); if (LuckyRobotsGameInstance) { LuckyRobotsGameInstance->CurrentSelectRobot = GetCurrentRobotData().Name; } BPUpdateSelectRobot(); InitLevelData(); } void UMainScreenUserWidget::UpdateSelectLevel() { ULuckyRobotsGameInstance* LuckyRobotsGameInstance = Cast(GetGameInstance()); if (LuckyRobotsGameInstance) { LuckyRobotsGameInstance->CurrentSelectLevel = GetCurrentLevelData().LevelEnum; } BPUpdateSelectLevel(); } void UMainScreenUserWidget::UpdateSelectQuality() { ULuckyRobotsGameInstance* LuckyRobotsGameInstance = Cast(GetGameInstance()); if (LuckyRobotsGameInstance) { LuckyRobotsGameInstance->CurrentSelectQuality = static_cast(iCurrentSelectQuality); } BPUpdateSelectQuality(); }