Resolution responsive UI, bug fixes, porting

This commit is contained in:
Oleg Petruny 2024-05-05 11:12:31 +02:00
parent b63299c50d
commit 9a2b717742
19 changed files with 216 additions and 36 deletions

View File

@ -61,8 +61,8 @@
bAltEnterTogglesFullscreen=True
bF11TogglesFullscreen=True
bUseMouseForTouch=False
bEnableMouseSmoothing=True
bEnableFOVScaling=True
bEnableMouseSmoothing=False
bEnableFOVScaling=False
bCaptureMouseOnLaunch=True
bEnableLegacyInputScales=True
bEnableMotionControls=True

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -9,7 +9,10 @@
"Type": "Runtime",
"LoadingPhase": "Default",
"AdditionalDependencies": [
"Engine"
"Engine",
"Slate",
"SlateCore",
"UMG"
]
}
],

View File

@ -6,16 +6,15 @@ public class Lost_Edge : ModuleRules {
public Lost_Edge(ReadOnlyTargetRules Target) : base(Target) {
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput" });
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
PrivateDependencyModuleNames.AddRange(new string[] { "EnhancedInput", "UMG" }); //"Slate", "SlateCore"
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
// GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, FString::Printf(TEXT("Rescaled: %f"), scale));
}
}

View File

