BaseUI: add recive fields methods

This commit is contained in:
Van
2026-02-17 21:35:40 +03:00
parent 7b43def5fb
commit bafe26983c

View File

@@ -1,11 +1,28 @@
local getInstance = Class.getInstance
---@class BaseUI
---Context for transferring data between displaying
---the form spec to the player and handling callbacks.
---@field name string
---@field contex table<string, table>
local BaseUI = {
contex = {}
}
---@param name string
function BaseUI:new(name)
---@type BaseUI
local instance = getInstance(self, {})
instance.name = name
-- Ensure that when the form is closed, the context is cleared.
instance:registerOnFormQuit(function(player)
instance:clearContex(player)
end)
return instance
end
---Set a value for a specific key.
---Recommended for use where `core.show_formspec` is used.
---
@@ -14,7 +31,7 @@ local BaseUI = {
---@param key string
---@param value any
function BaseUI:setContex(player, key, value)
if type(player) == 'table' then
if type(player) ~= 'string' then
player = player:get_player_name()
end
@@ -33,7 +50,7 @@ end
---@param key string
---@return any?
function BaseUI:getContex(player, key)
if type(player) == 'table' then
if type(player) ~= 'string' then
player = player:get_player_name()
end
@@ -45,11 +62,45 @@ end
---Recommended for use when closing formspec.
---@param player string|Player Player as an object (entity) or his name
function BaseUI:clearContex(player)
if type(player) == 'table' then
if type(player) ~= 'string' then
player = player:get_player_name()
end
self.contex[player] = {}
end
---Register a callback for a specific player action in FormSpec.
---
---The callback is guaranteed to be called only for the specified field.
---@param fieldName string the name of the field for which the callback will be called.
---@param callback fun(player:Player, fieldName:string, fieldValue:any) Callback to handle the action. Parameters:
--- * `player` - the player who performed the action.
--- * `fieldName` - the name of the field on which the callback is called.
--- * `fieldValue` - the field value that is passed in the `fields` table.
function BaseUI:registerOnFormFieldAction(fieldName, callback)
Smc.registerOnFormFieldAction(self.name, fieldName, callback)
end
---Register a callback function to close the FormSpec.
---
---The callback is guaranteed to be called only for the specified form.
---@param callback fun(player:Player) Callback to handle the close. Parameters:
--- * `player` - the player who closed the form.
function BaseUI:registerOnFormQuit(callback)
Smc.registerOnFormQuit(self.name, callback)
end
---Register a callback for a specific player action in FormSpec.
---
---The callback function is guaranteed to be called only for the specified form and
---for the field whose name matches the pattern.
---@param fieldNamePattern string the name pattern of the field for which the callback will be called.
---@param callback fun(player:Player, fieldName:string, fieldValue:any) Callback to handle the action. Parameters:
--- * `player` - the player who performed the action.
--- * `fieldName` - the name of the field on which the callback is called (will match the pattern).
--- * `fieldValue` - the field value that is passed in the `fields` table.
function BaseUI:registerOnFormFieldActionMatch( fieldNamePattern, callback)
Smc.registerOnFormFieldActionMatch(self.name, fieldNamePattern, callback)
end
return BaseUI