Skip to content

Commit

Permalink
Refactor SetMainViewport stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed May 5, 2024
1 parent 6fd8549 commit 1e29a4e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
36 changes: 26 additions & 10 deletions addons/imgui-godot/ImGuiGodot/ImGuiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public override void _Ready()

public override void _Process(double delta)
{
Vector2I vpSize = ImGuiLayer.Instance!.ViewportSize;
State.Instance.Update(delta, vpSize.ToImVec2());
State.Instance.Update(delta, State.Instance.ViewportSize.ToImVec2());
}
}

Expand All @@ -48,7 +47,7 @@ public override void _EnterTree()
AddChild(_helper);

Signaler = GetParent();
AddChild(new ImGuiLayer());
SetMainViewport(_window);
}

public override void _Ready()
Expand All @@ -63,7 +62,7 @@ public override void _ExitTree()

public override void _Process(double delta)
{
ImGuiLayer.Instance!.UpdateViewport();
State.Instance.Layer.UpdateViewport();
Signaler.EmitSignal("imgui_layout");
State.Instance.Render();
}
Expand All @@ -73,31 +72,48 @@ public override void _Notification(int what)
Internal.Input.ProcessNotification(what);
}

public void OnLayerExiting()
{
// an ImGuiLayer is being destroyed without calling SetMainViewport
if (State.Instance.Layer.GetViewport() != _window)
{
// revert to main window
SetMainViewport(_window);
}
}

public void SetMainViewport(Viewport vp)
{
ImGuiLayer? oldLayer = ImGuiLayer.Instance;
ImGuiLayer.Instance = null;
oldLayer?.Free();
ImGuiLayer? oldLayer = State.Instance.Layer;
if (oldLayer != null)
{
oldLayer.TreeExiting -= OnLayerExiting;
oldLayer.QueueFree();
}

var newLayer = new ImGuiLayer();
newLayer.TreeExiting += OnLayerExiting;

if (vp is Window window)
{
State.Instance.Input = new Internal.Input();
if (window == _window)
AddChild(new ImGuiLayer());
AddChild(newLayer);
else
window.AddChild(new ImGuiLayer());
window.AddChild(newLayer);
ImGui.GetIO().BackendFlags |= ImGuiBackendFlags.PlatformHasViewports;
}
else if (vp is SubViewport svp)
{
State.Instance.Input = new InputLocal();
svp.AddChild(new ImGuiLayer());
svp.AddChild(newLayer);
ImGui.GetIO().BackendFlags &= ~ImGuiBackendFlags.PlatformHasViewports;
}
else
{
throw new System.ArgumentException("secret third kind of viewport??", nameof(vp));
}
State.Instance.Layer = newLayer;
}

private void CheckContentScale()
Expand Down
26 changes: 7 additions & 19 deletions addons/imgui-godot/ImGuiGodot/ImGuiLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace ImGuiGodot;

public partial class ImGuiLayer : CanvasLayer
{
public static ImGuiLayer? Instance { get; set; }

private Rid _subViewportRid;
private Vector2I _subViewportSize = Vector2I.Zero;
private Rid _canvasItem;
Expand All @@ -19,12 +17,8 @@ public partial class ImGuiLayer : CanvasLayer

public override void _EnterTree()
{
if (Instance != null)
throw new System.InvalidOperationException();
Instance = this;

Name = "ImGuiLayer";
Layer = State.Instance.Layer;
Layer = State.Instance.LayerNum;

_parentViewport = GetViewport();
_subViewportRid = AddLayerSubViewport(this);
Expand All @@ -45,14 +39,6 @@ public override void _ExitTree()
{
RenderingServer.FreeRid(_canvasItem);
RenderingServer.FreeRid(_subViewportRid);

if (Instance == this)
{
Instance = null;
Window mainWindow = ImGuiController.Instance.GetWindow();
if (_parentViewport != mainWindow)
ImGuiController.Instance.SetMainViewport(mainWindow);
}
}

private void OnChangeVisibility()
Expand Down Expand Up @@ -83,20 +69,22 @@ public void UpdateViewport()
{
if (_visible)
{
Vector2I vpSize;
#pragma warning disable IDE0045 // Convert to conditional expression
if (_parentViewport is Window w)
ViewportSize = w.Size;
vpSize = w.Size;
else if (_parentViewport is SubViewport svp)
ViewportSize = svp.Size;
vpSize = svp.Size;
else
throw new System.InvalidOperationException();
#pragma warning restore IDE0045 // Convert to conditional expression
State.Instance.ViewportSize = vpSize;

var ft = _parentViewport.GetFinalTransform();
if (_subViewportSize != ViewportSize || _finalTransform != ft)
if (_subViewportSize != vpSize || _finalTransform != ft)
{
// this is more or less how SubViewportContainer works
_subViewportSize = ViewportSize;
_subViewportSize = vpSize;
_finalTransform = ft;
RenderingServer.ViewportSetSize(
_subViewportRid,
Expand Down
8 changes: 2 additions & 6 deletions addons/imgui-godot/ImGuiGodot/Internal/BackendNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ public float Scale

public bool Visible
{
get => ImGuiLayer.Instance?.Visible ?? false;
set
{
if (ImGuiLayer.Instance != null)
ImGuiLayer.Instance!.Visible = value;
}
get => State.Instance.Layer.Visible;
set => State.Instance.Layer.Visible = value;
}

public void AddFont(FontFile fontData, int fontSize, bool merge, ushort[]? glyphRanges)
Expand Down
4 changes: 2 additions & 2 deletions addons/imgui-godot/ImGuiGodot/Internal/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected virtual void UpdateMousePos(ImGuiIOPtr io)
}
else
{
var winPos = ImGuiLayer.Instance!.GetWindow().Position;
var winPos = State.Instance.Layer.GetWindow().Position;
io.AddMousePosEvent(mousePos.X - winPos.X, mousePos.Y - winPos.Y);
}
}
Expand Down Expand Up @@ -104,7 +104,7 @@ protected void ProcessSubViewportWidget(InputEvent evt)
var mousePos = DisplayServer.MouseGetPosition();
var windowPos = Vector2I.Zero;
if (!io.ConfigFlags.HasFlag(ImGuiConfigFlags.ViewportsEnable))
windowPos = ImGuiLayer.Instance!.GetWindow().Position;
windowPos = State.Instance.Layer.GetWindow().Position;

mouseEvent.Position = new Vector2(
mousePos.X - windowPos.X - CurrentSubViewportPos.X,
Expand Down
7 changes: 5 additions & 2 deletions addons/imgui-godot/ImGuiGodot/Internal/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ private enum RendererType
internal Fonts Fonts { get; }
internal Input Input { get; set; }
internal IRenderer Renderer { get; }

internal float Scale { get; set; } = 1.0f;
internal float JoyAxisDeadZone { get; set; } = 0.15f;
internal int Layer { get; private set; } = 128;
internal int LayerNum { get; private set; } = 128;
internal Vector2I ViewportSize { get; set; }
internal ImGuiLayer Layer { get; set; } = null!;

internal static State Instance { get; set; } = null!;

Expand Down Expand Up @@ -122,7 +125,7 @@ public static void Init(Resource cfg)
Instance = new(renderer)
{
Scale = (float)cfg.Get("Scale"),
Layer = (int)cfg.Get("Layer")
LayerNum = (int)cfg.Get("Layer")
};

ImGui.GetIO().SetIniFilename((string)cfg.Get("IniFilename"));
Expand Down

0 comments on commit 1e29a4e

Please sign in to comment.