@ -38,9 +38,9 @@ void APlayerBase::BeginPlay()
if(inputMapping)
{
if(auto player = Cast<APlayerController>(GetController()))
if(auto PC = Cast<APlayerController>(GetController()))
{
if(auto inputSubsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(player->GetLocalPlayer()))
if(auto inputSubsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
{
inputSubsystem->ClearAllMappings();
inputSubsystem->AddMappingContext(inputMapping.LoadSynchronous(), 0);

View File

@ -36,9 +36,9 @@ protected:
UPROPERTY(EditDefaultsOnly)
TSoftObjectPtr<UInputMappingContext> inputMapping;
UPROPERTY(EditDefaultsOnly)
UPROPERTY(EditAnywhere)
float moveSpeed = 200;
UPROPERTY(EditDefaultsOnly)
UPROPERTY(EditAnywhere)
float runSpeedMultiplier = 4;
UPROPERTY(EditAnywhere)
@ -48,9 +48,9 @@ protected:
UPROPERTY(EditAnywhere)
bool runLocked = false;
UPROPERTY(EditDefaultsOnly)
UPROPERTY(EditAnywhere)
float minPitch = -80;
UPROPERTY(EditDefaultsOnly)
UPROPERTY(EditAnywhere)
float maxPitch = 65;
// not visible :(

View File

@ -35,9 +35,9 @@ void ACameraModeBase::BeginPlay()
if(inputMapping)
{
if(auto player = Cast<APlayerController>(GetController()))
if(auto PC = Cast<APlayerController>(GetController()))
{
if(auto inputSubsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(player->GetLocalPlayer()))
if(auto inputSubsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
{
inputSubsystem->ClearAllMappings();
inputSubsystem->AddMappingContext(inputMapping.LoadSynchronous(), 0);

View File

@ -8,7 +8,7 @@
void UCustomGameInstanceBase::Init()
{
//Super::Init();
UGameInstance::Init();
ApplyMouseSettings();
}
@ -32,7 +32,7 @@ void UCustomGameInstanceBase::ApplyMouseSettings()
}
if(auto scalar_modifier = Cast<UInputModifierScalar>(*modifiers_it))
{
scalar_modifier->Scalar = FVector{ gameSettings->GetMouseSensetivity() };
scalar_modifier->Scalar = FVector{ gameSettings->GetMouseSensetivity() * 0.5 };
}
}
}

View File

@ -21,7 +21,7 @@ UCustomGameUserSettings* UCustomGameUserSettings::GetCustomGameUserSettings()
void UCustomGameUserSettings::SetMouseSensetivity(float value)
{
fMouseSensetivity = FMath::Clamp(value, 0.1f, 1.0f);
fMouseSensetivity = FMath::Clamp(value, 0.1f, 2.0f);
}
float UCustomGameUserSettings::GetMouseSensetivity() const

View File

@ -17,11 +17,11 @@ public:
static UCustomGameUserSettings* GetCustomGameUserSettings();
// Sets mouse sensetivity multiplier
// @param value [0.1 - 1.0]
// @param value [0.1 - 2.0]
UFUNCTION(BlueprintCallable, Category = Settings)
void SetMouseSensetivity(float value);
// Returns mouse sensetivity multiplier in [0.1 - 1.0]
// Returns mouse sensetivity multiplier in [0.1 - 2.0]
UFUNCTION(BlueprintCallable, Category = Settings)
float GetMouseSensetivity() const;

View File

@ -7,35 +7,119 @@
#include "GameFramework/SpectatorPawn.h"
#include "Kismet/GameplayStatics.h"
void AMainGameModeBase::StartPlay()
{
AGameModeBase::StartPlay();
InstantiateOverlayWidgets();
}
bool AMainGameModeBase::SetPause(APlayerController* PC, FCanUnpause CanUnpauseDelegate)
{
if(IsPaused())
{
HideOverlayWidgets();
}
else
{
ShowOverlayWidgets();
}
return AGameModeBase::SetPause(PC, CanUnpauseDelegate);
}
void AMainGameModeBase::SwitchCameraMode()
{
if(!_player_pawn)
static TWeakObjectPtr<APawn> _playerPawn = nullptr;
if(auto PC = UGameplayStatics::GetPlayerController(GetWorld(), 0))
{
if(auto controller = UGameplayStatics::GetPlayerController(GetWorld(), 0))
if(auto pawn = PC->GetPawn())
{
if(auto pawn = controller->GetPawn())
if(_playerPawn.IsValid())
{
auto spawnLoc = pawn->GetActorLocation();
auto spawnRot = pawn->GetActorRotation();
if(auto cameraPawn = GetWorld()->SpawnActor<APawn>(SpectatorClass, spawnLoc, spawnRot))
{
_player_pawn = pawn;
controller->Possess(cameraPawn);
cameraPawn->BeginPlay();
}
}
_playerPawn = pawn;
PC->Possess(cameraPawn);
}
}
else
{
if(auto controller = UGameplayStatics::GetPlayerController(GetWorld(), 0))
{
if(auto pawn = controller->GetPawn())
{
controller->Possess(_player_pawn);
PC->Possess(_playerPawn.Get());
pawn->Destroy();
_playerPawn = nullptr;
}
}
}
UpdateOverlayWidgetsOwner();
}
void AMainGameModeBase::InstantiateOverlayWidgets()
{
if(auto PC = UGameplayStatics::GetPlayerController(GetWorld(), 0))
{
for(auto widget : permaOverlayWidgets)
{
if(auto instance = CreateWidget<UUserWidget>(PC, widget))
{
permaOverlayWidgetsInstances.Add(instance);
instance->AddToViewport();
}
}
for(auto widget : overlayWidgets)
{
if(auto instance = CreateWidget<UUserWidget>(PC, widget))
{
overlayWidgetsInstances.Add(instance);
instance->AddToViewport();
}
}
}
}
void AMainGameModeBase::HideOverlayWidgets()
{
for(auto widget : overlayWidgetsInstances)
{
if(widget.IsValid())
{
widget->SetVisibility(ESlateVisibility::Hidden);
}
}
}
void AMainGameModeBase::ShowOverlayWidgets()
{
for(auto widget : overlayWidgetsInstances)
{
if(widget.IsValid())
{
widget->SetVisibility(ESlateVisibility::Visible);
}
}
}
void AMainGameModeBase::UpdateOverlayWidgetsOwner()
{
if(auto PC = UGameplayStatics::GetPlayerController(GetWorld(), 0))
{
for(auto widget : permaOverlayWidgetsInstances)
{
if(widget.IsValid())
{
widget->SetOwningPlayer(PC);
}
}
for(auto widget : overlayWidgetsInstances)
{
if(widget.IsValid())
{
widget->SetOwningPlayer(PC);
}
}
}
}

View File

@ -2,6 +2,7 @@
#pragma once
#include "Blueprint/UserWidget.h"
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "GameFramework/Pawn.h"
@ -14,10 +15,26 @@ class AMainGameModeBase : public AGameModeBase
GENERATED_BODY()
public:
virtual void StartPlay() override;
virtual bool SetPause(APlayerController* PC, FCanUnpause CanUnpauseDelegate = FCanUnpause()) override;
void SwitchCameraMode();
protected:
UPROPERTY(EditDefaultsOnly)
TArray<TSubclassOf<UUserWidget>> permaOverlayWidgets; // never hidden
TArray<TWeakObjectPtr<UUserWidget>> permaOverlayWidgetsInstances;
UPROPERTY(EditDefaultsOnly)
TArray<TSubclassOf<UUserWidget>> overlayWidgets; // hidden in pause
TArray<TWeakObjectPtr<UUserWidget>> overlayWidgetsInstances;
private:
APawn* _player_pawn = nullptr;
inline void InstantiateOverlayWidgets();
void HideOverlayWidgets();
void ShowOverlayWidgets();
void UpdateOverlayWidgetsOwner();
};

View File

@ -0,0 +1,40 @@
// Oleg Petruny proprietary.
#include "ResolutionResponsiveUserWidget.h"
#include "Components/PanelSlot.h"
#include "UnrealClient.h"
bool UResolutionResponsiveUserWidget::Initialize()
{
_viewportResizedEventHandle = FViewport::ViewportResizedEvent.AddUObject(this, &UResolutionResponsiveUserWidget::Rescale);
return UUserWidget::Initialize();
}
void UResolutionResponsiveUserWidget::BeginDestroy()
{
if(_viewportResizedEventHandle.IsValid())
{
FViewport::ViewportResizedEvent.Remove(_viewportResizedEventHandle);
}
UUserWidget::BeginDestroy();
}
void UResolutionResponsiveUserWidget::Rescale(FViewport* ViewPort, uint32 val)
{
_scale = (float)defaultResolution / ViewPort->GetSizeXY().GetMin();
Rescale();
}
void UResolutionResponsiveUserWidget::Rescale_Implementation()
{
SetRenderScale(FVector2D{ _scale });
}
float UResolutionResponsiveUserWidget::GetScale() const
{
return _scale;
}

View File

@ -0,0 +1,37 @@
// Oleg Petruny proprietary.
#pragma once
#include "Blueprint/UserWidget.h"
#include "CoreMinimal.h"
#include "ResolutionResponsiveUserWidget.generated.h"
UCLASS()
class UResolutionResponsiveUserWidget : public UUserWidget
{
GENERATED_BODY()
public:
//UResolutionResponsiveUserWidget(const FObjectInitializer& ObjectInitializer);
virtual bool Initialize() override;
virtual void BeginDestroy() override;
protected:
void Rescale(FViewport* ViewPort, uint32 val);
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = ResolutionResponsiveWidget)
void Rescale();
virtual void Rescale_Implementation();
UPROPERTY(EditDefaultsOnly)
int32 defaultResolution = 1080;
UFUNCTION(BlueprintCallable, Category = ResolutionResponsiveWidget)
float GetScale() const;
private:
float _scale;
FDelegateHandle _viewportResizedEventHandle;
};