graphics settings code

This commit is contained in:
Oleg Petruny 2025-01-09 18:17:39 +01:00
parent 07f34a7c6b
commit 509d2e9c98
7 changed files with 230 additions and 65 deletions

View File

@ -27,14 +27,6 @@ void ACameraModeBase::BeginPlay()
Super::BeginPlay();
auto world = GetWorld();
if(auto gameSettings = UCustomGameSettings::Get())
{
if(auto camera = FindComponentByClass<UCameraComponent>())
{
camera->PostProcessSettings.MotionBlurAmount = gameSettings->bUseMotionBlur ? 1.0f : 0.0f;
}
}
}
void ACameraModeBase::Tick(float DeltaTime)

View File

@ -0,0 +1,10 @@
// Oleg Petruny proprietary.
#pragma once
#include "CoreMinimal.h"
namespace CommonTexts
{
const FText Default = NSLOCTEXT("CommonTexts", "Default", "Default");
}

View File

@ -4,11 +4,12 @@
#include "Kismet/GameplayStatics.h"
#include "GraphicsSettingsHelper.h"
namespace
{
/** Game */
constexpr float fDefaultMouseSensetivity = 0.5f;
constexpr bool bDefaultMouseInverted = false;
/** Graphics */
constexpr bool bDefaultShowFps = false;
/** Audio */
constexpr float fDefaultMasterVolume = 0.4f;
@ -16,12 +17,17 @@ namespace
constexpr float fDefaultEffectsVolume = 1.0f;
constexpr float fDefaultVoicesVolume = 1.0f;
constexpr float fDefaultMenuVolume = 1.0f;
/** Game */
constexpr float fDefaultMouseSensetivity = 0.5f;
constexpr bool bDefaultMouseInverted = false;
}
UCustomGameSettings::UCustomGameSettings(const FObjectInitializer& ObjectInitializer) :Super(ObjectInitializer)
{
bUseMotionBlur = false;
bShowFps = false;
/** Graphics */
bShowFps = GetDefaultShowFps();
UGraphicsSettingsHelper::SetDefaults(this);
/** Audio */
fMasterVolume = GetDefaultMasterVolume();
@ -40,6 +46,11 @@ UCustomGameSettings* UCustomGameSettings::Get()
return Cast<UCustomGameSettings>(UGameUserSettings::GetGameUserSettings());
}
LOST_EDGE_API bool UCustomGameSettings::GetDefaultShowFps() const
{
return bDefaultShowFps;
}
/** Audio */

View File

