diff --git a/Source/ImGui/Private/ImGuiContextProxy.cpp b/Source/ImGui/Private/ImGuiContextProxy.cpp index 57ebd98..437755b 100644 --- a/Source/ImGui/Private/ImGuiContextProxy.cpp +++ b/Source/ImGui/Private/ImGuiContextProxy.cpp @@ -139,13 +139,17 @@ void FImGuiContextProxy::Tick(float DeltaSeconds) bHasActiveItem = ImGui::IsAnyItemActive(); bIsMouseHoveringAnyWindow = ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow); MouseCursor = ImGuiInterops::ToSlateMouseCursor(ImGui::GetMouseCursor()); - DisplaySize = ImGuiInterops::ToVector2D(ImGui::GetIO().DisplaySize); // Begin a new frame and set the context back to a state in which it allows to draw controls. BeginFrame(DeltaSeconds); } } +void FImGuiContextProxy::SetAdaptiveCanvasSize(bool bAdaptive) +{ + bAdaptiveCanvasSize = bAdaptive; +} + void FImGuiContextProxy::BeginFrame(float DeltaTime) { if (!bIsFrameStarted) @@ -156,6 +160,17 @@ void FImGuiContextProxy::BeginFrame(float DeltaTime) ImGuiInterops::CopyInput(IO, InputState); InputState.ClearUpdateState(); + if (bAdaptiveCanvasSize) { + if (GEngine && GEngine->GameViewport) + { + GEngine->GameViewport->GetViewportSize(DisplaySize); + IO.DisplaySize = ImVec2(DisplaySize.X, DisplaySize.Y); + } + } else { + IO.DisplaySize = { DEFAULT_CANVAS_WIDTH, DEFAULT_CANVAS_HEIGHT }; + DisplaySize = ImGuiInterops::ToVector2D(IO.DisplaySize); + } + ImGui::NewFrame(); bWantsMouseCapture = IO.WantCaptureMouse; diff --git a/Source/ImGui/Private/ImGuiContextProxy.h b/Source/ImGui/Private/ImGuiContextProxy.h index 3bcdf0f..78f17cb 100644 --- a/Source/ImGui/Private/ImGuiContextProxy.h +++ b/Source/ImGui/Private/ImGuiContextProxy.h @@ -71,6 +71,9 @@ public: // Tick to advance context to the next frame. Only one call per frame will be processed. void Tick(float DeltaSeconds); + // Set the adaptive canvas size configuration. + void SetAdaptiveCanvasSize(bool); + private: void BeginFrame(float DeltaTime = 1.f / 60.f); @@ -111,4 +114,6 @@ private: FSimpleMulticastDelegate* SharedDrawEvent = nullptr; std::string IniFilename; + + bool bAdaptiveCanvasSize = false; }; diff --git a/Source/ImGui/Private/ImGuiModuleManager.cpp b/Source/ImGui/Private/ImGuiModuleManager.cpp index 87e234b..e6a28a3 100644 --- a/Source/ImGui/Private/ImGuiModuleManager.cpp +++ b/Source/ImGui/Private/ImGuiModuleManager.cpp @@ -175,6 +175,7 @@ void FImGuiModuleManager::AddWidgetToViewport(UGameViewportClient* GameViewport) // Make sure that we have a context for this viewport's world and get its index. int32 ContextIndex; auto& ContextProxy = ContextManager.GetWorldContextProxy(*GameViewport->GetWorld(), ContextIndex); + ContextProxy.SetAdaptiveCanvasSize(Settings.AdaptiveCanvasSize()); // Make sure that textures are loaded before the first Slate widget is created. LoadTextures(); diff --git a/Source/ImGui/Private/ImGuiModuleSettings.cpp b/Source/ImGui/Private/ImGuiModuleSettings.cpp index d2a6b6f..8c91636 100644 --- a/Source/ImGui/Private/ImGuiModuleSettings.cpp +++ b/Source/ImGui/Private/ImGuiModuleSettings.cpp @@ -103,6 +103,7 @@ void FImGuiModuleSettings::UpdateSettings() SetShareMouseInput(SettingsObject->bShareMouseInput); SetUseSoftwareCursor(SettingsObject->bUseSoftwareCursor); SetToggleInputKey(SettingsObject->ToggleInput); + SetAdaptiveCanvasSize(SettingsObject->bAdaptiveCanvasSize); } } @@ -160,6 +161,14 @@ void FImGuiModuleSettings::SetToggleInputKey(const FImGuiKeyInfo& KeyInfo) } } +void FImGuiModuleSettings::SetAdaptiveCanvasSize(bool bAdaptive) +{ + if (bAdaptiveCanvasSize != bAdaptive) + { + bAdaptiveCanvasSize = bAdaptive; + } +} + #if WITH_EDITOR void FImGuiModuleSettings::OnPropertyChanged(class UObject* ObjectBeingModified, struct FPropertyChangedEvent& PropertyChangedEvent) diff --git a/Source/ImGui/Private/ImGuiModuleSettings.h b/Source/ImGui/Private/ImGuiModuleSettings.h index a58b8a5..d951e1d 100644 --- a/Source/ImGui/Private/ImGuiModuleSettings.h +++ b/Source/ImGui/Private/ImGuiModuleSettings.h @@ -103,6 +103,10 @@ protected: UPROPERTY(EditAnywhere, config, Category = "Keyboard Shortcuts") FImGuiKeyInfo ToggleInput; + // If true, the size of ImGui canvas will be adaptive to game viewport. + UPROPERTY(EditAnywhere, config, Category = "Canvas Size") + bool bAdaptiveCanvasSize = false; + // Deprecated name for ToggleInput. Kept temporarily to automatically move old configuration. UPROPERTY(config) FImGuiKeyInfo SwitchInputModeKey_DEPRECATED; @@ -148,6 +152,9 @@ public: // Get the shortcut configuration for 'ImGui.ToggleInput' command. const FImGuiKeyInfo& GetToggleInputKey() const { return ToggleInputKey; } + // Get the adaptive canvas size configuration. + bool AdaptiveCanvasSize() const { return bAdaptiveCanvasSize; } + // Delegate raised when ImGui Input Handle is changed. FStringClassReferenceChangeDelegate OnImGuiInputHandlerClassChanged; @@ -164,6 +171,7 @@ private: void SetShareMouseInput(bool bShare); void SetUseSoftwareCursor(bool bUse); void SetToggleInputKey(const FImGuiKeyInfo& KeyInfo); + void SetAdaptiveCanvasSize(bool bAdaptive); #if WITH_EDITOR void OnPropertyChanged(class UObject* ObjectBeingModified, struct FPropertyChangedEvent& PropertyChangedEvent); @@ -178,4 +186,5 @@ private: bool bShareGamepadInput = false; bool bShareMouseInput = false; bool bUseSoftwareCursor = false; + bool bAdaptiveCanvasSize = false; };