Files
FirstPersonDemo/Source/FirstPersonDemo/Surviver_FPS/Battle/SurviverPlayerController.cpp

74 lines
2.3 KiB
C++

// Copyright Majowaveon Games. All Rights Reserved.
#include "SurviverPlayerController.h"
#include "EnhancedInputSubsystems.h"
#include "Engine/LocalPlayer.h"
#include "InputMappingContext.h"
#include "FirstPersonDemoCameraManager.h"
#include "Blueprint/UserWidget.h"
#include "FirstPersonDemo.h"
#include "Widgets/Input/SVirtualJoystick.h"
#include "FirstPersonDemo/Surviver_FPS/UI/BattleHUD.h"
ASurviverPlayerController::ASurviverPlayerController() {
// set the player camera manager class
PlayerCameraManagerClass = AFirstPersonDemoCameraManager::StaticClass();
}
void ASurviverPlayerController::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(LogFirstPersonDemo, Error, TEXT("Could not spawn mobile controls widget."));
}
}
// 只在本地玩家控制器上创建 HUD
if (IsLocalPlayerController() && BattleHUDClass) {
BattleHUD = CreateWidget<UBattleHUD>(this, BattleHUDClass);
if (BattleHUD) {
BattleHUD->AddToViewport();
}
else {
UE_LOG(LogFirstPersonDemo, Error, TEXT("Could not create BattleHUD widget."));
}
}
}
void ASurviverPlayerController::SetupInputComponent() {
Super::SetupInputComponent();
// only add IMCs for local player controllers
if (IsLocalPlayerController()) {
// Add Input Mapping Context
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 ASurviverPlayerController::ShouldUseTouchControls() const {
// are we on a mobile platform? Should we force touch?
return SVirtualJoystick::ShouldDisplayTouchInterface() || bForceTouchControls;
}