Skip to content

Commit 7c124e5

Browse files
committed
Dear ImGui integration
Ported the example win32 & d3d11 backends from the main imgui repo. I didn't bother implementing gamepads or multi-viewport yet since Nanoforge doesn't use them yet. Multi viewports also looks like a pain to convert.
1 parent d59f12a commit 7c124e5

28 files changed

+1541
-176
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,4 @@ recovery/
285285
build/
286286

287287
BeefSpace_User.toml
288+
imgui.ini

Assets/fonts/NotoSans-SemiBold.ttf

544 KB
Binary file not shown.
-351 KB
Binary file not shown.

BeefProj.toml

-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ Dependencies = {corlib = "*", ImGui = "*"}
33

44
[Project]
55
Name = "Nanoforge"
6-
TargetType = "BeefGUIApplication"
76
StartupObject = "Nanoforge.Program"

src/App/App.bf

+2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System.Collections;
22
using System.Diagnostics;
33
using System.Reflection;
4+
using Nanoforge.Misc;
45
using Nanoforge.Math;
56
using System;
67

78
namespace Nanoforge.App
89
{
10+
[ReflectAll]
911
public enum AppState
1012
{
1113
None,

src/Gui/FontManager.bf

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Nanoforge.Gui
1616
private static append List<u16> _iconRanges = .();
1717
public static bool EnableNonLatinGlyphs = false; //Currently disabled by default since I (only dev) only use english & enabling these increases boot time. Will enable when necessary
1818

19-
public static void RegisterFonts(BuildConfig buildConfig)
19+
public static void LoadFonts(BuildConfig buildConfig)
2020
{
2121
static bool firstRun = true;
2222
if (firstRun) //Build glyph list the first time this is called

src/Gui/Gui.bf

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ namespace Nanoforge.Gui
3232
[SystemStage(.Update)]
3333
void Update(App app)
3434
{
35-
/*for (var layer in Layers)
35+
for (var layer in Layers)
3636
if (layer.Enabled)
37-
layer.Update(app);*/
37+
layer.Update(app);
3838
}
3939

4040
//Destroys all gui layers and their modules
@@ -94,7 +94,7 @@ namespace Nanoforge.Gui
9494
}
9595

9696
//Helper functions & constants
97-
public static Vec4<f32> SecondaryTextColor => .(0.2f, 0.7f, 1.0f, 1.00f) * 0.92f; //Light blue;
97+
public static Vec4<f32> SecondaryTextColor => .(0.32f, 0.67f, 1.0f, 1.00f);//.(0.2f, 0.7f, 1.0f, 1.00f) * 0.92f; //Light blue;
9898
public static Vec4<f32> TertiaryTextColor => .(0.64f, 0.67f, 0.69f, 1.00f); //Light grey;
9999
public static Vec4<f32> Red => .(0.784f, 0.094f, 0.035f, 1.0f);
100100

src/Gui/Panels/MainMenuBar.bf

+2-4
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,10 @@ namespace Nanoforge.Gui.Panels
6060
private void DrawDockspace(App app)
6161
{
6262
//Dockspace flags
63-
dockspaceFlags = ImGui.DockNodeFlags.None | ImGui.DockNodeFlags.PassthruCentralNode;
63+
dockspaceFlags = ImGui.DockNodeFlags.None;
6464

6565
//Parent window flags
66-
ImGui.WindowFlags windowFlags = ImGui.WindowFlags.NoDocking | ImGui.WindowFlags.NoTitleBar | ImGui.WindowFlags.NoCollapse
67-
| ImGui.WindowFlags.NoResize | ImGui.WindowFlags.NoMove
68-
| ImGui.WindowFlags.NoBringToFrontOnFocus | ImGui.WindowFlags.NoNavFocus | ImGui.WindowFlags.NoBackground;
66+
ImGui.WindowFlags windowFlags = .NoDocking | .NoTitleBar | .NoCollapse | .NoResize | .NoMove | .NoBringToFrontOnFocus | .NoNavFocus | .NoBackground;
6967

7068
//Set dockspace size and params
7169
var viewport = ImGui.GetMainViewport();

src/Misc/Extensions.bf

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ namespace System
2424
return str;
2525
}
2626

27-
public static String ValueToString<T>(String str, T value)
27+
public static String ValueToString<T>(String str, T value) where T : operator explicit int, enum
2828
{
29-
T val = value;
29+
int val = (int)value;
3030
Type type = typeof(T);
3131
Runtime.Assert(type.IsEnum);
3232
for (var enumField in type.GetFields())

src/Misc/Input.bf

+19-7
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ namespace Nanoforge.Misc
158158
//X2MouseButton = 6,
159159
Backspace = 8,
160160
Tab = 9,
161-
Clear = 0x10,
162161
Enter = 0xD,
163162
Shift = 0x10,
164163
Control = 0x11,
@@ -219,7 +218,7 @@ namespace Nanoforge.Misc
219218
Y = 0x59,
220219
Z = 0x5A,
221220
WindowsLeft = 0x5B,
222-
WindowRight = 0x5C,
221+
WindowsRight = 0x5C,
223222
Applications = 0x5D,
224223
Sleep = 0x5F,
225224
Numpad0 = 0x60,
@@ -232,12 +231,12 @@ namespace Nanoforge.Misc
232231
Numpad7 = 0x67,
233232
Numpad8 = 0x68,
234233
Numpad9 = 0x69,
235-
Multiply = 0x6A,
236-
Add = 0x6B,
234+
NumpadMultiply = 0x6A,
235+
NumpadAdd = 0x6B,
237236
Separator = 0x6C,
238-
Subtract = 0x6D,
239-
Decimal = 0x6E,
240-
Divide = 0x6F,
237+
NumpadSubtract = 0x6D,
238+
NumpadDecimal = 0x6E,
239+
NumpadDivide = 0x6F,
241240
F1 = 0x70,
242241
F2 = 0x71,
243242
F3 = 0x72,
@@ -262,6 +261,8 @@ namespace Nanoforge.Misc
262261
F22 = 0x85,
263262
F23 = 0x86,
264263
F24 = 0x87,
264+
NumLock = 0x90,
265+
ScrollLock = 0x91,
265266
LeftShift = 0xA0,
266267
RightShift = 0xA1,
267268
LeftControl = 0xA2,
@@ -275,6 +276,17 @@ namespace Nanoforge.Misc
275276
MediaPreviousTrack = 0xB1,
276277
MediaStop = 0xB2,
277278
MediaPlayPause = 0xB3,
279+
Comma = 0xBC,
280+
Minus = 0xBD,
281+
Period = 0xBE,
282+
Slash = 0xBF,
283+
Semicolon = 0xBA,
284+
AccentGrave = 0xC0,
285+
LeftBracket = 0xDB,
286+
BackSlash = 0xDC,
287+
RightBracket = 0xDD,
288+
Apostrophe = 0xDE,
289+
Equal = 0xBB,
278290
Play = 0xFA,
279291
Zoom = 0xFB
280292
}

src/Misc/Reflection.bf

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using System;
33

44
namespace Nanoforge.Misc
55
{
6-
[AttributeUsage(.Class | .Interface, .AlwaysIncludeTarget | .ReflectAttribute, ReflectUser = .All, AlwaysIncludeUser = .IncludeAllMethods | .AssumeInstantiated)]
6+
[AttributeUsage(.Class | .Interface | .Enum, .AlwaysIncludeTarget | .ReflectAttribute, ReflectUser = .All, AlwaysIncludeUser = .IncludeAllMethods | .AssumeInstantiated)]
77
public struct ReflectAllAttribute : Attribute
88
{
99

src/Misc/Window.bf

+16-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace Nanoforge.Misc
1818
public HWND Handle { get; private set; } = 0;
1919
public bool Focused { get; private set; } = true;
2020
public bool ShouldClose { get; private set; } = false;
21+
//Use to add other functions to pass window messages to
22+
public Event<delegate LRESULT(HWND hwnd, uint32 msg, WPARAM wParam, LPARAM lParam)> WndProcExtensions ~ _.Dispose();
2123
private Input _input = null;
2224

2325
///The actual WndProc logic is in a member function called by this delegate. That way it has access to the window fields
@@ -43,13 +45,13 @@ namespace Nanoforge.Misc
4345
//Define window class
4446
WNDCLASSEXW wc = .();
4547
wc.cbSize = sizeof(WNDCLASSEXW);
46-
wc.style = .HREDRAW | .VREDRAW | .DROPSHADOW;
48+
wc.style = .HREDRAW | .VREDRAW;
4749
wc.lpfnWndProc = => WNDPROC;
4850
wc.cbClsExtra = 0;
4951
wc.cbWndExtra = 0;
5052
wc.hInstance = hInstance;
5153
wc.hIcon = Win32.LoadIconW(0, Win32.IDI_WINLOGO);
52-
wc.hCursor = Win32.LoadCursorW(0, Win32.IDC_ARROW);
54+
wc.hCursor = Win32.LoadCursorW(0, Win32.IDC_SIZEWE);
5355
wc.hbrBackground = (int)SYS_COLOR_INDEX.COLOR_MENUTEXT;
5456
wc.lpszMenuName = null;
5557
wc.lpszClassName = &windowClassName;
@@ -62,8 +64,8 @@ namespace Nanoforge.Misc
6264
if (Handle == 0)
6365
Win32.FatalError!("CreateWindowExW() failed while creating window");
6466

65-
//Maximize window
66-
Win32.ShowWindow(Handle, .MAXIMIZE);
67+
//Show window
68+
Win32.ShowWindow(Handle, .SHOW);
6769
}
6870

6971
//Process window events
@@ -87,9 +89,13 @@ namespace Nanoforge.Misc
8789

8890
LRESULT InstanceWNDPROC(HWND hwnd, uint32 msg, WPARAM wParam, LPARAM lParam)
8991
{
92+
for (var dlg in WndProcExtensions)
93+
if (dlg(hwnd, msg, wParam, lParam) != 0)
94+
return 1;
95+
9096
switch (msg)
9197
{
92-
case Win32.WM_CLOSE,Win32.WM_QUIT,Win32.WM_DESTROY:
98+
case Win32.WM_CLOSE, Win32.WM_QUIT, Win32.WM_DESTROY:
9399
ShouldClose = true;
94100
return 0;
95101
case Win32.WM_SIZE:
@@ -125,6 +131,11 @@ namespace Nanoforge.Misc
125131
{
126132
return WndProcDelegate(hwnd, msg, wParam, lParam);
127133
}
134+
135+
public void Maximize()
136+
{
137+
Win32.ShowWindow(Handle, .MAXIMIZE);
138+
}
128139
}
129140

130141
//Matches wParam of WndProc WM_SIZE message: https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-size

src/Program.bf

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Nanoforge.Systems;
12
using Nanoforge.Render;
23
using Nanoforge.Misc;
34
using Nanoforge.App;
@@ -25,6 +26,7 @@ namespace Nanoforge
2526
..AddSystem<Input>(isResource: true)
2627
..AddSystem<Renderer>()
2728
..AddSystem<Gui>(isResource: true)
29+
..AddSystem<AppLogic>()
2830
.Run();
2931
}
3032
}

0 commit comments

Comments
 (0)