Add option to change placement of hud compass.

This commit is contained in:
David G 2019-12-24 14:24:10 -07:00
parent 80586adbc1
commit e580aa82ea
2 changed files with 64 additions and 28 deletions

@ -1,7 +1,7 @@
HUD Compass [hud_compass] HUD Compass [hud_compass]
------------------------- -------------------------
A Minetest mod to optionally place a HUD compass in the bottom right corner of the screen. A Minetest mod to optionally place a HUD compass on the screen.
By David G (kestral246) By David G (kestral246)
@ -12,11 +12,22 @@ How to enable
This mod defaults to not displaying compass. To enable, use the chat command: This mod defaults to not displaying compass. To enable, use the chat command:
> "/compass" "/compass" -> By default this places a compass in the bottom right corner of the screen.
Repeated use of this command will toggle the compass display off and on. Repeated use of this command will toggle the compass display off and on.
Local mod storage is used to maintain state of hud_compass display between sessions, per user. **New:** When given with an argument, the position of the compass can be changed. This is particularly useful with Android clients, where the bottom right corner of the screen has the jump button.
"/compass 1" -> top right corner
"/compass 2" -> bottom right corner
"/compass 3" -> bottom left corner
"/compass 4" -> top left corner
In addition:
"/compass 0" -> forces compass off.
Local mod storage is used to maintain the state and position of hud_compass display between sessions, per user.
Licenses Licenses

@ -2,45 +2,70 @@
-- Optionally place a compass on the screen. -- Optionally place a compass on the screen.
-- A HUD version of my realcompass mod. -- A HUD version of my realcompass mod.
-- By David_G (kestral246@gmail.com) -- By David_G (kestral246@gmail.com)
-- 2019-10-28 -- 2019-12-24
local hud_compass = {} local hud_compass = {}
local storage = minetest.get_mod_storage() local storage = minetest.get_mod_storage()
-- State of hud_compass
-- 1 == NE, 2 == SE, 3 == SW, 4 == NW
-- positive == enabled, negative == disabled
local default_corner = -2 -- SE corner, off by default
local lookup = {
{hud_elem_type="image", text="", position={x=1,y=0}, scale={x=4,y=4}, alignment={x=-1,y=1}, offset={x=-8,y=4}},
{hud_elem_type="image", text="", position={x=1,y=1}, scale={x=4,y=4}, alignment={x=-1,y=-1}, offset={x=-8,y=-4}},
{hud_elem_type="image", text="", position={x=0,y=1}, scale={x=4,y=4}, alignment={x=1,y=-1}, offset={x=8,y=-4}},
{hud_elem_type="image", text="", position={x=0,y=0}, scale={x=4,y=4}, alignment={x=1,y=1}, offset={x=8,y=4}}
}
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local pname = player:get_player_name() local pname = player:get_player_name()
local is_enabled = false local corner = default_corner
if storage:get(pname) and storage:get(pname) == "1" then if storage:get(pname) and tonumber(storage:get(pname)) then -- validate mod storage value
is_enabled = true local temp = math.floor(tonumber(storage:get(pname)))
if temp ~= nil and temp ~= 0 and temp >= -4 and temp <= 4 then
corner = temp
end
end end
hud_compass[pname] = { hud_compass[pname] = {
id = player:hud_add({ id = player:hud_add(lookup[math.abs(corner)]),
hud_elem_type = "image",
text = "",
position = {x=1.0, y=1.0},
scale = {x=4, y=4},
alignment = {x=-1, y=-1},
offset = {x=-8, y=-4}
}),
last_image = -1, last_image = -1,
enabled = is_enabled, state = corner,
} }
end) end)
minetest.register_chatcommand("compass", { minetest.register_chatcommand("compass", {
params = "", params = "[<corner>]",
description = "Toggle display of hud compass.", description = "Change display of hud compass.",
privs = {}, privs = {},
func = function(pname, param) func = function(pname, params)
local player = minetest.get_player_by_name(pname) local player = minetest.get_player_by_name(pname)
if hud_compass[pname].enabled == true then -- is enabled if params and string.len(params) > 0 then -- includes corner parameter
hud_compass[pname].enabled = false -- toggle to disabled local corner = tonumber(string.match(params, "^%d$"))
hud_compass[pname].last_image = -1 -- reset initial direction if corner and corner == 0 then -- disable compass
player:hud_change(hud_compass[pname].id, "text", "") -- blank hud player:hud_change(hud_compass[pname].id, "text", "") -- blank hud
storage:set_string(pname, "0") hud_compass[pname].last_image = -1
else -- is disabled hud_compass[pname].state = -1 * math.abs(hud_compass[pname].state)
hud_compass[pname].enabled = true -- toggle to enabled storage:set_string(pname, hud_compass[pname].state)
storage:set_string(pname, "1") elseif corner and corner > 0 and corner <= 4 then -- enable compass to given corner
player:hud_remove(hud_compass[pname].id) -- remove old hud
hud_compass[pname].id = player:hud_add(lookup[corner]) -- place new hud at requested corner
hud_compass[pname].last_image = -1
hud_compass[pname].state = corner
storage:set_string(pname, corner)
end
else -- just toggle hud
if hud_compass[pname].state > 0 then -- is enabled
hud_compass[pname].state = -1 * hud_compass[pname].state -- toggle to disabled
hud_compass[pname].last_image = -1 -- reset initial direction
player:hud_change(hud_compass[pname].id, "text", "") -- blank hud
storage:set_string(pname, hud_compass[pname].state)
else -- is disabled
hud_compass[pname].state = -1 * hud_compass[pname].state -- toggle to enabled
storage:set_string(pname, hud_compass[pname].state)
end
end end
end, end,
}) })
@ -60,7 +85,7 @@ minetest.register_globalstep(function(dtime)
local angle_relative = math.deg(dir) local angle_relative = math.deg(dir)
local image = math.floor((angle_relative/22.5) + 0.5)%16 local image = math.floor((angle_relative/22.5) + 0.5)%16
if hud_compass[pname].enabled and image ~= hud_compass[pname].last_image then if hud_compass[pname].state > 0 and image ~= hud_compass[pname].last_image then
local rc = player:hud_change(hud_compass[pname].id, "text", "realcompass_"..image..".png") local rc = player:hud_change(hud_compass[pname].id, "text", "realcompass_"..image..".png")
-- Check return code, seems to fix occasional startup glitch. -- Check return code, seems to fix occasional startup glitch.
if rc == 1 then if rc == 1 then