Skip to content

Commit

Permalink
[zgui] Fixed buffer type for inputText* to include null sentinel.
Browse files Browse the repository at this point in the history
The slice length is for maximum text length, whereas the sentinel
denotes the current text length.

Added output of current text length, which would have errored before
this change since the buffers didn't include a sentinel in their type.

See https://github.com/zig-gamedev/zig-gamedev/issues/352#issuecomment-2038175998
  • Loading branch information
Pyrolistical committed Apr 4, 2024
1 parent 064c6f9 commit 9fca180
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
6 changes: 3 additions & 3 deletions libs/zgui/src/gui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2333,7 +2333,7 @@ pub const InputTextCallbackData = extern struct {
pub const InputTextCallback = *const fn (data: *InputTextCallbackData) i32;
//--------------------------------------------------------------------------------------------------
pub fn inputText(label: [:0]const u8, args: struct {
buf: []u8,
buf: [:0]u8,
flags: InputTextFlags = .{},
callback: ?InputTextCallback = null,
user_data: ?*anyopaque = null,
Expand All @@ -2357,7 +2357,7 @@ extern fn zguiInputText(
) bool;
//--------------------------------------------------------------------------------------------------
pub fn inputTextMultiline(label: [:0]const u8, args: struct {
buf: []u8,
buf: [:0]u8,
w: f32 = 0.0,
h: f32 = 0.0,
flags: InputTextFlags = .{},
Expand Down Expand Up @@ -2388,7 +2388,7 @@ extern fn zguiInputTextMultiline(
//--------------------------------------------------------------------------------------------------
pub fn inputTextWithHint(label: [:0]const u8, args: struct {
hint: [:0]const u8,
buf: []u8,
buf: [:0]u8,
flags: InputTextFlags = .{},
callback: ?InputTextCallback = null,
user_data: ?*anyopaque = null,
Expand Down
49 changes: 39 additions & 10 deletions samples/gui_test_wgpu/src/gui_test_wgpu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const DemoState = struct {
font_normal: zgui.Font,
font_large: zgui.Font,
draw_list: zgui.DrawList,
alloced_input_text_buf: [:0]u8,
alloced_input_text_multiline_buf: [:0]u8,
alloced_input_text_with_hint_buf: [:0]u8,
};
var _te: *zgui.te.TestEngine = undefined;

Expand Down Expand Up @@ -105,7 +108,6 @@ fn create(allocator: std.mem.Allocator, window: *zglfw.Window) !*DemoState {
const style = zgui.getStyle();

style.window_min_size = .{ 320.0, 240.0 };
style.window_border_size = 8.0;
style.scrollbar_size = 6.0;
{
var color = style.getColor(.scrollbar_grab);
Expand Down Expand Up @@ -133,7 +135,13 @@ fn create(allocator: std.mem.Allocator, window: *zglfw.Window) !*DemoState {
.font_normal = font_normal,
.font_large = font_large,
.draw_list = draw_list,
.alloced_input_text_buf = try allocator.allocSentinel(u8, 128, 0),
.alloced_input_text_multiline_buf = try allocator.allocSentinel(u8, 128, 0),
.alloced_input_text_with_hint_buf = try allocator.allocSentinel(u8, 128, 0),
};
demo.alloced_input_text_buf[0] = 0;
demo.alloced_input_text_multiline_buf[0] = 0;
demo.alloced_input_text_with_hint_buf[0] = 0;

return demo;
}
Expand All @@ -144,6 +152,9 @@ fn destroy(allocator: std.mem.Allocator, demo: *DemoState) void {
zgui.destroyDrawList(demo.draw_list);
zgui.deinit();
demo.gctx.destroy(allocator);
allocator.free(demo.alloced_input_text_buf);
allocator.free(demo.alloced_input_text_multiline_buf);
allocator.free(demo.alloced_input_text_with_hint_buf);
allocator.destroy(demo);
}

Expand Down Expand Up @@ -463,9 +474,9 @@ fn update(demo: *DemoState) !void {

if (zgui.collapsingHeader("Widgets: Input with Keyboard", .{})) {
const static = struct {
var buf: [128]u8 = undefined;
var buf1: [128]u8 = undefined;
var buf2: [128]u8 = undefined;
var input_text_buf = [_:0]u8{0} ** 128;
var input_text_multiline_buf = [_:0]u8{0} ** 128;
var input_text_with_hint_buf = [_:0]u8{0} ** 128;
var v1: f32 = 0;
var v2: [2]f32 = .{ 0, 0 };
var v3: [3]f32 = .{ 0, 0, 0 };
Expand All @@ -478,12 +489,30 @@ fn update(demo: *DemoState) !void {
var si8: i8 = 0;
var v3u8: [3]u8 = .{ 0, 0, 0 };
};
_ = zgui.inputText("Input text", .{ .buf = static.buf[0..] });
_ = zgui.inputTextMultiline("Input text multiline", .{ .buf = static.buf1[0..] });
_ = zgui.inputTextWithHint(
"Input text with hint",
.{ .hint = "Enter your name", .buf = static.buf2[0..] },
);
zgui.separatorText("static input text");
_ = zgui.inputText("Input text", .{ .buf = static.input_text_buf[0..] });
_ = zgui.text("length of Input text {}", .{std.mem.len(@as([*:0]u8, static.input_text_buf[0..]))});

_ = zgui.inputTextMultiline("Input text multiline", .{ .buf = static.input_text_multiline_buf[0..] });
_ = zgui.text("length of Input text multiline {}", .{std.mem.len(@as([*:0]u8, static.input_text_multiline_buf[0..]))});
_ = zgui.inputTextWithHint("Input text with hint", .{
.hint = "Enter your name",
.buf = static.input_text_with_hint_buf[0..],
});
_ = zgui.text("length of Input text with hint {}", .{std.mem.len(@as([*:0]u8, static.input_text_with_hint_buf[0..]))});

zgui.separatorText("alloced input text");
_ = zgui.inputText("Input text alloced", .{ .buf = demo.alloced_input_text_buf });
_ = zgui.text("length of Input text alloced {}", .{std.mem.len(demo.alloced_input_text_buf.ptr)});
_ = zgui.inputTextMultiline("Input text multiline alloced", .{ .buf = demo.alloced_input_text_multiline_buf });
_ = zgui.text("length of Input text multiline {}", .{std.mem.len(demo.alloced_input_text_multiline_buf.ptr)});
_ = zgui.inputTextWithHint("Input text with hint alloced", .{
.hint = "Enter your name",
.buf = demo.alloced_input_text_with_hint_buf,
});
_ = zgui.text("length of Input text with hint alloced {}", .{std.mem.len(demo.alloced_input_text_with_hint_buf.ptr)});

zgui.separatorText("input numeric");
_ = zgui.inputFloat("Input float 1", .{ .v = &static.v1 });
_ = zgui.inputFloat2("Input float 2", .{ .v = &static.v2 });
_ = zgui.inputFloat3("Input float 3", .{ .v = &static.v3 });
Expand Down

0 comments on commit 9fca180

Please sign in to comment.