Skip to content

Commit 1a3895d

Browse files
committed
Gui cleanup
Remove gui layers & move imgui extension functions to that namespace. Gui layers are a concept from the project I used as a base for this. Not needed by Nanoforge at the moment
1 parent 74b30a4 commit 1a3895d

File tree

5 files changed

+46
-136
lines changed

5 files changed

+46
-136
lines changed

Diff for: src/Gui/Extensions.bf

+34
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
using Nanoforge.Math;
2+
using Nanoforge.Gui;
23
using Nanoforge;
34
using System;
45

56
namespace ImGui
67
{
78
extension ImGui
89
{
10+
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;
11+
public static Vec4<f32> TertiaryTextColor => .(0.64f, 0.67f, 0.69f, 1.00f); //Light grey;
12+
public static Vec4<f32> Red => .(0.784f, 0.094f, 0.035f, 1.0f);
13+
914
[Comptime]
1015
private static void ComptimeChecks()
1116
{
@@ -45,6 +50,35 @@ namespace ImGui
4550
return ImGui.TreeNodeEx(str.Ptr, flags);
4651
}
4752

53+
//Draw label and value next to each other with value using secondary color
54+
public static void LabelAndValue(StringView label, StringView value, Vec4<f32> color)
55+
{
56+
ImGui.Text(label);
57+
ImGui.SameLine();
58+
ImGui.SetCursorPosX(ImGui.GetCursorPosX() - 4.0f);
59+
ImGui.TextColored(value, color);
60+
}
61+
62+
//Add mouse-over tooltip to previous ui element. Returns true if the target is being hovered
63+
public static bool TooltipOnPrevious(StringView description, ImGui.Font* Font = null)
64+
{
65+
var font = Font;
66+
if(font == null)
67+
font = FontManager.FontDefault.Font;
68+
69+
bool hovered = ImGui.IsItemHovered();
70+
if (hovered)
71+
{
72+
ImGui.PushFont(Font);
73+
ImGui.BeginTooltip();
74+
ImGui.PushTextWrapPos(ImGui.GetFontSize() * 35.0f);
75+
ImGui.TextUnformatted(description.Ptr);
76+
ImGui.PopTextWrapPos();
77+
ImGui.EndTooltip();
78+
ImGui.PopFont();
79+
}
80+
return hovered;
81+
}
4882

4983
extension Vec4
5084
{

Diff for: src/Gui/Gui.bf

+6-101
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Nanoforge.Gui
1111
[System]
1212
public class Gui : ISystem
1313
{
14-
public List<GuiLayer> Layers = new List<GuiLayer>() ~ DeleteContainerAndItems!(Layers);
14+
public List<IGuiPanel> Panels = new .() ~DeleteContainerAndItems!(_);
1515

1616
static void ISystem.Build(App app)
1717
{
@@ -21,111 +21,16 @@ namespace Nanoforge.Gui
2121
[SystemInit]
2222
void Init(App app)
2323
{
24-
//Add default debug UI elements
25-
GuiLayer debugLayer = GetOrCreateGuiLayer("Debug");
26-
debugLayer._panels.Add(new MainMenuBar());
27-
debugLayer._panels.Add(new DebugGui());
28-
debugLayer._panels.Add(new StateViewer());
29-
debugLayer.Enabled = true;
24+
Panels.Add(new MainMenuBar());
25+
Panels.Add(new DebugGui());
26+
Panels.Add(new StateViewer());
3027
}
3128

3229
[SystemStage(.Update)]
3330
void Update(App app)
3431
{
35-
for (var layer in Layers)
36-
if (layer.Enabled)
37-
layer.Update(app);
38-
}
39-
40-
//Destroys all gui layers and their modules
41-
public void Reset()
42-
{
43-
ClearAndDeleteItems(Layers);
44-
}
45-
46-
public void EnableLayer(char8* name)
47-
{
48-
for (var layer in Layers)
49-
if (layer.Name == name)
50-
layer.Enabled = true;
51-
}
52-
53-
public void DisableLayer(char8* name)
54-
{
55-
for (var layer in Layers)
56-
if (layer.Name == name)
57-
layer.Enabled = false;
58-
}
59-
60-
public void ToggleLayer(char8* name)
61-
{
62-
for (var layer in Layers)
63-
if (layer.Name == name)
64-
layer.Enabled = !layer.Enabled;
65-
}
66-
67-
public void AddGuiToLayer(char8* layerName, IGuiPanel gui)
68-
{
69-
var layer = GetOrCreateGuiLayer(layerName);
70-
layer.[Friend]_panels.Add(gui);
71-
}
72-
73-
private Result<GuiLayer> GetGuiLayer(char8* layerName)
74-
{
75-
for (var layer in Layers)
76-
if (layer.Name == layerName)
77-
return layer;
78-
79-
return .Err;
80-
}
81-
82-
private GuiLayer GetOrCreateGuiLayer(char8* layerName)
83-
{
84-
var maybeLayer = GetGuiLayer(layerName);
85-
if (maybeLayer == .Err)
86-
{
87-
Layers.Add(new GuiLayer(layerName, new List<IGuiPanel>(), true));
88-
return Layers.Back;
89-
}
90-
else
91-
{
92-
return maybeLayer.Value;
93-
}
94-
}
95-
96-
//Helper functions & constants
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;
98-
public static Vec4<f32> TertiaryTextColor => .(0.64f, 0.67f, 0.69f, 1.00f); //Light grey;
99-
public static Vec4<f32> Red => .(0.784f, 0.094f, 0.035f, 1.0f);
100-
101-
//Draw label and value next to each other with value using secondary color
102-
public static void LabelAndValue(StringView label, StringView value, Vec4<f32> color = SecondaryTextColor)
103-
{
104-
ImGui.Text(label);
105-
ImGui.SameLine();
106-
ImGui.SetCursorPosX(ImGui.GetCursorPosX() - 4.0f);
107-
ImGui.TextColored(value, color);
108-
}
109-
110-
//Add mouse-over tooltip to previous ui element. Returns true if the target is being hovered
111-
public static bool TooltipOnPrevious(StringView description, ImGui.Font* Font = null)
112-
{
113-
var font = Font;
114-
if(font == null)
115-
font = FontManager.FontDefault.Font;
116-
117-
bool hovered = ImGui.IsItemHovered();
118-
if (hovered)
119-
{
120-
ImGui.PushFont(Font);
121-
ImGui.BeginTooltip();
122-
ImGui.PushTextWrapPos(ImGui.GetFontSize() * 35.0f);
123-
ImGui.TextUnformatted(description.Ptr);
124-
ImGui.PopTextWrapPos();
125-
ImGui.EndTooltip();
126-
ImGui.PopFont();
127-
}
128-
return hovered;
32+
for (IGuiPanel panel in Panels)
33+
panel.Update(app);
12934
}
13035
}
13136
}

Diff for: src/Gui/GuiLayer.bf

-29
This file was deleted.

Diff for: src/Gui/Panels/MainMenuBar.bf

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ namespace Nanoforge.Gui.Panels
4646
}
4747

4848
var drawList = ImGui.GetWindowDrawList();
49-
String realFrameTime = scope String()..AppendF("{0:G4}", frameData.RealFrameTime * 1000.0f);
49+
String realFrameTime = scope String()..AppendF("{0:G3}", frameData.AverageFrameTime * 1000.0f);
5050
String totalFrameTime = scope String()..AppendF("/ {0:G4}", frameData.DeltaTime * 1000.0f);
5151
drawList.AddText(.(ImGui.GetCursorPosX(), 5.0f), 0xF2F5FAFF, "| Frametime (ms): ");
5252
var textSize = ImGui.CalcTextSize("| Frametime (ms): ");
53-
drawList.AddText(.(ImGui.GetCursorPosX() + (f32)textSize.x, 5.0f), ImGui.ColorConvertFloat4ToU32(Gui.SecondaryTextColor), realFrameTime.CStr());
54-
drawList.AddText(.(ImGui.GetCursorPosX() + (f32)textSize.x + 42.0f, 5.0f), ImGui.ColorConvertFloat4ToU32(Gui.SecondaryTextColor), totalFrameTime.CStr());
53+
drawList.AddText(.(ImGui.GetCursorPosX() + (f32)textSize.x, 5.0f), ImGui.ColorConvertFloat4ToU32(ImGui.SecondaryTextColor), realFrameTime.CStr());
54+
drawList.AddText(.(ImGui.GetCursorPosX() + (f32)textSize.x + 42.0f, 5.0f), ImGui.ColorConvertFloat4ToU32(ImGui.SecondaryTextColor), totalFrameTime.CStr());
5555

5656
ImGui.EndMainMenuBar();
5757
}

Diff for: src/Gui/Panels/StateViewer.bf

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Nanoforge.Gui.Panels
2828
ImGui.SameLine();
2929
if (i == app.[Friend]_stateStack.Count - 1) //Top of the state stack (active state)
3030
{
31-
ImGui.TextColored(Enum.ValueToString<AppState>(.. scope .(), state), Gui.SecondaryTextColor);
31+
ImGui.TextColored(Enum.ValueToString<AppState>(.. scope .(), state), ImGui.SecondaryTextColor);
3232
}
3333
else
3434
{
@@ -187,10 +187,10 @@ namespace Nanoforge.Gui.Panels
187187
{
188188
ImGui.TableNextRow();
189189
ImGui.TableNextColumn();
190-
ImGui.TextColored(scope $"{systemFunc.ClassName}.", Gui.TertiaryTextColor);
190+
ImGui.TextColored(scope $"{systemFunc.ClassName}.", ImGui.TertiaryTextColor);
191191
ImGui.SameLine();
192192
ImGui.SetCursorPosX(ImGui.GetCursorPosX() - 8.0f);
193-
ImGui.TextColored(scope $"{systemFunc.Name}()", Gui.SecondaryTextColor);
193+
ImGui.TextColored(scope $"{systemFunc.Name}()", ImGui.SecondaryTextColor);
194194

195195
ImGui.TableNextColumn();
196196
if (runTypeName != null)

0 commit comments

Comments
 (0)