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
19 changes: 18 additions & 1 deletion src/gui/windows/multiplayer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ fn discoverIpAddressFromNewThread() void {
discoverIpAddress();
}

fn raiseNotification(text: []const u8) void {
main.gui.closeWindow("notification");
main.gui.windowlist.notification.setNotificationText(text);
main.gui.openWindow("notification");
}
somanshurath marked this conversation as resolved.
Show resolved Hide resolved

fn join(_: usize) void {
if(thread) |_thread| {
_thread.join();
Expand All @@ -56,17 +62,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", .{});
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.globalAllocator.allocator, "Encountered error while opening world: {s}", .{@errorName(err)}) catch unreachable;
somanshurath marked this conversation as resolved.
Show resolved Hide resolved
std.log.err("{s}", .{formattedError});
raiseNotification(formattedError);
main.globalAllocator.free(formattedError);
somanshurath marked this conversation as resolved.
Show resolved Hide resolved
return;
somanshurath marked this conversation as resolved.
Show resolved Hide resolved
};
connection = null;
} else {
std.log.err("No connection found. Cannot connect.", .{});
raiseNotification("No connection found. Cannot connect.");
}
for(gui.openWindows.items) |openWindow| {
gui.closeWindowFromRef(openWindow);
Expand Down
48 changes: 48 additions & 0 deletions src/gui/windows/notification.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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.globalAllocator.free(text);
}

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

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