Added option for DPI scaling. Current implementation scales the widget in Slate and invert-scales the ImGui canvas size to maintain consistent size on the screen.

This commit is contained in:
Sebastian 2020-05-11 11:24:59 +01:00
parent 253eff0525
commit 8de65f3d34
8 changed files with 88 additions and 8 deletions

View File

@ -5,9 +5,11 @@ Versions marked as 'unofficial' are labelled only for the needs of this changelo
Change History Change History
-------------- --------------
Version: 1.19 WIP (2020/03-04) Version: 1.19 (2020/03-04)
- Integrated fix for issue with ImGui popup/modal windows not being able to be closed in transparent mouse input mode. - Integrated fix for issue with ImGui popup/modal windows not being able to be closed in transparent mouse input mode.
- Integrated first version of Adaptive Canvas Size. - Integrated first version of Adaptive Canvas Size.
- Added different options to define canvas size, with Adaptive Canvas Size being one of the options (viewport).
- Added option for DPI scaling. Current implementation scales the whole Slate widget and invert-scales the ImGui canvas size to maintain consistent size on the screen.
Version: 1.18 (2020/01) Version: 1.18 (2020/01)
- Updated to engine version 4.24. - Updated to engine version 4.24.

View File

@ -9,7 +9,7 @@ Dear ImGui is an immediate-mode graphical user interface library that is very li
Status Status
------ ------
Version: 1.19 WIP Version: 1.19
ImGui version: 1.74 ImGui version: 1.74
@ -236,7 +236,7 @@ There is a self-debug functionality build into this plugin. This is hidden by de
- `ImGui.Debug.Input` - Show debug for input state. - `ImGui.Debug.Input` - Show debug for input state.
### Settings ### Settings
Plugin settings can be found in *Project Settings/Plugins/ImGui* panel. Right now they amount to a few properties that customize input handling. Plugin settings can be found in *Project Settings/Plugins/ImGui* panel. There is a bunch of properties allowing to tweak input handling, keyboard shortcuts (one for now), canvas size and DPI scale.
##### Extensions ##### Extensions
- `ImGui Input Handler Class` - Path to own implementation of ImGui Input Handler that allows limited customization of the input handling. If not set, then the default implementation is used. - `ImGui Input Handler Class` - Path to own implementation of ImGui Input Handler that allows limited customization of the input handling. If not set, then the default implementation is used.

View File

@ -104,6 +104,7 @@ void FImGuiModuleSettings::UpdateSettings()
SetUseSoftwareCursor(SettingsObject->bUseSoftwareCursor); SetUseSoftwareCursor(SettingsObject->bUseSoftwareCursor);
SetToggleInputKey(SettingsObject->ToggleInput); SetToggleInputKey(SettingsObject->ToggleInput);
SetCanvasSizeInfo(SettingsObject->CanvasSize); SetCanvasSizeInfo(SettingsObject->CanvasSize);
SetDPIScale(SettingsObject->DPIScale);
} }
} }
@ -170,6 +171,15 @@ void FImGuiModuleSettings::SetCanvasSizeInfo(const FImGuiCanvasSizeInfo& CanvasS
} }
} }
void FImGuiModuleSettings::SetDPIScale(float Scale)
{
if (DPIScale != Scale)
{
DPIScale = Scale;
OnDPIScaleChangeDelegate.Broadcast(DPIScale);
}
}
#if WITH_EDITOR #if WITH_EDITOR
void FImGuiModuleSettings::OnPropertyChanged(class UObject* ObjectBeingModified, struct FPropertyChangedEvent& PropertyChangedEvent) void FImGuiModuleSettings::OnPropertyChanged(class UObject* ObjectBeingModified, struct FPropertyChangedEvent& PropertyChangedEvent)

View File

