Display both compass and 24-hour clock.

master
David G 2019-12-29 20:48:00 -07:00
parent e580aa82ea
commit 602acc0dfb
28 changed files with 47 additions and 24 deletions

View File

@ -1,7 +1,7 @@
HUD Compass [hud_compass]
-------------------------
A Minetest mod to optionally place a HUD compass on the screen.
A Minetest mod to optionally place a HUD compass and 24-hour clock on the screen.
By David G (kestral246)
@ -10,13 +10,13 @@ By David G (kestral246)
How to enable
-------------
This mod defaults to not displaying compass. To enable, use the chat command:
This mod defaults to not displaying compass and clock. To enable, use the chat command:
"/compass" -> By default this places a compass in the bottom right corner of the screen.
"/compass" -> By default this places a compass and clock 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 display of the compass and clock off and on.
**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.
**New:** When given with an argument, the position of the compass and clock 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

View File

@ -1,8 +1,8 @@
-- hud_compass
-- Optionally place a compass on the screen.
-- Optionally place a compass and 24-hour clock on the screen.
-- A HUD version of my realcompass mod.
-- By David_G (kestral246@gmail.com)
-- 2019-12-24
-- 2019-12-29
local hud_compass = {}
local storage = minetest.get_mod_storage()
@ -13,7 +13,13 @@ local storage = minetest.get_mod_storage()
local default_corner = -2 -- SE corner, off by default
local lookup = {
local lookup_compass = {
{hud_elem_type="image", text="", position={x=1,y=0}, scale={x=4,y=4}, alignment={x=-1,y=1}, offset={x=-76,y=4}},
{hud_elem_type="image", text="", position={x=1,y=1}, scale={x=4,y=4}, alignment={x=-1,y=-1}, offset={x=-76,y=-4}},
{hud_elem_type="image", text="", position={x=0,y=1}, scale={x=4,y=4}, alignment={x=1,y=-1}, offset={x=76,y=-4}},
{hud_elem_type="image", text="", position={x=0,y=0}, scale={x=4,y=4}, alignment={x=1,y=1}, offset={x=76,y=4}}
}
local lookup_clock = {
{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}},
@ -30,8 +36,10 @@ minetest.register_on_joinplayer(function(player)
end
end
hud_compass[pname] = {
id = player:hud_add(lookup[math.abs(corner)]),
last_image = -1,
id_compass = player:hud_add(lookup_compass[math.abs(corner)]),
last_image_compass = -1,
id_clock = player:hud_add(lookup_clock[math.abs(corner)]),
last_image_clock = -1,
state = corner,
}
end)
@ -44,23 +52,30 @@ minetest.register_chatcommand("compass", {
local player = minetest.get_player_by_name(pname)
if params and string.len(params) > 0 then -- includes corner parameter
local corner = tonumber(string.match(params, "^%d$"))
if corner and corner == 0 then -- disable compass
player:hud_change(hud_compass[pname].id, "text", "") -- blank hud
hud_compass[pname].last_image = -1
if corner and corner == 0 then -- disable compass and clock
player:hud_change(hud_compass[pname].id_compass, "text", "") -- blank hud compass
hud_compass[pname].last_image_compass = -1
player:hud_change(hud_compass[pname].id_clock, "text", "") -- blank hud clock
hud_compass[pname].last_image_clock = -1
hud_compass[pname].state = -1 * math.abs(hud_compass[pname].state)
storage:set_string(pname, hud_compass[pname].state)
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
elseif corner and corner > 0 and corner <= 4 then -- enable compass and clock to given corner
player:hud_remove(hud_compass[pname].id_compass) -- remove old hud compass
player:hud_remove(hud_compass[pname].id_clock) -- remove old hud clock
hud_compass[pname].id_compass = player:hud_add(lookup_compass[corner]) -- place new hud compass at requested corner
hud_compass[pname].last_image_compass = -1
hud_compass[pname].id_clock = player:hud_add(lookup_clock[corner]) -- place new hud clock at requested corner
hud_compass[pname].last_image_clock = -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
player:hud_change(hud_compass[pname].id_compass, "text", "") -- blank hud compass
hud_compass[pname].last_image_compass = -1
player:hud_change(hud_compass[pname].id_clock, "text", "") -- blank hud clock
hud_compass[pname].last_image_clock = -1
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
@ -83,13 +98,21 @@ minetest.register_globalstep(function(dtime)
local pname = player:get_player_name()
local dir = player:get_look_horizontal()
local angle_relative = math.deg(dir)
local image = math.floor((angle_relative/22.5) + 0.5)%16
local image_compass = math.floor((angle_relative/22.5) + 0.5)%16
local image_clock = math.floor(24 * minetest.get_timeofday())
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")
if hud_compass[pname].state > 0 and image_compass ~= hud_compass[pname].last_image_compass then
local rc = player:hud_change(hud_compass[pname].id_compass, "text", "realcompass_"..image_compass..".png")
-- Check return code, seems to fix occasional startup glitch.
if rc == 1 then
hud_compass[pname].last_image = image
hud_compass[pname].last_image_compass = image_compass
end
end
if hud_compass[pname].state > 0 and image_clock ~= hud_compass[pname].last_image_clock then
local rc = player:hud_change(hud_compass[pname].id_clock, "text", "hud_24hr_clock_"..image_clock..".png")
-- Check return code, seems to fix occasional startup glitch.
if rc == 1 then
hud_compass[pname].last_image_clock = image_clock
end
end
end

View File

@ -1,2 +1,2 @@
name = hud_compass
description = Optionally place a HUD compass on the screen.
description = Optionally place a HUD compass and 24-hour clock on the screen.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 350 KiB

After

Width:  |  Height:  |  Size: 371 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB