From cbc71c0d9420844cbda600fd1e2d58d9f1035281 Mon Sep 17 00:00:00 2001 From: David G Date: Mon, 30 Dec 2019 10:20:52 -0700 Subject: [PATCH] Select between compass, or compass and clock. --- README.md | 28 +++++++++++++++++----------- init.lua | 45 ++++++++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 168f9c6..f870453 100644 --- a/README.md +++ b/README.md @@ -10,22 +10,29 @@ By David G (kestral246) How to enable ------------- -This mod defaults to not displaying compass and clock. To enable, use the chat command: +This mod defaults to not displaying a compass or a clock. To enable, use the chat command: - "/compass" -> By default this places a compass and clock in the bottom right corner of the screen. + "/compass" -> By default this places just a compass in the bottom right corner of the screen. -Repeated use of this command will toggle the display of the compass and clock off and on. +Repeated use of this command will toggle the display of the compass off and on. -**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. +When given with an argument, the user can select whether to display just a compass, or a compass and a clock, and which corner of the screen to place them in. 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 + "/compass 1" -> compass only, top right corner + "/compass 2" -> compass only, bottom right corner + "/compass 3" -> compass only, bottom left corner + "/compass 4" -> compass only, top left corner + + "/compass 5" -> compass and clock, top right corner + "/compass 6" -> compass and clock, bottom right corner + "/compass 7" -> compass and clock, bottom left corner + "/compass 8" -> compass and clock, top left corner In addition: - "/compass 0" -> forces compass off. + "/compass 0" -> forces compass and clock off. + +**Note:** The clock is a 24-hour clock, with only one hand that displays the hours. Noon is at the top and midnight is at the bottom. Local mod storage is used to maintain the state and position of hud_compass display between sessions, per user. @@ -40,8 +47,7 @@ Media (textures) > Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) -> (Textures were copied from my realcompass mod, which were originally based on the textures created by tacotexmex for the ccompass mod.) - +> (Compass textures were copied from my realcompass mod, which were originally based on the textures created by tacotexmex for the ccompass mod. Clock textures were derived from these same textures.) diff --git a/init.lua b/init.lua index ee28b28..4d37662 100644 --- a/init.lua +++ b/init.lua @@ -2,24 +2,33 @@ -- 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-29 +-- 2019-12-30 local hud_compass = {} local storage = minetest.get_mod_storage() -- State of hud_compass --- 1 == NE, 2 == SE, 3 == SW, 4 == NW +-- 1 = NE, 2 = SE, 3 = SW, 4 = NW (just compass) +-- 5 = NE, 6 = SE, 7 = SW, 8 = NW (both compass and clock) -- positive == enabled, negative == disabled -local default_corner = -2 -- SE corner, off by default +local default_corner = -2 -- SE corner, compass only, off by default 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=-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}}, {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}}, + {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}}, {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}}, @@ -31,7 +40,7 @@ minetest.register_on_joinplayer(function(player) local corner = default_corner if storage:get(pname) and tonumber(storage:get(pname)) then -- validate mod storage value local temp = math.floor(tonumber(storage:get(pname))) - if temp ~= nil and temp ~= 0 and temp >= -4 and temp <= 4 then + if temp ~= nil and temp ~= 0 and temp >= -8 and temp <= 8 then corner = temp end end @@ -46,7 +55,7 @@ end) minetest.register_chatcommand("compass", { params = "[]", - description = "Change display of hud compass.", + description = "Change display of HUD Compass.", privs = {}, func = function(pname, params) local player = minetest.get_player_by_name(pname) @@ -59,13 +68,21 @@ minetest.register_chatcommand("compass", { 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 and clock to given corner + elseif corner and corner > 0 and corner <= 4 then -- enable compass only 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 + player:hud_remove(hud_compass[pname].id_clock) -- remove old hud clock hud_compass[pname].last_image_clock = -1 + hud_compass[pname].id_compass = player:hud_add(lookup_compass[corner]) -- place new hud compass at requested corner + hud_compass[pname].state = corner + storage:set_string(pname, corner) + elseif corner and corner >= 5 and corner <= 8 then -- enable compass and clock to given corner + player:hud_remove(hud_compass[pname].id_compass) -- remove old hud compass + hud_compass[pname].last_image_compass = -1 + player:hud_remove(hud_compass[pname].id_clock) -- remove old hud clock + hud_compass[pname].last_image_clock = -1 + hud_compass[pname].id_compass = player:hud_add(lookup_compass[corner]) -- place new hud compass at requested corner + hud_compass[pname].id_clock = player:hud_add(lookup_clock[corner]) -- place new hud clock at requested corner hud_compass[pname].state = corner storage:set_string(pname, corner) end @@ -75,12 +92,9 @@ minetest.register_chatcommand("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 - 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 + hud_compass[pname].state = -1 * hud_compass[pname].state -- toggle state + storage:set_string(pname, hud_compass[pname].state) end end, }) @@ -97,6 +111,7 @@ minetest.register_globalstep(function(dtime) for i,player in ipairs(players) do local pname = player:get_player_name() local dir = player:get_look_horizontal() + -- Calculate image indexes for compass and clock. local angle_relative = math.deg(dir) local image_compass = math.floor((angle_relative/22.5) + 0.5)%16 local image_clock = math.floor(24 * minetest.get_timeofday()) @@ -108,7 +123,7 @@ minetest.register_globalstep(function(dtime) 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 + if hud_compass[pname].state >= 5 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