Messages are now displayed in a single HUD element
This commit is contained in:
parent
d557b5d4e4
commit
4d6193d6cc
78
init.lua
78
init.lua
@ -1,7 +1,8 @@
|
|||||||
cmsg = {}
|
cmsg = {}
|
||||||
cmsg.hudids = {}
|
cmsg.hudids = {}
|
||||||
cmsg.active_messages = {}
|
cmsg.messages = {}
|
||||||
cmsg.settings = {}
|
cmsg.settings = {}
|
||||||
|
cmsg.next_msgids = {}
|
||||||
|
|
||||||
cmsg.settings.max_messages = 7
|
cmsg.settings.max_messages = 7
|
||||||
local setting = minetest.setting_get("central_message_max")
|
local setting = minetest.setting_get("central_message_max")
|
||||||
@ -13,6 +14,27 @@ end
|
|||||||
|
|
||||||
cmsg.default_color = 0xFFFFFF
|
cmsg.default_color = 0xFFFFFF
|
||||||
|
|
||||||
|
local function update_display(player, pname)
|
||||||
|
local messages = {}
|
||||||
|
local start, stop
|
||||||
|
stop = #cmsg.messages[pname]
|
||||||
|
if cmsg.settings.max_messages ~= nil then
|
||||||
|
local max = math.min(cmsg.settings.max_messages, #cmsg.messages[pname])
|
||||||
|
if #cmsg.messages[pname] > cmsg.settings.max_messages then
|
||||||
|
start = stop - max
|
||||||
|
else
|
||||||
|
start = 1
|
||||||
|
end
|
||||||
|
else
|
||||||
|
start = 1
|
||||||
|
end
|
||||||
|
for i=start, stop do
|
||||||
|
table.insert(messages, cmsg.messages[pname][i].text)
|
||||||
|
end
|
||||||
|
local concat = table.concat(messages, "\n")
|
||||||
|
player:hud_change(cmsg.hudids[pname], "text", concat)
|
||||||
|
end
|
||||||
|
|
||||||
cmsg.push_message_player = function(player, text, color)
|
cmsg.push_message_player = function(player, text, color)
|
||||||
local function push(tbl)
|
local function push(tbl)
|
||||||
-- Horrible Workaround code starts here
|
-- Horrible Workaround code starts here
|
||||||
@ -29,41 +51,35 @@ cmsg.push_message_player = function(player, text, color)
|
|||||||
local pname = player:get_player_name()
|
local pname = player:get_player_name()
|
||||||
if color == nil then color = cmsg.default_color end
|
if color == nil then color = cmsg.default_color end
|
||||||
if cmsg.hudids[pname] == nil then
|
if cmsg.hudids[pname] == nil then
|
||||||
cmsg.hudids[pname] = {}
|
cmsg.hudids[pname] = player:hud_add({
|
||||||
cmsg.active_messages[pname] = 0
|
hud_elem_type = "text",
|
||||||
|
text = text,
|
||||||
|
number = color,
|
||||||
|
position = {x=0.5, y=0.5},
|
||||||
|
offset = {x=0,y=-128},
|
||||||
|
direction = 0,
|
||||||
|
alignment = {x=0,y=-1},
|
||||||
|
scale = {x=300,y=18},
|
||||||
|
})
|
||||||
|
cmsg.messages[pname] = {}
|
||||||
|
cmsg.next_msgids[pname] = 0
|
||||||
|
table.insert(cmsg.messages[pname], {text=text, msgid=cmsg.next_msgids[pname]})
|
||||||
else
|
else
|
||||||
-- move older HUD IDs up
|
cmsg.next_msgids[pname] = cmsg.next_msgids[pname] + 1
|
||||||
for hudid,tbl in pairs(cmsg.hudids[pname]) do
|
table.insert(cmsg.messages[pname], {text=text, msgid=cmsg.next_msgids[pname]})
|
||||||
tbl.stackpos = tbl.stackpos + 1
|
update_display(player, pname)
|
||||||
if cmsg.settings.max_messages and tbl.stackpos >= cmsg.settings.max_messages then
|
|
||||||
player:hud_remove(hudid)
|
|
||||||
cmsg.hudids[pname][hudid] = nil
|
|
||||||
cmsg.active_messages[pname] = cmsg.active_messages[pname] - 1
|
|
||||||
else
|
|
||||||
player:hud_change(hudid, "offset", {x=0,y=-128-(18*tbl.stackpos)})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
local hudid = player:hud_add({
|
|
||||||
hud_elem_type = "text",
|
|
||||||
text = text,
|
|
||||||
number = color,
|
|
||||||
position = {x=0.5, y=0.5},
|
|
||||||
offset = {x=0,y=-128},
|
|
||||||
direction = 0,
|
|
||||||
alignment = {x=0,y=0},
|
|
||||||
scale = {x=300,y=18},
|
|
||||||
})
|
|
||||||
|
|
||||||
cmsg.hudids[pname][hudid] = {stackpos=0}
|
|
||||||
cmsg.active_messages[pname] = cmsg.active_messages[pname] + 1
|
|
||||||
|
|
||||||
minetest.after(5, function(param)
|
minetest.after(5, function(param)
|
||||||
local pname = param.player:get_player_name()
|
local pname = param.player:get_player_name()
|
||||||
param.player:hud_remove(param.hudid)
|
for i=1, #cmsg.messages[pname] do
|
||||||
cmsg.hudids[pname][param.hudid] = nil
|
if param.msgid == cmsg.messages[pname][i].msgid then
|
||||||
cmsg.active_messages[pname] = cmsg.active_messages[pname] - 1
|
table.remove(cmsg.messages[pname], i)
|
||||||
end, {player=player, hudid = hudid})
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
update_display(player, pname)
|
||||||
|
end, {player=player, msgid=cmsg.next_msgids[pname]})
|
||||||
|
|
||||||
-- Update timer for Horrible Workaround
|
-- Update timer for Horrible Workaround
|
||||||
cmsg.last_push = cmsg.steps
|
cmsg.last_push = cmsg.steps
|
||||||
|
Loading…
Reference in New Issue
Block a user