Skip to content

Zig: Forms

Declarations

gui.Handler

Zig
pub const Handler = fn (user: ?*anyopaque, response: Response) void;

What a handler receives: the user pointer given at send and the answer.

gui.CustomForm

Zig
1
2
3
4
5
6
7
pub const CustomForm = struct {
    allocator: Allocator,
    title: []const u8,
    submit_text: ?[]const u8 = null,
    elements: std.ArrayList(u8) = .empty,
    // ...
};

A custom form: input fields, toggles, dropdowns and sliders, each named so the answer can be read by name. title is borrowed and must outlive the send.

gui.CustomForm.init

Zig
pub fn init(allocator: Allocator, title: []const u8) CustomForm
  • Parameters:
    • allocator : Allocator
    • title : []const u8
  • Return type: CustomForm

gui.CustomForm.deinit

Zig
pub fn deinit(self: *CustomForm) void
  • Return type: void

gui.CustomForm.setSubmit

Zig
pub fn setSubmit(self: *CustomForm, text: []const u8) void

The text of the submit button.

  • Parameters:
    • text : []const u8
  • Return type: void

gui.CustomForm.header

Zig
pub fn header(self: *CustomForm, text: []const u8) Allocator.Error!void
  • Parameters:
    • text : []const u8
  • Return type: Allocator.Error!void

gui.CustomForm.label

Zig
pub fn label(self: *CustomForm, text: []const u8) Allocator.Error!void
  • Parameters:
    • text : []const u8
  • Return type: Allocator.Error!void

gui.CustomForm.divider

Zig
pub fn divider(self: *CustomForm) Allocator.Error!void
  • Return type: Allocator.Error!void

gui.CustomForm.input

Zig
pub fn input(self: *CustomForm, name: []const u8, text: []const u8, placeholder: []const u8, default: []const u8) Allocator.Error!void
  • Parameters:
    • name : []const u8
    • text : []const u8
    • placeholder : []const u8
    • default : []const u8
  • Return type: Allocator.Error!void

gui.CustomForm.toggle

Zig
pub fn toggle(self: *CustomForm, name: []const u8, text: []const u8, default: bool) Allocator.Error!void
  • Parameters:
    • name : []const u8
    • text : []const u8
    • default : bool
  • Return type: Allocator.Error!void

gui.CustomForm.dropdown

Zig
pub fn dropdown(self: *CustomForm, name: []const u8, text: []const u8, options: []const []const u8, default: usize) Allocator.Error!void
  • Parameters:
    • name : []const u8
    • text : []const u8
    • options : []const []const u8
    • default : usize
  • Return type: Allocator.Error!void

gui.CustomForm.stepSlider

Zig
pub fn stepSlider(self: *CustomForm, name: []const u8, text: []const u8, steps: []const []const u8, default: usize) Allocator.Error!void
  • Parameters:
    • name : []const u8
    • text : []const u8
    • steps : []const []const u8
    • default : usize
  • Return type: Allocator.Error!void

gui.CustomForm.slider

Zig
pub fn slider(self: *CustomForm, name: []const u8, text: []const u8, min: f64, max: f64, step: f64, default: f64) Allocator.Error!void
  • Parameters:
    • name : []const u8
    • text : []const u8
    • min : f64
    • max : f64
    • step : f64
    • default : f64
  • Return type: Allocator.Error!void

gui.CustomForm.send

Zig
pub fn send(self: *const CustomForm, player: props.Player, user: ?*anyopaque, comptime handler: Handler) core.Error!void
  • Parameters:
    • player : props.Player
    • user : ?*anyopaque
    • handler : comptime Handler
  • Return type: core.Error!void
  • Slots: form_send

gui.SimpleForm

Zig
1
2
3
4
5
6
7
8
pub const SimpleForm = struct {
    allocator: Allocator,
    title: []const u8,
    content: []const u8 = "",
    elements: std.ArrayList(u8) = .empty,
    buttons: usize = 0,
    // ...
};

A simple form: a column of buttons. title and content are borrowed and must outlive the send.

gui.SimpleForm.init

Zig
pub fn init(allocator: Allocator, title: []const u8) SimpleForm
  • Parameters:
    • allocator : Allocator
    • title : []const u8
  • Return type: SimpleForm

gui.SimpleForm.deinit

Zig
pub fn deinit(self: *SimpleForm) void
  • Return type: void

gui.SimpleForm.setContent

Zig
pub fn setContent(self: *SimpleForm, content: []const u8) void
  • Parameters:
    • content : []const u8
  • Return type: void

gui.SimpleForm.button

Zig
pub fn button(self: *SimpleForm, text: []const u8) Allocator.Error!void
  • Parameters:
    • text : []const u8
  • Return type: Allocator.Error!void

gui.SimpleForm.buttonWithImage

