From 47a854d3cca360de435981896fffeb0b0698c335 Mon Sep 17 00:00:00 2001 From: OneAvargeCoder193 <85588535+OneAvargeCoder193@users.noreply.github.com> Date: Thu, 23 Jan 2025 12:37:37 -0500 Subject: [PATCH] make a open log button (#946) * make a open log button * move to function * make an icon * do something * small changes --- src/files.zig | 26 ++++++++++++++++++++++++++ src/gui/windows/error_prompt.zig | 25 +++++++++++++++++++++++-- src/gui/windows/save_selection.zig | 22 +++------------------- 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/files.zig b/src/files.zig index 89908c2e..c93577c6 100644 --- a/src/files.zig +++ b/src/files.zig @@ -21,6 +21,32 @@ pub fn writeZon(path: []const u8, zon: ZonElement) !void { try cwd().writeZon(path, zon); } +pub fn openDirInWindow(path: []const u8) void { + const newPath = main.stackAllocator.dupe(u8, path); + defer main.stackAllocator.free(newPath); + + if (builtin.os.tag == .windows) { + std.mem.replaceScalar(u8, newPath, '/', '\\'); + } + + const command = if(builtin.os.tag == .windows) .{"explorer", newPath} else .{"open", newPath}; + + const result = std.process.Child.run(.{ + .allocator = main.stackAllocator.allocator, + .argv = &command, + }) catch |err| { + std.log.err("Got error while trying to open file explorer: {s}", .{@errorName(err)}); + return; + }; + defer { + main.stackAllocator.free(result.stderr); + main.stackAllocator.free(result.stdout); + } + if(result.stderr.len != 0) { + std.log.err("Got error while trying to open file explorer: {s}", .{result.stderr}); + } +} + pub fn openDir(path: []const u8) !Dir { return Dir { .dir = try std.fs.cwd().makeOpenPath(path, .{}), diff --git a/src/gui/windows/error_prompt.zig b/src/gui/windows/error_prompt.zig index d0fb76d5..38d99611 100644 --- a/src/gui/windows/error_prompt.zig +++ b/src/gui/windows/error_prompt.zig @@ -4,10 +4,15 @@ const main = @import("root"); const files = main.files; const settings = main.settings; const Vec2f = main.vec.Vec2f; +const Texture = main.graphics.Texture; const gui = @import("../gui.zig"); const GuiWindow = gui.GuiWindow; const Label = @import("../components/Label.zig"); +const VerticalList = @import("../components/VerticalList.zig"); +const Button = @import("../components/Button.zig"); + +var fileExplorerIcon: Texture = undefined; pub var window = GuiWindow { .contentSize = Vec2f{128, 64}, @@ -19,15 +24,31 @@ pub var window = GuiWindow { }, }; +pub fn init() void { + fileExplorerIcon = Texture.initFromFile("assets/cubyz/ui/file_explorer_icon.png"); +} + +pub fn deinit() void { + fileExplorerIcon.deinit(); +} + +fn openLog(_: usize) void { + main.files.openDirInWindow("logs"); +} + const padding: f32 = 8; pub fn update() void { if (main.Window.Gamepad.wereControllerMappingsDownloaded()) { gui.closeWindowFromRef(&window); } } + pub fn onOpen() void { - const label = Label.init(.{padding, 16 + padding}, 128, "#ffff00The game encountered errors. Check the logs for details", .center); - window.rootComponent = label.toComponent(); + const list = VerticalList.init(.{padding, 16 + padding}, 300, 16); + list.add(Label.init(.{padding, 16 + padding}, 128, "#ffff00The game encountered errors. Check the logs for details", .center)); + list.add(Button.initIcon(.{0, 0}, .{16, 16}, fileExplorerIcon, false, .{.callback = &openLog})); + list.finish(.center); + window.rootComponent = list.toComponent(); window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @as(Vec2f, @splat(padding)); gui.updateWindowPositions(); } diff --git a/src/gui/windows/save_selection.zig b/src/gui/windows/save_selection.zig index 3ccf1a2f..691ee398 100644 --- a/src/gui/windows/save_selection.zig +++ b/src/gui/windows/save_selection.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const builtin = @import("builtin"); const main = @import("root"); const ConnectionManager = main.network.ConnectionManager; @@ -86,25 +85,10 @@ fn openFolder(namePtr: usize) void { const nullTerminatedName: [*:0]const u8 = @ptrFromInt(namePtr); const name = std.mem.span(nullTerminatedName); - const command = if(builtin.os.tag == .windows) .{"explorer"} else .{"open"}; - - const path_fmt = if (builtin.os.tag == .windows) "saves\\{s}" else "saves/{s}"; // Use backslashes on windows because it forces you to - const path = std.fmt.allocPrint(main.stackAllocator.allocator, path_fmt, .{name}) catch unreachable; + const path = std.fmt.allocPrint(main.stackAllocator.allocator, "saves/{s}", .{name}) catch unreachable; defer main.stackAllocator.free(path); - const result = std.process.Child.run(.{ - .allocator = main.stackAllocator.allocator, - .argv = &(command ++ .{path}), - }) catch |err| { - std.log.err("Got error while trying to open file explorer: {s}", .{@errorName(err)}); - return; - }; - defer { - main.stackAllocator.free(result.stderr); - main.stackAllocator.free(result.stdout); - } - if(result.stderr.len != 0) { - std.log.err("Got error while trying to open file explorer: {s}", .{result.stderr}); - } + + main.files.openDirInWindow(path); } fn parseEscapedFolderName(allocator: NeverFailingAllocator, name: []const u8) []const u8 {