mirror of
https://gitlab.com/4w/mtimer.git
synced 2024-11-28 10:13:50 +01:00
93771fbbaf
Formspec, menu entry, and logic are done. Logic for setting the timer offsets on receiving formspec fields needs to be done.
120 lines
4.1 KiB
Lua
120 lines
4.1 KiB
Lua
local m = mtimer
|
||
local deserialize = minetest.deserialize
|
||
|
||
|
||
-- Calculate HUD positions and offsets
|
||
--
|
||
-- Based on the given named position a table of positional tables is returned
|
||
-- by this helper function. When an invalid named position is provided all
|
||
-- tables only contain 0 values. Valid positions shown in the diagram below.
|
||
--
|
||
-- +--------------------------------+
|
||
-- | tl tc tr |
|
||
-- | |
|
||
-- | |
|
||
-- | ml mc mr |
|
||
-- | |
|
||
-- | |
|
||
-- | bl bc br |
|
||
-- +--------------------------------+
|
||
--
|
||
-- For orientation: `mc` is the center of the screen (where the crosshair is).
|
||
-- `bc` is the location of the hotbar and health bars, etc.
|
||
-- Both are valid positions but should not be used.
|
||
--
|
||
-- Provided offsets will be added or substracted according to the position. In
|
||
-- result the pffset behaves like a border
|
||
--
|
||
-- @param pos A positional string as described
|
||
-- @param poffset An offset table like { x=1, y=1 }
|
||
-- @return table a Table containing the positional tables based on the string
|
||
local get_hud_positions = function (pos, offset)
|
||
local p = { x = 0, y = 0 }
|
||
local a = { x = 0, y = 0 }
|
||
local o = { x = 0, y = 0 }
|
||
|
||
if pos == 'tl' then
|
||
p = { x = 0, y = 0 }
|
||
a = { x = 1, y = 1 }
|
||
o = { x = 5 + offset.x, y = 3 + offset.y }
|
||
end
|
||
if pos == 'tc' then
|
||
p = { x = 0.5, y = 0 }
|
||
a = { x = 0, y = 1 }
|
||
o = { x = 0 - offset.x, y = 3 + offset.y }
|
||
end
|
||
if pos == 'tr' then
|
||
p = { x = 1, y = 0 }
|
||
a = { x=-1, y = 1 }
|
||
o = { x = -6 - offset.x, y = 3 + offset.y }
|
||
end
|
||
if pos == 'ml' then
|
||
p = { x = 0, y = 0.5 }
|
||
a = { x = 1, y = 0 }
|
||
o = { x = 5 + offset.x, y = 0 + offset.y }
|
||
end
|
||
if pos == 'mc' then
|
||
p = { x = 0.5,y = 0.5 }
|
||
a = { x = 0, y = 0 }
|
||
o = { x = 0 + offset.x, y = 0 + offset.y }
|
||
end
|
||
if pos == 'mr' then
|
||
p = { x = 1, y = 0.5 }
|
||
a = { x = -1,y = 0 }
|
||
o = { x = -6 - offset.x, y = 0 + offset.y }
|
||
end
|
||
if pos == 'bl' then
|
||
p = { x = 0, y = 1 }
|
||
a = { x = 1, y = -1 }
|
||
o = { x = 5 + offset.x, y = 0 - offset.y }
|
||
end
|
||
if pos == 'bc' then
|
||
p = { x = 0.5, y = 1 }
|
||
a = { x = 0, y = -1 }
|
||
o = { x = 0 + offset.x, y = 0 - offset.y }
|
||
end
|
||
if pos == 'br' then
|
||
p = { x = 1, y = 1 }
|
||
a = { x = -1, y = -1 }
|
||
o = { x = -6 - offset.x, y = 0 - offset.y }
|
||
end
|
||
|
||
return { position = p, alignment = a, offset = o }
|
||
end
|
||
|
||
|
||
-- Update the timer
|
||
--
|
||
-- This function updates the timer for the given player referenced by the
|
||
-- player’s name. The function is called when a formspec update (fields) is
|
||
-- sent to the server and is automatically called by the registered globalstep.
|
||
--
|
||
-- The function sets the needed values based on the player meta data and uses
|
||
-- the `mtimer.get_timer_data` function for the actual data to be shown.
|
||
--
|
||
-- @param player_name Name of the player to update the timer for
|
||
-- @return void
|
||
mtimer.update_timer = function (player_name)
|
||
local player = minetest.get_player_by_name(player_name)
|
||
local meta = player:get_meta()
|
||
local m = m.meta
|
||
local hud_id = meta:get_string('mtimer:hud_id')
|
||
|
||
local text = mtimer.get_timer_data(player_name).formatted
|
||
local number = meta:get_string(m.color.key):gsub('#', '0x')
|
||
local size = meta:get_string(m.hud_element_size.key)
|
||
|
||
local position = meta:get_string(m.position.key)
|
||
local offset = deserialize(meta:get_string(m.hud_element_offset.key))
|
||
local orientation = get_hud_positions(position, offset)
|
||
|
||
if meta:get_string(m.visible.key) == 'false' then text = '' end
|
||
|
||
player:hud_change(hud_id, 'text', text)
|
||
player:hud_change(hud_id, 'number', number)
|
||
player:hud_change(hud_id, 'position', orientation.position)
|
||
player:hud_change(hud_id, 'alignment', orientation.alignment)
|
||
player:hud_change(hud_id, 'size', {x=size, y=size})
|
||
player:hud_change(hud_id, 'offset', orientation.offset)
|
||
end
|