BaseUI: create base UI class

This commit is contained in:
Van
2026-02-12 21:08:43 +03:00
parent 1d43ae1255
commit 25f38d1acd
3 changed files with 61 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
local modInfo = Mod.getInfo('smc__core__base_ui')
local require = modInfo.require
return require('BaseUI')

View File

@@ -0,0 +1,2 @@
name = smc__core__base_ui
depends = smc__system__system_loader

View File

@@ -0,0 +1,55 @@
---@class BaseUI
---Context for transferring data between displaying
---the form spec to the player and handling callbacks.
---@field contex table<string, table>
local BaseUI = {
contex = {}
}
---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) == 'table' 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) == 'table' 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) == 'table' then
player = player:get_player_name()
end
self.contex[player] = {}
end
return BaseUI