@ -148,6 +148,14 @@ protected:
UPROPERTY(EditAnywhere, config, Category = "Canvas Size") UPROPERTY(EditAnywhere, config, Category = "Canvas Size")
FImGuiCanvasSizeInfo CanvasSize; FImGuiCanvasSizeInfo CanvasSize;
// DPI scale for the ImGui widgets.
//
// Note that when this scale is other than 1.0, canvas size will be scaled before it is passed to the ImGui.
// It will be scaled to keep the same screen size as defined by the Canvas Size property. If the default
// canvas size is 3840x2160 and the DPI scale is 2.0, the size passed to the ImGui will be 1920x1080.
UPROPERTY(EditAnywhere, Category = "DPI Scale", meta = (ClampMin = 0, UIMin = 0))
float DPIScale = 1.f;
// Deprecated name for ToggleInput. Kept temporarily to automatically move old configuration. // Deprecated name for ToggleInput. Kept temporarily to automatically move old configuration.
UPROPERTY(config) UPROPERTY(config)
FImGuiKeyInfo SwitchInputModeKey_DEPRECATED; FImGuiKeyInfo SwitchInputModeKey_DEPRECATED;
@ -170,6 +178,7 @@ public:
// Generic delegate used to notify changes of boolean properties. // Generic delegate used to notify changes of boolean properties.
DECLARE_MULTICAST_DELEGATE_OneParam(FBoolChangeDelegate, bool); DECLARE_MULTICAST_DELEGATE_OneParam(FBoolChangeDelegate, bool);
DECLARE_MULTICAST_DELEGATE_OneParam(FFloatChangeDelegate, float);
DECLARE_MULTICAST_DELEGATE_OneParam(FStringClassReferenceChangeDelegate, const FStringClassReference&); DECLARE_MULTICAST_DELEGATE_OneParam(FStringClassReferenceChangeDelegate, const FStringClassReference&);
DECLARE_MULTICAST_DELEGATE_OneParam(FImGuiCanvasSizeInfoChangeDelegate, const FImGuiCanvasSizeInfo&); DECLARE_MULTICAST_DELEGATE_OneParam(FImGuiCanvasSizeInfoChangeDelegate, const FImGuiCanvasSizeInfo&);
@ -197,6 +206,9 @@ public:
// Get the information how to calculate the canvas size. // Get the information how to calculate the canvas size.
const FImGuiCanvasSizeInfo& GetCanvasSizeInfo() const { return CanvasSize; } const FImGuiCanvasSizeInfo& GetCanvasSizeInfo() const { return CanvasSize; }
// Get the DPI Scale.
float GetDPIScale() const { return DPIScale; }
// Delegate raised when ImGui Input Handle is changed. // Delegate raised when ImGui Input Handle is changed.
FStringClassReferenceChangeDelegate OnImGuiInputHandlerClassChanged; FStringClassReferenceChangeDelegate OnImGuiInputHandlerClassChanged;
@ -206,6 +218,9 @@ public:
// Delegate raised when information how to calculate the canvas size is changed. // Delegate raised when information how to calculate the canvas size is changed.
FImGuiCanvasSizeInfoChangeDelegate OnCanvasSizeInfoChangeDelegate; FImGuiCanvasSizeInfoChangeDelegate OnCanvasSizeInfoChangeDelegate;
// Delegate raised when the DPI scale is changed.
FFloatChangeDelegate OnDPIScaleChangeDelegate;
private: private:
void UpdateSettings(); void UpdateSettings();
@ -217,6 +232,7 @@ private:
void SetUseSoftwareCursor(bool bUse); void SetUseSoftwareCursor(bool bUse);
void SetToggleInputKey(const FImGuiKeyInfo& KeyInfo); void SetToggleInputKey(const FImGuiKeyInfo& KeyInfo);
void SetCanvasSizeInfo(const FImGuiCanvasSizeInfo& CanvasSizeInfo); void SetCanvasSizeInfo(const FImGuiCanvasSizeInfo& CanvasSizeInfo);
void SetDPIScale(float DPIScale);
#if WITH_EDITOR #if WITH_EDITOR
void OnPropertyChanged(class UObject* ObjectBeingModified, struct FPropertyChangedEvent& PropertyChangedEvent); void OnPropertyChanged(class UObject* ObjectBeingModified, struct FPropertyChangedEvent& PropertyChangedEvent);
@ -228,6 +244,7 @@ private:
FStringClassReference ImGuiInputHandlerClass; FStringClassReference ImGuiInputHandlerClass;
FImGuiKeyInfo ToggleInputKey; FImGuiKeyInfo ToggleInputKey;
FImGuiCanvasSizeInfo CanvasSize; FImGuiCanvasSizeInfo CanvasSize;
float DPIScale = 1.f;
bool bShareKeyboardInput = false; bool bShareKeyboardInput = false;
bool bShareGamepadInput = false; bool bShareGamepadInput = false;
bool bShareMouseInput = false; bool bShareMouseInput = false;

View File

