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| {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,11 +32,8 @@ 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| {
|
impl.deinit();
|
||||||
if(comptime std.mem.eql(u8, decl.name, "deinit")) {
|
|
||||||
impl.deinit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -77,11 +74,8 @@ 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| {
|
impl.updateSelected();
|
||||||
if(comptime std.mem.eql(u8, decl.name, "updateSelected")) {
|
|
||||||
impl.updateSelected();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,11 +84,8 @@ pub const GuiComponent = union(enum) {
|
|||||||
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| {
|
impl.updateHovered(mousePosition);
|
||||||
if(comptime std.mem.eql(u8, decl.name, "updateHovered")) {
|
|
||||||
impl.updateHovered(mousePosition);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,11 +94,8 @@ pub const GuiComponent = union(enum) {
|
|||||||
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| {
|
try impl.render(mousePosition);
|
||||||
if(comptime std.mem.eql(u8, decl.name, "render")) {
|
|
||||||
try impl.render(mousePosition);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,11 +104,8 @@ pub const GuiComponent = union(enum) {
|
|||||||
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| {
|
impl.mainButtonPressed(mousePosition);
|
||||||
if(comptime std.mem.eql(u8, decl.name, "mainButtonPressed")) {
|
|
||||||
impl.mainButtonPressed(mousePosition);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -129,11 +114,8 @@ pub const GuiComponent = union(enum) {
|
|||||||
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| {
|
impl.mainButtonReleased(mousePosition);
|
||||||
if(comptime std.mem.eql(u8, decl.name, "mainButtonReleased")) {
|
|
||||||
impl.mainButtonReleased(mousePosition);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
@ -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,
|
||||||
|
@ -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,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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,
|
||||||
|
@ -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,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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,
|
||||||
|
@ -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,
|
||||||
|
@ -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,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user