Files
KingshearthLegacy/Source/KingshearthLegacy/KingshearthLegacyPlayerController.cpp
T
Dolobarsch a900208273 Füge neues Inventar- und Ausrüstungs-UI-System hinzu
Ein neues modulares Inventar- und Ausrüstungs-UI-System wurde implementiert, das folgende Hauptfunktionen bietet:
- Unterstützung für Ausrüstungs-Slots (`UEquipmentWidget`, `UEquipmentSlotWidget`).
- Darstellung von Inventargegenständen in Listen- oder Rasteransicht (`UInventoryItemListWidget`, `UInventoryGridWidget`).
- Schnellzugriffsleiste für große und kleine Slots (`UQuickSlotListWidget`).
- Verwaltung von Inventarbehältern mit zusammenklappbaren Bereichen (`UInventoryContainerListWidget`).

Neue Datenstrukturen (`InventoryTypes.h`) definieren Enums und Strukturen für Gegenstände, Ausrüstungs-Slots und Behälter. Der `PlayerController` wurde erweitert, um das Inventar-UI zu öffnen/schließen (`ToggleInventory`). Neue `.uasset`-Dateien wurden hinzugefügt, um die UI-Komponenten zu unterstützen.
2026-09-14 16:26:35 +02:00

170 lines
4.4 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "KingshearthLegacyPlayerController.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "Engine/GameInstance.h"
#include "Engine/LocalPlayer.h"
#include "InputMappingContext.h"
#include "Blueprint/UserWidget.h"
#include "KingshearthLegacy.h"
#include "Widgets/Input/SVirtualJoystick.h"
#include "UI/PlayerHUDWidget.h"
#include "UI/Menu/InGameMenuWidget.h"
#include "UI/Menu/MenuManagerSubsystem.h"
#include "UI/Inventory/EquipmentInventoryTabWidget.h"
#include "KingshearthLegacyCharacter.h"
void AKingshearthLegacyPlayerController::BeginPlay()
{
Super::BeginPlay();
// only spawn touch controls on local player controllers
if (IsLocalPlayerController() && ShouldUseTouchControls())
{
// spawn the mobile controls widget
MobileControlsWidget = CreateWidget<UUserWidget>(this, MobileControlsWidgetClass);
if (MobileControlsWidget)
{
// add the controls to the player screen
MobileControlsWidget->AddToPlayerScreen(0);
} else {
UE_LOG(LogKingshearthLegacy, Error, TEXT("Could not spawn mobile controls widget."));
}
}
}
void AKingshearthLegacyPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
// only add IMCs for local player controllers
if (IsLocalPlayerController())
{
// Add Input Mapping Contexts
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
{
for (UInputMappingContext* CurrentContext : DefaultMappingContexts)
{
Subsystem->AddMappingContext(CurrentContext, 0);
}
// only add these IMCs if we're not using mobile touch input
if (!ShouldUseTouchControls())
{
for (UInputMappingContext* CurrentContext : MobileExcludedMappingContexts)
{
Subsystem->AddMappingContext(CurrentContext, 0);
}
}
}
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(InputComponent))
{
if (MenuAction)
{
EnhancedInputComponent->BindAction(MenuAction, ETriggerEvent::Started, this, &AKingshearthLegacyPlayerController::ToggleInGameMenu);
}
if (InventoryAction)
{
EnhancedInputComponent->BindAction(InventoryAction, ETriggerEvent::Started, this, &AKingshearthLegacyPlayerController::ToggleInventory);
}
}
}
}
void AKingshearthLegacyPlayerController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
if (!IsLocalPlayerController() || !PlayerHUDClass || !InPawn)
{
return;
}
if (!PlayerHUD)
{
PlayerHUD = CreateWidget<UPlayerHUDWidget>(this, PlayerHUDClass);
if (PlayerHUD)
{
PlayerHUD->AddToViewport();
}
else
{
UE_LOG(LogKingshearthLegacy, Error, TEXT("Could not spawn player HUD widget."));
}
}
if (PlayerHUD)
{
const AKingshearthLegacyCharacter* PossessedCharacter = Cast<AKingshearthLegacyCharacter>(InPawn);
UHealthAttributeSet* PossessedHealthAttributeSet = PossessedCharacter ? PossessedCharacter->GetHealthAttributeSet() : nullptr;
UStaminaAttributeSet* PossessedStaminaAttributeSet = PossessedCharacter ? PossessedCharacter->GetStaminaAttributeSet() : nullptr;
UInteractionComponent* PossessedInteractionComponent = PossessedCharacter ? PossessedCharacter->GetInteractionComponent() : nullptr;
PlayerHUD->InitializeHUD(PossessedHealthAttributeSet, PossessedStaminaAttributeSet, PossessedInteractionComponent);
}
}
bool AKingshearthLegacyPlayerController::ShouldUseTouchControls() const
{
// are we on a mobile platform? Should we force touch?
return SVirtualJoystick::ShouldDisplayTouchInterface() || bForceTouchControls;
}
void AKingshearthLegacyPlayerController::ToggleInGameMenu()
{
if (!IsLocalPlayerController() || !InGameMenuClass)
{
return;
}
UGameInstance* GI = GetGameInstance();
UMenuManagerSubsystem* MenuManager = GI ? GI->GetSubsystem<UMenuManagerSubsystem>() : nullptr;
if (!MenuManager)
{
return;
}
if (MenuManager->IsAnyScreenOpen())
{
MenuManager->PopScreen();
}
else
{
MenuManager->PushScreen(InGameMenuClass);
}
}
void AKingshearthLegacyPlayerController::ToggleInventory()
{
if (!IsLocalPlayerController() || !InventoryScreenClass)
{
return;
}
UGameInstance* GI = GetGameInstance();
UMenuManagerSubsystem* MenuManager = GI ? GI->GetSubsystem<UMenuManagerSubsystem>() : nullptr;
if (!MenuManager)
{
return;
}
if (MenuManager->IsAnyScreenOpen())
{
MenuManager->PopScreen();
}
else
{
MenuManager->PushScreen(InventoryScreenClass);
}
}