From 25f38d1acdd1064f5a2c21ed797c829fbdecfb7c Mon Sep 17 00:00:00 2001 From: Vanic Date: Thu, 12 Feb 2026 21:08:43 +0300 Subject: [PATCH] `BaseUI`: create base UI class --- mods/CORE/BaseUI/init.lua | 4 +++ mods/CORE/BaseUI/mod.conf | 2 ++ mods/CORE/BaseUI/src/BaseUI.lua | 55 +++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 mods/CORE/BaseUI/init.lua create mode 100644 mods/CORE/BaseUI/mod.conf create mode 100644 mods/CORE/BaseUI/src/BaseUI.lua diff --git a/mods/CORE/BaseUI/init.lua b/mods/CORE/BaseUI/init.lua new file mode 100644 index 0000000..9b276ca --- /dev/null +++ b/mods/CORE/BaseUI/init.lua @@ -0,0 +1,4 @@ +local modInfo = Mod.getInfo('smc__core__base_ui') +local require = modInfo.require + +return require('BaseUI') diff --git a/mods/CORE/BaseUI/mod.conf b/mods/CORE/BaseUI/mod.conf new file mode 100644 index 0000000..3b698e4 --- /dev/null +++ b/mods/CORE/BaseUI/mod.conf @@ -0,0 +1,2 @@ +name = smc__core__base_ui +depends = smc__system__system_loader diff --git a/mods/CORE/BaseUI/src/BaseUI.lua b/mods/CORE/BaseUI/src/BaseUI.lua new file mode 100644 index 0000000..d65d6cd --- /dev/null +++ b/mods/CORE/BaseUI/src/BaseUI.lua @@ -0,0 +1,55 @@ +---@class BaseUI +---Context for transferring data between displaying +---the form spec to the player and handling callbacks. +---@field contex 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