BaseUI: add recive fields methods
This commit is contained in:
@@ -1,11 +1,28 @@
|
|||||||
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
---@class BaseUI
|
---@class BaseUI
|
||||||
---Context for transferring data between displaying
|
---Context for transferring data between displaying
|
||||||
---the form spec to the player and handling callbacks.
|
---the form spec to the player and handling callbacks.
|
||||||
|
---@field name string
|
||||||
---@field contex table<string, table>
|
---@field contex table<string, table>
|
||||||
local BaseUI = {
|
local BaseUI = {
|
||||||
contex = {}
|
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.
|
---Set a value for a specific key.
|
||||||
---Recommended for use where `core.show_formspec` is used.
|
---Recommended for use where `core.show_formspec` is used.
|
||||||
---
|
---
|
||||||
@@ -14,7 +31,7 @@ local BaseUI = {
|
|||||||
---@param key string
|
---@param key string
|
||||||
---@param value any
|
---@param value any
|
||||||
function BaseUI:setContex(player, key, value)
|
function BaseUI:setContex(player, key, value)
|
||||||
if type(player) == 'table' then
|
if type(player) ~= 'string' then
|
||||||
player = player:get_player_name()
|
player = player:get_player_name()
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -33,7 +50,7 @@ end
|
|||||||
---@param key string
|
---@param key string
|
||||||
---@return any?
|
---@return any?
|
||||||
function BaseUI:getContex(player, key)
|
function BaseUI:getContex(player, key)
|
||||||
if type(player) == 'table' then
|
if type(player) ~= 'string' then
|
||||||
player = player:get_player_name()
|
player = player:get_player_name()
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -45,11 +62,45 @@ end
|
|||||||
---Recommended for use when closing formspec.
|
---Recommended for use when closing formspec.
|
||||||
---@param player string|Player Player as an object (entity) or his name
|
---@param player string|Player Player as an object (entity) or his name
|
||||||
function BaseUI:clearContex(player)
|
function BaseUI:clearContex(player)
|
||||||
if type(player) == 'table' then
|
if type(player) ~= 'string' then
|
||||||
player = player:get_player_name()
|
player = player:get_player_name()
|
||||||
end
|
end
|
||||||
|
|
||||||
self.contex[player] = {}
|
self.contex[player] = {}
|
||||||
end
|
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
|
return BaseUI
|
||||||
|
|||||||
Reference in New Issue
Block a user