Skip to content

Commit

Permalink
most stuff working
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Jan 20, 2024
1 parent 2edc323 commit db2b2b3
Show file tree
Hide file tree
Showing 23 changed files with 203 additions and 43 deletions.
18 changes: 16 additions & 2 deletions addons/imgui-godot/ImGuiGodot/ImGuiGD.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#if GODOT_PC
using Godot;
using ImGuiGodot.Internal;
using System;

namespace ImGuiGodot;

public static class ImGuiGD
{
private static readonly Internal.IBackend _backend;
private static readonly IBackend _backend;

/// <summary>
/// Deadzone for all axes
Expand Down Expand Up @@ -40,7 +41,7 @@ public static float Scale

static ImGuiGD()
{
_backend = ClassDB.ClassExists("ImGuiGD") ? new Internal.BackendNative() : new Internal.BackendNet();
_backend = ClassDB.ClassExists("ImGuiGD") ? new BackendNative() : new BackendNet();
}

public static IntPtr BindTexture(Texture2D tex)
Expand Down Expand Up @@ -84,6 +85,19 @@ public static void Connect(Action action)
Connect(Callable.From(action));
}

public static bool ToolInit()
{
if (_backend is BackendNative nbe)
{
nbe.ToolInit();
return true;
}
else
{
return false;
}
}

