Use @hasDecl and automatically assign the window function pointers.

This commit is contained in:
IntegratedQuantum 2023-03-23 17:13:40 +01:00
parent c2b0428539
commit 758fe2c8ab
13 changed files with 19 additions and 58 deletions

View File

@ -38,10 +38,14 @@ pub fn init(_allocator: Allocator) !void {
inline for(@typeInfo(windowlist).Struct.decls) |decl| { inline for(@typeInfo(windowlist).Struct.decls) |decl| {
const windowStruct = @field(windowlist, decl.name); const windowStruct = @field(windowlist, decl.name);
try addWindow(&windowStruct.window); try addWindow(&windowStruct.window);
inline for(@typeInfo(windowStruct).Struct.decls) |_decl| { if(@hasDecl(windowStruct, "init")) {
if(comptime std.mem.eql(u8, _decl.name, "init")) {
try 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);
}
} }
} }
try GuiWindow.__init(); try GuiWindow.__init();

View File

@ -32,15 +32,12 @@ pub const GuiComponent = union(enum) {
pub fn deinit(self: GuiComponent) void { pub fn deinit(self: GuiComponent) void {
switch(self) { switch(self) {
inline else => |impl| { inline else => |impl| {
// Only call the function if it exists: if(@hasDecl(@TypeOf(impl.*), "deinit")) {
inline for(@typeInfo(@TypeOf(impl.*)).Struct.decls) |decl| {
if(comptime std.mem.eql(u8, decl.name, "deinit")) {
impl.deinit(); impl.deinit();
} }
} }
} }
} }
}
pub fn mutPos(self: GuiComponent) *Vec2f { pub fn mutPos(self: GuiComponent) *Vec2f {
switch(self) { switch(self) {
@ -77,67 +74,52 @@ pub const GuiComponent = union(enum) {
pub fn updateSelected(self: GuiComponent) void { pub fn updateSelected(self: GuiComponent) void {
switch(self) { switch(self) {
inline else => |impl| { inline else => |impl| {
// Only call the function if it exists: if(@hasDecl(@TypeOf(impl.*), "updateSelected")) {
inline for(@typeInfo(@TypeOf(impl.*)).Struct.decls) |decl| {
if(comptime std.mem.eql(u8, decl.name, "updateSelected")) {
impl.updateSelected(); impl.updateSelected();
} }
} }
} }
} }
}
pub fn updateHovered(self: GuiComponent, mousePosition: Vec2f) void { pub fn updateHovered(self: GuiComponent, mousePosition: Vec2f) void {
switch(self) { switch(self) {
inline else => |impl| { inline else => |impl| {
// Only call the function if it exists: if(@hasDecl(@TypeOf(impl.*), "updateHovered")) {
inline for(@typeInfo(@TypeOf(impl.*)).Struct.decls) |decl| {
if(comptime std.mem.eql(u8, decl.name, "updateHovered")) {
impl.updateHovered(mousePosition); impl.updateHovered(mousePosition);
} }
} }
} }
} }
}
pub fn render(self: GuiComponent, mousePosition: Vec2f) !void { pub fn render(self: GuiComponent, mousePosition: Vec2f) !void {
switch(self) { switch(self) {
inline else => |impl| { inline else => |impl| {
// Only call the function if it exists: if(@hasDecl(@TypeOf(impl.*), "render")) {
inline for(@typeInfo(@TypeOf(impl.*)).Struct.decls) |decl| {
if(comptime std.mem.eql(u8, decl.name, "render")) {
try impl.render(mousePosition); try impl.render(mousePosition);
} }
} }
} }
} }
}
pub fn mainButtonPressed(self: GuiComponent, mousePosition: Vec2f) void { pub fn mainButtonPressed(self: GuiComponent, mousePosition: Vec2f) void {
switch(self) { switch(self) {
inline else => |impl| { inline else => |impl| {
// Only call the function if it exists: if(@hasDecl(@TypeOf(impl.*), "mainButtonPressed")) {
inline for(@typeInfo(@TypeOf(impl.*)).Struct.decls) |decl| {
if(comptime std.mem.eql(u8, decl.name, "mainButtonPressed")) {
impl.mainButtonPressed(mousePosition); impl.mainButtonPressed(mousePosition);
} }
} }
} }
} }
}
pub fn mainButtonReleased(self: GuiComponent, mousePosition: Vec2f) void { pub fn mainButtonReleased(self: GuiComponent, mousePosition: Vec2f) void {
switch(self) { switch(self) {
inline else => |impl| { inline else => |impl| {
// Only call the function if it exists: if(@hasDecl(@TypeOf(impl.*), "mainButtonReleased")) {
inline for(@typeInfo(@TypeOf(impl.*)).Struct.decls) |decl| {
if(comptime std.mem.eql(u8, decl.name, "mainButtonReleased")) {
impl.mainButtonReleased(mousePosition); impl.mainButtonReleased(mousePosition);
} }
} }
} }
} }
}
pub fn contains(_pos: Vec2f, _size: Vec2f, point: Vec2f) bool { pub fn contains(_pos: Vec2f, _size: Vec2f, point: Vec2f) bool {
return @reduce(.And, point >= _pos) and @reduce(.And, point < _pos + _size); return @reduce(.And, point >= _pos) and @reduce(.And, point < _pos + _size);

View File

@ -18,8 +18,6 @@ pub var window = GuiWindow {
.contentSize = Vec2f{128, 256}, .contentSize = Vec2f{128, 256},
.id = "cubyz:change_name", .id = "cubyz:change_name",
.title = "Change Name", .title = "Change Name",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.components = &components, .components = &components,
}; };
var textComponent: *TextInput = undefined; var textComponent: *TextInput = undefined;

View File

@ -18,10 +18,6 @@ pub var window: GuiWindow = GuiWindow {
.contentSize = Vec2f{128, 256}, .contentSize = Vec2f{128, 256},
.id = "cubyz:chat", .id = "cubyz:chat",
.title = "Chat", .title = "Chat",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.updateFn = &update,
.renderFn = &render,
.components = &components, .components = &components,
.showTitleBar = false, .showTitleBar = false,
.hasBackground = false, .hasBackground = false,

View File

@ -17,9 +17,6 @@ pub var window = GuiWindow {
.contentSize = Vec2f{128, 256}, .contentSize = Vec2f{128, 256},
.id = "cubyz:controls", .id = "cubyz:controls",
.title = "Controls", .title = "Controls",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.renderFn = &render,
.components = &components, .components = &components,
}; };

View File

@ -16,7 +16,6 @@ pub var window = GuiWindow {
.contentSize = Vec2f{size, size}, .contentSize = Vec2f{size, size},
.title = "Crosshair", .title = "Crosshair",
.id = "cubyz:crosshair", .id = "cubyz:crosshair",
.renderFn = &render,
.showTitleBar = false, .showTitleBar = false,
.hasBackground = false, .hasBackground = false,
.isHud = true, .isHud = true,

View File

@ -18,8 +18,6 @@ pub var window = GuiWindow {
.contentSize = Vec2f{128, 256}, .contentSize = Vec2f{128, 256},
.id = "cubyz:graphics", .id = "cubyz:graphics",
.title = "Graphics", .title = "Graphics",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.components = &components, .components = &components,
}; };

View File

@ -15,7 +15,6 @@ pub var window = GuiWindow {
.contentSize = Vec2f{128, 16}, .contentSize = Vec2f{128, 16},
.title = "Health Bar", .title = "Health Bar",
.id = "cubyz:healthbar", .id = "cubyz:healthbar",
.renderFn = &render,
.isHud = true, .isHud = true,
.showTitleBar = false, .showTitleBar = false,
.hasBackground = false, .hasBackground = false,

View File

@ -16,9 +16,6 @@ pub var window = GuiWindow {
.contentSize = Vec2f{64*8, 64}, .contentSize = Vec2f{64*8, 64},
.title = "Hotbar", .title = "Hotbar",
.id = "cubyz:hotbar", .id = "cubyz:hotbar",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.renderFn = &render,
.components = &components, .components = &components,
.isHud = true, .isHud = true,
.showTitleBar = false, .showTitleBar = false,

View File

@ -14,8 +14,6 @@ var components: [1]GuiComponent = undefined;
pub var window = GuiWindow { pub var window = GuiWindow {
.contentSize = Vec2f{128, 256}, .contentSize = Vec2f{128, 256},
.id = "cubyz:main", .id = "cubyz:main",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.components = &components, .components = &components,
}; };

View File

@ -19,9 +19,6 @@ pub var window = GuiWindow {
.contentSize = Vec2f{128, 256}, .contentSize = Vec2f{128, 256},
.id = "cubyz:multiplayer", .id = "cubyz:multiplayer",
.title = "Multiplayer", .title = "Multiplayer",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.updateFn = &update,
.components = &components, .components = &components,
}; };

View File

@ -15,8 +15,6 @@ pub var window: GuiWindow = GuiWindow {
.contentSize = Vec2f{128, 256}, .contentSize = Vec2f{128, 256},
.id = "cubyz:settings", .id = "cubyz:settings",
.title = "Settings", .title = "Settings",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.components = &components, .components = &components,
}; };

View File

@ -15,8 +15,6 @@ pub var window = GuiWindow {
.contentSize = Vec2f{128, 256}, .contentSize = Vec2f{128, 256},
.id = "cubyz:sound", .id = "cubyz:sound",
.title = "Sound TODO", .title = "Sound TODO",
.onOpenFn = &onOpen,
.onCloseFn = &onClose,
.components = &components, .components = &components,
}; };