-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ImGuiManager.cs
226 lines (196 loc) · 10.9 KB
/
ImGuiManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
namespace ExampleFramework.ImGuiDemo
{
using Hexa.NET.ImGui;
using Hexa.NET.ImGui.Backends;
using Hexa.NET.ImGui.Backends.SDL2;
using Hexa.NET.ImGui.Utilities;
using Hexa.NET.ImGuizmo;
using Hexa.NET.ImNodes;
using Hexa.NET.ImPlot;
using System.Numerics;
public class ImGuiManager : IDisposable
{
private ImGuiContextPtr guiContext;
private ImNodesContextPtr nodesContext;
private ImPlotContextPtr plotContext;
public unsafe ImGuiManager()
{
// Create ImGui context
guiContext = ImGui.CreateContext(null);
// Set ImGui context
ImGui.SetCurrentContext(guiContext);
ImGuiImplSDL2.SetCurrentContext(guiContext);
// Set ImGui context for ImGuizmo
ImGuizmo.SetImGuiContext(guiContext);
// Set ImGui context for ImPlot
ImPlot.SetImGuiContext(guiContext);
// Set ImGui context for ImNodes
ImNodes.SetImGuiContext(guiContext);
// Create and set ImNodes context and set style
nodesContext = ImNodes.CreateContext();
ImNodes.SetCurrentContext(nodesContext);
ImNodes.StyleColorsDark(ImNodes.GetStyle());
// Create and set ImPlot context and set style
plotContext = ImPlot.CreateContext();
ImPlot.SetCurrentContext(plotContext);
ImPlot.StyleColorsDark(ImPlot.GetStyle());
// Setup ImGui config.
var io = ImGui.GetIO();
io.ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags.NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags.DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable; // Enable Multi-Viewport / Platform Windows
io.ConfigViewportsNoAutoMerge = false;
io.ConfigViewportsNoTaskBarIcon = false;
// setup fonts.
ImGuiFontBuilder builder = new();
builder
.AddDefaultFont()
.SetOption(config => { config.GlyphMinAdvanceX = 18; config.GlyphOffset = new(0, 4); })
.AddFontFromFileTTF("assets/fonts/SEGMDL2.TTF", 14, [(char)0xE700, (char)0xF800]);
// setup ImGui style
var style = ImGui.GetStyle();
var colors = style.Colors;
colors[(int)ImGuiCol.Text] = new Vector4(1.00f, 1.00f, 1.00f, 1.00f);
colors[(int)ImGuiCol.TextDisabled] = new Vector4(0.50f, 0.50f, 0.50f, 1.00f);
colors[(int)ImGuiCol.WindowBg] = new Vector4(0.10f, 0.10f, 0.10f, 1.00f);
colors[(int)ImGuiCol.ChildBg] = new Vector4(0.00f, 0.00f, 0.00f, 0.00f);
colors[(int)ImGuiCol.PopupBg] = new Vector4(0.19f, 0.19f, 0.19f, 0.92f);
colors[(int)ImGuiCol.Border] = new Vector4(0.19f, 0.19f, 0.19f, 0.29f);
colors[(int)ImGuiCol.BorderShadow] = new Vector4(0.00f, 0.00f, 0.00f, 0.24f);
colors[(int)ImGuiCol.FrameBg] = new Vector4(0.05f, 0.05f, 0.05f, 0.54f);
colors[(int)ImGuiCol.FrameBgHovered] = new Vector4(0.19f, 0.19f, 0.19f, 0.54f);
colors[(int)ImGuiCol.FrameBgActive] = new Vector4(0.20f, 0.22f, 0.23f, 1.00f);
colors[(int)ImGuiCol.TitleBg] = new Vector4(0.00f, 0.00f, 0.00f, 1.00f);
colors[(int)ImGuiCol.TitleBgActive] = new Vector4(0.06f, 0.06f, 0.06f, 1.00f);
colors[(int)ImGuiCol.TitleBgCollapsed] = new Vector4(0.00f, 0.00f, 0.00f, 1.00f);
colors[(int)ImGuiCol.MenuBarBg] = new Vector4(0.14f, 0.14f, 0.14f, 1.00f);
colors[(int)ImGuiCol.ScrollbarBg] = new Vector4(0.05f, 0.05f, 0.05f, 0.54f);
colors[(int)ImGuiCol.ScrollbarGrab] = new Vector4(0.34f, 0.34f, 0.34f, 0.54f);
colors[(int)ImGuiCol.ScrollbarGrabHovered] = new Vector4(0.40f, 0.40f, 0.40f, 0.54f);
colors[(int)ImGuiCol.ScrollbarGrabActive] = new Vector4(0.56f, 0.56f, 0.56f, 0.54f);
colors[(int)ImGuiCol.CheckMark] = new Vector4(0.33f, 0.67f, 0.86f, 1.00f);
colors[(int)ImGuiCol.SliderGrab] = new Vector4(0.34f, 0.34f, 0.34f, 0.54f);
colors[(int)ImGuiCol.SliderGrabActive] = new Vector4(0.56f, 0.56f, 0.56f, 0.54f);
colors[(int)ImGuiCol.Button] = new Vector4(0.05f, 0.05f, 0.05f, 0.54f);
colors[(int)ImGuiCol.ButtonHovered] = new Vector4(0.19f, 0.19f, 0.19f, 0.54f);
colors[(int)ImGuiCol.ButtonActive] = new Vector4(0.20f, 0.22f, 0.23f, 1.00f);
colors[(int)ImGuiCol.Header] = new Vector4(0.00f, 0.00f, 0.00f, 0.52f);
colors[(int)ImGuiCol.HeaderHovered] = new Vector4(0.00f, 0.00f, 0.00f, 0.36f);
colors[(int)ImGuiCol.HeaderActive] = new Vector4(0.20f, 0.22f, 0.23f, 0.33f);
colors[(int)ImGuiCol.Separator] = new Vector4(0.28f, 0.28f, 0.28f, 0.29f);
colors[(int)ImGuiCol.SeparatorHovered] = new Vector4(0.44f, 0.44f, 0.44f, 0.29f);
colors[(int)ImGuiCol.SeparatorActive] = new Vector4(0.40f, 0.44f, 0.47f, 1.00f);
colors[(int)ImGuiCol.ResizeGrip] = new Vector4(0.28f, 0.28f, 0.28f, 0.29f);
colors[(int)ImGuiCol.ResizeGripHovered] = new Vector4(0.44f, 0.44f, 0.44f, 0.29f);
colors[(int)ImGuiCol.ResizeGripActive] = new Vector4(0.40f, 0.44f, 0.47f, 1.00f);
colors[(int)ImGuiCol.Tab] = new Vector4(0.00f, 0.00f, 0.00f, 0.52f);
colors[(int)ImGuiCol.TabHovered] = new Vector4(0.14f, 0.14f, 0.14f, 1.00f);
colors[(int)ImGuiCol.TabSelected] = new Vector4(0.20f, 0.20f, 0.20f, 0.36f);
colors[(int)ImGuiCol.TabDimmed] = new Vector4(0.00f, 0.00f, 0.00f, 0.52f);
colors[(int)ImGuiCol.TabDimmedSelected] = new Vector4(0.14f, 0.14f, 0.14f, 1.00f);
colors[(int)ImGuiCol.DockingPreview] = new Vector4(0.33f, 0.67f, 0.86f, 1.00f);
colors[(int)ImGuiCol.DockingEmptyBg] = new Vector4(1.00f, 0.00f, 0.00f, 1.00f);
colors[(int)ImGuiCol.PlotLines] = new Vector4(1.00f, 0.00f, 0.00f, 1.00f);
colors[(int)ImGuiCol.PlotLinesHovered] = new Vector4(1.00f, 0.00f, 0.00f, 1.00f);
colors[(int)ImGuiCol.PlotHistogram] = new Vector4(1.00f, 0.00f, 0.00f, 1.00f);
colors[(int)ImGuiCol.PlotHistogramHovered] = new Vector4(1.00f, 0.00f, 0.00f, 1.00f);
colors[(int)ImGuiCol.TableHeaderBg] = new Vector4(0.00f, 0.00f, 0.00f, 0.52f);
colors[(int)ImGuiCol.TableBorderStrong] = new Vector4(0.00f, 0.00f, 0.00f, 0.52f);
colors[(int)ImGuiCol.TableBorderLight] = new Vector4(0.28f, 0.28f, 0.28f, 0.29f);
colors[(int)ImGuiCol.TableRowBg] = new Vector4(0.00f, 0.00f, 0.00f, 0.00f);
colors[(int)ImGuiCol.TableRowBgAlt] = new Vector4(1.00f, 1.00f, 1.00f, 0.06f);
colors[(int)ImGuiCol.TextSelectedBg] = new Vector4(0.20f, 0.22f, 0.23f, 1.00f);
colors[(int)ImGuiCol.DragDropTarget] = new Vector4(0.33f, 0.67f, 0.86f, 1.00f);
colors[(int)ImGuiCol.NavCursor] = new Vector4(1.00f, 0.00f, 0.00f, 1.00f);
colors[(int)ImGuiCol.NavWindowingHighlight] = new Vector4(1.00f, 0.00f, 0.00f, 0.70f);
colors[(int)ImGuiCol.NavWindowingDimBg] = new Vector4(1.00f, 0.00f, 0.00f, 0.20f);
colors[(int)ImGuiCol.ModalWindowDimBg] = new Vector4(0.10f, 0.10f, 0.10f, 0.00f);
style.WindowPadding = new Vector2(8.00f, 8.00f);
style.FramePadding = new Vector2(5.00f, 2.00f);
style.CellPadding = new Vector2(6.00f, 6.00f);
style.ItemSpacing = new Vector2(6.00f, 6.00f);
style.ItemInnerSpacing = new Vector2(6.00f, 6.00f);
style.TouchExtraPadding = new Vector2(0.00f, 0.00f);
style.IndentSpacing = 25;
style.ScrollbarSize = 15;
style.GrabMinSize = 10;
style.WindowBorderSize = 1;
style.ChildBorderSize = 1;
style.PopupBorderSize = 1;
style.FrameBorderSize = 1;
style.TabBorderSize = 1;
style.WindowRounding = 7;
style.ChildRounding = 4;
style.FrameRounding = 3;
style.PopupRounding = 4;
style.ScrollbarRounding = 9;
style.GrabRounding = 3;
style.LogSliderDeadzone = 4;
style.TabRounding = 4;
// When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if ((io.ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0)
{
style.WindowRounding = 0.0f;
style.Colors[(int)ImGuiCol.WindowBg].W = 1.0f;
}
}
public unsafe void NewFrame()
{
// Set ImGui context
ImGui.SetCurrentContext(guiContext);
// Set ImGui context for ImGuizmo
ImGuizmo.SetImGuiContext(guiContext);
// Set ImGui context for ImPlot
ImPlot.SetImGuiContext(guiContext);
// Set ImGui context for ImNodes
ImNodes.SetImGuiContext(guiContext);
// Set ImNodes context
ImNodes.SetCurrentContext(nodesContext);
// Set ImPlot context
ImPlot.SetCurrentContext(plotContext);
// Start new frame, call order matters.
ImGuiImplSDL2.NewFrame();
ImGui.NewFrame();
ImGuizmo.BeginFrame(); // mandatory for ImGuizmo
// Example for getting the central dockspace id of a window/viewport.
ImGui.PushStyleColor(ImGuiCol.WindowBg, Vector4.Zero);
DockSpaceId = ImGui.DockSpaceOverViewport(null, ImGuiDockNodeFlags.PassthruCentralNode, null); // passing null as first argument will use the main viewport
ImGui.PopStyleColor(1);
}
public static uint DockSpaceId { get; private set; }
public event Action OnRenderDrawData;
public unsafe void EndFrame()
{
// Renders ImGui Data
var io = ImGui.GetIO();
ImGui.Render();
ImGui.EndFrame();
OnRenderDrawData.Invoke();
// Update and Render additional Platform Windows
if ((io.ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0)
{
ImGui.UpdatePlatformWindows();
ImGui.RenderPlatformWindowsDefault();
}
}
private bool disposed = false;
public void Dispose()
{
if (!disposed)
{
ImPlot.SetCurrentContext(null);
ImPlot.SetImGuiContext(null);
ImNodes.SetCurrentContext(null);
ImNodes.SetImGuiContext(null);
ImGuizmo.SetImGuiContext(null);
ImPlot.DestroyContext(plotContext);
ImNodes.DestroyContext(nodesContext);
ImGui.SetCurrentContext(null);
ImGui.DestroyContext(guiContext);
disposed = true;
GC.SuppressFinalize(this);
}
}
}
}