Add DPI scaling to IMGUI

Signed-off-by: nvsickle <nvsickle@amazon.com>
main
nvsickle 5 years ago
parent 0c97888d0f
commit dd20a598b2

@ -92,6 +92,11 @@ namespace AZ
//! Alternatively, connect to ViewportContextNotificationsBus and listen to ViewportContextNotifications::OnViewportSizeChanged.
void ConnectSizeChangedHandler(SizeChangedEvent::Handler& handler);
using ScalarChangedEvent = AZ::Event<float>;
//! Notifies consumers when the viewport DPI scaling ratio has changed.
//! Alternatively, connect to ViewportContextNotificationsBus and listen to ViewportContextNotifications::OnViewportDpiScalingChanged.
void ConnectDpiScalingFactorChangedHandler(ScalarChangedEvent::Handler& handler);
using MatrixChangedEvent = AZ::Event<const AZ::Matrix4x4&>;
//! Notifies consumers when the view matrix has changed.
void ConnectViewMatrixChangedHandler(MatrixChangedEvent::Handler& handler);
@ -141,6 +146,7 @@ namespace AZ
float m_viewportDpiScaleFactor = 1.0f;
SizeChangedEvent m_sizeChangedEvent;
ScalarChangedEvent m_dpiScalingFactorChangedEvent;
MatrixChangedEvent m_viewMatrixChangedEvent;
MatrixChangedEvent::Handler m_onViewMatrixChangedHandler;
MatrixChangedEvent m_projectionMatrixChangedEvent;

@ -105,8 +105,10 @@ namespace AZ
class ViewportContextNotifications
{
public:
//! Called when the underlying native window size changes for a given viewport context name.
//! Called when the underlying native window size changes for a given viewport context.
virtual void OnViewportSizeChanged(AzFramework::WindowSize size){AZ_UNUSED(size);}
//! Called when the window DPI scaling changes for a given viewport context.
virtual void OnViewportDpiScalingChanged(float dpiScale){AZ_UNUSED(dpiScale);}
//! Called when the active view for a given viewport context name changes.
virtual void OnViewportDefaultViewChanged(AZ::RPI::ViewPtr view){AZ_UNUSED(view);}
//! Called when the viewport is to be rendered.

@ -51,6 +51,7 @@ namespace AZ
{
AZStd::weak_ptr<ViewportContext> context;
ViewportContext::SizeChangedEvent::Handler sizeChangedHandler;
ViewportContext::ScalarChangedEvent::Handler dpiScalingChangedHandler;
};
// ViewportContextManager is a singleton owned solely by RPISystem, which is tagged as a friend

@ -163,6 +163,11 @@ namespace AZ
handler.Connect(m_sizeChangedEvent);
}
void ViewportContext::ConnectDpiScalingFactorChangedHandler(ScalarChangedEvent::Handler& handler)
{
handler.Connect(m_dpiScalingFactorChangedEvent);
}
void ViewportContext::ConnectViewMatrixChangedHandler(MatrixChangedEvent::Handler& handler)
{
handler.Connect(m_viewMatrixChangedEvent);
@ -303,6 +308,7 @@ namespace AZ
void ViewportContext::OnDpiScaleFactorChanged(float dpiScaleFactor)
{
m_viewportDpiScaleFactor = dpiScaleFactor;
m_dpiScalingFactorChangedEvent.Signal(dpiScaleFactor);
}
} // namespace RPI
} // namespace AZ

