136 lines
4.7 KiB
Lua
136 lines
4.7 KiB
Lua
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 getFormSpecString fun(self:BaseUI,playerName:string,...):string
|
|
---@field contex table<string, table>
|
|
local BaseUI = {
|
|
contex = {}
|
|
}
|
|
|
|
---@param name string The name of the formspec that will be shown to the player. It is recommended that the name be unique.
|
|
---@param getFormSpecString fun(self:BaseUI,playerName:string,...):string
|
|
---@param clearContex boolean? Whether to clear the context stored for a player when that player closes the form. Default `true`.
|
|
function BaseUI:new(name, getFormSpecString, clearContex)
|
|
if clearContex == nil then
|
|
clearContex = true
|
|
end
|
|
|
|
---@type BaseUI
|
|
local instance = getInstance(self, {})
|
|
instance.name = name
|
|
instance.getFormSpecString = getFormSpecString
|
|
|
|
if clearContex then
|
|
-- Ensure that when the form is closed, the context is cleared.
|
|
instance:registerOnFormQuit(function(player)
|
|
instance:clearContex(player)
|
|
end)
|
|
end
|
|
|
|
instance:registerOnFormQuit(function(player)
|
|
Smc.clearActiveFormspec(player:get_player_name())
|
|
end)
|
|
|
|
return instance
|
|
end
|
|
|
|
---Set a value for a specific key.
|
|
---Recommended for use where `core.show_formspec` is used.
|
|
---
|
|
---In the future, the key value can be obtained in the formspec callback.
|
|
---@param player string|Player Player as an object (entity) or his name
|
|
---@param key string
|
|
---@param value any
|
|
function BaseUI:setContex(player, key, value)
|
|
if type(player) ~= 'string' then
|
|
player = player:get_player_name()
|
|
end
|
|
|
|
local contex = self.contex[player]
|
|
|
|
if contex == nil then
|
|
self.contex[player] = {[key]=value}
|
|
else
|
|
contex[key] = value
|
|
end
|
|
end
|
|
|
|
---Get the value of a specific key.
|
|
---Recommended for use in a formspec callback.
|
|
---@param player string|Player Player as an object (entity) or his name
|
|
---@param key string
|
|
---@return any?
|
|
function BaseUI:getContex(player, key)
|
|
if type(player) ~= 'string' then
|
|
player = player:get_player_name()
|
|
end
|
|
|
|
local contex = self.contex[player] or {}
|
|
return contex[key]
|
|
end
|
|
|
|
---Clear saved context data for a player.
|
|
---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) ~= '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
|
|
|
|
---Shows the player the FormSpec registered by the instance of this UI.
|
|
---@param playerName string
|
|
function BaseUI:showForm(playerName, ...)
|
|
core.show_formspec(playerName, self.name, self:getFormSpecString(playerName, ...))
|
|
Smc.setActiveFormspec(playerName, self.name)
|
|
end
|
|
|
|
---Close the player the FormSpec registered by the instance of this UI.
|
|
---@param playerName string
|
|
function BaseUI:closeForm(playerName)
|
|
core.close_formspec(playerName, self.name)
|
|
Smc.clearActiveFormspec(playerName)
|
|
end
|
|
|
|
|
|
return BaseUI
|