diff --git a/src/gui/GuiWindow.zig b/src/gui/GuiWindow.zig index bff8a275..80032e75 100644 --- a/src/gui/GuiWindow.zig +++ b/src/gui/GuiWindow.zig @@ -55,7 +55,7 @@ spacing: f32 = 0, relativePosition: [2]RelativePosition = .{.{.ratio = 0.5}, .{.ratio = 0.5}}, title: []const u8 = "", id: []const u8, -components: []GuiComponent = &.{}, +rootComponent: ?GuiComponent = null, showTitleBar: bool = true, titleBarExpanded: bool = false, hasBackground: bool = true, @@ -125,15 +125,11 @@ pub fn mainButtonPressed(self: *const GuiWindow, mousePosition: Vec2f) void { grabPosition = mousePosition; selfPositionWhenGrabbed = self.pos; } else { - var selectedComponent: ?*GuiComponent = null; - for(self.components) |*component| { + if(self.rootComponent) |*component| { if(GuiComponent.contains(component.pos(), component.size(), scaledMousePos)) { - selectedComponent = component; + component.mainButtonPressed(scaledMousePos); } } - if(selectedComponent) |component| { - component.mainButtonPressed(scaledMousePos); - } } } @@ -175,7 +171,7 @@ pub fn mainButtonReleased(self: *GuiWindow, mousePosition: Vec2f) void { } grabPosition = null; grabbedWindow = undefined; - for(self.components) |*component| { + if(self.rootComponent) |*component| { component.mainButtonReleased((mousePosition - self.pos)/@splat(2, self.scale)); } } @@ -335,20 +331,16 @@ pub fn updateSelected(self: *GuiWindow, mousePosition: Vec2f) !void { self.pos = @min(self.pos, windowSize - self.size); gui.updateWindowPositions(); }; - for(self.components) |*component| { + if(self.rootComponent) |*component| { component.updateSelected(); } } pub fn updateHovered(self: *GuiWindow, mousePosition: Vec2f) !void { try self.updateHoveredFn(); - var i: usize = self.components.len; - while(i != 0) { - i -= 1; - const component = &self.components[i]; + if(self.rootComponent) |component| { if(GuiComponent.contains(component.pos(), component.size(), (mousePosition - self.pos)/@splat(2, self.scale))) { component.updateHovered((mousePosition - self.pos)/@splat(2, self.scale)); - break; } } } @@ -455,7 +447,7 @@ pub fn render(self: *const GuiWindow, mousePosition: Vec2f) !void { draw.customShadedRect(windowUniforms, .{0, 0}, self.size/@splat(2, self.scale)); } try self.renderFn(); - for(self.components) |*component| { + if(self.rootComponent) |*component| { try component.render((mousePosition - self.pos)/@splat(2, self.scale)); } if(self.showTitleBar) { diff --git a/src/gui/windows/change_name.zig b/src/gui/windows/change_name.zig index d7252e28..a68cbbd0 100644 --- a/src/gui/windows/change_name.zig +++ b/src/gui/windows/change_name.zig @@ -13,12 +13,10 @@ const Label = @import("../components/Label.zig"); const TextInput = @import("../components/TextInput.zig"); const VerticalList = @import("../components/VerticalList.zig"); -var components: [1]GuiComponent = undefined; pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:change_name", .title = "Change Name", - .components = &components, }; var textComponent: *TextInput = undefined; @@ -57,13 +55,13 @@ pub fn onOpen() Allocator.Error!void { try list.add(textComponent); try list.add(try Button.init(.{0, 0}, 100, "Apply", &apply)); list.finish(.center); - components[0] = list.toComponent(); - window.contentSize = components[0].pos() + components[0].size() + @splat(2, @as(f32, padding)); + window.rootComponent = list.toComponent(); + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @splat(2, @as(f32, padding)); gui.updateWindowPositions(); } pub fn onClose() void { - for(&components) |*comp| { + if(window.rootComponent) |*comp| { comp.deinit(); } } \ No newline at end of file diff --git a/src/gui/windows/chat.zig b/src/gui/windows/chat.zig index 158de27a..1d12caad 100644 --- a/src/gui/windows/chat.zig +++ b/src/gui/windows/chat.zig @@ -13,12 +13,10 @@ const MutexComponent = GuiComponent.MutexComponent; const TextInput = GuiComponent.TextInput; const VerticalList = @import("../components/VerticalList.zig"); -var components: [1]GuiComponent = undefined; pub var window: GuiWindow = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:chat", .title = "Chat", - .components = &components, .showTitleBar = false, .hasBackground = false, .isHud = true, @@ -36,11 +34,11 @@ var fadeOutEnd: u32 = 0; var input: *TextInput = undefined; var hideInput: bool = true; -fn refresh(deleteOld: bool) Allocator.Error!void { +fn refresh() Allocator.Error!void { std.debug.assert(!mutexComponent.mutex.tryLock()); // mutex must be locked! - if(deleteOld) { - components[0].mutexComponent.child.verticalList.children.clearRetainingCapacity(); - components[0].deinit(); + if(window.rootComponent) |old| { + old.mutexComponent.child.verticalList.children.clearRetainingCapacity(); + old.deinit(); } var list = try VerticalList.init(.{padding, 16 + padding}, 300, 0); for(history.items[if(hideInput) historyStart else 0 ..]) |msg| { @@ -54,8 +52,8 @@ fn refresh(deleteOld: bool) Allocator.Error!void { list.finish(.center); list.scrollBar.currentState = 1; try mutexComponent.updateInner(list); - components[0] = mutexComponent.toComponent(); - window.contentSize = components[0].pos() + components[0].size() + @splat(2, @as(f32, padding)); + window.rootComponent = mutexComponent.toComponent(); + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @splat(2, @as(f32, padding)); gui.updateWindowPositions(); } @@ -66,7 +64,7 @@ pub fn onOpen() Allocator.Error!void { input = try TextInput.init(.{0, 0}, 256, 32, "", &sendMessage); mutexComponent.mutex.lock(); defer mutexComponent.mutex.unlock(); - try refresh(false); + try refresh(); } pub fn onClose() void { @@ -78,8 +76,9 @@ pub fn onClose() void { history.deinit(); expirationTime.deinit(); input.deinit(); - components[0].mutexComponent.child.verticalList.children.clearRetainingCapacity(); - components[0].deinit(); + window.rootComponent.?.mutexComponent.child.verticalList.children.clearRetainingCapacity(); + window.rootComponent.?.deinit(); + window.rootComponent = null; } pub fn update() Allocator.Error!void { @@ -92,14 +91,14 @@ pub fn update() Allocator.Error!void { if(@truncate(i32, std.time.milliTimestamp()) -% time >= messageFade) { historyStart += 1; hideInput = main.Window.grabbed; - try refresh(true); + try refresh(); } else { label.alpha = 1.0 - @intToFloat(f32, @truncate(i32, std.time.milliTimestamp()) -% time)/@intToFloat(f32, messageFade); } } if(hideInput != main.Window.grabbed) { hideInput = main.Window.grabbed; - try refresh(true); + try refresh(); } } @@ -115,7 +114,7 @@ pub fn addMessage(message: []const u8) Allocator.Error!void { defer mutexComponent.mutex.unlock(); try history.append(try Label.init(.{0, 0}, 256, message, .left)); try expirationTime.append(@truncate(i32, std.time.milliTimestamp()) +% messageTimeout); - try refresh(true); + try refresh(); } pub fn sendMessage() void { diff --git a/src/gui/windows/controls.zig b/src/gui/windows/controls.zig index a5113d7b..0dcd9c36 100644 --- a/src/gui/windows/controls.zig +++ b/src/gui/windows/controls.zig @@ -12,12 +12,10 @@ const HorizontalList = @import("../components/HorizontalList.zig"); const Label = @import("../components/Label.zig"); const VerticalList = @import("../components/VerticalList.zig"); -var components: [1]GuiComponent = undefined; pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:controls", .title = "Controls", - .components = &components, }; const padding: f32 = 8; @@ -58,13 +56,13 @@ pub fn onOpen() Allocator.Error!void { try list.add(row); } list.finish(.center); - components[0] = list.toComponent(); - window.contentSize = components[0].pos() + components[0].size() + @splat(2, @as(f32, padding)); + window.rootComponent = list.toComponent(); + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @splat(2, @as(f32, padding)); gui.updateWindowPositions(); } pub fn onClose() void { - for(&components) |*comp| { + if(window.rootComponent) |*comp| { comp.deinit(); } } diff --git a/src/gui/windows/creative.zig b/src/gui/windows/creative.zig index 077fd81a..53c95054 100644 --- a/src/gui/windows/creative.zig +++ b/src/gui/windows/creative.zig @@ -14,12 +14,10 @@ const HorizontalList = GuiComponent.HorizontalList; const VerticalList = GuiComponent.VerticalList; const ItemSlot = GuiComponent.ItemSlot; -var components: [1]GuiComponent = undefined; pub var window = GuiWindow { .contentSize = Vec2f{64*8, 64*4}, .title = "Creative Inventory", .id = "cubyz:creative_inventory", - .components = &components, }; const padding: f32 = 8; @@ -47,13 +45,13 @@ pub fn onOpen() Allocator.Error!void { try list.add(row); } list.finish(.center); - components[0] = list.toComponent(); - window.contentSize = components[0].pos() + components[0].size() + @splat(2, padding); + window.rootComponent = list.toComponent(); + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @splat(2, padding); gui.updateWindowPositions(); } pub fn onClose() void { - for(&components) |*comp| { + if(window.rootComponent) |*comp| { comp.deinit(); } itemStacks.deinit(); diff --git a/src/gui/windows/graphics.zig b/src/gui/windows/graphics.zig index 88bf4da4..64d4a369 100644 --- a/src/gui/windows/graphics.zig +++ b/src/gui/windows/graphics.zig @@ -13,12 +13,10 @@ const CheckBox = @import("../components/CheckBox.zig"); const Slider = @import("../components/Slider.zig"); const VerticalList = @import("../components/VerticalList.zig"); -var components: [1]GuiComponent = undefined; pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:graphics", .title = "Graphics", - .components = &components, }; const padding: f32 = 8; @@ -50,13 +48,13 @@ pub fn onOpen() Allocator.Error!void { try list.add(try CheckBox.init(.{0, 0}, 128, "Bloom", settings.bloom, &bloomCallback)); try list.add(try CheckBox.init(.{0, 0}, 128, "Vertical Synchronization", settings.vsync, &vsyncCallback)); list.finish(.center); - components[0] = list.toComponent(); - window.contentSize = components[0].pos() + components[0].size() + @splat(2, @as(f32, padding)); + window.rootComponent = list.toComponent(); + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @splat(2, @as(f32, padding)); gui.updateWindowPositions(); } pub fn onClose() void { - for(&components) |*comp| { + if(window.rootComponent) |*comp| { comp.deinit(); } } \ No newline at end of file diff --git a/src/gui/windows/hotbar.zig b/src/gui/windows/hotbar.zig index 9c12bd84..3d36cc53 100644 --- a/src/gui/windows/hotbar.zig +++ b/src/gui/windows/hotbar.zig @@ -11,12 +11,10 @@ const GuiWindow = gui.GuiWindow; const HorizontalList = GuiComponent.HorizontalList; const ItemSlot = GuiComponent.ItemSlot; -var components: [1]GuiComponent = undefined; pub var window = GuiWindow { .contentSize = Vec2f{64*8, 64}, .title = "Hotbar", .id = "cubyz:hotbar", - .components = &components, .isHud = true, .showTitleBar = false, .hasBackground = false, @@ -28,13 +26,13 @@ pub fn onOpen() Allocator.Error!void { try list.add(try ItemSlot.init(.{0, 0}, &Player.inventory__SEND_CHANGES_TO_SERVER.items[i])); } list.finish(.{0, 0}, .center); - components[0] = list.toComponent(); - window.contentSize = components[0].size(); + window.rootComponent = list.toComponent(); + window.contentSize = window.rootComponent.?.size(); gui.updateWindowPositions(); } pub fn onClose() void { - for(&components) |*comp| { + if(window.rootComponent) |*comp| { comp.deinit(); } } \ No newline at end of file diff --git a/src/gui/windows/inventory.zig b/src/gui/windows/inventory.zig index d266bd9e..e21e6d96 100644 --- a/src/gui/windows/inventory.zig +++ b/src/gui/windows/inventory.zig @@ -12,12 +12,10 @@ const HorizontalList = GuiComponent.HorizontalList; const VerticalList = GuiComponent.VerticalList; const ItemSlot = GuiComponent.ItemSlot; -var components: [1]GuiComponent = undefined; pub var window = GuiWindow { .contentSize = Vec2f{64*8, 64*4}, .title = "Inventory", .id = "cubyz:inventory", - .components = &components, }; const padding: f32 = 8; @@ -34,13 +32,13 @@ pub fn onOpen() Allocator.Error!void { try list.add(row); } list.finish(.center); - components[0] = list.toComponent(); - window.contentSize = components[0].pos() + components[0].size() + @splat(2, padding); + window.rootComponent = list.toComponent(); + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @splat(2, padding); gui.updateWindowPositions(); } pub fn onClose() void { - for(&components) |*comp| { + if(window.rootComponent) |*comp| { comp.deinit(); } } \ No newline at end of file diff --git a/src/gui/windows/main.zig b/src/gui/windows/main.zig index 3fc9363f..ee5042ca 100644 --- a/src/gui/windows/main.zig +++ b/src/gui/windows/main.zig @@ -10,11 +10,9 @@ const GuiWindow = gui.GuiWindow; const Button = @import("../components/Button.zig"); const VerticalList = @import("../components/VerticalList.zig"); -var components: [1]GuiComponent = undefined; pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:main", - .components = &components, }; pub fn buttonCallbackTest() void { @@ -30,13 +28,13 @@ pub fn onOpen() Allocator.Error!void { try list.add(try Button.init(.{0, 0}, 128, "Settings", gui.openWindowFunction("cubyz:settings"))); try list.add(try Button.init(.{0, 0}, 128, "Exit TODO", &buttonCallbackTest)); list.finish(.center); - components[0] = list.toComponent(); - window.contentSize = components[0].pos() + components[0].size() + @splat(2, @as(f32, padding)); + window.rootComponent = list.toComponent(); + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @splat(2, @as(f32, padding)); gui.updateWindowPositions(); } pub fn onClose() void { - for(&components) |*comp| { + if(window.rootComponent) |*comp| { comp.deinit(); } } \ No newline at end of file diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index adf909b0..bbd52396 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -14,12 +14,10 @@ const Label = @import("../components/Label.zig"); const TextInput = @import("../components/TextInput.zig"); const VerticalList = @import("../components/VerticalList.zig"); -var components: [1]GuiComponent = undefined; pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:multiplayer", .title = "Multiplayer", - .components = &components, }; var ipAddressLabel: *Label = undefined; @@ -95,8 +93,8 @@ pub fn onOpen() Allocator.Error!void { try list.add(try TextInput.init(.{0, 0}, width, 32, settings.lastUsedIPAddress, &join)); try list.add(try Button.init(.{0, 0}, 100, "Join", &join)); list.finish(.center); - components[0] = list.toComponent(); - window.contentSize = components[0].pos() + components[0].size() + @splat(2, @as(f32, padding)); + window.rootComponent = list.toComponent(); + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @splat(2, @as(f32, padding)); gui.updateWindowPositions(); thread = std.Thread.spawn(.{}, discoverIpAddressFromNewThread, .{}) catch |err| blk: { @@ -120,7 +118,7 @@ pub fn onClose() void { ipAddress = ""; } - for(&components) |*comp| { + if(window.rootComponent) |*comp| { comp.deinit(); } } diff --git a/src/gui/windows/settings.zig b/src/gui/windows/settings.zig index 18861216..89444d92 100644 --- a/src/gui/windows/settings.zig +++ b/src/gui/windows/settings.zig @@ -10,12 +10,10 @@ const GuiWindow = gui.GuiWindow; const Button = @import("../components/Button.zig"); const VerticalList = @import("../components/VerticalList.zig"); -var components: [1]GuiComponent = undefined; pub var window: GuiWindow = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:settings", .title = "Settings", - .components = &components, }; const padding: f32 = 8; @@ -27,13 +25,13 @@ pub fn onOpen() Allocator.Error!void { try list.add(try Button.init(.{0, 0}, 128, "Controls", gui.openWindowFunction("cubyz:controls"))); try list.add(try Button.init(.{0, 0}, 128, "Change Name", gui.openWindowFunction("cubyz:change_name"))); list.finish(.center); - components[0] = list.toComponent(); - window.contentSize = components[0].pos() + components[0].size() + @splat(2, @as(f32, padding)); + window.rootComponent = list.toComponent(); + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @splat(2, @as(f32, padding)); gui.updateWindowPositions(); } pub fn onClose() void { - for(&components) |*comp| { + if(window.rootComponent) |*comp| { comp.deinit(); } } \ No newline at end of file diff --git a/src/gui/windows/sound.zig b/src/gui/windows/sound.zig index 2ec1b180..50d2ce16 100644 --- a/src/gui/windows/sound.zig +++ b/src/gui/windows/sound.zig @@ -10,12 +10,10 @@ const GuiWindow = gui.GuiWindow; const Button = @import("../components/Button.zig"); const VerticalList = @import("../components/VerticalList.zig"); -var components: [1]GuiComponent = undefined; pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, .id = "cubyz:sound", .title = "Sound TODO", - .components = &components, }; const padding: f32 = 8; @@ -24,13 +22,13 @@ pub fn onOpen() Allocator.Error!void { var list = try VerticalList.init(.{padding, 16 + padding}, 300, 16); // TODO list.finish(.center); - components[0] = list.toComponent(); - window.contentSize = components[0].pos() + components[0].size() + @splat(2, @as(f32, padding)); + window.rootComponent = list.toComponent(); + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @splat(2, @as(f32, padding)); gui.updateWindowPositions(); } pub fn onClose() void { - for(&components) |*comp| { + if(window.rootComponent) |*comp| { comp.deinit(); } } \ No newline at end of file diff --git a/src/items.zig b/src/items.zig index 9a3e85b0..7a3664e3 100644 --- a/src/items.zig +++ b/src/items.zig @@ -1048,7 +1048,7 @@ const Tool = struct { if(self.tooltip) |tooltip| return tooltip; self.tooltip = try std.fmt.allocPrint( main.globalAllocator, - \\Time to swing: {} s + \\Time to swing: {d:.2} s \\Pickaxe power: {} % \\Axe power: {} % \\Shover power: {} %