Refactor: Limit window components to one. Also use better formatting for tooltips.

This commit is contained in:
IntegratedQuantum 2023-03-29 22:11:08 +02:00
parent 58d1428392
commit 9fdfa04734
13 changed files with 51 additions and 80 deletions

View File

@ -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,16 +125,12 @@ 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;
}
}
if(selectedComponent) |component| {
component.mainButtonPressed(scaledMousePos);
}
}
}
}
pub fn mainButtonReleased(self: *GuiWindow, mousePosition: Vec2f) void {
@ -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) {

View File

@ -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();
}
}

View File

@ -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 {

View File

@ -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();
}
}

View File

@ -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();

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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: {} %