Files
ChildHood/Source/ChildHood/ChildHoodPlayerController.cpp
T
DESKTOP-2K5TJI4\admin 05b1e1bd95 config: 初始化UE5项目文件与C++源码
- ChildHood.uproject + Config/ 引擎配置
- Source/ 含第三人称模板、战斗/平台/横版变体
- .vsconfig Visual Studio依赖配置
2026-06-01 18:26:28 +08:00

68 lines
1.7 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "ChildHoodPlayerController.h"
#include "EnhancedInputSubsystems.h"
#include "Engine/LocalPlayer.h"
#include "InputMappingContext.h"
#include "Blueprint/UserWidget.h"
#include "ChildHood.h"
#include "Widgets/Input/SVirtualJoystick.h"
void AChildHoodPlayerController::BeginPlay()
{
Super::BeginPlay();
// only spawn touch controls on local player controllers
if (ShouldUseTouchControls() && IsLocalPlayerController())
{
// 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(LogChildHood, Error, TEXT("Could not spawn mobile controls widget."));
}
}
}
void AChildHoodPlayerController::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);
}
}
}
}
}
bool AChildHoodPlayerController::ShouldUseTouchControls() const
{
// are we on a mobile platform? Should we force touch?
return SVirtualJoystick::ShouldDisplayTouchInterface() || bForceTouchControls;
}