Backlog/extras/gameExtras/src/debuggers/fileBrowser.zig

108 lines
3.2 KiB
Zig

pub const SortMode = enum {
unordered,
name,
dateModified,
};
pub fn Browser(comptime T: type) type {
return struct {
list: ?*[]T = null,
display: std.ArrayListUnmanaged(u32) = .{},
allocator: std.mem.Allocator,
sameWindow: bool = false,
windowOpen: bool = false,
browserName: []u8,
sortMode: SortMode = .unordered,
pub fn create(allocator: std.mem.Allocator) !*@This() {
const self = try allocator.create(@This());
self.* = .{
.browserName = try std.fmt.allocPrintSentinel(allocator, "Browser", .{}, 0),
.allocator = allocator,
};
return self;
}
pub fn setList(self: *@This(), list: *[]T) void {
self.list = list;
}
fn sortList(self: *@This()) void {
if (self.list) |list| {
self.display.clearRetainingCapacity();
if (self.sortMode == .unordered) {
for (list.*, 0..) |x, i| {
_ = x;
self.display.append(self.allocator, @intCast(i)) catch unreachable;
}
}
}
}
pub fn tickDisplay(self: *@This()) void {
self.sortList();
if (!self.sameWindow) {
if (!ig.begin(self.browserName.ptr, &self.windowOpen, .{})) {
ig.end();
return;
}
}
if (ig.beginTable("FileBrowserTable", 2, .{ .resizable = true }, .{}, 0)) {
ig.tableSetupColumn("File Name", .{ .width_stretch = true }, 0, 0);
ig.tableSetupColumn("Modified Time", .{ .width_fixed = true }, 150, 0);
ig.tableHeadersRow();
if (self.list) |list| {
for (self.display.items) |i| {
const item = &list.*[i];
ig.tableNextRow(.{}, 0);
_ = ig.tableSetColumnIndex(0);
if (@hasDecl(T, "fileName")) {
const filename_str = item.fileName();
ig.textSlice(filename_str);
} else {
continue;
}
_ = ig.tableSetColumnIndex(1);
if (@hasDecl(T, "modtime")) {
const modtime_str = item.modtime();
ig.textSlice(modtime_str);
} else {
ig.textSlice("N/A");
}
}
} else {}
ig.endTable();
}
if (!self.sameWindow) {
ig.end();
}
}
pub fn destroy(self: *@This()) void {
self.display.deinit(self.allocator);
self.allocator.free(self.browserName);
self.allocator.destroy(self);
}
};
}
const std = @import("std");
const backlog = @import("Backlog");
const core = backlog.core;
const rend = backlog.rend;
const ig = backlog.imgui.api;
const igutils = backlog.imgui.utils;