Acquire size of ImGUI canvas from game viewport (#27)

- WIP: Does not support multi-PIE. All instances get the same size.
- Setting to enable/disable adaptive size.
This commit is contained in:
kkawachi 2020-04-16 21:08:32 +09:00 committed by GitHub
parent 73cdfe8d83
commit 174e38cfc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 1 deletions

View File

@ -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;

View File

@ -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;
};

View File

@ -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();

View File

@ -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)

View File

@ -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;
};