internal static bool SubViewportWidget(SubViewport svp)
{
return _backend.SubViewportWidget(svp);
Expand Down
2 changes: 1 addition & 1 deletion addons/imgui-godot/ImGuiGodot/ImGuiLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public override void _Ready()

public override void _ExitTree()
{
Internal.State.Instance.Shutdown();
Internal.State.Instance.Dispose();
RenderingServer.FreeRid(_ci);
RenderingServer.FreeRid(_subViewportRid);
}
Expand Down
4 changes: 3 additions & 1 deletion addons/imgui-godot/ImGuiGodot/ImGuiSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ namespace ImGuiGodot;

public partial class ImGuiSync : GodotObject
{
public static readonly StringName GetImGuiPtrs = "GetImGuiPtrs";

public static void SyncPtrs()
{
GodotObject gd = Engine.GetSingleton("ImGuiGD");
long[] ptrs = (long[])gd.Call("GetImGuiPtrs",
long[] ptrs = (long[])gd.Call(GetImGuiPtrs,
ImGui.GetVersion(),
Marshal.SizeOf<ImGuiIO>(),
Marshal.SizeOf<ImDrawVert>(),
Expand Down
40 changes: 33 additions & 7 deletions addons/imgui-godot/ImGuiGodot/Internal/BackendNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,64 @@ namespace ImGuiGodot.Internal;

internal sealed class BackendNative : IBackend
{
private readonly GodotObject _gd = Engine.GetSingleton("ImGuiGD");

private sealed class MethodName
{
public static readonly StringName AddFont = "AddFont";
public static readonly StringName AddFontDefault = "AddFontDefault";
public static readonly StringName Connect = "Connect";
public static readonly StringName RebuildFontAtlas = "RebuildFontAtlas";
public static readonly StringName ResetFonts = "ResetFonts";
public static readonly StringName SubViewport = "SubViewport";
public static readonly StringName ToolInit = "ToolInit";
}

private sealed class PropertyName
{
public static readonly StringName JoyAxisDeadZone = "JoyAxisDeadZone";
public static readonly StringName Scale = "Scale";
public static readonly StringName Visible = "Visible";
}

public float JoyAxisDeadZone { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
public float Scale { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
public float Scale { get; set; } = 1.0f; // TODO: make property
public bool Visible { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }

public void AddFont(FontFile fontData, int fontSize, bool merge)
{
throw new System.NotImplementedException();
_gd.Call(MethodName.AddFont, fontData, fontSize, merge);
}

public void AddFontDefault()
{
throw new System.NotImplementedException();
_gd.Call(MethodName.AddFontDefault);
}

public void Connect(Callable callable)
{
throw new System.NotImplementedException();
_gd.Call(MethodName.Connect, callable);
}

public void RebuildFontAtlas(float scale)
{
throw new System.NotImplementedException();
_gd.Call(MethodName.RebuildFontAtlas, scale);
}

public void ResetFonts()
{
throw new System.NotImplementedException();
_gd.Call(MethodName.ResetFonts);
}

public bool SubViewportWidget(SubViewport svp)
{
throw new System.NotImplementedException();
return (bool)_gd.Call(MethodName.SubViewport, svp);
}

public void ToolInit()
{
_gd.Call(MethodName.ToolInit);
ImGuiSync.SyncPtrs();
}
}
#endif
4 changes: 2 additions & 2 deletions addons/imgui-godot/ImGuiGodot/Internal/CanvasRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void InitViewport(Rid vprid)
};
}

public void RenderDrawData()
public void Render()
{
var pio = ImGui.GetPlatformIO();
for (int vpidx = 0; vpidx < pio.Viewports.Size; vpidx++)
Expand Down Expand Up @@ -186,7 +186,7 @@ public void OnHide()
ClearCanvasItems();
}

public void Shutdown()
public void Dispose()
{
ClearCanvasItems();
foreach (ViewportData vd in _vpData.Values)
Expand Down
4 changes: 2 additions & 2 deletions addons/imgui-godot/ImGuiGodot/Internal/DummyRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public void OnHide()
{
}

public void RenderDrawData()
public void Render()
{
}

public void Shutdown()
public void Dispose()
{
}
}
Expand Down
6 changes: 3 additions & 3 deletions addons/imgui-godot/ImGuiGodot/Internal/IRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#if GODOT_PC
using Godot;
using System;

namespace ImGuiGodot.Internal;

internal interface IRenderer
internal interface IRenderer : IDisposable
{
public string Name { get; }
public void InitViewport(Rid vprid);
public void CloseViewport(Rid vprid);
public void RenderDrawData();
public void Render();
public void OnHide();
public void Shutdown();
}
#endif
4 changes: 2 additions & 2 deletions addons/imgui-godot/ImGuiGodot/Internal/RdRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ protected void FreeUnusedTextures()
_usedTextures.Clear();
}

public void RenderDrawData()
public void Render()
{
var pio = ImGui.GetPlatformIO();
for (int i = 0; i < pio.Viewports.Size; ++i)
Expand Down Expand Up @@ -371,7 +371,7 @@ public void OnHide()
{
}

public void Shutdown()
public void Dispose()
{
RD.FreeRid(_sampler);
RD.FreeRid(_shader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public RdRendererThreadSafe() : base()
RenderingServer.FramePreDraw -= OnFramePreDraw;
}

public new void RenderDrawData()
public new void Render()
{
var pio = ImGui.GetPlatformIO();
var newData = new SharedList(pio.Viewports.Size);
Expand Down
12 changes: 2 additions & 10 deletions addons/imgui-godot/ImGuiGodot/Internal/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ public State(Window mainWindow, Rid mainSubViewport, IRenderer renderer)
public void Dispose()
{
if (ImGui.GetCurrentContext() != IntPtr.Zero)
{
ImGui.DestroyContext();
}
Renderer.Dispose();
}

public static void Init(Window mainWindow, Rid mainSubViewport, Resource cfg)
Expand Down Expand Up @@ -181,17 +180,10 @@ public void Render()
ImGui.Render();

ImGui.UpdatePlatformWindows();
Renderer.RenderDrawData();
Renderer.Render();
//_inProcessFrame = false;
}

public void Shutdown()
{
Renderer.Shutdown();
if (ImGui.GetCurrentContext() != IntPtr.Zero)
ImGui.DestroyContext();
}

/// <returns>
/// True if the InputEvent was consumed
/// </returns>
Expand Down
1 change: 1 addition & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/Viewports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void Dispose()
{
if (GodotWindow.GetParent() != null)
{
State.Instance.Renderer.CloseViewport(Util.ConstructRid((ulong)_vp.RendererUserData));
GodotWindow.QueueFree();
}
_gcHandle.Free();
Expand Down
1 change: 1 addition & 0 deletions addons/imgui-godot/scripts/ImGuiPlugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ func _enter_tree():
if ProjectSettings.has_setting("autoload/ImGuiLayer"):
remove_autoload_singleton("ImGuiLayer")
add_autoload_singleton("ImGuiRoot", "res://addons/imgui-godot/data/ImGuiRoot.tscn")
Engine.register_singleton("ImGuiPlugin", self)

func _exit_tree():
remove_autoload_singleton("ImGuiRoot")
2 changes: 2 additions & 0 deletions gdext/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
target_sources(imgui-godot-native PRIVATE
common.h
CanvasRenderer.cpp
CanvasRenderer.h
Context.cpp
Context.h
DummyRenderer.h
Expand Down
54 changes: 54 additions & 0 deletions gdext/src/CanvasRenderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "CanvasRenderer.h"
#include "common.h"
#include <godot_cpp/classes/rendering_server.hpp>
#include <unordered_map>
#include <vector>

using namespace godot;

namespace ImGui::Godot {

struct CanvasRenderer::Impl
{
struct ViewportData
{
RID canvas;
RID rootCanvasItem;
};

std::unordered_map<RID, std::vector<RID>> canvasItemPools;
std::unordered_map<RID, ViewportData> vpData;
};

CanvasRenderer::CanvasRenderer()
{
}

CanvasRenderer::~CanvasRenderer()
{
}

void CanvasRenderer::InitViewport(RID vprid)
{
RenderingServer* RS = RenderingServer::get_singleton();
RID canvas = RS->canvas_create();
RID canvasItem = RS->canvas_item_create();
RS->viewport_attach_canvas(vprid, canvas);
RS->canvas_item_set_parent(canvasItem, canvas);

impl->vpData[vprid] = {canvas, canvasItem};
}

void CanvasRenderer::CloseViewport(RID vprid)
{
}

void CanvasRenderer::Render()
{
}

void CanvasRenderer::OnHide()
{
}

} // namespace ImGui::Godot
35 changes: 35 additions & 0 deletions gdext/src/CanvasRenderer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
#include "Renderer.h"
#include <imgui.h>
#include <memory>

#pragma warning(push, 0)
#include <godot_cpp/variant/rid.hpp>
#pragma warning(pop)

using godot::RID;

namespace ImGui::Godot {

class CanvasRenderer : public Renderer
{
public:
CanvasRenderer();
virtual ~CanvasRenderer();

virtual const char* Name() override
{
return "godot4_canvas";
}

void InitViewport(RID vprid) override;
void CloseViewport(RID vprid) override;
void Render() override;
void OnHide() override;

private:
struct Impl;
std::unique_ptr<Impl> impl;
};

} // namespace ImGui::Godot
13 changes: 13 additions & 0 deletions gdext/src/DummyRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class DummyRenderer : public Renderer
DummyRenderer()
{
}

~DummyRenderer()
{
}
Expand All @@ -18,9 +19,21 @@ class DummyRenderer : public Renderer
return "godot4_dummy";
}

void InitViewport(RID vprid) override
{
}

void CloseViewport(RID vprid) override
{
}

void Render() override
{
}

void OnHide() override
{
}
};

} // namespace ImGui::Godot
Loading

0 comments on commit db2b2b3

Please sign in to comment.