diff --git a/examples/gui/message_box.zig b/examples/gui/message_box.zig index 6f7077c..880ff2c 100644 --- a/examples/gui/message_box.zig +++ b/examples/gui/message_box.zig @@ -13,9 +13,9 @@ const std = @import("std"); const rl = @import("raylib"); const rg = @import("raygui"); -// `rl.getColor` only accepts a `u32`. Performing `@intCast` on the return value -// of `rg.getStyle` invokes checked undefined behavior from Zig when passed to -// `rl.getColor`, hence the custom implementation here... +/// `rl.getColor` only accepts a `u32`. Performing `@intCast` on the return value +/// of `rg.getStyle` invokes checked undefined behavior from Zig when passed to +/// `rl.getColor`, hence the custom implementation here... fn getColor(hex: i32) rl.Color { var color: rl.Color = .black; // zig fmt: off @@ -43,7 +43,7 @@ pub fn main() !void { rl.clearBackground(getColor(color_int)); - if (rg.button(.init(24, 24, 120, 30), "#191#Show Message") > 0) + if (rg.button(.init(24, 24, 120, 30), "#191#Show Message")) show_message_box = true; if (show_message_box) { diff --git a/lib/generate_functions.py b/lib/generate_functions.py index ff5d4f6..d61dadb 100755 --- a/lib/generate_functions.py +++ b/lib/generate_functions.py @@ -98,6 +98,10 @@ MANUAL = [ "GuiListViewEx", "GuiPanel", "GuiScrollPanel", + "GuiButton", + "GuiLabelButton", + "GuiCheckBox", + "GuiTextBox", "DrawSplineLinear", "DrawSplineBasis", "DrawSplineCatmullRom", diff --git a/lib/preludes/raygui-prelude.zig b/lib/preludes/raygui-prelude.zig index ba84a85..fb69d6b 100644 --- a/lib/preludes/raygui-prelude.zig +++ b/lib/preludes/raygui-prelude.zig @@ -487,3 +487,25 @@ pub fn scrollPanel(bounds: Rectangle, text: ?[*:0]const u8, content: Rectangle, } return @as(i32, cdef.GuiScrollPanel(bounds, textFinal, content, @as([*c]Vector2, @ptrCast(scroll)), @as([*c]Rectangle, @ptrCast(view)))); } + +/// Button control, returns true when clicked +pub fn button(bounds: Rectangle, text: [:0]const u8) bool { + return @as(i32, cdef.GuiButton(bounds, @as([*c]const u8, @ptrCast(text)))) > 0; +} + +/// Label button control, returns true when clicked +pub fn labelButton(bounds: Rectangle, text: [:0]const u8) i32 { + return @as(i32, cdef.GuiLabelButton(bounds, @as([*c]const u8, @ptrCast(text)))) > 0; +} + +/// Check Box control, returns true when active +pub fn checkBox(bounds: Rectangle, text: [:0]const u8, checked: *bool) bool { + return @as(i32, cdef.GuiCheckBox(bounds, @as([*c]const u8, @ptrCast(text)), @as([*c]bool, @ptrCast(checked)))) > 0; +} + +/// Text Box control, updates input text +/// Returns true on ENTER pressed (useful for data validation) +pub fn textBox(bounds: Rectangle, text: [:0]u8, textSize: i32, editMode: bool) bool { + return @as(i32, cdef.GuiTextBox(bounds, @as([*c]u8, @ptrCast(text)), @as(c_int, textSize), editMode)) > 0; +} + diff --git a/lib/raygui.zig b/lib/raygui.zig index 32fe26d..cfe63be 100644 --- a/lib/raygui.zig +++ b/lib/raygui.zig @@ -488,6 +488,28 @@ pub fn scrollPanel(bounds: Rectangle, text: ?[*:0]const u8, content: Rectangle, return @as(i32, cdef.GuiScrollPanel(bounds, textFinal, content, @as([*c]Vector2, @ptrCast(scroll)), @as([*c]Rectangle, @ptrCast(view)))); } +/// Button control, returns true when clicked +pub fn button(bounds: Rectangle, text: [:0]const u8) bool { + return @as(i32, cdef.GuiButton(bounds, @as([*c]const u8, @ptrCast(text)))) > 0; +} + +/// Label button control, returns true when clicked +pub fn labelButton(bounds: Rectangle, text: [:0]const u8) i32 { + return @as(i32, cdef.GuiLabelButton(bounds, @as([*c]const u8, @ptrCast(text)))) > 0; +} + +/// Check Box control, returns true when active +pub fn checkBox(bounds: Rectangle, text: [:0]const u8, checked: *bool) bool { + return @as(i32, cdef.GuiCheckBox(bounds, @as([*c]const u8, @ptrCast(text)), @as([*c]bool, @ptrCast(checked)))) > 0; +} + +/// Text Box control, updates input text +/// Returns true on ENTER pressed (useful for data validation) +pub fn textBox(bounds: Rectangle, text: [:0]u8, textSize: i32, editMode: bool) bool { + return @as(i32, cdef.GuiTextBox(bounds, @as([*c]u8, @ptrCast(text)), @as(c_int, textSize), editMode)) > 0; +} + + /// Enable gui controls (global state) pub fn enable() void { cdef.GuiEnable(); @@ -598,16 +620,6 @@ pub fn label(bounds: Rectangle, text: [:0]const u8) i32 { return @as(i32, cdef.GuiLabel(bounds, @as([*c]const u8, @ptrCast(text)))); } -/// Button control, returns true when clicked -pub fn button(bounds: Rectangle, text: [:0]const u8) i32 { - return @as(i32, cdef.GuiButton(bounds, @as([*c]const u8, @ptrCast(text)))); -} - -/// Label button control, returns true when clicked -pub fn labelButton(bounds: Rectangle, text: [:0]const u8) i32 { - return @as(i32, cdef.GuiLabelButton(bounds, @as([*c]const u8, @ptrCast(text)))); -} - /// Toggle Button control pub fn toggle(bounds: Rectangle, text: [:0]const u8, active: *bool) i32 { return @as(i32, cdef.GuiToggle(bounds, @as([*c]const u8, @ptrCast(text)), @as([*c]bool, @ptrCast(active)))); @@ -623,11 +635,6 @@ pub fn toggleSlider(bounds: Rectangle, text: [:0]const u8, active: *i32) i32 { return @as(i32, cdef.GuiToggleSlider(bounds, @as([*c]const u8, @ptrCast(text)), @as([*c]c_int, @ptrCast(active)))); } -/// Check Box control, returns true when active -pub fn checkBox(bounds: Rectangle, text: [:0]const u8, checked: *bool) i32 { - return @as(i32, cdef.GuiCheckBox(bounds, @as([*c]const u8, @ptrCast(text)), @as([*c]bool, @ptrCast(checked)))); -} - /// Combo Box control pub fn comboBox(bounds: Rectangle, text: [:0]const u8, active: *i32) i32 { return @as(i32, cdef.GuiComboBox(bounds, @as([*c]const u8, @ptrCast(text)), @as([*c]c_int, @ptrCast(active)))); @@ -653,11 +660,6 @@ pub fn valueBoxFloat(bounds: Rectangle, text: [:0]const u8, textValue: [:0]u8, v return @as(i32, cdef.GuiValueBoxFloat(bounds, @as([*c]const u8, @ptrCast(text)), @as([*c]u8, @ptrCast(textValue)), @as([*c]f32, @ptrCast(value)), editMode)); } -/// Text Box control, updates input text -pub fn textBox(bounds: Rectangle, text: [:0]u8, textSize: i32, editMode: bool) i32 { - return @as(i32, cdef.GuiTextBox(bounds, @as([*c]u8, @ptrCast(text)), @as(c_int, textSize), editMode)); -} - /// Slider control pub fn slider(bounds: Rectangle, textLeft: [:0]const u8, textRight: [:0]const u8, value: *f32, minValue: f32, maxValue: f32) i32 { return @as(i32, cdef.GuiSlider(bounds, @as([*c]const u8, @ptrCast(textLeft)), @as([*c]const u8, @ptrCast(textRight)), @as([*c]f32, @ptrCast(value)), minValue, maxValue));