Skip to content

Commit f3d18b5

Browse files
committed
add support for ImNodes and ImPlot
Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 5292a8b commit f3d18b5

2 files changed

Lines changed: 53 additions & 5 deletions

File tree

src/Raylib.NET.ImGui/Raylib.NET.ImGui.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<ItemGroup>
1717
<ProjectReference Include="..\Raylib.NET\Raylib.NET.csproj" />
1818
<PackageReference Include="Hexa.NET.ImGui" Version="2.2.9" />
19+
<PackageReference Include="Hexa.NET.ImNodes" Version="2.2.9" />
20+
<PackageReference Include="Hexa.NET.ImPlot" Version="2.2.9" />
1921
</ItemGroup>
2022
</Project>
2123

src/Raylib.NET.ImGui/RlImGui.cs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Numerics;
22
using System.Runtime.InteropServices;
33
using Hexa.NET.ImGui;
4+
using Hexa.NET.ImNodes;
5+
using Hexa.NET.ImPlot;
46

57
namespace RaylibNET;
68

@@ -99,10 +101,24 @@ public static void BeginSetup()
99101
SetupKeymap();
100102
SetupMouseCursors();
101103

102-
if (ImGuiContext.Handle == null)
104+
if (ImGuiContext.Handle == null) {
103105
ImGuiContext = ImGui.CreateContext();
106+
ImGui.SetCurrentContext(ImGuiContext);
107+
}
104108

105-
ImGui.SetCurrentContext(ImGuiContext);
109+
if (ImNodesContext.Handle == null)
110+
{
111+
ImNodes.SetImGuiContext(ImGuiContext);
112+
ImNodesContext = ImNodes.CreateContext();
113+
}
114+
115+
if (ImPlotContext.Handle == null)
116+
{
117+
ImPlot.SetImGuiContext(ImGuiContext);
118+
ImPlotContext = ImPlot.CreateContext();
119+
}
120+
121+
SetContexts();
106122
}
107123

108124
/// <summary>
@@ -113,7 +129,7 @@ public static void BeginSetup()
113129
/// </summary>
114130
public static void EndSetup()
115131
{
116-
ImGui.SetCurrentContext(ImGuiContext);
132+
SetContexts();
117133
SetupFontAwesome();
118134
SetupBackend();
119135
}
@@ -124,8 +140,7 @@ public static void EndSetup()
124140
/// <param name="dt">Delta time (optional, uses GetFrameTime if negative)</param>
125141
public static void Begin(float dt = -1)
126142
{
127-
ImGui.SetCurrentContext(ImGuiContext);
128-
143+
SetContexts();
129144
NewFrame(dt);
130145
FrameEvents();
131146
ImGui.NewFrame();
@@ -181,6 +196,18 @@ public static void Shutdown()
181196

182197
ImGui.DestroyContext(ImGuiContext);
183198
ImGuiContext = default;
199+
200+
if (ImPlotContext.Handle != null)
201+
{
202+
ImPlot.DestroyContext(ImPlotContext);
203+
ImPlotContext = default;
204+
}
205+
206+
if (ImNodesContext.Handle != null)
207+
{
208+
ImNodes.DestroyContext(ImNodesContext);
209+
ImNodesContext = default;
210+
}
184211
}
185212

186213
/// <summary>
@@ -366,6 +393,8 @@ public static bool ImageButton(string name, Texture image, Vector2 size, Vector4
366393
//----------------------------------------------------------------------
367394

368395
private static ImGuiContextPtr ImGuiContext;
396+
private static ImNodesContextPtr ImNodesContext;
397+
private static ImPlotContextPtr ImPlotContext;
369398
private static ImGuiMouseCursor CurrentMouseCursor = ImGuiMouseCursor.Count;
370399
private static readonly Dictionary<ImGuiMouseCursor, int> MouseCursorMap = [];
371400
private static readonly Dictionary<int, ImGuiKey> RaylibKeyMap = [];
@@ -384,6 +413,23 @@ public static bool ImageButton(string name, Texture image, Vector2 size, Vector4
384413
private static bool IsAltDown() => Raylib.IsKeyDown((int)KeyboardKey.KEY_LEFT_ALT) || Raylib.IsKeyDown((int)KeyboardKey.KEY_RIGHT_ALT);
385414
private static bool IsSuperDown() => Raylib.IsKeyDown((int)KeyboardKey.KEY_LEFT_SUPER) || Raylib.IsKeyDown((int)KeyboardKey.KEY_RIGHT_SUPER);
386415

416+
private static void SetContexts()
417+
{
418+
ImGui.SetCurrentContext(ImGuiContext);
419+
420+
if (ImPlotContext.Handle != null)
421+
{
422+
ImPlot.SetImGuiContext(ImGuiContext);
423+
ImPlot.SetCurrentContext(ImPlotContext);
424+
}
425+
426+
if (ImNodesContext.Handle != null)
427+
{
428+
ImNodes.SetImGuiContext(ImGuiContext);
429+
ImNodes.SetCurrentContext(ImNodesContext);
430+
}
431+
}
432+
387433
private static void SetupBackend()
388434
{
389435
var io = ImGui.GetIO();

0 commit comments

Comments
 (0)