LOTS OF STUFF

* Move all global variables to UI_Context struct
* Add string formatting to API
* Allocate formatted (and const too, gross) strings via a frame allocator
* Add higher level Grid component

still lots of jank and built-in assumptions
master
Patrick Cleavelin 2023-07-11 16:06:40 -05:00
parent 8e7a5bb3a8
commit ad8ddae790
2 changed files with 841 additions and 675 deletions

View File

@ -3,45 +3,58 @@ const ui = @import("lib.zig");
const raylib = ui.raylib; const raylib = ui.raylib;
pub const Grid = struct { pub const Grid = struct {
fn MakeColumnHeader(columns: [][:0]const u8) !void { fn MakeColumnHeader(ctx: *ui.UIContext, columns: [][]const u8) !void {
_ = try ui.PushBox("GridColumnHeader", .{ .drawBackground = true, .drawBorder = false }, .leftToRight, .fitToChildren); _ = try ctx.PushBox("GridColumnHeader", .{ .drawBackground = true, .drawBorder = false }, .leftToRight, .fitToChildren);
defer ui.PopBox(); defer ctx.PopBox();
try ui.PushStyle(.{ // try ctx.PushStyle(.{ //
.color = raylib.WHITE, // .color = raylib.WHITE, //
.text_color = .{ .r = 0x18, .g = 0x90, .b = 0xff, .a = 0xff }, // .text_color = .{ .r = 0x18, .g = 0x90, .b = 0xff, .a = 0xff }, //
.text_size = 12, // .text_size = 12, //
.text_padding = 12, // .text_padding = 12, //
}); });
defer ui.PopStyle(); defer ctx.PopStyle();
for (columns) |column| { for (columns) |column| {
try MakeLabel(column, @as(i32, @intCast(columns.len))); try MakeLabel(ctx, column, @as(i32, @intCast(columns.len)));
} }
} }
pub fn MakeButton(label: [:0]const u8, items: i32) !bool { pub fn MakeButton(ctx: *ui.UIContext, label: []const u8, items: i32) !bool {
return try ui.MakeBox(label, .{ return try ctx.MakeBox(label, .{
.clickable = true, .clickable = true,
.drawText = true, .drawText = true,
}, .leftToRight, .{ .percentOfParent = ui.Vec2{ .x = 1.0 / @as(f32, @floatFromInt(items)), .y = 1.0 } }); }, .leftToRight, .{ .percentOfParent = ui.Vec2{ .x = 1.0 / @as(f32, @floatFromInt(items)), .y = 1.0 } });
} }
pub fn MakeLabel(label: [:0]const u8, items: i32) !void { pub fn MakeFormattedLabel(ctx: *ui.UIContext, comptime str: []const u8, args: anytype, items: i32) !void {
_ = try ui.MakeLabelWithLayout(label, .{ .percentOfParent = ui.Vec2{ .x = 1.0 / @as(f32, @floatFromInt(items)), .y = 1.0 } }); _ = try ctx.MakeFormattedLabelWithLayout(str, args, .{ .percentOfParent = ui.Vec2{ .x = 1.0 / @as(f32, @floatFromInt(items)), .y = 1.0 } });
} }
pub fn MakeGrid(comptime T: type, columns: [][:0]const u8, data: []T, MakeBody: *const fn (data: *const T, size: i32) anyerror!void) !void { pub fn MakeLabel(ctx: *ui.UIContext, label: []const u8, items: i32) !void {
_ = try ui.PushBox("GridContainer", .{ .drawBackground = true }, .topToBottom, .fill); _ = try ctx.MakeLabelWithLayout(label, .{ .percentOfParent = ui.Vec2{ .x = 1.0 / @as(f32, @floatFromInt(items)), .y = 1.0 } });
defer ui.PopBox(); }
try MakeColumnHeader(columns); pub fn MakeGrid(ctx: *ui.UIContext, comptime T: type, columns: [][]const u8, data: []T, MakeBody: *const fn (ctx: *ui.UIContext, data: *const T, size: i32) anyerror!void) !void {
_ = try ctx.PushBox("GridContainer", .{ .drawBackground = true }, .topToBottom, .fill);
defer ctx.PopBox();
try MakeColumnHeader(ctx, columns);
{ {
_ = try ui.PushBox("Grid", .{ .drawBackground = true, .scrollable = true }, .topToBottom, .fill); try ctx.PushStyle(.{ //
defer ui.PopBox(); .color = raylib.WHITE, //
.hover_color = .{ .r = 0x1a, .g = 0x7c, .b = 0xd3, .a = 0xff }, //
.text_color = raylib.BLACK, //
.text_size = 12, //
.text_padding = 12, //
});
defer ctx.PopStyle();
try ui.PushStyle(.{ // _ = try ctx.PushBox("Grid", .{ .drawBackground = true, .scrollable = true }, .topToBottom, .fill);
defer ctx.PopBox();
try ctx.PushStyle(.{ //
.color = raylib.WHITE, // .color = raylib.WHITE, //
.hover_color = raylib.LIGHTGRAY, // .hover_color = raylib.LIGHTGRAY, //
.text_color = raylib.BLACK, // .text_color = raylib.BLACK, //
@ -49,12 +62,12 @@ pub const Grid = struct {
.text_padding = 12, // .text_padding = 12, //
}); });
for (data) |item| { for (data) |item| {
_ = try ui.PushBox("GridItem", .{ .drawBackground = true, .drawBorder = false, .hoverable = true }, .leftToRight, .fitToChildren); _ = try ctx.PushBox("GridItem", .{ .drawBackground = true, .drawBorder = false, .hoverable = true }, .leftToRight, .fitToChildren);
defer ui.PopBox(); defer ctx.PopBox();
try MakeBody(&item, @as(i32, @intCast(columns.len))); try MakeBody(ctx, &item, @as(i32, @intCast(columns.len)));
} }
defer ui.PopStyle(); ctx.PopStyle();
} }
} }
}; };

File diff suppressed because it is too large Load Diff