mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-09-07 19:21:14 -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,
|
showTitleBar: bool = true,
|
||||||
title: []const u8 = "",
|
title: []const u8 = "",
|
||||||
id: []const u8,
|
id: []const u8,
|
||||||
components: []GuiComponent,
|
components: []GuiComponent = &.{},
|
||||||
|
isHud: bool = false,
|
||||||
|
|
||||||
/// Called every frame.
|
/// Called every frame.
|
||||||
renderFn: *const fn()Allocator.Error!void = &defaultErrorFunction,
|
renderFn: *const fn()Allocator.Error!void = &defaultErrorFunction,
|
||||||
|
@ -35,7 +35,7 @@ pub fn init(_allocator: Allocator) !void {
|
|||||||
hudWindows = std.ArrayList(*GuiWindow).init(allocator);
|
hudWindows = std.ArrayList(*GuiWindow).init(allocator);
|
||||||
openWindows = std.ArrayList(*GuiWindow).init(allocator);
|
openWindows = std.ArrayList(*GuiWindow).init(allocator);
|
||||||
inline for(@typeInfo(windowlist).Struct.decls) |decl| {
|
inline for(@typeInfo(windowlist).Struct.decls) |decl| {
|
||||||
try @field(windowlist, decl.name).init();
|
try addWindow(&@field(windowlist, decl.name).window);
|
||||||
}
|
}
|
||||||
try GuiWindow.__init();
|
try GuiWindow.__init();
|
||||||
try Button.__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| {
|
for(windowList.items) |other| {
|
||||||
if(std.mem.eql(u8, window.id, other.id)) {
|
if(std.mem.eql(u8, window.id, other.id)) {
|
||||||
std.log.err("Duplicate window id: {s}", .{window.id});
|
std.log.err("Duplicate window id: {s}", .{window.id});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(isHudWindow) {
|
if(window.isHud) {
|
||||||
try hudWindows.append(window);
|
try hudWindows.append(window);
|
||||||
window.showTitleBar = false;
|
window.showTitleBar = false;
|
||||||
}
|
}
|
||||||
|
@ -13,10 +13,8 @@ const Label = @import("../components/Label.zig");
|
|||||||
const TextInput = @import("../components/TextInput.zig");
|
const TextInput = @import("../components/TextInput.zig");
|
||||||
const VerticalList = @import("../components/VerticalList.zig");
|
const VerticalList = @import("../components/VerticalList.zig");
|
||||||
|
|
||||||
var window: GuiWindow = undefined;
|
|
||||||
var components: [1]GuiComponent = undefined;
|
var components: [1]GuiComponent = undefined;
|
||||||
pub fn init() !void {
|
pub var window = GuiWindow {
|
||||||
window = GuiWindow{
|
|
||||||
.contentSize = Vec2f{128, 256},
|
.contentSize = Vec2f{128, 256},
|
||||||
.id = "cubyz:change_name",
|
.id = "cubyz:change_name",
|
||||||
.title = "Change Name",
|
.title = "Change Name",
|
||||||
@ -24,8 +22,6 @@ pub fn init() !void {
|
|||||||
.onCloseFn = &onClose,
|
.onCloseFn = &onClose,
|
||||||
.components = &components,
|
.components = &components,
|
||||||
};
|
};
|
||||||
try gui.addWindow(&window, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
const padding: f32 = 8;
|
const padding: f32 = 8;
|
||||||
|
|
||||||
|
@ -11,10 +11,8 @@ const Button = @import("../components/Button.zig");
|
|||||||
const Label = @import("../components/Label.zig");
|
const Label = @import("../components/Label.zig");
|
||||||
const VerticalList = @import("../components/VerticalList.zig");
|
const VerticalList = @import("../components/VerticalList.zig");
|
||||||
|
|
||||||
var window: GuiWindow = undefined;
|
|
||||||
var components: [1]GuiComponent = undefined;
|
var components: [1]GuiComponent = undefined;
|
||||||
pub fn init() !void {
|
pub var window = GuiWindow {
|
||||||
window = GuiWindow{
|
|
||||||
.contentSize = Vec2f{128, 256},
|
.contentSize = Vec2f{128, 256},
|
||||||
.id = "cubyz:controls",
|
.id = "cubyz:controls",
|
||||||
.title = "Controls",
|
.title = "Controls",
|
||||||
@ -23,8 +21,6 @@ pub fn init() !void {
|
|||||||
.renderFn = &render,
|
.renderFn = &render,
|
||||||
.components = &components,
|
.components = &components,
|
||||||
};
|
};
|
||||||
try gui.addWindow(&window, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
const padding: f32 = 8;
|
const padding: f32 = 8;
|
||||||
var selectedKey: ?*main.Key = null;
|
var selectedKey: ?*main.Key = null;
|
||||||
|
@ -13,10 +13,8 @@ const CheckBox = @import("../components/CheckBox.zig");
|
|||||||
const Slider = @import("../components/Slider.zig");
|
const Slider = @import("../components/Slider.zig");
|
||||||
const VerticalList = @import("../components/VerticalList.zig");
|
const VerticalList = @import("../components/VerticalList.zig");
|
||||||
|
|
||||||
var window: GuiWindow = undefined;
|
|
||||||
var components: [1]GuiComponent = undefined;
|
var components: [1]GuiComponent = undefined;
|
||||||
pub fn init() !void {
|
pub var window = GuiWindow {
|
||||||
window = GuiWindow{
|
|
||||||
.contentSize = Vec2f{128, 256},
|
.contentSize = Vec2f{128, 256},
|
||||||
.id = "cubyz:graphics",
|
.id = "cubyz:graphics",
|
||||||
.title = "Graphics",
|
.title = "Graphics",
|
||||||
@ -24,8 +22,6 @@ pub fn init() !void {
|
|||||||
.onCloseFn = &onClose,
|
.onCloseFn = &onClose,
|
||||||
.components = &components,
|
.components = &components,
|
||||||
};
|
};
|
||||||
try gui.addWindow(&window, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
const padding: f32 = 8;
|
const padding: f32 = 8;
|
||||||
|
|
||||||
|
@ -8,17 +8,13 @@ const gui = @import("../gui.zig");
|
|||||||
const GuiWindow = gui.GuiWindow;
|
const GuiWindow = gui.GuiWindow;
|
||||||
const GuiComponent = gui.GuiComponent;
|
const GuiComponent = gui.GuiComponent;
|
||||||
|
|
||||||
var healthbarWindow: GuiWindow = undefined;
|
pub var window = GuiWindow {
|
||||||
pub fn init() !void {
|
|
||||||
healthbarWindow = GuiWindow{
|
|
||||||
.contentSize = Vec2f{128, 16},
|
.contentSize = Vec2f{128, 16},
|
||||||
.title = "Health Bar",
|
.title = "Health Bar",
|
||||||
.id = "cubyz:healthbar",
|
.id = "cubyz:healthbar",
|
||||||
.renderFn = &render,
|
.renderFn = &render,
|
||||||
.components = &[_]GuiComponent{},
|
.isHud = true,
|
||||||
};
|
};
|
||||||
try gui.addWindow(&healthbarWindow, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn render() Allocator.Error!void {
|
pub fn render() Allocator.Error!void {
|
||||||
|
|
||||||
|
@ -8,35 +8,14 @@ const gui = @import("../gui.zig");
|
|||||||
const GuiComponent = gui.GuiComponent;
|
const GuiComponent = gui.GuiComponent;
|
||||||
const GuiWindow = gui.GuiWindow;
|
const GuiWindow = gui.GuiWindow;
|
||||||
|
|
||||||
var hotbarWindow: GuiWindow = undefined;
|
pub var window = GuiWindow {
|
||||||
var hotbarWindow2: GuiWindow = undefined;
|
|
||||||
var hotbarWindow3: GuiWindow = undefined;
|
|
||||||
pub fn init() !void {
|
|
||||||
hotbarWindow = GuiWindow {
|
|
||||||
.contentSize = Vec2f{64*8, 64},
|
.contentSize = Vec2f{64*8, 64},
|
||||||
.title = "Hotbar",
|
.title = "Hotbar",
|
||||||
.id = "cubyz:hotbar",
|
.id = "cubyz:hotbar",
|
||||||
.renderFn = &render,
|
.renderFn = &render,
|
||||||
.components = &[_]GuiComponent{},
|
.components = &[_]GuiComponent{},
|
||||||
|
.isHud = true,
|
||||||
};
|
};
|
||||||
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 fn render() Allocator.Error!void {
|
pub fn render() Allocator.Error!void {
|
||||||
|
|
||||||
|
@ -10,18 +10,14 @@ const GuiWindow = gui.GuiWindow;
|
|||||||
const Button = @import("../components/Button.zig");
|
const Button = @import("../components/Button.zig");
|
||||||
const VerticalList = @import("../components/VerticalList.zig");
|
const VerticalList = @import("../components/VerticalList.zig");
|
||||||
|
|
||||||
var window: GuiWindow = undefined;
|
|
||||||
var components: [1]GuiComponent = undefined;
|
var components: [1]GuiComponent = undefined;
|
||||||
pub fn init() !void {
|
pub var window = GuiWindow {
|
||||||
window = GuiWindow{
|
|
||||||
.contentSize = Vec2f{128, 256},
|
.contentSize = Vec2f{128, 256},
|
||||||
.id = "cubyz:main",
|
.id = "cubyz:main",
|
||||||
.onOpenFn = &onOpen,
|
.onOpenFn = &onOpen,
|
||||||
.onCloseFn = &onClose,
|
.onCloseFn = &onClose,
|
||||||
.components = &components,
|
.components = &components,
|
||||||
};
|
};
|
||||||
try gui.addWindow(&window, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn buttonCallbackTest() void {
|
pub fn buttonCallbackTest() void {
|
||||||
std.log.info("Clicked!", .{});
|
std.log.info("Clicked!", .{});
|
||||||
|
@ -14,10 +14,8 @@ const Label = @import("../components/Label.zig");
|
|||||||
const TextInput = @import("../components/TextInput.zig");
|
const TextInput = @import("../components/TextInput.zig");
|
||||||
const VerticalList = @import("../components/VerticalList.zig");
|
const VerticalList = @import("../components/VerticalList.zig");
|
||||||
|
|
||||||
var window: GuiWindow = undefined;
|
|
||||||
var components: [1]GuiComponent = undefined;
|
var components: [1]GuiComponent = undefined;
|
||||||
pub fn init() !void {
|
pub var window = GuiWindow {
|
||||||
window = GuiWindow{
|
|
||||||
.contentSize = Vec2f{128, 256},
|
.contentSize = Vec2f{128, 256},
|
||||||
.id = "cubyz:multiplayer",
|
.id = "cubyz:multiplayer",
|
||||||
.title = "Multiplayer",
|
.title = "Multiplayer",
|
||||||
@ -25,8 +23,6 @@ pub fn init() !void {
|
|||||||
.onCloseFn = &onClose,
|
.onCloseFn = &onClose,
|
||||||
.components = &components,
|
.components = &components,
|
||||||
};
|
};
|
||||||
try gui.addWindow(&window, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
const padding: f32 = 8;
|
const padding: f32 = 8;
|
||||||
|
|
||||||
|
@ -10,10 +10,8 @@ const GuiWindow = gui.GuiWindow;
|
|||||||
const Button = @import("../components/Button.zig");
|
const Button = @import("../components/Button.zig");
|
||||||
const VerticalList = @import("../components/VerticalList.zig");
|
const VerticalList = @import("../components/VerticalList.zig");
|
||||||
|
|
||||||
var window: GuiWindow = undefined;
|
|
||||||
var components: [1]GuiComponent = undefined;
|
var components: [1]GuiComponent = undefined;
|
||||||
pub fn init() !void {
|
pub var window: GuiWindow = GuiWindow {
|
||||||
window = GuiWindow{
|
|
||||||
.contentSize = Vec2f{128, 256},
|
.contentSize = Vec2f{128, 256},
|
||||||
.id = "cubyz:settings",
|
.id = "cubyz:settings",
|
||||||
.title = "Settings",
|
.title = "Settings",
|
||||||
@ -21,8 +19,6 @@ pub fn init() !void {
|
|||||||
.onCloseFn = &onClose,
|
.onCloseFn = &onClose,
|
||||||
.components = &components,
|
.components = &components,
|
||||||
};
|
};
|
||||||
try gui.addWindow(&window, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
const padding: f32 = 8;
|
const padding: f32 = 8;
|
||||||
|
|
||||||
|
@ -10,10 +10,8 @@ const GuiWindow = gui.GuiWindow;
|
|||||||
const Button = @import("../components/Button.zig");
|
const Button = @import("../components/Button.zig");
|
||||||
const VerticalList = @import("../components/VerticalList.zig");
|
const VerticalList = @import("../components/VerticalList.zig");
|
||||||
|
|
||||||
var window: GuiWindow = undefined;
|
|
||||||
var components: [1]GuiComponent = undefined;
|
var components: [1]GuiComponent = undefined;
|
||||||
pub fn init() !void {
|
pub var window = GuiWindow {
|
||||||
window = GuiWindow{
|
|
||||||
.contentSize = Vec2f{128, 256},
|
.contentSize = Vec2f{128, 256},
|
||||||
.id = "cubyz:sound",
|
.id = "cubyz:sound",
|
||||||
.title = "Sound TODO",
|
.title = "Sound TODO",
|
||||||
@ -21,8 +19,6 @@ pub fn init() !void {
|
|||||||
.onCloseFn = &onClose,
|
.onCloseFn = &onClose,
|
||||||
.components = &components,
|
.components = &components,
|
||||||
};
|
};
|
||||||
try gui.addWindow(&window, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
const padding: f32 = 8;
|
const padding: f32 = 8;
|
||||||
|
|
||||||
|
@ -500,10 +500,6 @@ pub fn main() !void {
|
|||||||
if(settings.playerName.len == 0) {
|
if(settings.playerName.len == 0) {
|
||||||
try gui.openWindow("cubyz:change_name");
|
try gui.openWindow("cubyz:change_name");
|
||||||
} else {
|
} 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");
|
try gui.openWindow("cubyz:main");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user