@ -62,9 +62,20 @@ namespace AZ
}
ViewportContextIdNotificationBus::Event(viewportId, &ViewportContextIdNotificationBus::Events::OnViewportSizeChanged, size);
};
auto onDpiScalingChanged = [this, viewportId](float dpiScalingFactor)
{
// Ensure we emit OnViewportDpiScalingChanged with the correct name.
auto viewportContext = this->GetViewportContextById(viewportId);
if (viewportContext)
{
ViewportContextNotificationBus::Event(viewportContext->GetName(), &ViewportContextNotificationBus::Events::OnViewportDpiScalingChanged, dpiScalingFactor);
}
ViewportContextIdNotificationBus::Event(viewportId, &ViewportContextIdNotificationBus::Events::OnViewportDpiScalingChanged, dpiScalingFactor);
};
viewportContext->m_name = contextName;
viewportData.sizeChangedHandler = ViewportContext::SizeChangedEvent::Handler(onSizeChanged);
viewportContext->ConnectSizeChangedHandler(viewportData.sizeChangedHandler);
viewportContext->ConnectDpiScalingFactorChangedHandler(viewportData.dpiScalingChangedHandler);
ViewPtrStack& associatedViews = GetOrCreateViewStackForContext(contextName);
viewportContext->SetDefaultView(associatedViews.back());
onSizeChanged(viewportContext->GetViewportSize());
@ -176,6 +187,7 @@ namespace AZ
UpdateViewForContext(newContextName);
// Ensure anyone listening on per-name viewport size updates gets notified.
ViewportContextNotificationBus::Event(newContextName, &ViewportContextNotificationBus::Events::OnViewportSizeChanged, viewportContext->GetViewportSize());
ViewportContextNotificationBus::Event(newContextName, &ViewportContextNotificationBus::Events::OnViewportDpiScalingChanged, viewportContext->GetDpiScalingFactor());
}
void ViewportContextManager::EnumerateViewportContexts(AZStd::function<void(ViewportContextPtr)> visitorFunction)

@ -105,10 +105,20 @@ namespace AZ
// Let our ImguiAtomSystemComponent know once we successfully connect and update the viewport size.
if (!m_initialized)
{
auto atomViewportRequests = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
auto defaultViewportContext = atomViewportRequests->GetDefaultViewportContext();
OnViewportDpiScalingChanged(defaultViewportContext->GetDpiScalingFactor());
m_initialized = true;
}
});
#endif
#endif //define(IMGUI_ENABLED)
}
void ImguiAtomSystemComponent::OnViewportDpiScalingChanged(float dpiScale)
{
#if defined(IMGUI_ENABLED)
ImGui::ImGuiManagerBus::Broadcast(&ImGui::ImGuiManagerBus::Events::SetDpiScalingFactor, dpiScale);
#endif //define(IMGUI_ENABLED)
}
}
}

@ -51,6 +51,7 @@ namespace AZ
// ViewportContextNotificationBus overrides...
void OnRenderTick() override;
void OnViewportSizeChanged(AzFramework::WindowSize size) override;
void OnViewportDpiScalingChanged(float dpiScale) override;
DebugConsole m_debugConsole;
bool m_initialized = false;

@ -86,6 +86,8 @@ namespace ImGui
virtual void SetImGuiRenderResolution(const ImVec2& res) = 0;
virtual void OverrideRenderWindowSize(uint32_t width, uint32_t height) = 0;
virtual void RestoreRenderWindowSizeToDefault() = 0;
virtual void SetDpiScalingFactor(float dpiScalingFactor) = 0;
virtual float GetDpiScalingFactor() const = 0;
virtual void Render() = 0;
};

@ -277,6 +277,20 @@ void ImGui::ImGuiManager::RestoreRenderWindowSizeToDefault()
InitWindowSize();
}
void ImGui::ImGuiManager::SetDpiScalingFactor(float dpiScalingFactor)
{
ImGuiIO& io = ImGui::GetIO();
// Set the global font scale to size our UI to the scaling factor
// Note: Currently we use the default, 13px fixed-size IMGUI font, so this can get somewhat blurry
io.FontGlobalScale = dpiScalingFactor;
}
float ImGui::ImGuiManager::GetDpiScalingFactor() const
{
ImGuiIO& io = ImGui::GetIO();
return io.FontGlobalScale;
}
void ImGuiManager::Render()
{
if (m_clientMenuBarState == DisplayState::Hidden && m_editorWindowState == DisplayState::Hidden)

@ -57,6 +57,8 @@ namespace ImGui
void SetImGuiRenderResolution(const ImVec2& res) override { m_renderResolution = res; }
void OverrideRenderWindowSize(uint32_t width, uint32_t height) override;
void RestoreRenderWindowSizeToDefault() override;
void SetDpiScalingFactor(float dpiScalingFactor) override;
float GetDpiScalingFactor() const override;
void Render() override;
// -- ImGuiManagerBus Interface -------------------------------------------------------------------

Loading…
Cancel
Save