From fa4f23af736264be423c2b1203bdd46fa36e7a1d Mon Sep 17 00:00:00 2001 From: cheapie Date: Fri, 10 Jul 2026 13:41:41 -0500 Subject: Improve call button behavior on touchscreen devices --- callbuttons.lua | 96 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 77 insertions(+), 19 deletions(-) diff --git a/callbuttons.lua b/callbuttons.lua index 84ccf12..2c36147 100644 --- a/callbuttons.lua +++ b/callbuttons.lua @@ -1,7 +1,54 @@ celevator.callbutton = {} +local fscontext = {} + local S = core.get_translator("celevator") +local function showfs(pos,name) + local nname = celevator.get_node(pos).name + fscontext[name] = pos + + local fs = "formspec_version[10]size[4,6]" + fs = fs.."background[0,0;4,6;celevator_callbutton_panel.png;true]" + + local uplit = core.get_item_group(nname,"_celevator_callbutton_up_lit") == 1 + local uptex = "celevator_callbutton_up.png" + local uplittex = "[combine:17x9:0,0="..uptex..":9,1=celevator_callbutton_light.png" + if uplit then uptex = uplittex end + fs = fs.."image_button[0.75,1;2.5,1.25;"..uptex..";up;;false;false;"..uplittex.."]" + + local downlit = core.get_item_group(nname,"_celevator_callbutton_down_lit") == 1 + local downtex = "celevator_callbutton_down.png" + local downlittex = "[combine:17x9:0,0="..downtex..":9,1=celevator_callbutton_light.png" + if downlit then downtex = downlittex end + fs = fs.."image_button[0.75,3.5;2.5,1.25;"..downtex..";down;;false;false;"..downlittex.."]" + + core.show_formspec(name,"celevator:call_button",fs) +end + +core.register_on_player_receive_fields(function(player,formname,fields) + if formname ~= "celevator:call_button" then + return false + end + local name = player:get_player_name() + local pos = fscontext[name] + if not pos then return true end + if fields.quit then + fscontext[name] = nil + elseif fields.up then + local nname = celevator.get_node(pos).name + if core.get_item_group(nname,"_celevator_callbutton_has_up") == 1 then + core.registered_nodes["celevator:callbutton_up"].on_rightclick(pos) + end + elseif fields.down then + local nname = celevator.get_node(pos).name + if core.get_item_group(nname,"_celevator_callbutton_has_down") == 1 then + core.registered_nodes["celevator:callbutton_down"].on_rightclick(pos) + end + end + return true +end) + local function makebuttontex(dir,upon,downon,inventory) local tex = "[combine:64x64".. ":0,0=celevator_cabinet_sides.png".. @@ -96,30 +143,41 @@ function celevator.callbutton.setlight(pos,dir,newstate) node.name = newname core.swap_node(pos,node) end + for name,contextpos in pairs(fscontext) do + if vector.equals(pos,contextpos) then + showfs(pos,name) + end + end end local function disambiguatedir(pos,player) if player and not player.is_fake_player then - local eyepos = vector.add(player:get_pos(),vector.add(player:get_eye_offset(),vector.new(0,1.5,0))) - local lookdir = player:get_look_dir() - local distance = vector.distance(eyepos,pos) - local endpos = vector.add(eyepos,vector.multiply(lookdir,distance+1)) - local ray = core.raycast(eyepos,endpos,true,false) - local pointed,button,hitpos - repeat - pointed = ray:next() - if pointed and pointed.type == "node" then - local node = core.get_node(pointed.under) - if node.name and (core.get_item_group(node.name,"_celevator_callbutton") == 1) then - button = pointed.under - hitpos = vector.subtract(pointed.intersection_point,button) + local name = player:get_player_name() + local windowinfo = core.get_player_window_information(name) + if windowinfo and windowinfo.touch_controls then + showfs(pos,name) + else + local eyepos = vector.add(player:get_pos(),vector.add(player:get_eye_offset(),vector.new(0,1.5,0))) + local lookdir = player:get_look_dir() + local distance = vector.distance(eyepos,pos) + local endpos = vector.add(eyepos,vector.multiply(lookdir,distance+1)) + local ray = core.raycast(eyepos,endpos,true,false) + local pointed,button,hitpos + repeat + pointed = ray:next() + if pointed and pointed.type == "node" then + local node = core.get_node(pointed.under) + if node.name and (core.get_item_group(node.name,"_celevator_callbutton") == 1) then + button = pointed.under + hitpos = vector.subtract(pointed.intersection_point,button) + end end - end - until button or not pointed - if not hitpos then return end - hitpos.y = -1*hitpos.y - hitpos.y = math.floor((hitpos.y+0.5)*64+0.5)+1 - return hitpos.y >= 40 and "down" or "up" + until button or not pointed + if not hitpos then return end + hitpos.y = -1*hitpos.y + hitpos.y = math.floor((hitpos.y+0.5)*64+0.5)+1 + return hitpos.y >= 40 and "down" or "up" + end end end -- cgit v1.2.3