CogImGui: remove the scale widget containing the imgui widget

This commit is contained in:
Arnaud Jamin
2023-11-24 12:57:59 -05:00
parent ecddecf3e5
commit 463e943890
7 changed files with 71 additions and 62 deletions
@@ -41,16 +41,7 @@ void FImGuiInputProcessor::Tick(const float DeltaTime, FSlateApplication& SlateA
UE_LOG(LogCogImGui, VeryVerbose, TEXT("FImGuiInputProcessor::Tick | HasGamePad Changed"));
}
if (IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
const FVector2D MousePosition = SlateApp.GetCursorPos();
IO.AddMousePosEvent(MousePosition.X, MousePosition.Y);
}
else
{
const FVector2D MousePosition = TransformScreenPointToImGui(MainWidget->GetTickSpaceGeometry(), SlateApp.GetCursorPos());
IO.AddMousePosEvent(MousePosition.X, MousePosition.Y);
}
AddMousePosEvent(SlateApp.GetCursorPos());
if ((IO.ConfigFlags & ImGuiConfigFlags_NoMouse) == 0)
{
@@ -182,24 +173,14 @@ bool FImGuiInputProcessor::HandleAnalogInputEvent(FSlateApplication& SlateApp, c
//--------------------------------------------------------------------------------------------------------------------------
bool FImGuiInputProcessor::HandleMouseMoveEvent(FSlateApplication& SlateApp, const FPointerEvent& Event)
{
ImGuiIO& IO = ImGui::GetIO();
if (IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
const FVector2D MousePosition = Event.GetScreenSpacePosition();;
IO.AddMousePosEvent(MousePosition.X, MousePosition.Y);
}
else
{
const FVector2D MousePosition = TransformScreenPointToImGui(MainWidget->GetTickSpaceGeometry(), Event.GetScreenSpacePosition());
IO.AddMousePosEvent(MousePosition.X, MousePosition.Y);
}
AddMousePosEvent(Event.GetScreenSpacePosition());
if (MainWidget->GetEnableInput() && MainWidget->GetShareMouse() == false)
{
return TerminateEvent;
}
ImGuiIO& IO = ImGui::GetIO();
const bool Result = IO.WantCaptureMouse ? TerminateEvent : ForwardEvent;
return Result;
}
@@ -241,6 +222,20 @@ bool FImGuiInputProcessor::HandleMouseButtonEvent(FSlateApplication& SlateApp, c
UE_LOG(LogCogImGui, VeryVerbose, TEXT("FImGuiInputProcessor::HandleMouseButtonEvent | Button:%d | IsButtonDown:%d | WantCaptureMouse:%d | TerminateEvent:%d"), Button, IsButtonDown, IO.WantCaptureMouse, Result);
return Result;
}
//--------------------------------------------------------------------------------------------------------------------------
void FImGuiInputProcessor::AddMousePosEvent(const FVector2D& MousePosition) const
{
ImGuiIO& IO = ImGui::GetIO();
if (IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
IO.AddMousePosEvent(MousePosition.X, MousePosition.Y);
}
else
{
const FVector2D TransformedMousePosition = MousePosition - MainWidget->GetTickSpaceGeometry().GetAbsolutePosition();
IO.AddMousePosEvent(TransformedMousePosition.X, TransformedMousePosition.Y);
}
}
//--------------------------------------------------------------------------------------------------------------------------
bool FImGuiInputProcessor::HandleMouseWheelOrGestureEvent(FSlateApplication& SlateApp, const FPointerEvent& Event, const FPointerEvent* GestureEvent)
@@ -70,20 +70,9 @@ TSharedPtr<SCogImguiWidget> FCogImguiModule::CreateImGuiWidget(UGameViewportClie
SAssignNew(ImguiWidget, SCogImguiWidget)
.GameViewport(GameViewport)
.FontAtlas(FontAtlas)
.Render(Render)
.Clipping(EWidgetClipping::ClipToBounds);
.Render(Render);
TSharedPtr<SScaleBox> ScaleWidget;
SAssignNew(ScaleWidget, SScaleBox)
.IgnoreInheritedScale(true)
.HAlign(HAlign_Fill)
.VAlign(VAlign_Fill)
.Visibility(EVisibility::SelfHitTestInvisible)
[
ImguiWidget.ToSharedRef()
];
GameViewport->AddViewportWidgetContent(ScaleWidget.ToSharedRef(), Cog_ZOrder);
GameViewport->AddViewportWidgetContent(ImguiWidget.ToSharedRef(), TNumericLimits<int32>::Max());
return ImguiWidget;
}
@@ -98,20 +98,6 @@ void SCogImguiWidget::Tick(const FGeometry& AllottedGeometry, const double InCur
TickKeyModifiers();
TickFocus();
TickImGui(InDeltaTime);
//ImGuiIO& IO = ImGui::GetIO();
//if (GetEnableInput())
//{
// IO.ConfigFlags &= ~ImGuiConfigFlags_NoMouse;
// IO.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
// IO.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
//}
//else
//{
// IO.ConfigFlags |= ImGuiConfigFlags_NoMouse;
// IO.ConfigFlags &= ~ImGuiConfigFlags_NavEnableKeyboard;
// IO.ConfigFlags &= ~ImGuiConfigFlags_NavEnableGamepad;
//}
}
//--------------------------------------------------------------------------------------------------------------------------
@@ -138,18 +124,18 @@ void SCogImguiWidget::TickImGui(float InDeltaTime)
}
ImGui::SetCurrentContext(ImGuiContext);
ImGuiIO& IO = ImGui::GetIO();
IO.DeltaTime = InDeltaTime;
ImPlot::SetImGuiContext(ImGuiContext);
ImPlot::SetCurrentContext(ImPlotContext);
FVector2D DisplaySize;
GameViewport->GetViewportSize(DisplaySize);
IO.DisplaySize = FCogImguiHelper::ToImVec2(DisplaySize);
ImGuiIO& IO = ImGui::GetIO();
IO.DeltaTime = FApp::GetDeltaTime();
IO.DisplaySize = FCogImguiHelper::ToImVec2(GetTickSpaceGeometry().GetAbsoluteSize());
ImGui::NewFrame();
Render(InDeltaTime);
//DrawDebug();
ImGui::Render();
if ((IO.ConfigFlags & ImGuiConfigFlags_NoMouse) == 0)
@@ -247,7 +233,7 @@ int32 SCogImguiWidget::OnPaint(
bool bParentEnabled) const
{
const FSlateRenderTransform& WidgetToScreen = AllottedGeometry.GetAccumulatedRenderTransform();
const FSlateRenderTransform& WidgetToScreen = AllottedGeometry.GetAccumulatedRenderTransform().GetTranslation();
const FSlateRenderTransform ImGuiToScreen = FCogImguiHelper::RoundTranslation(ImGuiRenderTransform.Concatenate(WidgetToScreen));
FCogImguiTextureManager& TextureManager = FCogImguiModule::Get().GetTextureManager();
@@ -281,13 +267,14 @@ int32 SCogImguiWidget::OnPaint(
}
}
return Super::OnPaint(Args, AllottedGeometry, MyClippingRect, OutDrawElements, LayerId, WidgetStyle, bParentEnabled);
return LayerId;
}
//--------------------------------------------------------------------------------------------------------------------------
FVector2D SCogImguiWidget::ComputeDesiredSize(float Scale) const
{
return Super::ComputeDesiredSize(Scale);
return FVector2D::ZeroVector;
//return Super::ComputeDesiredSize(Scale);
}
//--------------------------------------------------------------------------------------------------------------------------
@@ -419,3 +406,35 @@ FReply SCogImguiWidget::OnKeyChar(const FGeometry& MyGeometry, const FCharacterE
const FReply Result = IO.WantCaptureKeyboard ? FReply::Handled() : FReply::Unhandled();
return Result;
}
//--------------------------------------------------------------------------------------------------------------------------
void SCogImguiWidget::DrawDebug()
{
if (ImGui::Begin("ImGui Integration Debug"))
{
ImGui::BeginDisabled();
ImVec2 AbsPos = FCogImguiHelper::ToImVec2(GetTickSpaceGeometry().GetAbsolutePosition());
ImGui::InputFloat2("Widget Abs Pos", &AbsPos.x, "%0.1f");
ImVec2 AbsSize = FCogImguiHelper::ToImVec2(GetTickSpaceGeometry().GetAbsoluteSize());
ImGui::InputFloat2("Widget Abs Size", &AbsSize.x, "%0.1f");
ImVec2 LocalSize = FCogImguiHelper::ToImVec2(GetTickSpaceGeometry().GetLocalSize());
ImGui::InputFloat2("Widget Local Size", &LocalSize.x, "%0.1f");
FSlateApplication& SlateApp = FSlateApplication::Get();
ImVec2 MousePosition = FCogImguiHelper::ToImVec2(SlateApp.GetCursorPos());
ImGui::InputFloat2("Mouse", &MousePosition.x, "%0.1f");
ImGuiIO& IO = ImGui::GetIO();
ImGui::InputFloat2("ImGui Mouse", &IO.MousePos.x, "%0.1f");
ImGui::EndDisabled();
}
ImGui::End();
}
@@ -37,6 +37,8 @@ protected:
bool HandleMouseButtonEvent(FSlateApplication& SlateApp, const FPointerEvent& Event, bool IsButtonDown);
void AddMousePosEvent(const FVector2D& MousePosition) const;
bool IsKeyBoundToCommand(const FKeyEvent& KeyEvent);
static ImGuiKey ToImKey(const FKey& Key);
@@ -5,7 +5,7 @@
#include "Rendering/RenderingCommon.h"
#include "UObject/WeakObjectPtr.h"
#include "Widgets/DeclarativeSyntaxSupport.h"
#include "Widgets/SCompoundWidget.h"
#include "Widgets/SLeafWidget.h"
class IInputProcessor;
class UGameViewportClient;
@@ -17,9 +17,9 @@ struct ImPlotContext;
using FCogImguiRenderFunction = TFunction<void(float DeltaTime)>;
//--------------------------------------------------------------------------------------------------------------------------
class COGIMGUI_API SCogImguiWidget : public SCompoundWidget
class COGIMGUI_API SCogImguiWidget : public SLeafWidget
{
typedef SCompoundWidget Super;
typedef SLeafWidget Super;
public:
@@ -88,6 +88,8 @@ protected:
virtual bool IsConsoleOpened() const;
virtual void DrawDebug();
TWeakObjectPtr<UGameViewportClient> GameViewport;
ImFontAtlas* FontAtlas = nullptr;
@@ -110,7 +110,6 @@ void FCogWindow::Render(float DeltaTime)
}
RenderContent();
ImGui::End();
}
else
{
@@ -120,6 +119,8 @@ void FCogWindow::Render(float DeltaTime)
}
}
ImGui::End();
PostRender();
}
@@ -37,6 +37,7 @@ void FCogWindow_Inputs::PreSaveConfig()
if (SCogImguiWidget* ImGuiWidget = GetOwner()->GetImGuiWidget().Get())
{
Config->bEnableInput = ImGuiWidget->GetEnableInput();
Config->bShareMouse = ImGuiWidget->GetShareMouse();
}
}