BaseUI: add showForm & closeForm methods

This commit is contained in:
Van
2026-02-18 22:18:10 +03:00
parent b64c8e9350
commit c70bf96734

View File

@@ -3,17 +3,20 @@ 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 name string
---@field contex table<string, table> ---@field getFormSpecString fun(self:BaseUI,playerName:string,...):string
---@field contex table<string, table>
local BaseUI = { local BaseUI = {
contex = {} contex = {}
} }
---@param name string ---@param name string
function BaseUI:new(name) ---@param getFormSpecString fun(self:BaseUI,playerName:string,...):string
function BaseUI:new(name, getFormSpecString)
---@type BaseUI ---@type BaseUI
local instance = getInstance(self, {}) local instance = getInstance(self, {})
instance.name = name instance.name = name
instance.getFormSpecString = getFormSpecString
-- Ensure that when the form is closed, the context is cleared. -- Ensure that when the form is closed, the context is cleared.
instance:registerOnFormQuit(function(player) instance:registerOnFormQuit(function(player)
@@ -103,4 +106,17 @@ function BaseUI:registerOnFormFieldActionMatch( fieldNamePattern, callback)
Smc.registerOnFormFieldActionMatch(self.name, fieldNamePattern, callback) Smc.registerOnFormFieldActionMatch(self.name, fieldNamePattern, callback)
end 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, ...))
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)
end
return BaseUI return BaseUI