Messages are now displayed in a single HUD element

This commit is contained in:
Wuzzy 2015-07-03 21:54:59 +02:00
parent d557b5d4e4
commit 4d6193d6cc

View File

@ -1,7 +1,8 @@
cmsg = {}
cmsg.hudids = {}
cmsg.active_messages = {}
cmsg.messages = {}
cmsg.settings = {}
cmsg.next_msgids = {}
cmsg.settings.max_messages = 7
local setting = minetest.setting_get("central_message_max")
@ -13,6 +14,27 @@ end
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)
local function push(tbl)
-- Horrible Workaround code starts here
@ -29,41 +51,35 @@ cmsg.push_message_player = function(player, text, color)
local pname = player:get_player_name()
if color == nil then color = cmsg.default_color end
if cmsg.hudids[pname] == nil then
cmsg.hudids[pname] = {}
cmsg.active_messages[pname] = 0
cmsg.hudids[pname] = 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=-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
-- move older HUD IDs up
for hudid,tbl in pairs(cmsg.hudids[pname]) do
tbl.stackpos = tbl.stackpos + 1
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
cmsg.next_msgids[pname] = cmsg.next_msgids[pname] + 1
table.insert(cmsg.messages[pname], {text=text, msgid=cmsg.next_msgids[pname]})
update_display(player, pname)
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)
local pname = param.player:get_player_name()
param.player:hud_remove(param.hudid)
cmsg.hudids[pname][param.hudid] = nil
cmsg.active_messages[pname] = cmsg.active_messages[pname] - 1
end, {player=player, hudid = hudid})
for i=1, #cmsg.messages[pname] do
if param.msgid == cmsg.messages[pname][i].msgid then
table.remove(cmsg.messages[pname], i)
break
end
end
update_display(player, pname)
end, {player=player, msgid=cmsg.next_msgids[pname]})
-- Update timer for Horrible Workaround
cmsg.last_push = cmsg.steps