mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-03 19:28:49 -04:00
Use @hasDecl
and automatically assign the window function pointers.
This commit is contained in:
parent
c2b0428539
commit
758fe2c8ab
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
};
|
||||
|
||||
|
@ -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,
|
||||
|
@ -18,8 +18,6 @@ pub var window = GuiWindow {
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:graphics",
|
||||
.title = "Graphics",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
};
|
||||
|
||||
|
@ -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,
|
||||
};
|
||||
|
||||
|
@ -15,8 +15,6 @@ pub var window: GuiWindow = GuiWindow {
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:settings",
|
||||
.title = "Settings",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
|
||||
|
@ -15,8 +15,6 @@ pub var window = GuiWindow {
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:sound",
|
||||
.title = "Sound TODO",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user