@ -15,78 +15,78 @@ class UCustomGameSettings : public UGameUserSettings
{
GENERATED_BODY()
friend class UGraphicsSettingsHelper;
public:
// Is auto defined by UE but implementation is in cpp
UCustomGameSettings(const FObjectInitializer& ObjectInitializer);
UFUNCTION(BlueprintPure, Category = Settings, meta = (DisplayName = "Get Custom Game Settings"))
UFUNCTION(BlueprintPure, Category = "Settings", meta = (DisplayName = "Get Custom Game Settings"))
static UCustomGameSettings* Get();
/** Graphics */
UPROPERTY(Config, BlueprintReadWrite)
bool bUseMotionBlur;
UPROPERTY(Config, BlueprintReadWrite)
UFUNCTION(BlueprintPure, Category = "Settings|Graphics")
LOST_EDGE_API inline bool GetDefaultShowFps() const;
UPROPERTY(Config, BlueprintReadWrite, Category = "Settings|Graphics")
bool bShowFps;
/**
* Audio
* All values are clamped in [0.0 - 1.0]
*/
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Audio")
LOST_EDGE_API inline float GetDefaultMasterVolume() const; // UFUNCTION doesn't support constexpr functions
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Audio")
LOST_EDGE_API inline float GetDefaultMusicVolume() const; // UFUNCTION doesn't support constexpr functions
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Audio")
LOST_EDGE_API inline float GetDefaultEffectsVolume() const; // UFUNCTION doesn't support constexpr functions
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Audio")
LOST_EDGE_API inline float GetDefaultVoicesVolume() const; // UFUNCTION doesn't support constexpr functions
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Audio")
LOST_EDGE_API inline float GetDefaultMenuVolume() const; // UFUNCTION doesn't support constexpr functions
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Audio")
float GetMasterVolume() const;
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Audio")
float GetMusicVolume() const;
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Audio")
float GetEffectsVolume() const;
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Audio")
float GetVoicesVolume() const;
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Audio")
float GetMenuVolume() const;
UFUNCTION(BlueprintCallable, Category = Settings)
UFUNCTION(BlueprintCallable, Category = "Settings|Audio")
void SetMasterVolume(float value);
UFUNCTION(BlueprintCallable, Category = Settings)
UFUNCTION(BlueprintCallable, Category = "Settings|Audio")
void SetMusicVolume(float value);
UFUNCTION(BlueprintCallable, Category = Settings)
UFUNCTION(BlueprintCallable, Category = "Settings|Audio")
void SetEffectsVolume(float value);
UFUNCTION(BlueprintCallable, Category = Settings)
UFUNCTION(BlueprintCallable, Category = "Settings|Audio")
void SetVoicesVolume(float value);
UFUNCTION(BlueprintCallable, Category = Settings)
UFUNCTION(BlueprintCallable, Category = "Settings|Audio")
void SetMenuVolume(float value);
/** Game */
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Game")
LOST_EDGE_API inline float GetDefaultMouseSensetivity() const;
/** Returns mouse sensetivity multiplier in [0.1 - 2.0] */
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Game")
float GetMouseSensetivity() const;
/**
* Sets mouse sensetivity multiplier
* @param value [0.1 - 2.0]
*/
UFUNCTION(BlueprintCallable, Category = Settings)
UFUNCTION(BlueprintCallable, Category = "Settings|Game")
void SetMouseSensetivity(float value);
UFUNCTION(BlueprintPure, Category = Settings)
UFUNCTION(BlueprintPure, Category = "Settings|Game")
LOST_EDGE_API inline bool GetDefaultMouseInverted() const;
UPROPERTY(Config, BlueprintReadWrite)
UPROPERTY(Config, BlueprintReadWrite, Category = "Settings|Game")
bool bMouseInverted;

View File

@ -5,15 +5,44 @@
#include "GenericPlatform/GenericApplication.h"
#include "CommonFunctions.h"
#include "CommonTexts.h"
#include "CustomGameSettings.h"
#include <optional>
namespace
{
const FIntPoint ipDefaultWindowPosition = { 0, 0 };
std::optional<FIntPoint> ipPreviousWindowPosition;
constexpr int32 iDefaultMonitorId = 0;
const FIntPoint ipDefaultResolution = { 1920, 1080 };
std::optional<FIntPoint> ipPreviousResolution;
constexpr float fDefaultResolutionScale = 1.0f;
constexpr int32 iDefaultFrameRateLimit = 120;
constexpr EDisplayMode eDefaultDisplayMode = EDisplayMode::Fullscreen;
std::optional<EDisplayMode> ePreviousDisplayMode;
}
bool UGraphicsSettingsHelper::AreSettingsRestorable(UCustomGameSettings* settings)
{
return ipPreviousWindowPosition.has_value()
|| ipPreviousResolution.has_value()
|| ePreviousDisplayMode.has_value();
}
void UGraphicsSettingsHelper::SetDefaults(UCustomGameSettings* settings)
{
{
FIntPoint pos = GetDefaultWindowPosition(settings);
settings->WindowPosX = pos.X;
settings->WindowPosY = pos.Y;
}
{
FIntPoint res = GetDefaultResolution(settings);
settings->ResolutionSizeX = res.X;
settings->ResolutionSizeY = res.Y;
}
settings->FullscreenMode = DisplayToWindowMode(GetDefaultDisplayMode(settings));
}
FIntPoint UGraphicsSettingsHelper::GetDefaultWindowPosition([[maybe_unused]] UCustomGameSettings* settings)
@ -35,18 +64,29 @@ FIntPoint UGraphicsSettingsHelper::GetWindowPosition([[maybe_unused]] UCustomGam
return GetDefaultWindowPosition(settings);
}
void UGraphicsSettingsHelper::SetWindowPosition([[maybe_unused]] UCustomGameSettings* settings, const FIntPoint& pos)
void UGraphicsSettingsHelper::SetWindowPosition(UCustomGameSettings* settings, const FIntPoint& pos)
{
if(settings && GEngine && GEngine->GameViewport)
{
if(auto window = GEngine->GameViewport->GetWindow())
{
if(ipPreviousWindowPosition == pos)
ipPreviousWindowPosition.reset();
else
ipPreviousWindowPosition = GetWindowPosition(settings);
window->MoveWindowTo(pos);
settings->SetWindowPosition(pos.X, pos.Y);
}
}
}
void UGraphicsSettingsHelper::RestoreWindowPosition(UCustomGameSettings* settings)
{
if(!ipPreviousWindowPosition.has_value())
return;
SetWindowPosition(settings, ipPreviousWindowPosition.value());
}
int32 UGraphicsSettingsHelper::GetDefaultMonitorId([[maybe_unused]] UCustomGameSettings* settings)
@ -120,6 +160,11 @@ void UGraphicsSettingsHelper::SetMonitor(UCustomGameSettings* settings, const in
});
}
void UGraphicsSettingsHelper::RestoreMonitor(UCustomGameSettings* settings)
{
RestoreWindowPosition(settings);
}
FIntPoint UGraphicsSettingsHelper::GetDefaultResolution([[maybe_unused]] UCustomGameSettings* settings)
{
return ipDefaultResolution;
@ -223,9 +268,23 @@ void UGraphicsSettingsHelper::SetResolution(UCustomGameSettings* settings, const
return;
settings->bUseDynamicResolution = true;
if(ipPreviousResolution.has_value() && ipPreviousResolution == resolution)
ipPreviousResolution.reset();
else
ipPreviousResolution = resolution;
settings->SetScreenResolution(resolution);
}
void UGraphicsSettingsHelper::RestoreResolution(UCustomGameSettings* settings)
{
if(!ipPreviousResolution.has_value())
return;
SetResolution(settings, ipPreviousResolution.value());
}
float UGraphicsSettingsHelper::GetDefaultResolutionScale([[maybe_unused]] UCustomGameSettings* settings)
{
return fDefaultResolutionScale;
@ -295,3 +354,85 @@ TArray<FIntPoint> UGraphicsSettingsHelper::GetAvailableAspectRatiousOfMonitor(co
return aspects.Array();
}
EDisplayMode UGraphicsSettingsHelper::GetDefaultDisplayMode([[maybe_unused]] UCustomGameSettings* settings)
{
return eDefaultDisplayMode;
}
EDisplayMode UGraphicsSettingsHelper::GetDisplayMode(UCustomGameSettings* settings)
{
if(!settings)
return GetDefaultDisplayMode(settings);
return WindowToDisplayMode(settings->GetFullscreenMode());
}
EDisplayMode UGraphicsSettingsHelper::TextToDisplayMode(const FText& in)
{
static const UEnum* enumPtr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EDisplayMode"), true);
if(!enumPtr)
return GetDefaultDisplayMode(nullptr);
return static_cast<EDisplayMode>(enumPtr->GetValueByName(FName{ *FTextInspector::GetSourceString(in) }));
}
FText UGraphicsSettingsHelper::DisplayModeToText(const EDisplayMode mode)
{
static const UEnum* enumPtr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EDisplayMode"), true);
if(!enumPtr)
return CommonTexts::Default;
return enumPtr->GetDisplayNameTextByIndex(static_cast<int32>(mode));
}
void UGraphicsSettingsHelper::SetDisplayMode(UCustomGameSettings* settings, const EDisplayMode mode)
{
if(!settings)
return;
if(ePreviousDisplayMode.has_value() && ePreviousDisplayMode == mode)
ePreviousDisplayMode.reset();
else
ePreviousDisplayMode = mode;
settings->SetFullscreenMode(DisplayToWindowMode(mode));
}
void UGraphicsSettingsHelper::RestoreDisplayMode(UCustomGameSettings* settings)
{
if(!ePreviousDisplayMode.has_value())
return;
SetDisplayMode(settings, ePreviousDisplayMode.value());
}
inline constexpr EDisplayMode UGraphicsSettingsHelper::WindowToDisplayMode(const EWindowMode::Type mode)
{
switch(mode)
{
case EWindowMode::Type::WindowedFullscreen:
return EDisplayMode::Borderless;
case EWindowMode::Type::Windowed:
return EDisplayMode::Windowed;
case EWindowMode::Type::Fullscreen:
default:
return EDisplayMode::Fullscreen;
}
}
inline constexpr EWindowMode::Type UGraphicsSettingsHelper::DisplayToWindowMode(const EDisplayMode mode)
{
switch(mode)
{
case EDisplayMode::Borderless:
return EWindowMode::Type::WindowedFullscreen;
case EDisplayMode::Windowed:
return EWindowMode::Type::Windowed;
case EDisplayMode::Fullscreen:
default:
return EWindowMode::Type::Fullscreen;
}
}

View File

@ -18,19 +18,20 @@ public:
TArray<int32> refreshRates;
};
/** Redefine of EWindowType, because it has duplicate without blueprint macro */
UENUM(BlueprintType, Category = "Settings|Graphics|Enums")
enum class EDisplayMode : uint8
{
Fullscreen = 0,
Borderless = 1,
Windowed = 2
Fullscreen = 0 UMETA(DisplayName = "Fullscreen"),
Borderless = 1 UMETA(DisplayName = "Borderless"),
Windowed = 2 UMETA(DisplayName = "Windowed")
};
/**
* Helper for trivial and complex graphic settings.
* Most of functions work without settings pointer. It is there just for Blueprint call convinience.
* Helps with window position, monitor selection, resolution, resolution scale, frame rate limit, aspect ratio,
* display mode and gamma.
* Helps with window position, monitor selection, resolution, resolution scale, frame rate limit, aspect ratio, display mode.
*
*/
UCLASS()
class UGraphicsSettingsHelper : public UBlueprintFunctionLibrary
@ -38,13 +39,22 @@ class UGraphicsSettingsHelper : public UBlueprintFunctionLibrary
GENERATED_BODY()
public:
UFUNCTION(BlueprintPure, Category = "Settings|Graphics", meta = (DisplayName = "AreGraphicsSettingsRestorable"))
static bool AreSettingsRestorable(class UCustomGameSettings* settings);
UFUNCTION(BlueprintCallable, Category = "Settings|Graphics", meta = (DisplayName = "SetGraphicsDefaults"))
static void SetDefaults(class UCustomGameSettings* settings);
protected: // Window position setting is on the thin edge of user safeness, therefore protected. Please use SetMonitor().
UFUNCTION(BlueprintPure, Category = "Settings|Graphics|Window")
static FIntPoint GetDefaultWindowPosition(class UCustomGameSettings* settings); // inline functions need API macro, but API macro with static inline function results in "no definition"
UFUNCTION(BlueprintPure, Category = "Settings|Graphics|Window")
static FIntPoint GetWindowPosition(class UCustomGameSettings* settings);
UFUNCTION(BlueprintCallable, Category = "Settings|Graphics|Window")
static void SetWindowPosition(class UCustomGameSettings* settings, const FIntPoint& pos);
UFUNCTION(BlueprintCallable, Category = "Settings|Graphics|Window")
static void RestoreWindowPosition(class UCustomGameSettings* settings);
public:
UFUNCTION(BlueprintPure, Category = "Settings|Graphics|Monitor")
static int32 GetDefaultMonitorId(class UCustomGameSettings* settings);
UFUNCTION(BlueprintPure, Category = "Settings|Graphics|Monitor")
@ -56,6 +66,8 @@ public:
static TArray<FMonitorInfo> GetAvailableMonitors();
UFUNCTION(BlueprintCallable, Category = "Settings|Graphics|Monitor")
static void SetMonitor(class UCustomGameSettings* settings, const int32 id);
UFUNCTION(BlueprintCallable, Category = "Settings|Graphics|Monitor")
static void RestoreMonitor(class UCustomGameSettings* settings);
UFUNCTION(BlueprintPure, Category = "Settings|Graphics|Resolution")
static FIntPoint GetDefaultResolution(class UCustomGameSettings* settings);
@ -73,6 +85,8 @@ public:
static TArray<FIntPoint> FilterResolutionsViaAspectRatio(const TArray<FIntPoint>& resolutions, const FIntPoint& aspectRatio);
UFUNCTION(BlueprintCallable, Category = "Settings|Graphics|Resolution")
static void SetResolution(class UCustomGameSettings* settings, const FIntPoint& resolution);
UFUNCTION(BlueprintCallable, Category = "Settings|Graphics|Resolution")
static void RestoreResolution(class UCustomGameSettings* settings);
UFUNCTION(BlueprintPure, Category = "Settings|Graphics|Resolution Scale")
static float GetDefaultResolutionScale(class UCustomGameSettings* settings);
@ -97,17 +111,20 @@ public:
UFUNCTION(BlueprintPure, Category = "Settings|Graphics|Aspect Ratio")
static TArray<FIntPoint> GetAvailableAspectRatiousOfMonitor(const int32 monitor);
//UFUNCTION(BlueprintPure, Category = "Settings Video|Display Mode")
//static EDisplayMode StringToDisplayMode(FString in);
//UFUNCTION(BlueprintPure, Category = "Settings Video|Display Mode")
//static FString DisplayModeToString(EDisplayMode mode);
//UFUNCTION(BlueprintPure, Category = "Settings Video|Display Mode")
//static void IntToDisplayMode(int in, EDisplayMode& mode);
//UFUNCTION(BlueprintPure, Category = "Settings Video|Display Mode")
//static void DisplayModeToInt(EDisplayMode mode, int& out);
//UFUNCTION(BlueprintPure, Category = "Settings Video|Display Mode")
//static void GetCurrentDisplayMode(EDisplayMode& mode);
//UFUNCTION(BlueprintCallable, Category = "Settings Video|Display Mode")
//static void SetDisplayMode(EDisplayMode mode);
UFUNCTION(BlueprintPure, Category = "Settings|Graphics|Display Mode")
static EDisplayMode GetDefaultDisplayMode(class UCustomGameSettings* settings);
UFUNCTION(BlueprintPure, Category = "Settings|Graphics|Display Mode")
static EDisplayMode GetDisplayMode(class UCustomGameSettings* settings);
UFUNCTION(BlueprintPure, Category = "Settings|Graphics|Display Mode")
static EDisplayMode TextToDisplayMode(const FText& in);
UFUNCTION(BlueprintPure, Category = "Settings|Graphics|Display Mode")
static FText DisplayModeToText(const EDisplayMode mode);
UFUNCTION(BlueprintCallable, Category = "Settings|Graphics|Display Mode")
static void SetDisplayMode(class UCustomGameSettings* settings, const EDisplayMode mode);
UFUNCTION(BlueprintCallable, Category = "Settings|Graphics|Display Mode")
static void RestoreDisplayMode(class UCustomGameSettings* settings);
private:
inline constexpr static EDisplayMode WindowToDisplayMode(const EWindowMode::Type mode);
inline constexpr static EWindowMode::Type DisplayToWindowMode(const EDisplayMode mode);
};

View File

@ -85,12 +85,6 @@ void APlayerBase::BeginPlay()
cameraManager->ViewPitchMax = maxPitch;
}
auto gameSettings = UCustomGameSettings::Get();
if(gameSettings && camera)
{
camera->PostProcessSettings.MotionBlurAmount = gameSettings->bUseMotionBlur ? 1.0f : 0.0f;
}
LoadInteractablesActivators();
}