mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-03 19:28:49 -04:00
Refactor: Simplify window creation by limiting it to a single window per namespace.
This commit is contained in:
parent
966c3f8857
commit
fd0e903431
@ -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,
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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{
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:change_name",
|
||||
.title = "Change Name",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
try gui.addWindow(&window, true);
|
||||
}
|
||||
pub var window = GuiWindow {
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:change_name",
|
||||
.title = "Change Name",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
|
||||
const padding: f32 = 8;
|
||||
|
||||
|
@ -11,20 +11,16 @@ 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{
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:controls",
|
||||
.title = "Controls",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.renderFn = &render,
|
||||
.components = &components,
|
||||
};
|
||||
try gui.addWindow(&window, true);
|
||||
}
|
||||
pub var window = GuiWindow {
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:controls",
|
||||
.title = "Controls",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.renderFn = &render,
|
||||
.components = &components,
|
||||
};
|
||||
|
||||
const padding: f32 = 8;
|
||||
var selectedKey: ?*main.Key = null;
|
||||
|
@ -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{
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:graphics",
|
||||
.title = "Graphics",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
try gui.addWindow(&window, true);
|
||||
}
|
||||
pub var window = GuiWindow {
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:graphics",
|
||||
.title = "Graphics",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
|
||||
const padding: f32 = 8;
|
||||
|
||||
|
@ -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{
|
||||
.contentSize = Vec2f{128, 16},
|
||||
.title = "Health Bar",
|
||||
.id = "cubyz:healthbar",
|
||||
.renderFn = &render,
|
||||
.components = &[_]GuiComponent{},
|
||||
};
|
||||
try gui.addWindow(&healthbarWindow, true);
|
||||
}
|
||||
pub var window = GuiWindow {
|
||||
.contentSize = Vec2f{128, 16},
|
||||
.title = "Health Bar",
|
||||
.id = "cubyz:healthbar",
|
||||
.renderFn = &render,
|
||||
.isHud = true,
|
||||
};
|
||||
|
||||
pub fn render() Allocator.Error!void {
|
||||
|
||||
|
@ -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 {
|
||||
.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);
|
||||
}
|
||||
pub var window = GuiWindow {
|
||||
.contentSize = Vec2f{64*8, 64},
|
||||
.title = "Hotbar",
|
||||
.id = "cubyz:hotbar",
|
||||
.renderFn = &render,
|
||||
.components = &[_]GuiComponent{},
|
||||
.isHud = true,
|
||||
};
|
||||
|
||||
pub fn render() Allocator.Error!void {
|
||||
|
||||
|
@ -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{
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:main",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
try gui.addWindow(&window, true);
|
||||
}
|
||||
pub var window = GuiWindow {
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:main",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
|
||||
pub fn buttonCallbackTest() void {
|
||||
std.log.info("Clicked!", .{});
|
||||
|
@ -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{
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:multiplayer",
|
||||
.title = "Multiplayer",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
try gui.addWindow(&window, true);
|
||||
}
|
||||
pub var window = GuiWindow {
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:multiplayer",
|
||||
.title = "Multiplayer",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
|
||||
const padding: f32 = 8;
|
||||
|
||||
|
@ -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{
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:settings",
|
||||
.title = "Settings",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
try gui.addWindow(&window, true);
|
||||
}
|
||||
pub var window: GuiWindow = GuiWindow {
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:settings",
|
||||
.title = "Settings",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
|
||||
const padding: f32 = 8;
|
||||
|
||||
|
@ -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{
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:sound",
|
||||
.title = "Sound TODO",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
try gui.addWindow(&window, true);
|
||||
}
|
||||
pub var window = GuiWindow {
|
||||
.contentSize = Vec2f{128, 256},
|
||||
.id = "cubyz:sound",
|
||||
.title = "Sound TODO",
|
||||
.onOpenFn = &onOpen,
|
||||
.onCloseFn = &onClose,
|
||||
.components = &components,
|
||||
};
|
||||
|
||||
const padding: f32 = 8;
|
||||
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user