Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upd: error notification gui #809

Merged
merged 15 commits into from
Dec 7, 2024
1 change: 1 addition & 0 deletions src/gui/windows/_windowlist.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub const invite = @import("invite.zig");
pub const main = @import("main.zig");
pub const manage_players = @import("manage_players.zig");
pub const multiplayer = @import("multiplayer.zig");
pub const notification = @import("notification.zig");
pub const pause = @import("pause.zig");
pub const pause_gear = @import("pause_gear.zig");
pub const performance_graph = @import("performance_graph.zig");
Expand Down
13 changes: 12 additions & 1 deletion src/gui/windows/multiplayer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,28 @@ fn join(_: usize) void {
ipAddress = "";
}
if(connection) |_connection| {
if (ipAddressEntry.currentString.items.len == 0) {
somanshurath marked this conversation as resolved.
Show resolved Hide resolved
std.log.err("IP address cannot be empty", .{});
main.gui.windowlist.notification.raiseNotification("IP address cannot be empty");
return;
}
somanshurath marked this conversation as resolved.
Show resolved Hide resolved
_connection.world = &main.game.testWorld;
main.globalAllocator.free(settings.lastUsedIPAddress);
settings.lastUsedIPAddress = main.globalAllocator.dupe(u8, ipAddressEntry.currentString.items);
settings.save();
main.game.world = &main.game.testWorld;
std.log.info("Connecting to server: {s}", .{ipAddressEntry.currentString.items});
main.game.testWorld.init(ipAddressEntry.currentString.items, _connection) catch |err| {
somanshurath marked this conversation as resolved.
Show resolved Hide resolved
std.log.err("Encountered error while opening world: {s}", .{@errorName(err)});
const formattedError = std.fmt.allocPrint(main.stackAllocator.allocator, "Encountered error while opening world: {s}", .{@errorName(err)}) catch unreachable;
defer main.stackAllocator.free(formattedError);
somanshurath marked this conversation as resolved.
Show resolved Hide resolved
std.log.err("{s}", .{formattedError});
main.gui.windowlist.notification.raiseNotification(formattedError);
return;
somanshurath marked this conversation as resolved.
Show resolved Hide resolved
};
connection = null;
} else {
std.log.err("No connection found. Cannot connect.", .{});
main.gui.windowlist.notification.raiseNotification("No connection found. Cannot connect.");
}
for(gui.openWindows.items) |openWindow| {
gui.closeWindowFromRef(openWindow);
Expand Down
55 changes: 55 additions & 0 deletions src/gui/windows/notification.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const std = @import("std");

const main = @import("root");
const Vec2f = main.vec.Vec2f;

const gui = @import("../gui.zig");
const GuiWindow = gui.GuiWindow;
const Button = @import("../components/Button.zig");
const VerticalList = @import("../components/VerticalList.zig");
const Label = @import("../components/Label.zig");

pub var window: GuiWindow = GuiWindow {
.contentSize = Vec2f{128, 256},
};

const padding: f32 = 16;
const width: f32 = 256;

var text: []const u8 = "";

pub fn deinit() void {
main.stackAllocator.free(text);
}

fn setNotificationText(newText: []const u8) void {
main.stackAllocator.free(text);
text = main.stackAllocator.dupe(u8, newText);
}

pub fn raiseNotification(notifText: []const u8) void {
main.gui.closeWindow("notification");
setNotificationText(notifText);
main.gui.openWindow("notification");
}


fn ack(_: usize) void {
gui.closeWindowFromRef(&window);
}

pub fn onOpen() void {
const list = VerticalList.init(.{padding, 16 + padding}, 300, 16);
list.add(Label.init(.{0, 0}, width, text, .center));
list.add(Button.initText(.{0, 0}, 100, "OK", .{ .callback = &ack }));
list.finish(.center);
window.rootComponent = list.toComponent();
window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @as(Vec2f, @splat(padding));
gui.updateWindowPositions();
}

pub fn onClose() void {
if(window.rootComponent) |*comp| {
comp.deinit();
}
}
Loading