Zig
pub fn buttonWithImage(self: *SimpleForm, text: []const u8, image: []const u8, image_type: []const u8) Allocator.Error!void

A button with an icon. image_type is "path" or "url".

  • Parameters:
    • text : []const u8
    • image : []const u8
    • image_type : []const u8
  • Return type: Allocator.Error!void

gui.SimpleForm.header

Zig
pub fn header(self: *SimpleForm, text: []const u8) Allocator.Error!void
  • Parameters:
    • text : []const u8
  • Return type: Allocator.Error!void

gui.SimpleForm.label

Zig
pub fn label(self: *SimpleForm, text: []const u8) Allocator.Error!void
  • Parameters:
    • text : []const u8
  • Return type: Allocator.Error!void

gui.SimpleForm.divider

Zig
pub fn divider(self: *SimpleForm) Allocator.Error!void
  • Return type: Allocator.Error!void

gui.SimpleForm.send

Zig
pub fn send(self: *const SimpleForm, player: props.Player, user: ?*anyopaque, comptime handler: Handler) core.Error!void

Sends it. A form with no button at all is Refused, since a player cannot press it.

  • Parameters:
    • player : props.Player
    • user : ?*anyopaque
    • handler : comptime Handler
  • Return type: core.Error!void
  • Slots: form_send

gui.FormValue

Zig
1
2
3
4
5
6
7
pub const FormValue = union(enum) {
    index: usize,
    number: f64,
    text: []const u8,
    boolean: bool,
    // ...
};

The value one control of a custom form returns.

gui.FormValue.asIndex

Zig
pub fn asIndex(self: FormValue) ?usize

The chosen position of a dropdown or a step slider.

  • Return type: ?usize

gui.FormValue.asF64

Zig
pub fn asF64(self: FormValue) ?f64
  • Return type: ?f64

gui.FormValue.asI64

Zig
pub fn asI64(self: FormValue) ?i64
  • Return type: ?i64

gui.FormValue.asBool

Zig
pub fn asBool(self: FormValue) ?bool
  • Return type: ?bool

gui.FormValue.asText

Zig
pub fn asText(self: FormValue) ?[]const u8
  • Return type: ?[]const u8

gui.Response

Zig
pub const Response = union(enum) {
    /// The player closed the form; the reason is the engine's cancel code, or -1.
    cancelled: i32,
    /// The index of the button a simple form's player pressed.
    button: usize,
    /// Whether the player pressed the upper button of a modal form.
    modal: bool,
    custom: Custom,
    /// An answer in a shape this binding does not know, as the host wrote it.
    unknown: []const u8,
    // ...
};

A player's answer to a form.

gui.Response.isCancelled

Zig
pub fn isCancelled(self: Response) bool
  • Return type: bool

gui.Response.value

Zig
pub fn value(self: Response, name: []const u8) ?FormValue

The value of one control of a custom form.

  • Parameters:
    • name : []const u8
  • Return type: ?FormValue

gui.Response.text

Zig
pub fn text(self: Response, name: []const u8) ?[]const u8

The text of the selected item of one control of a custom form.

  • Parameters:
    • name : []const u8
  • Return type: ?[]const u8

gui.Custom

Zig
1
2
3
4
pub const Custom = struct {
    entries: []const Entry,
    // ...
};

The answers of a custom form, one entry per named control.

gui.Custom.value

Zig
pub fn value(self: Custom, name: []const u8) ?FormValue

The value of one control.

  • Parameters:
    • name : []const u8
  • Return type: ?FormValue

gui.Custom.text

Zig
pub fn text(self: Custom, name: []const u8) ?[]const u8

The text of the selected item of one control.

  • Parameters:
    • name : []const u8
  • Return type: ?[]const u8

gui.ModalForm

Zig
1
2
3
4
5
6
7
8
pub const ModalForm = struct {
    allocator: Allocator,
    title: []const u8,
    content: []const u8,
    upper: []const u8 = "OK",
    lower: []const u8 = "Cancel",
    // ...
};

A modal form: some text and two buttons. Every string is borrowed and must outlive the send.

gui.ModalForm.init

Zig
pub fn init(allocator: Allocator, title: []const u8, content: []const u8) ModalForm
  • Parameters:
    • allocator : Allocator
    • title : []const u8
    • content : []const u8
  • Return type: ModalForm

gui.ModalForm.send

Zig
pub fn send(self: *const ModalForm, player: props.Player, user: ?*anyopaque, comptime handler: Handler) core.Error!void
  • Parameters:
    • player : props.Player
    • user : ?*anyopaque
    • handler : comptime Handler
  • Return type: core.Error!void
  • Slots: form_send

gui.Custom.Entry

Zig
1
2
3
4
5
6
7
pub const Entry = struct {
    name: []const u8,
    value: FormValue,
    /// The text of the selected item, for a control that has one.
    text: ?[]const u8,
    // ...
};