@ -5,6 +5,8 @@
#include "SImGuiLayout.h" #include "SImGuiLayout.h"
#include "SImGuiWidget.h" #include "SImGuiWidget.h"
#include "ImGuiModuleManager.h"
#include <Widgets/Layout/SConstraintCanvas.h> #include <Widgets/Layout/SConstraintCanvas.h>
#include <Widgets/Layout/SDPIScaler.h> #include <Widgets/Layout/SDPIScaler.h>
#include <Widgets/Layout/SScaleBox.h> #include <Widgets/Layout/SScaleBox.h>
@ -15,9 +17,18 @@ void SImGuiLayout::Construct(const FArguments& InArgs)
{ {
checkf(InArgs._GameViewport, TEXT("Null Game Viewport argument")); checkf(InArgs._GameViewport, TEXT("Null Game Viewport argument"));
ModuleManager = InArgs._ModuleManager;
GameViewport = InArgs._GameViewport; GameViewport = InArgs._GameViewport;
// TODO: Remove instantiation of ImGui Widget outside of this class. if (ModuleManager)
{
auto& Settings = ModuleManager->GetSettings();
SetDPIScale(Settings.GetDPIScale());
if (!Settings.OnDPIScaleChangeDelegate.IsBoundToObject(this))
{
Settings.OnDPIScaleChangeDelegate.AddRaw(this, &SImGuiLayout::SetDPIScale);
}
}
ChildSlot ChildSlot
[ [
@ -28,10 +39,9 @@ void SImGuiLayout::Construct(const FArguments& InArgs)
.VAlign(VAlign_Fill) .VAlign(VAlign_Fill)
.Visibility(EVisibility::SelfHitTestInvisible) .Visibility(EVisibility::SelfHitTestInvisible)
[ [
// Apply custom scale if necessary. // Apply custom scale if needed.
// TODO: Bind to relevant parameter.
SNew(SDPIScaler) SNew(SDPIScaler)
.DPIScale(1.f) .DPIScale(TAttribute<float>(this, &SImGuiLayout::GetDPIScale))
.Visibility(EVisibility::SelfHitTestInvisible) .Visibility(EVisibility::SelfHitTestInvisible)
[ [
SNew(SConstraintCanvas) SNew(SConstraintCanvas)
@ -56,4 +66,13 @@ void SImGuiLayout::Construct(const FArguments& InArgs)
SetVisibility(EVisibility::SelfHitTestInvisible); SetVisibility(EVisibility::SelfHitTestInvisible);
} }
SImGuiLayout::~SImGuiLayout()
{
if (ModuleManager)
{
ModuleManager->GetSettings().OnDPIScaleChangeDelegate.RemoveAll(this);
}
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION END_SLATE_FUNCTION_BUILD_OPTIMIZATION

View File

@ -24,9 +24,18 @@ public:
void Construct(const FArguments& InArgs); void Construct(const FArguments& InArgs);
~SImGuiLayout();
const TWeakObjectPtr<UGameViewportClient>& GetGameViewport() const { return GameViewport; } const TWeakObjectPtr<UGameViewportClient>& GetGameViewport() const { return GameViewport; }
private: private:
float GetDPIScale() const { return DPIScale; }
void SetDPIScale(float Scale) { DPIScale = Scale; }
FImGuiModuleManager* ModuleManager = nullptr;
TWeakObjectPtr<UGameViewportClient> GameViewport; TWeakObjectPtr<UGameViewportClient> GameViewport;
float DPIScale = 1.f;
}; };

View File

@ -84,6 +84,7 @@ void SImGuiWidget::Construct(const FArguments& InArgs)
const auto& Settings = ModuleManager->GetSettings(); const auto& Settings = ModuleManager->GetSettings();
SetHideMouseCursor(Settings.UseSoftwareCursor()); SetHideMouseCursor(Settings.UseSoftwareCursor());
CreateInputHandler(Settings.GetImGuiInputHandlerClass()); CreateInputHandler(Settings.GetImGuiInputHandlerClass());
SetDPIScale(Settings.GetDPIScale());
SetCanvasSizeInfo(Settings.GetCanvasSizeInfo()); SetCanvasSizeInfo(Settings.GetCanvasSizeInfo());
// Initialize state. // Initialize state.
@ -283,6 +284,10 @@ void SImGuiWidget::RegisterImGuiSettingsDelegates()
{ {
Settings.OnUseSoftwareCursorChanged.AddRaw(this, &SImGuiWidget::SetHideMouseCursor); Settings.OnUseSoftwareCursorChanged.AddRaw(this, &SImGuiWidget::SetHideMouseCursor);
} }
if (!Settings.OnDPIScaleChangeDelegate.IsBoundToObject(this))
{
Settings.OnDPIScaleChangeDelegate.AddRaw(this, &SImGuiWidget::SetDPIScale);
}
if (!Settings.OnCanvasSizeInfoChangeDelegate.IsBoundToObject(this)) if (!Settings.OnCanvasSizeInfoChangeDelegate.IsBoundToObject(this))
{ {
Settings.OnCanvasSizeInfoChangeDelegate.AddRaw(this, &SImGuiWidget::SetCanvasSizeInfo); Settings.OnCanvasSizeInfoChangeDelegate.AddRaw(this, &SImGuiWidget::SetCanvasSizeInfo);
@ -295,6 +300,7 @@ void SImGuiWidget::UnregisterImGuiSettingsDelegates()
Settings.OnImGuiInputHandlerClassChanged.RemoveAll(this); Settings.OnImGuiInputHandlerClassChanged.RemoveAll(this);
Settings.OnUseSoftwareCursorChanged.RemoveAll(this); Settings.OnUseSoftwareCursorChanged.RemoveAll(this);
Settings.OnDPIScaleChangeDelegate.RemoveAll(this);
Settings.OnCanvasSizeInfoChangeDelegate.RemoveAll(this); Settings.OnCanvasSizeInfoChangeDelegate.RemoveAll(this);
} }
@ -503,6 +509,15 @@ void SImGuiWidget::HandleWindowFocusLost()
} }
} }
void SImGuiWidget::SetDPIScale(float Scale)
{
if (DPIScale != Scale)
{
DPIScale = Scale;
bUpdateCanvasSize = true;
}
}
void SImGuiWidget::SetCanvasSizeInfo(const FImGuiCanvasSizeInfo& CanvasSizeInfo) void SImGuiWidget::SetCanvasSizeInfo(const FImGuiCanvasSizeInfo& CanvasSizeInfo)
{ {
switch (CanvasSizeInfo.SizeType) switch (CanvasSizeInfo.SizeType)
@ -535,7 +550,6 @@ void SImGuiWidget::SetCanvasSizeInfo(const FImGuiCanvasSizeInfo& CanvasSizeInfo)
} }
bUpdateCanvasSize = true; bUpdateCanvasSize = true;
UpdateCanvasSize();
} }
void SImGuiWidget::UpdateCanvasSize() void SImGuiWidget::UpdateCanvasSize()
@ -557,6 +571,10 @@ void SImGuiWidget::UpdateCanvasSize()
bUpdateCanvasSize = false; bUpdateCanvasSize = false;
} }
// Clamping DPI Scale to keep the canvas size from getting too big.
CanvasSize /= FMath::Max(DPIScale, 0.01f);
CanvasSize = CanvasSize.RoundToVector();
ContextProxy->SetDisplaySize(CanvasSize); ContextProxy->SetDisplaySize(CanvasSize);
} }
} }
@ -820,6 +838,7 @@ void SImGuiWidget::OnDebugDraw()
TwoColumns::Value("Is Updating", bUpdateCanvasSize); TwoColumns::Value("Is Updating", bUpdateCanvasSize);
TwoColumns::ValueWidthHeight("Min Canvas Size", MinCanvasSize); TwoColumns::ValueWidthHeight("Min Canvas Size", MinCanvasSize);
TwoColumns::ValueWidthHeight("Canvas Size", CanvasSize); TwoColumns::ValueWidthHeight("Canvas Size", CanvasSize);
TwoColumns::Value("DPI Scale", DPIScale);
}); });
TwoColumns::CollapsingGroup("Context", [&]() TwoColumns::CollapsingGroup("Context", [&]()

View File

@ -103,6 +103,8 @@ private:
void UpdateTransparentMouseInput(const FGeometry& AllottedGeometry); void UpdateTransparentMouseInput(const FGeometry& AllottedGeometry);
void HandleWindowFocusLost(); void HandleWindowFocusLost();
void SetDPIScale(float Scale);
void SetCanvasSizeInfo(const FImGuiCanvasSizeInfo& CanvasSizeInfo); void SetCanvasSizeInfo(const FImGuiCanvasSizeInfo& CanvasSizeInfo);
void UpdateCanvasSize(); void UpdateCanvasSize();
@ -137,6 +139,8 @@ private:
FVector2D MinCanvasSize = FVector2D::ZeroVector; FVector2D MinCanvasSize = FVector2D::ZeroVector;
FVector2D CanvasSize = FVector2D::ZeroVector; FVector2D CanvasSize = FVector2D::ZeroVector;
float DPIScale = 1.f;
bool bInputEnabled = false; bool bInputEnabled = false;
bool bForegroundWindow = false; bool bForegroundWindow = false;
bool bHideMouseCursor = true; bool bHideMouseCursor = true;