Formspec: adding the remaining methods. close #8

This commit is contained in:
Van
2026-01-22 14:17:28 +03:00
parent 6904090d19
commit c3bdb29852

View File

@@ -1,3 +1,6 @@
local tableConcat, tableKeyValueList, tableInsert
= table.concat, table.key_value_list, table.insert
local getInstance = Class.getInstance
---@class Formspec
@@ -107,25 +110,26 @@ function Formspec:new(def)
return instance
end
---TODO: описание
---Returns a string specifying the form parameters:
---`version`, `size`, `position`, `anchor`, `padding`, `no_prepend`, `real_coordinates`, `allowClose`.
---@return string
function Formspec:getParams()
return self.params
end
---TODO: описание
---Returns a string containing all elements registered in the form.
---@return string
function Formspec:getElements()
return self.elements
end
---TODO: описание
---Returns a formspec string that is a form definition for Luanti.
---@return string
function Formspec:toString()
return self:getParams()..self:getElements()
end
---TODO: описание
---Adds an element to the list of form elements.
---@param element string
function Formspec:addElement(element)
self.elements = self.elements..element
@@ -144,7 +148,7 @@ function Formspec:container(container, x, y)
self:addElement(element)
end
---Start of a scroll_container block. All contained elements will
---Create scroll_container block. All contained elements will
---take the scroll_container coordinate as position origin,
---be additionally moved by the current value of the scrollbar with
---the name `scrollbar name` times `scroll factor` along the orientation `orientation` and
@@ -159,7 +163,8 @@ end
---@param scrollbarName string
---@param orientation 'vertical'|'horizontal'
---@param scrollFactor number? Defaults to 0.1.
---@param contentPadding string? TODO: разобраться, что тут
---@param contentPadding number? In formspec coordinate units. If specified, the scrollbar properties `max` and `thumbsize` are calculated
--- automatically based on the content size plus `content padding` at the end of the container. `min` is set to `0`.
function Formspec:scrollContainer(container, x, y, w, h, scrollbarName, orientation, scrollFactor, contentPadding)
local element = ('scroll_container[%s,%s;%s,%s;%s;%s;%s;%s]'):format(
x, y, w, h, scrollbarName, orientation, scrollFactor or '', contentPadding or '')..
@@ -192,10 +197,10 @@ function Formspec:list(inventoryLocation, listName, x, y, w, h, startingItemInde
self:addElement(element)
end
---TODO: немного отличается от листринга луанти, так что надо дописать подробности
---Creates and appends a list of `listring[<inventory location>;<list name>]` items based on the passed inventory list.
---
---**Elements:** `listring[<inventory location>;<list name>]` or `listring[]`
---@param lists [string, string][]? List of looping lists in the format `[inventory location, list name]`
---@param lists [string, string][]? List of looping lists in the format `[inventory location, list name]`. If `nil`, it will be added `listring[]`.
function Formspec:listring(lists)
if lists == nil then
self:addElement('listring[]')
@@ -668,7 +673,7 @@ function Formspec:imageButton(x, y, w, h, textureName, name, label, pressedTextu
self:addElement(element)
end
---TODO: описание
---Clickable item image button. When clicked, fields will be sent.
---
---**Element:** `item_image_button[<X>,<Y>;<W>,<H>;<item name>;<name>;<label>]`
---@param x number
@@ -756,10 +761,10 @@ function Formspec:textlist(x, y, w, h, name, listElements, selectedIdx, transpar
if selectedIdx == nil and transparent == nil then
element = ('textlist[%s,%s;%s,%s;%s;%s]'):format(
x, y, w, h, name, table.concat(listElements, ','))
x, y, w, h, name, tableConcat(listElements, ','))
else
element = ('textlist[%s,%s;%s,%s;%s;%s;%s;%s]'):format(
x, y, w, h, name, table.concat(listElements, ','), selectedIdx or '', transparent or '')
x, y, w, h, name, tableConcat(listElements, ','), selectedIdx or '', transparent or '')
end
self:addElement(element)
@@ -785,13 +790,13 @@ function Formspec:tabheader(x, y, w, h, name, listCaptions, currentTab, transpar
if w == nil and h == nil then
element = ('tabheader[%s,%s;%s;%s;%s;%s;%s]'):format(
x, y, name, table.concat(listCaptions, ','), currentTab or '', transparent or '', drawBorder or '')
x, y, name, tableConcat(listCaptions, ','), currentTab or '', transparent or '', drawBorder or '')
elseif w == nil then
element = ('tabheader[%s,%s;%s;%s;%s;%s;%s;%s]'):format(
x, y, name, h, table.concat(listCaptions, ','), currentTab or '', transparent or '', drawBorder or '')
x, y, name, h, tableConcat(listCaptions, ','), currentTab or '', transparent or '', drawBorder or '')
else
element = ('tabheader[%s,%s;%s,%s;%s;%s,%s;%s;%s;%s]'):format(
x, y, name, w, h, table.concat(listCaptions, ','), currentTab or '', transparent or '', drawBorder or '')
x, y, name, w, h, tableConcat(listCaptions, ','), currentTab or '', transparent or '', drawBorder or '')
end
self:addElement(element)
@@ -838,10 +843,10 @@ function Formspec:dropdown(x, y, w, h, name, listItems, selectedIdx, indexEvent)
if h == nil then
element = ('dropdown[%s,%s;%s;%s;%s;%s;%s]'):format(
x, y, w, name, table.concat(listItems, ','), selectedIdx or '', indexEvent or '')
x, y, w, name, tableConcat(listItems, ','), selectedIdx or '', indexEvent or '')
else
element = ('dropdown[%s,%s;%s,%s;%s;%s;%s;%s]'):format(
x, y, w, h, name, table.concat(listItems, ','), selectedIdx or '', indexEvent or '')
x, y, w, h, name, tableConcat(listItems, ','), selectedIdx or '', indexEvent or '')
end
self:addElement(element)
@@ -906,7 +911,7 @@ end
---@param options Formspec.scrollbarOptionsDef
function Formspec:scrollbarOptions(options)
local element = ('scrollbaroptions[%s]'):format(
table.key_value_list(options, ';'))
tableKeyValueList(options, ';'))
self:addElement(element)
end
@@ -926,7 +931,7 @@ end
---@param selectedIdx number? Index of row to be selected within table (first row = `1`)
function Formspec:table(x, y, w, h, name, listCells, selectedIdx)
local element = ('table[%s,%s;%s,%s;%s;%s;%s]'):format(
x, y, w, h, name, table.concat(listCells, ','), selectedIdx or '')
x, y, w, h, name, tableConcat(listCells, ','), selectedIdx or '')
self:addElement(element)
end
@@ -952,7 +957,7 @@ end
---@param options Formspec.tableOptionsDef
function Formspec:tableOptions(options)
local element = ('tableoptions[%s]'):format(
table.key_value_list(options, ';'))
tableKeyValueList(options, ';'))
self:addElement(element)
end
@@ -992,25 +997,67 @@ function Formspec:tablecolumns(columns)
local columnsStrings = {}
for _, column in pairs(columns) do
table.insert(columnsStrings,
column.type..','..table.key_value_list(column.options,',')
tableInsert(columnsStrings,
column.type..','..tableKeyValueList(column.options,',')
)
end
local element = ('tablecolumns[%s]'):format(
table.key_value_list(columnsStrings, ';'))
tableKeyValueList(columnsStrings, ';'))
self:addElement(element)
end
---TODO
function Formspec:style()
---Set the style for the element(s) matching `selector` by name.
---
---`state` is a list of states separated by the `+` character.
--- * If a state is provided, the style will only take effect when the element is in that state.
--- * All provided states must be active for the style to apply.
---
---**Note**: this must be before the element is defined.
---
---See [Styling Formspecs](https://api.luanti.org/formspec/#styling-formspecs).
---
---**Element**: `style[<selector 1>,<selector 2>,...;<prop1>;<prop2>;...]`
---@param selectors string|string[] Can be one of:
--- * `<name>` - An element name. Includes `*`, which represents every element.
--- * `<name>:<state>` - An element name, a colon, and one or more states.
---@param properties string[] TODO: добавить тут алиас, который бы содержал доступные настройки
function Formspec:style(selectors, properties)
if type(selectors) == 'string' then
selectors = { selectors }
end
local element = ('style[%s;%s]'):format(
tableConcat(selectors, ','), tableConcat(properties, ','))
self:addElement(element)
end
---TODO
function Formspec:styleType()
---Set the style for the element(s) matching `selector` by type.
---
---`state` is a list of states separated by the `+` character.
--- * If a state is provided, the style will only take effect when the element is in that state.
--- * All provided states must be active for the style to apply.
---
---**Note**: this must be before the element is defined.
---
---See [Styling Formspecs](https://api.luanti.org/formspec/#styling-formspecs).
---
---**Element**: `style[<selector 1>,<selector 2>,...;<prop1>;<prop2>;...]`
---@param selectors string|string[] Can be one of:
--- * `<name>` - An element name. Includes `*`, which represents every element.
--- * `<name>:<state>` - An element name, a colon, and one or more states.
---@param properties string[] TODO: добавить тут алиас, который бы содержал доступные настройки
function Formspec:styleType(selectors, properties)
if type(selectors) == 'string' then
selectors = { selectors }
end
local element = ('style[%s;%s]'):format(
tableConcat(selectors, ','), tableConcat(properties, ','))
self:addElement(element)
end
---Sets the focus to the element with the same name parameter.