Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 304b673

Browse files
committed
🎨 Refactor NervWindow.cs
1 parent 2a03670 commit 304b673

2 files changed

Lines changed: 145 additions & 129 deletions

File tree

src/NervUI/Application.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,4 @@ public static void DisableLogs()
9090
DefaultFont = null;
9191
Fonts.Clear();
9292
}
93-
94-
//AOT Does not support reflection so this method is disabled for now maybe will use mapper to do the trick
95-
/*public void PushLayer<T>()
96-
{
97-
//=> Layers.Add(((Layer)Activator.CreateInstance(typeof(T))));
98-
}*/
9993
}

src/NervUI/Framework/NervWindow.cs

Lines changed: 145 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,43 @@
1212

1313
namespace NervUI.Framework;
1414

15-
//TODO (ALL CLASSES) Clean and optimize code.
16-
1715
public unsafe class NervWindow : NativeWindow
1816
{
19-
private static bool _firstTime = true;
20-
17+
//Render and Platform controller
2118
private PlatformBackend _platformBackend;
2219
private RendererBackend _rendererBackend;
2320

21+
//Actions
2422
internal Action<uint> DockSpaceCallback;
2523
internal Action MenuBarCallback;
2624
internal Action StyleCallback;
25+
2726
internal Application Instance;
27+
private ApplicationOptions _options;
28+
private bool _firstTime = true;
29+
30+
private const string GLSL_VERSION_130 = "#version 130";
2831

32+
/// <summary>
33+
/// Create new NervWindow Instance
34+
/// </summary>
35+
/// <param name="nativeWindowSettings"></param>
36+
/// <param name="options"></param>
37+
/// <param name="instance"></param>
2938
public NervWindow(NativeWindowSettings nativeWindowSettings, ApplicationOptions options, Application instance) : base(nativeWindowSettings)
3039
{
31-
Instance = instance;
3240
Context.MakeCurrent();
41+
42+
Instance = instance;
43+
_options = options;
3344
VSync = options.VSync ? VSyncMode.On : VSyncMode.Off;
3445
WindowBorder = options.WindowBorder;
3546
WindowState = options.WindowState;
47+
3648
GL.LoadBindings(new GLFWBindingsContext());
3749
CenterWindow();
38-
39-
ImGui.CreateContext();
40-
var io = ImGui.GetIO();
41-
42-
string glsl_version = "#version 130";
43-
44-
if (options.Docking)
45-
io->ConfigFlags |= ImGuiConfigFlags.DockingEnable;
46-
47-
io->ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard;
48-
io->ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
49-
50-
if (Instance.DefaultFont != null)
51-
{
52-
Instance.DefaultFont.FontData =
53-
io->Fonts->AddFontFromFileTTF(Instance.DefaultFont.Path, Instance.DefaultFont.Size);
54-
io->FontDefault = Instance.DefaultFont.FontData;
55-
Instance.DefaultFont.Loaded = true;
56-
}
57-
58-
59-
foreach (var font in Instance.Fonts.Where(c => c.Loaded == false))
60-
{
61-
Core.Log($"Loaded font {font.Name} from {font.Path}", LogType.DEBUG);
62-
font.FontData = io->Fonts->AddFontFromFileTTF(font.Path, font.Size);
63-
font.Loaded = true;
64-
}
6550

66-
var style = ImGui.GetStyle();
67-
68-
if (StyleCallback == null)
69-
Util.SetStyle();
70-
else
71-
StyleCallback();
72-
73-
if (io->ConfigFlags.HasFlag(ImGuiConfigFlags.ViewportsEnable))
74-
{
75-
style->WindowRounding = 1f;
76-
style->Colors[(int)ImGuiCol.WindowBg].W = 1f;
77-
}
78-
79-
_platformBackend = new(this, true);
80-
_rendererBackend = new(glsl_version);
51+
SetupImGui();
8152

8253
//Log render info
8354
{
@@ -94,99 +65,149 @@ public NervWindow(NativeWindowSettings nativeWindowSettings, ApplicationOptions
9465
foreach (var layer in Instance.Layers)
9566
layer.OnWindowLoad();
9667
}
97-
68+
9869
internal void Run()
9970
{
100-
var io = ImGui.GetIO();
101-
Vector3 clearColor = new(0.45f, 0.55f, 0.6f);
102-
71+
ImGuiIO* io = ImGui.GetIO();
10372
while (!GLFW.WindowShouldClose(WindowPtr))
10473
{
10574
ProcessEvents();
10675

10776
_rendererBackend.NewFrame();
10877
_platformBackend.NewFrame();
10978
ImGui.NewFrame();
79+
11080
FileDialog.RenderFileDialog();
111-
//Dockspace and MenuBar
81+
82+
SetupDockspace();
83+
if (MessageBox.showMB)
11284
{
113-
var dockNodeFlags = ImGuiDockNodeFlags.None;
114-
var windowFlags = ImGuiWindowFlags.NoDocking;
115-
116-
if (MenuBarCallback != null)
117-
windowFlags |= ImGuiWindowFlags.MenuBar;
118-
119-
var viewport = ImGui.GetMainViewport();
120-
121-
ImGui.SetNextWindowPos(new Vector2(viewport->WorkPos.X, viewport->WorkPos.Y), ImGuiCond.None, new Vector2(0, 0));
122-
ImGui.SetNextWindowSize(viewport->WorkSize);
123-
ImGui.SetNextWindowViewport(viewport->ID);
124-
125-
windowFlags |= ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoResize |
126-
ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoBringToFrontOnFocus |
127-
ImGuiWindowFlags.NoNavFocus;
128-
129-
if (dockNodeFlags.HasFlag(ImGuiDockNodeFlags.PassthruCentralNode))
130-
windowFlags |= ImGuiWindowFlags.NoBackground;
131-
132-
ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, new Vector2(0, 0));
133-
ImGui.Begin("NervUI Dockspace", null, windowFlags);
134-
ImGui.PopStyleVar();
135-
136-
if (io->ConfigFlags.HasFlag(ImGuiConfigFlags.DockingEnable))
137-
{
138-
var dockspace_id = ImGui.GetID("OpenGLAppDockspace");
139-
ImGui.DockSpace(dockspace_id, new Vector2(0, 0), dockNodeFlags);
140-
141-
if (_firstTime)
142-
{
143-
_firstTime = false;
144-
145-
ImGuiInternal.DockBuilderRemoveNode(dockspace_id);
146-
ImGuiInternal.DockBuilderAddNode(dockspace_id, dockNodeFlags);
147-
ImGuiInternal.DockBuilderSetNodeSize(dockspace_id, viewport->Size);
148-
149-
if (DockSpaceCallback != null)
150-
DockSpaceCallback(dockspace_id);//dockNodeFlags
151-
}
152-
}
153-
154-
if (MenuBarCallback != null)
155-
if (ImGui.BeginMenuBar())
156-
{
157-
MenuBarCallback();
158-
ImGui.EndMenuBar();
159-
}
160-
161-
if (MessageBox.showMB)
162-
{
163-
ImGui.OpenPopup("MessageBoxPopup");
164-
MessageBox.RenderMessageBox();
165-
}
85+
//TODO Allow custom MessageBox Titles
86+
ImGui.OpenPopup("MessageBoxPopup");
87+
MessageBox.RenderMessageBox();
16688
}
167-
89+
16890
foreach (var layer in Instance.Layers)
16991
layer.OnUIRender();
92+
93+
ImGui.Render();
94+
GLFW.GetFramebufferSize(WindowPtr, out var displayW, out var displayH);
95+
GL.Viewport(0, 0, displayW, displayH);
96+
GL.ClearColor(0.45f, 0.55f, 0.6f, 1f);
97+
GL.Clear(ClearBufferMask.ColorBufferBit);
98+
_rendererBackend.RenderDrawData(ImGui.GetDrawData());
99+
100+
if (io->ConfigFlags.HasFlag(ImGuiConfigFlags.ViewportsEnable))
101+
{
102+
var backupCurrentContext = GLFW.GetCurrentContext();
103+
ImGui.UpdatePlatformWindows();
104+
ImGui.RenderPlatformWindowsDefault();
105+
GLFW.MakeContextCurrent(backupCurrentContext);
106+
}
170107

108+
GLFW.SwapBuffers(WindowPtr);
109+
}
110+
}
111+
112+
private void SetupDockspace()
113+
{
114+
ImGuiIO* io = ImGui.GetIO();
115+
116+
var dockNodeFlags = ImGuiDockNodeFlags.None;
117+
var windowFlags = ImGuiWindowFlags.NoDocking;
118+
windowFlags |= MenuBarCallback != null ? ImGuiWindowFlags.MenuBar : 0;
119+
120+
var viewport = ImGui.GetMainViewport();
121+
ImGui.SetNextWindowPos(new Vector2(viewport->WorkPos.X, viewport->WorkPos.Y), ImGuiCond.None, new Vector2(0, 0));
122+
ImGui.SetNextWindowSize(viewport->WorkSize);
123+
ImGui.SetNextWindowViewport(viewport->ID);
124+
125+
windowFlags |= ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoResize |
126+
ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoBringToFrontOnFocus |
127+
ImGuiWindowFlags.NoNavFocus;
128+
129+
windowFlags |= dockNodeFlags.HasFlag(ImGuiDockNodeFlags.PassthruCentralNode)
130+
? ImGuiWindowFlags.NoBackground
131+
: 0;
132+
133+
ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, new Vector2(0, 0));
134+
ImGui.Begin("NervUI Dockspace", null, windowFlags);
135+
ImGui.PopStyleVar();
136+
137+
if (io->ConfigFlags.HasFlag(ImGuiConfigFlags.DockingEnable))
138+
{
139+
var dockspace_id = ImGui.GetID("OpenGLAppDockspace");
140+
ImGui.DockSpace(dockspace_id, new Vector2(0, 0), dockNodeFlags);
141+
142+
if (_firstTime)
171143
{
172-
ImGui.Render();
173-
GLFW.GetFramebufferSize(WindowPtr, out var displayW, out var displayH);
174-
GL.Viewport(0, 0, displayW, displayH);
175-
GL.ClearColor(clearColor.X, clearColor.Y, clearColor.Z, 1f);
176-
GL.Clear(ClearBufferMask.ColorBufferBit);
177-
_rendererBackend.RenderDrawData(ImGui.GetDrawData());
178-
179-
if (io->ConfigFlags.HasFlag(ImGuiConfigFlags.ViewportsEnable))
180-
{
181-
var backupCurrentContext = GLFW.GetCurrentContext();
182-
ImGui.UpdatePlatformWindows();
183-
ImGui.RenderPlatformWindowsDefault();
184-
GLFW.MakeContextCurrent(backupCurrentContext);
185-
}
186-
187-
GLFW.SwapBuffers(WindowPtr);
144+
_firstTime = false;
145+
146+
ImGuiInternal.DockBuilderRemoveNode(dockspace_id);
147+
ImGuiInternal.DockBuilderAddNode(dockspace_id, dockNodeFlags);
148+
ImGuiInternal.DockBuilderSetNodeSize(dockspace_id, viewport->Size);
149+
150+
if (DockSpaceCallback != null)
151+
DockSpaceCallback(dockspace_id);//dockNodeFlags
188152
}
189153
}
154+
155+
if (MenuBarCallback != null)
156+
if (ImGui.BeginMenuBar())
157+
{
158+
MenuBarCallback();
159+
ImGui.EndMenuBar();
160+
}
161+
}
162+
163+
private void SetupImGui()
164+
{
165+
ImGui.CreateContext();
166+
ImGuiIO* io = ImGui.GetIO();
167+
168+
io->ConfigFlags |= _options.Docking ? ImGuiConfigFlags.DockingEnable : 0;
169+
io->ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard;
170+
io->ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
171+
172+
LoadFonts();
173+
174+
ImGuiStyle* style = ImGui.GetStyle();
175+
176+
if (StyleCallback == null)
177+
Util.SetStyle();
178+
else
179+
StyleCallback();
180+
181+
if (io->ConfigFlags.HasFlag(ImGuiConfigFlags.ViewportsEnable))
182+
{
183+
style->WindowRounding = 1f;
184+
style->Colors[(int)ImGuiCol.WindowBg].W = 1f;
185+
}
186+
187+
_platformBackend = new(this, true);
188+
_rendererBackend = new(GLSL_VERSION_130);
189+
}
190+
191+
private void LoadFonts()
192+
{
193+
ImGuiIO* io = ImGui.GetIO();
194+
195+
//Load Default font
196+
if (Instance.DefaultFont != null)
197+
{
198+
Instance.DefaultFont.FontData =
199+
io->Fonts->AddFontFromFileTTF(Instance.DefaultFont.Path, Instance.DefaultFont.Size);
200+
io->FontDefault = Instance.DefaultFont.FontData;
201+
Instance.DefaultFont.Loaded = true;
202+
}
203+
204+
//Load additional font(s)
205+
foreach (var font in Instance.Fonts.Where(c => c.Loaded == false))
206+
{
207+
Core.Log($"Loaded font {font.Name} from {font.Path}", LogType.DEBUG);
208+
font.FontData = io->Fonts->AddFontFromFileTTF(font.Path, font.Size);
209+
font.Loaded = true;
210+
}
190211
}
191212

192213
~NervWindow()
@@ -198,4 +219,5 @@ internal void Run()
198219
StyleCallback = null;
199220
Instance = null;
200221
}
201-
}
222+
}
223+

0 commit comments

Comments
 (0)