From 8742b86a4451f215419e1aa2e513a394b0b693af Mon Sep 17 00:00:00 2001 From: Vanic Date: Sun, 18 Jan 2026 16:34:37 +0300 Subject: [PATCH] Formspec: add some methods. #8 --- mods/CORE/Formspec/src/Formspec.lua | 225 +++++++++++++++++++++++++--- 1 file changed, 201 insertions(+), 24 deletions(-) diff --git a/mods/CORE/Formspec/src/Formspec.lua b/mods/CORE/Formspec/src/Formspec.lua index 71ac66c..f5c0b20 100644 --- a/mods/CORE/Formspec/src/Formspec.lua +++ b/mods/CORE/Formspec/src/Formspec.lua @@ -9,7 +9,7 @@ local Formspec = { ---Set the formspec version to a certain number. If not specified, version `10` is assumed. ---@field version? number ---Define the size of the menu in inventory slots. `fixed_size` is optional, default `true` ----@field size {x:number, y:number, fixed_size:boolean?} +---@field size? {x:number, y:number, fixed_size:boolean?} ---Defines the position on the game window of the formspec's anchor point. ---@field position? {x:number, y:number} ---Defines the location of the anchor point within the formspec. @@ -36,7 +36,12 @@ function Formspec:new(def) local version = ('formspec_version[%s]'):format( def.version or 10) -- formspec version 10 (5.13.0) - local size = ('size[%s,%s,%s]'):format(def.size.x, def.size.y, def.size.fixed_size or true) + local size + if def.size ~= nil then + size = ('size[%s,%s,%s]'):format(def.size.x, def.size.y, def.size.fixed_size or true) + else + size = '' + end local position if def.position ~= nil then @@ -322,8 +327,9 @@ end ---@param w number ---@param h number ---@param textureName string ----@param autoClip boolean? If is `true`, the background is clipped ----to the formspec size (x and y are used as offset values, w and h are ignored) +---@param autoClip boolean? If is `true`, the background is clipped. +--- to the formspec size (x and y are used as offset +--- values, w and h are ignored) function Formspec:background(x, y, w, h, textureName, autoClip) local element @@ -338,54 +344,225 @@ function Formspec:background(x, y, w, h, textureName, autoClip) self:addElement(element) end ----TODO -function Formspec:background9() +---9-sliced background. See https://en.wikipedia.org/wiki/9-slice_scaling +---@param x number +---@param y number +---@param w number +---@param h number +---@param textureName string +---@param autoClip boolean? If is `true`, the background is clipped. +--- to the formspec size (x and y are used as offset +--- values, w and h are ignored) +---@param middle string Middle is a rect which defines the middle of the 9-slice. +--- * `x` - The middle will be x pixels from all sides. +--- * `x,y` - The middle will be x pixels from the horizontal and y from the vertical. +--- * `x,y,x2,y2` - The middle will start at x,y, and end at x2, y2. +--- Negative x2 and y2 values will be added to the width and height of +--- the texture, allowing it to be used as the distance from the far end. +--- * All numbers in middle are integers. +function Formspec:background9(x, y, w, h, textureName, autoClip, middle) + local element = ('background9[%s,%s;%s,%s;%s;%s]'):format( + x, y, w, h, textureName, autoClip, middle) + self:addElement(element) end ----TODO -function Formspec:pwdfield() +---Textual password style field; will be sent to server when a button is clicked +---When enter is pressed in field, `fields.key_enter_field` will be sent with the name of this field. +---With the old coordinate system, fields are a set height, but will be vertically centered on `H`. +---With the new coordinate system, `H` will modify the height. +--- +---See field_close_on_enter to stop enter closing the formspec. +---@param x number +---@param y number +---@param w number +---@param h number +---@param name string Is the name of the field as returned in `fields to on_receive_fields`. +---@param label string? If not blank, will be text printed on the top left above the field. +function Formspec:pwdfield(x, y, w, h, name, label) + local element = ('pwdfield[%s,%s;%s,%s;%s;%s]'):format( + x, y, w, h, name, label or '') + self:addElement(element) end ----TODO -function Formspec:field() +---Textual field; will be sent to server when a button is clicked. +---When enter is pressed in field, `fields.key_enter_field` will be sent with the name of this field. +---With the old coordinate system, fields are a set height, but will be vertically centered on `H`. +---With the new coordinate system, `H` will modify the height. +--- +---See field_close_on_enter to stop enter closing the formspec. +---@param x number +---@param y number +---@param w number +---@param h number +---@param name string Is the name of the field as returned in `fields to on_receive_fields`. +---@param label string? If not blank, will be text printed on the top left above the field. +---@param default string? is the default value of the field default may contain variable references +--- such as `${text}` which will fill the value from the metadata value text. +--- Note: no extra text or more than a single variable is supported ATM. +function Formspec:field(x, y, w, h, name, label, default) + local element = ('field[%s,%s;%s,%s;%s;%s;%s]'):format( + x, y, w, h, name, label or '', default or '') + self:addElement(element) end ----TODO -function Formspec:fieldCloseOnEnter() +---Textual field; will be sent to server when a button is clicked. +---When enter is pressed in field, `fields.key_enter_field` will be sent with the name of this field. +---Special field for creating simple forms, such as sign text input +---Must be used without a `size[]` element +---A "Proceed" button will be added automatically +--- +---See field_close_on_enter to stop enter closing the formspec. +---@param name string Is the name of the field as returned in `fields to on_receive_fields`. +---@param label string? If not blank, will be text printed on the top left above the field. +---@param default string? is the default value of the field default may contain variable references +--- such as `${text}` which will fill the value from the metadata value text. +--- Note: no extra text or more than a single variable is supported ATM. +function Formspec:fieldWithoutPos(name, label, default) + local element = ('field[%s,%s;%s,%s;%s;%s;%s]'):format( + name, label or '', default or '') + self:addElement(element) end ----TODO -function Formspec:textArea() +---**Experimental**, may be subject to change or removal at any time. +---Only affects Android clients. +---@param name string Is the name of the field. +---@param enterAfterEdit boolean? If is true, pressing the "Done" button in the Android +--- text input dialog will simulate an Enter keypress. Defaults to `false` +--- when not specified (i.e. no tag for a field). +function Formspec:fieldEnterAfterEdit(name, enterAfterEdit) + local element = ('field_enter_after_edit[%s;%s]'):format( + name, enterAfterEdit or false) + + self:addElement(element) end ----TODO -function Formspec:label() +---@param name string Is the name of the field. +---@param closeOnEnter boolean? If` is false, pressing Enter in the field will submit the form but not close it. +--- Defaults to true when not specified (i.e. no tag for a field). +function Formspec:fieldCloseOnEnter(name, closeOnEnter) + local element = ('field_enter_after_edit[%s;%s]'):format( + name, closeOnEnter or false) + self:addElement(element) end ----TODO -function Formspec:hypertext() +---Same as text fields, but with multi-line input. +---Text is wrapped to fit within the given bounds. +---If the text overflows, a vertical scrollbar is added. +---@param x number +---@param y number +---@param w number +---@param h number +---@param name string? Is the name of the field as returned in `fields to on_receive_fields`. +--- If the name is empty, the textarea is read-only and the background is not shown, +--- which corresponds to a multi-line label. See also `label[,;,;