Merge pull request 'mcl_title API: enable styling' (#2730) from title-API-fixes into master

Reviewed-on: https://git.minetest.land/MineClone2/MineClone2/pulls/2730
Reviewed-by: cora <cora@noreply.git.minetest.land>
This commit is contained in:
cora 2022-10-09 19:35:48 +00:00
commit 3e9cb597e6

View File

@ -11,8 +11,10 @@
--Note that the table storing timeouts use playername as index insteed of player objects (faster) --Note that the table storing timeouts use playername as index insteed of player objects (faster)
--This is intended in order to speedup the process of removing HUD elements the the timeout is up --This is intended in order to speedup the process of removing HUD elements the the timeout is up
---@type table<string, table<ObjectRef, any>>
local huds_idx = {} local huds_idx = {}
---@type table<string, table<string, number>>
local hud_hide_timeouts = {} local hud_hide_timeouts = {}
hud_hide_timeouts.title = {} hud_hide_timeouts.title = {}
@ -35,6 +37,8 @@ local get_color = mcl_util.get_color
--local string = string --local string = string
local pairs = pairs local pairs = pairs
---@param gametick integer
---@return number?
local function gametick_to_secondes(gametick) local function gametick_to_secondes(gametick)
if gametick then if gametick then
return gametick / 20 return gametick / 20
@ -44,7 +48,10 @@ local function gametick_to_secondes(gametick)
end end
--https://github.com/minetest/minetest/blob/b3b075ea02034306256b486dd45410aa765f035a/doc/lua_api.txt#L8477 --https://github.com/minetest/minetest/blob/b3b075ea02034306256b486dd45410aa765f035a/doc/lua_api.txt#L8477
--[[
---@param bold? boolean
---@param italic? boolean
---@return integer
local function style_to_bits(bold, italic) local function style_to_bits(bold, italic)
if bold then if bold then
if italic then if italic then
@ -60,9 +67,11 @@ local function style_to_bits(bold, italic)
end end
end end
end end
]]
--PARAMS SYSTEM local no_style = style_to_bits(false, false)
---PARAMS SYSTEM
---@type table<ObjectRef, {stay: integer}>
local player_params = {} local player_params = {}
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
@ -78,7 +87,7 @@ minetest.register_on_joinplayer(function(player)
position = mcl_title.layout.title.position, position = mcl_title.layout.title.position,
alignment = mcl_title.layout.title.alignment, alignment = mcl_title.layout.title.alignment,
text = "", text = "",
--style = 0, style = no_style,
size = { x = mcl_title.layout.title.size }, size = { x = mcl_title.layout.title.size },
number = hex_color, number = hex_color,
z_index = 100, z_index = 100,
@ -88,7 +97,7 @@ minetest.register_on_joinplayer(function(player)
position = mcl_title.layout.subtitle.position, position = mcl_title.layout.subtitle.position,
alignment = mcl_title.layout.subtitle.alignment, alignment = mcl_title.layout.subtitle.alignment,
text = "", text = "",
--style = 0, style = no_style,
size = { x = mcl_title.layout.subtitle.size }, size = { x = mcl_title.layout.subtitle.size },
number = hex_color, number = hex_color,
z_index = 100, z_index = 100,
@ -98,7 +107,7 @@ minetest.register_on_joinplayer(function(player)
position = mcl_title.layout.actionbar.position, position = mcl_title.layout.actionbar.position,
offset = { x = 0, y = -210 }, offset = { x = 0, y = -210 },
alignment = mcl_title.layout.actionbar.alignment, alignment = mcl_title.layout.actionbar.alignment,
--style = 0, style = no_style,
text = "", text = "",
size = { x = mcl_title.layout.actionbar.size }, size = { x = mcl_title.layout.actionbar.size },
number = hex_color, number = hex_color,
@ -123,6 +132,8 @@ minetest.register_on_leaveplayer(function(player)
hud_hide_timeouts.actionbar[playername] = nil hud_hide_timeouts.actionbar[playername] = nil
end) end)
---@param player ObjectRef
---@param data {stay: integer}
function mcl_title.params_set(player, data) function mcl_title.params_set(player, data)
player_params[player] = { player_params[player] = {
stay = data.stay or mcl_title.defaults.stay, stay = data.stay or mcl_title.defaults.stay,
@ -131,12 +142,18 @@ function mcl_title.params_set(player, data)
} }
end end
---@param player ObjectRef
---@return {stay: integer}
function mcl_title.params_get(player) function mcl_title.params_get(player)
return player_params[player] return player_params[player]
end end
--API FUNCTIONS --API FUNCTIONS
---@param player ObjectRef
---@param type '"title"'|'"subtitle"'|'"actionbar"'
---@param data {text: string, color: string, stay: integer, bold: boolean, italic: boolean}
---@return boolean
function mcl_title.set(player, type, data) function mcl_title.set(player, type, data)
if not data.color then if not data.color then
data.color = "white" data.color = "white"
@ -149,20 +166,25 @@ function mcl_title.set(player, type, data)
player:hud_change(huds_idx[type][player], "text", data.text) player:hud_change(huds_idx[type][player], "text", data.text)
player:hud_change(huds_idx[type][player], "number", hex_color) player:hud_change(huds_idx[type][player], "number", hex_color)
--apply bold and italic -- Apply bold and italic
--player:hud_change(huds_idx[type][player], "style", style_to_bits(data.bold, data.italic)) player:hud_change(huds_idx[type][player], "style", style_to_bits(data.bold, data.italic))
hud_hide_timeouts[type][player:get_player_name()] = gametick_to_secondes(data.stay) or
gametick_to_secondes(mcl_title.params_get(player).stay)
hud_hide_timeouts[type][player:get_player_name()] = gametick_to_secondes(data.stay) or gametick_to_secondes(mcl_title.params_get(player).stay)
return true return true
end end
---@param player ObjectRef?
---@param type '"title"'|'"subtitle"'|'"actionbar"'
function mcl_title.remove(player, type) function mcl_title.remove(player, type)
if player then if player then
player:hud_change(huds_idx[type][player], "text", "") player:hud_change(huds_idx[type][player], "text", "")
--player:hud_change(huds_idx[type][player], "style", 0) --no styling player:hud_change(huds_idx[type][player], "style", no_style)
end end
end end
---@param player ObjectRef
function mcl_title.clear(player) function mcl_title.clear(player)
mcl_title.remove(player, "title") mcl_title.remove(player, "title")
mcl_title.remove(player, "subtitle") mcl_title.remove(player, "subtitle")
@ -179,6 +201,7 @@ minetest.register_globalstep(function(dtime)
subtitle = {}, subtitle = {},
actionbar = {}, actionbar = {},
} }
for element, content in pairs(hud_hide_timeouts) do for element, content in pairs(hud_hide_timeouts) do
for name, timeout in pairs(content) do for name, timeout in pairs(content) do
timeout = timeout - dtime timeout = timeout - dtime
@ -190,47 +213,95 @@ minetest.register_globalstep(function(dtime)
end end
end end
end end
hud_hide_timeouts = new_timeouts hud_hide_timeouts = new_timeouts
end) end)
--DEBUG STUFF!! --DEBUG STUFF!!
--[[ --TODO:Proper /title command that can send the title to other players.
--These commands are just for debugging right now.
local dbg_msg = "Note that these are just debug commands right now. e.g. the title is only sent to he player issuing the command. Proper /title commands will be added in the future."
minetest.register_chatcommand("title", { minetest.register_chatcommand("title", {
privs = { debug = true },
func = function(name, param) func = function(name, param)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if player then
mcl_title.set(player, "title", { text = param, color = "gold", bold = true, italic = true }) mcl_title.set(player, "title", { text = param, color = "gold", bold = true, italic = true })
return true, dbg_msg
else
return false, dbg_msg
end
end, end,
}) })
minetest.register_chatcommand("subtitle", { minetest.register_chatcommand("subtitle", {
privs = { debug = true },
func = function(name, param) func = function(name, param)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if player then
mcl_title.set(player, "subtitle", { text = param, color = "gold" }) mcl_title.set(player, "subtitle", { text = param, color = "gold" })
return true, dbg_msg
else
return false, dbg_msg
end
end, end,
}) })
minetest.register_chatcommand("actionbar", { minetest.register_chatcommand("actionbar", {
privs = { debug = true },
func = function(name, param) func = function(name, param)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
mcl_title.set(player, "actionbar", {text=param, color="gold"}) if player then
mcl_title.set(player, "actionbar", { text = param, color = "gold", bold = true, italic = true })
return true, dbg_msg
else
return false, dbg_msg
end
end, end,
}) })
minetest.register_chatcommand("timeout", { minetest.register_chatcommand("title_timeout", {
privs = { debug = true },
func = function(name, param) func = function(name, param)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if player then
mcl_title.params_set(player, { stay = 600 }) mcl_title.params_set(player, { stay = 600 })
return true, dbg_msg
else
return false, dbg_msg
end
end, end,
}) })
minetest.register_chatcommand("all", { minetest.register_chatcommand("title_all", {
privs = { debug = true },
func = function(name, param) func = function(name, param)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if player then
mcl_title.params_set(player, { stay = 600 }) mcl_title.params_set(player, { stay = 600 })
mcl_title.set(player, "title", { text = param, color = "gold" }) mcl_title.set(player, "title", { text = param, color = "gold" })
mcl_title.set(player, "subtitle", { text = param, color = "gold" }) mcl_title.set(player, "subtitle", { text = param, color = "gold" })
mcl_title.set(player, "actionbar", { text = param, color = "gold" }) mcl_title.set(player, "actionbar", { text = param, color = "gold" })
return true, dbg_msg
else
return false, dbg_msg
end
end,
})
minetest.register_chatcommand("title_all_styles", {
privs = { debug = true },
func = function(name, param)
local player = minetest.get_player_by_name(name)
if player then
mcl_title.params_set(player, { stay = 600 })
mcl_title.set(player, "title", { text = param, color = "gold" })
mcl_title.set(player, "subtitle", { text = param, color = "gold", bold = true })
mcl_title.set(player, "actionbar", { text = param, color = "gold", italic = true })
return true, dbg_msg
else
return false, dbg_msg
end
end, end,
}) })
]]