diff --git a/src/gui/gui.zig b/src/gui/gui.zig index c21905dd..af15d1ce 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -38,9 +38,13 @@ pub fn init(_allocator: Allocator) !void { inline for(@typeInfo(windowlist).Struct.decls) |decl| { const windowStruct = @field(windowlist, decl.name); try addWindow(&windowStruct.window); - inline for(@typeInfo(windowStruct).Struct.decls) |_decl| { - if(comptime std.mem.eql(u8, _decl.name, "init")) { - try windowStruct.init(); + if(@hasDecl(windowStruct, "init")) { + try windowStruct.init(); + } + const functionNames = [_][]const u8{"render", "update", "updateSelected", "updateHovered", "onOpen", "onClose"}; + inline for(functionNames) |function| { + if(@hasDecl(windowStruct, function)) { + @field(windowStruct.window, function ++ "Fn") = &@field(windowStruct, function); } } } diff --git a/src/gui/gui_component.zig b/src/gui/gui_component.zig index dd61410b..2f2de31f 100644 --- a/src/gui/gui_component.zig +++ b/src/gui/gui_component.zig @@ -32,11 +32,8 @@ pub const GuiComponent = union(enum) { pub fn deinit(self: GuiComponent) void { switch(self) { inline else => |impl| { - // Only call the function if it exists: - inline for(@typeInfo(@TypeOf(impl.*)).Struct.decls) |decl| { - if(comptime std.mem.eql(u8, decl.name, "deinit")) { - impl.deinit(); - } + if(@hasDecl(@TypeOf(impl.*), "deinit")) { + impl.deinit(); } } } @@ -77,11 +74,8 @@ pub const GuiComponent = union(enum) { pub fn updateSelected(self: GuiComponent) void { switch(self) { inline else => |impl| { - // Only call the function if it exists: - inline for(@typeInfo(@TypeOf(impl.*)).Struct.decls) |decl| { - if(comptime std.mem.eql(u8, decl.name, "updateSelected")) { - impl.updateSelected(); - } + if(@hasDecl(@TypeOf(impl.*), "updateSelected")) { + impl.updateSelected(); } } } @@ -90,11 +84,8 @@ pub const GuiComponent = union(enum) { pub fn updateHovered(self: GuiComponent, mousePosition: Vec2f) void { switch(self) { inline else => |impl| { - // Only call the function if it exists: - inline for(@typeInfo(@TypeOf(impl.*)).Struct.decls) |decl| { - if(comptime std.mem.eql(u8, decl.name, "updateHovered")) { - impl.updateHovered(mousePosition); - } + if(@hasDecl(@TypeOf(impl.*), "updateHovered")) { + impl.updateHovered(mousePosition); } } } @@ -103,11 +94,8 @@ pub const GuiComponent = union(enum) { pub fn render(self: GuiComponent, mousePosition: Vec2f) !void { switch(self) { inline else => |impl| { - // Only call the function if it exists: - inline for(@typeInfo(@TypeOf(impl.*)).Struct.decls) |decl| { - if(comptime std.mem.eql(u8, decl.name, "render")) { - try impl.render(mousePosition); - } + if(@hasDecl(@TypeOf(impl.*), "render")) { + try impl.render(mousePosition); } } } @@ -116,11 +104,8 @@ pub const GuiComponent = union(enum) { pub fn mainButtonPressed(self: GuiComponent, mousePosition: Vec2f) void { switch(self) { inline else => |impl| { - // Only call the function if it exists: - inline for(@typeInfo(@TypeOf(impl.*)).Struct.decls) |decl| { - if(comptime std.mem.eql(u8, decl.name, "mainButtonPressed")) { - impl.mainButtonPressed(mousePosition); - } + if(@hasDecl(@TypeOf(impl.*), "mainButtonPressed")) { + impl.mainButtonPressed(mousePosition); } } } @@ -129,11 +114,8 @@ pub const GuiComponent = union(enum) { pub fn mainButtonReleased(self: GuiComponent, mousePosition: Vec2f) void { switch(self) { inline else => |impl| { - // Only call the function if it exists: - inline for(@typeInfo(@TypeOf(impl.*)).Struct.decls) |decl| { - if(comptime std.mem.eql(u8, decl.name, "mainButtonReleased")) { - impl.mainButtonReleased(mousePosition); - } + if(@hasDecl(@TypeOf(impl.*), "mainButtonReleased")) { + impl.mainButtonReleased(mousePosition); } } } diff --git a/src/gui/windows/change_name.zig b/src/gui/windows/change_name.zig index b0ee66c0..d7252e28 100644 --- a/src/gui/windows/change_name.zig +++ b/src/gui/windows/change_name.zig @@ -18,8 +18,6 @@ pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:change_name", .title = "Change Name", - .onOpenFn = &onOpen, - .onCloseFn = &onClose, .components = &components, }; var textComponent: *TextInput = undefined; diff --git a/src/gui/windows/chat.zig b/src/gui/windows/chat.zig index d98fa9bb..158de27a 100644 --- a/src/gui/windows/chat.zig +++ b/src/gui/windows/chat.zig @@ -18,10 +18,6 @@ pub var window: GuiWindow = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:chat", .title = "Chat", - .onOpenFn = &onOpen, - .onCloseFn = &onClose, - .updateFn = &update, - .renderFn = &render, .components = &components, .showTitleBar = false, .hasBackground = false, diff --git a/src/gui/windows/controls.zig b/src/gui/windows/controls.zig index 942c1db7..a5113d7b 100644 --- a/src/gui/windows/controls.zig +++ b/src/gui/windows/controls.zig @@ -17,9 +17,6 @@ pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:controls", .title = "Controls", - .onOpenFn = &onOpen, - .onCloseFn = &onClose, - .renderFn = &render, .components = &components, }; diff --git a/src/gui/windows/crosshair.zig b/src/gui/windows/crosshair.zig index 773135d1..4330832f 100644 --- a/src/gui/windows/crosshair.zig +++ b/src/gui/windows/crosshair.zig @@ -16,7 +16,6 @@ pub var window = GuiWindow { .contentSize = Vec2f{size, size}, .title = "Crosshair", .id = "cubyz:crosshair", - .renderFn = &render, .showTitleBar = false, .hasBackground = false, .isHud = true, diff --git a/src/gui/windows/graphics.zig b/src/gui/windows/graphics.zig index af884dd3..88bf4da4 100644 --- a/src/gui/windows/graphics.zig +++ b/src/gui/windows/graphics.zig @@ -18,8 +18,6 @@ pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:graphics", .title = "Graphics", - .onOpenFn = &onOpen, - .onCloseFn = &onClose, .components = &components, }; diff --git a/src/gui/windows/healthbar.zig b/src/gui/windows/healthbar.zig index c449e16e..e2062d91 100644 --- a/src/gui/windows/healthbar.zig +++ b/src/gui/windows/healthbar.zig @@ -15,7 +15,6 @@ pub var window = GuiWindow { .contentSize = Vec2f{128, 16}, .title = "Health Bar", .id = "cubyz:healthbar", - .renderFn = &render, .isHud = true, .showTitleBar = false, .hasBackground = false, diff --git a/src/gui/windows/hotbar.zig b/src/gui/windows/hotbar.zig index bad33537..d5af3738 100644 --- a/src/gui/windows/hotbar.zig +++ b/src/gui/windows/hotbar.zig @@ -16,9 +16,6 @@ pub var window = GuiWindow { .contentSize = Vec2f{64*8, 64}, .title = "Hotbar", .id = "cubyz:hotbar", - .onOpenFn = &onOpen, - .onCloseFn = &onClose, - .renderFn = &render, .components = &components, .isHud = true, .showTitleBar = false, diff --git a/src/gui/windows/main.zig b/src/gui/windows/main.zig index 91926d36..3fc9363f 100644 --- a/src/gui/windows/main.zig +++ b/src/gui/windows/main.zig @@ -14,8 +14,6 @@ var components: [1]GuiComponent = undefined; pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:main", - .onOpenFn = &onOpen, - .onCloseFn = &onClose, .components = &components, }; diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index fcdf2ffb..adf909b0 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -19,9 +19,6 @@ pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:multiplayer", .title = "Multiplayer", - .onOpenFn = &onOpen, - .onCloseFn = &onClose, - .updateFn = &update, .components = &components, }; diff --git a/src/gui/windows/settings.zig b/src/gui/windows/settings.zig index e0a10e2e..18861216 100644 --- a/src/gui/windows/settings.zig +++ b/src/gui/windows/settings.zig @@ -15,8 +15,6 @@ pub var window: GuiWindow = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:settings", .title = "Settings", - .onOpenFn = &onOpen, - .onCloseFn = &onClose, .components = &components, }; diff --git a/src/gui/windows/sound.zig b/src/gui/windows/sound.zig index 9e918662..2ec1b180 100644 --- a/src/gui/windows/sound.zig +++ b/src/gui/windows/sound.zig @@ -15,8 +15,6 @@ pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:sound", .title = "Sound TODO", - .onOpenFn = &onOpen, - .onCloseFn = &onClose, .components = &components, };