Refactor: Simplify window creation by limiting it to a single window per namespace.

This commit is contained in:
IntegratedQuantum 2023-03-17 11:25:46 +01:00
parent 966c3f8857
commit fd0e903431
12 changed files with 76 additions and 132 deletions

View File

@ -56,7 +56,8 @@ relativePosition: [2]RelativePosition = .{.{.ratio = 0.5}, .{.ratio = 0.5}},
showTitleBar: bool = true,
title: []const u8 = "",
id: []const u8,
components: []GuiComponent,
components: []GuiComponent = &.{},
isHud: bool = false,
/// Called every frame.
renderFn: *const fn()Allocator.Error!void = &defaultErrorFunction,

View File

@ -35,7 +35,7 @@ pub fn init(_allocator: Allocator) !void {
hudWindows = std.ArrayList(*GuiWindow).init(allocator);
openWindows = std.ArrayList(*GuiWindow).init(allocator);
inline for(@typeInfo(windowlist).Struct.decls) |decl| {
try @field(windowlist, decl.name).init();
try addWindow(&@field(windowlist, decl.name).window);
}
try GuiWindow.__init();
try Button.__init();
@ -174,14 +174,14 @@ pub fn updateGuiScale() void {
}
}
pub fn addWindow(window: *GuiWindow, isHudWindow: bool) !void {
fn addWindow(window: *GuiWindow) !void {
for(windowList.items) |other| {
if(std.mem.eql(u8, window.id, other.id)) {
std.log.err("Duplicate window id: {s}", .{window.id});
return;
}
}
if(isHudWindow) {
if(window.isHud) {
try hudWindows.append(window);
window.showTitleBar = false;
}

View File

@ -13,19 +13,15 @@ const Label = @import("../components/Label.zig");
const TextInput = @import("../components/TextInput.zig");
const VerticalList = @import("../components/VerticalList.zig");
var window: GuiWindow = undefined;
var components: [1]GuiComponent = undefined;
pub fn init() !void {
window = GuiWindow{
pub var window = GuiWindow {
.contentSize = Vec2f{128, 256},
.id = "cubyz:change_name",
.title = "Change Name",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.components = &components,
};
try gui.addWindow(&window, true);
}
};
const padding: f32 = 8;

View File

@ -11,10 +11,8 @@ const Button = @import("../components/Button.zig");
const Label = @import("../components/Label.zig");
const VerticalList = @import("../components/VerticalList.zig");
var window: GuiWindow = undefined;
var components: [1]GuiComponent = undefined;
pub fn init() !void {
window = GuiWindow{
pub var window = GuiWindow {
.contentSize = Vec2f{128, 256},
.id = "cubyz:controls",
.title = "Controls",
@ -22,9 +20,7 @@ pub fn init() !void {
.onCloseFn = &onClose,
.renderFn = &render,
.components = &components,
};
try gui.addWindow(&window, true);
}
};
const padding: f32 = 8;
var selectedKey: ?*main.Key = null;

View File

@ -13,19 +13,15 @@ const CheckBox = @import("../components/CheckBox.zig");
const Slider = @import("../components/Slider.zig");
const VerticalList = @import("../components/VerticalList.zig");
var window: GuiWindow = undefined;
var components: [1]GuiComponent = undefined;
pub fn init() !void {
window = GuiWindow{
pub var window = GuiWindow {
.contentSize = Vec2f{128, 256},
.id = "cubyz:graphics",
.title = "Graphics",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.components = &components,
};
try gui.addWindow(&window, true);
}
};
const padding: f32 = 8;

View File

@ -8,17 +8,13 @@ const gui = @import("../gui.zig");
const GuiWindow = gui.GuiWindow;
const GuiComponent = gui.GuiComponent;
var healthbarWindow: GuiWindow = undefined;
pub fn init() !void {
healthbarWindow = GuiWindow{
pub var window = GuiWindow {
.contentSize = Vec2f{128, 16},
.title = "Health Bar",
.id = "cubyz:healthbar",
.renderFn = &render,
.components = &[_]GuiComponent{},
};
try gui.addWindow(&healthbarWindow, true);
}
.isHud = true,
};
pub fn render() Allocator.Error!void {

View File

@ -8,35 +8,14 @@ const gui = @import("../gui.zig");
const GuiComponent = gui.GuiComponent;
const GuiWindow = gui.GuiWindow;
var hotbarWindow: GuiWindow = undefined;
var hotbarWindow2: GuiWindow = undefined;
var hotbarWindow3: GuiWindow = undefined;
pub fn init() !void {
hotbarWindow = GuiWindow {
pub var window = GuiWindow {
.contentSize = Vec2f{64*8, 64},
.title = "Hotbar",
.id = "cubyz:hotbar",
.renderFn = &render,
.components = &[_]GuiComponent{},
};
try gui.addWindow(&hotbarWindow, true);
hotbarWindow2 = GuiWindow {
.contentSize = Vec2f{64*8, 64},
.title = "Hotbar2",
.id = "cubyz:hotbar2",
.renderFn = &render,
.components = &[_]GuiComponent{},
};
try gui.addWindow(&hotbarWindow2, true);
hotbarWindow3 = GuiWindow {
.contentSize = Vec2f{64*8, 64},
.title = "Hotbar3",
.id = "cubyz:hotbar3",
.renderFn = &render,
.components = &[_]GuiComponent{},
};
try gui.addWindow(&hotbarWindow3, true);
}
.isHud = true,
};
pub fn render() Allocator.Error!void {

View File

@ -10,18 +10,14 @@ const GuiWindow = gui.GuiWindow;
const Button = @import("../components/Button.zig");
const VerticalList = @import("../components/VerticalList.zig");
var window: GuiWindow = undefined;
var components: [1]GuiComponent = undefined;
pub fn init() !void {
window = GuiWindow{
pub var window = GuiWindow {
.contentSize = Vec2f{128, 256},
.id = "cubyz:main",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.components = &components,
};
try gui.addWindow(&window, true);
}
};
pub fn buttonCallbackTest() void {
std.log.info("Clicked!", .{});

View File

@ -14,19 +14,15 @@ const Label = @import("../components/Label.zig");
const TextInput = @import("../components/TextInput.zig");
const VerticalList = @import("../components/VerticalList.zig");
var window: GuiWindow = undefined;
var components: [1]GuiComponent = undefined;
pub fn init() !void {
window = GuiWindow{
pub var window = GuiWindow {
.contentSize = Vec2f{128, 256},
.id = "cubyz:multiplayer",
.title = "Multiplayer",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.components = &components,
};
try gui.addWindow(&window, true);
}
};
const padding: f32 = 8;

View File

@ -10,19 +10,15 @@ const GuiWindow = gui.GuiWindow;
const Button = @import("../components/Button.zig");
const VerticalList = @import("../components/VerticalList.zig");
var window: GuiWindow = undefined;
var components: [1]GuiComponent = undefined;
pub fn init() !void {
window = GuiWindow{
pub var window: GuiWindow = GuiWindow {
.contentSize = Vec2f{128, 256},
.id = "cubyz:settings",
.title = "Settings",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.components = &components,
};
try gui.addWindow(&window, true);
}
};
const padding: f32 = 8;

View File

@ -10,19 +10,15 @@ const GuiWindow = gui.GuiWindow;
const Button = @import("../components/Button.zig");
const VerticalList = @import("../components/VerticalList.zig");
var window: GuiWindow = undefined;
var components: [1]GuiComponent = undefined;
pub fn init() !void {
window = GuiWindow{
pub var window = GuiWindow {
.contentSize = Vec2f{128, 256},
.id = "cubyz:sound",
.title = "Sound TODO",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.components = &components,
};
try gui.addWindow(&window, true);
}
};
const padding: f32 = 8;

View File

@ -500,10 +500,6 @@ pub fn main() !void {
if(settings.playerName.len == 0) {
try gui.openWindow("cubyz:change_name");
} else {
try gui.openWindow("cubyz:hotbar");
try gui.openWindow("cubyz:hotbar2");
try gui.openWindow("cubyz:hotbar3");
try gui.openWindow("cubyz:healthbar");
try gui.openWindow("cubyz:main");
}