From 9c3cd7a851e51755ecce3616f5e955620f0ac733 Mon Sep 17 00:00:00 2001 From: techniX Date: Sat, 7 Dec 2019 11:42:08 +0200 Subject: [PATCH] better hud --- hud.lua | 102 +++++++++++++++------ init.lua | 6 +- textures/ham_radio_hud_bg.png | Bin 0 -> 251 bytes textures/ham_radio_hud_indicator_empty.png | Bin 0 -> 175 bytes textures/ham_radio_hud_indicator_full.png | Bin 0 -> 174 bytes textures/indicator.xcf | Bin 0 -> 2520 bytes 6 files changed, 75 insertions(+), 33 deletions(-) create mode 100644 textures/ham_radio_hud_bg.png create mode 100644 textures/ham_radio_hud_indicator_empty.png create mode 100644 textures/ham_radio_hud_indicator_full.png create mode 100644 textures/indicator.xcf diff --git a/hud.lua b/hud.lua index 4effa50..72f7375 100644 --- a/hud.lua +++ b/hud.lua @@ -1,37 +1,79 @@ -function ham_radio.init_hud(player) +function ham_radio.toggle_hud(player) local name = player:get_player_name() - ham_radio.playerhuds[name] = player:hud_add({ - hud_elem_type = "text", - text = "", - position = ham_radio.settings.hud_pos, - offset = { x = ham_radio.settings.hud_offset.x, y = ham_radio.settings.hud_offset.y }, - alignment = ham_radio.settings.hud_alignment, - number = 0xFFFFFF, - scale= { x = 100, y = 20 }, - }) + local item = player:get_wielded_item() + + -- remove hud if user does not wield a receiver + if item:get_name() ~= "ham_radio:receiver" then + if ham_radio.is_receiver_wielded[name] then + player:hud_remove(ham_radio.playerhuds[name].background) + player:hud_remove(ham_radio.playerhuds[name].frequency) + player:hud_remove(ham_radio.playerhuds[name].signal_meter) + player:hud_remove(ham_radio.playerhuds[name].signal_level) + ham_radio.is_receiver_wielded[name] = false + end + return false + end + + -- if hud is already enabled, pass + if ham_radio.is_receiver_wielded[name] then + return true + end + + -- create hud + ham_radio.is_receiver_wielded[name] = true + + local hud_pos = ham_radio.settings.hud_pos + + ham_radio.playerhuds[name] = { + background = player:hud_add({ + hud_elem_type = "image", + position = hud_pos, + offset = { x = -250, y = 20 }, + text = "ham_radio_hud_bg.png", + scale = { x = 2, y = 3 }, + alignment = { x = 1, y = 0 }, + }), + frequency = player:hud_add({ + hud_elem_type = "text", + text = "", + position = hud_pos, + offset = { x = 0, y = 0 }, + alignment = 0, + number = 0xFFFFFF, + scale= { x = 100, y = 20 }, + }), + signal_meter = player:hud_add({ + hud_elem_type = "image", + position = hud_pos, + offset = { x = -220, y = 30 }, + text = "ham_radio_hud_indicator_empty.png", + scale = { x = 2, y = 2 }, + alignment = { x = 1, y = 0 }, + }), + signal_level = player:hud_add({ + hud_elem_type = "image", + position = hud_pos, + offset = { x = -220, y = 30 }, + text = "ham_radio_hud_indicator_full.png", + scale = { x = 0, y = 2 }, + alignment = { x = 1, y = 0 }, + }) + } + return true end function ham_radio:update_hud_display(player) - local transmitter_signal = 0 - local name = player:get_player_name() - local item = player:get_wielded_item() - - if item:get_name() ~= "ham_radio:receiver" then - if self.playerlocators[name] then - player:hud_change(self.playerhuds[name], "text", "") - self.playerlocators[name] = false - end + if not ham_radio.toggle_hud(player) then return end - self.playerlocators[name] = true - - local meta = item:get_meta() - local frequency = meta:get_string("frequency") - minetest.chat_send_player(player:get_player_name(), "Configured freq:"..frequency) + local transmitter_signal = 0 + local name = player:get_player_name() + local meta = player:get_wielded_item():get_meta() + local frequency = meta:get_string("frequency") if frequency ~= nil and frequency ~= "" then local transmitter = self.read_transmitter(frequency) @@ -45,15 +87,17 @@ function ham_radio:update_hud_display(player) --local indicator = string.rep('|', transmitter.distance)..string.rep('|', transmitter.signal)..string.rep(':', 25-(transmitter.distance + transmitter.signal)) local indicator = string.rep('|', transmitter_signal)..string.rep(':', 20 - transmitter_signal) - local text = "[ Frequency: "..tostring(meta:get_string("frequency")).." ]"..indicator - - player:hud_change(self.playerhuds[name], "text", text) + --local text = "[ Frequency: "..tostring(meta:get_string("frequency")).." ]"..indicator + local text = "[ Frequency: "..tostring(meta:get_string("frequency")).." ]" + player:hud_change(self.playerhuds[name].frequency, "text", text) + player:hud_change(self.playerhuds[name].signal_level, "scale", { x = (transmitter_signal * 20) / 200 or 0.1, y = 2 }) end -minetest.register_on_newplayer(ham_radio.init_hud) -minetest.register_on_joinplayer(ham_radio.init_hud) +minetest.register_on_newplayer(ham_radio.toggle_hud) +minetest.register_on_joinplayer(ham_radio.toggle_hud) minetest.register_on_leaveplayer(function(player) + ham_radio.is_receiver_wielded[name] = false ham_radio.playerhuds[player:get_player_name()] = nil end) diff --git a/init.lua b/init.lua index 554eaff..611d540 100644 --- a/init.lua +++ b/init.lua @@ -5,11 +5,9 @@ ham_radio = rawget(_G, "ham_radio") or {} ham_radio = { playerhuds = {}, - playerlocators = {}, + is_receiver_wielded = {}, settings = { - hud_pos = { x = 0.45, y = 0.7 }, - hud_offset = { x = 15, y = 15 }, - hud_alignment = { x = 1, y = 0 } + hud_pos = { x = 0.5, y = 0.8 }, } } diff --git a/textures/ham_radio_hud_bg.png b/textures/ham_radio_hud_bg.png new file mode 100644 index 0000000000000000000000000000000000000000..9ed15cced3418c32d66498afd08d072124e3b616 GIT binary patch literal 251 zcmeAS@N?(olHy`uVBq!ia0vp^zkpbdgAGWsS~9N$Qfx`y?k)`fL2$v|<&%LToCO|{ z#S9F3${@^GvDCf{D9B#o>Fdh=n1@Y>N9ooJj^#ih$r9Iy66gHf+|;}h2Ir#G#FEq$ zh4Rdj3> literal 0 HcmV?d00001 diff --git a/textures/ham_radio_hud_indicator_empty.png b/textures/ham_radio_hud_indicator_empty.png new file mode 100644 index 0000000000000000000000000000000000000000..22cddb78085c42be005017f0303a6b34b2e49532 GIT binary patch literal 175 zcmeAS@N?(olHy`uVBq!ia0vp^cYv6cg9%73uSBw6AbQR1ARo123eO8TLK@pq>xZ O#Ng@b=d#Wzp$P!VJuAxq literal 0 HcmV?d00001 diff --git a/textures/ham_radio_hud_indicator_full.png b/textures/ham_radio_hud_indicator_full.png new file mode 100644 index 0000000000000000000000000000000000000000..8a05e88054625d9dff3c3f40fb4621872845c0f1 GIT binary patch literal 174 zcmeAS@N?(olHy`uVBq!ia0vp^cYv6cg9%73niE8N)J_E&aJdOT)sXO=vN%B`fP-PT4&!Fd+D)^7 PdKf%i{an^LB{Ts5`aLYu literal 0 HcmV?d00001 diff --git a/textures/indicator.xcf b/textures/indicator.xcf new file mode 100644 index 0000000000000000000000000000000000000000..31a83b1fb182d4acf48c742df33eb5ee3fc84893 GIT binary patch literal 2520 zcmds1-D(p-6rRa+Gf87etbY^)SFJRyMiUW>2r2|Ag(~qzyxk_7?Lz*@Ms2*Qcxx_s-V6jo|spuR8nk?$Y5`46(BSr@(VSriN(**m+<9xCvZT zoZ>DZeiJ_LVfk{S*Xg+3A^Op3OSOUP4Be*v)*tTLn_JK7G*XO0{f^_gWpCg&RWxU% zU^r^KcF=44&9%i+%WL%7y@6edTzTN_ICkZBC9(ukRYPl-x|1+fkK5A;P2CInPQ&kd zc2%zhag-z~t^B&{4{g8O*mVP%-)pr3cR2Y#n*FB~E61nOIT*%&#+~#FL)28?E#2=@ z-3jQBx1EtYup7Pph@$A$$mn41dmBrhyy=+^Fe${GH#qG8WjIc8DaJfS zja3lnqOrI|>}a5f4^%ooT|P>a79n;QWoMR|WOB^~?3`d3ComgrpM4ZrWL-Xi0u@^q z&=1B2I6Vqvx~MGhy!p(~;Q#cp=*`oc__0vn4GGbEm5_y