diff options
author | Y. Wang <yw05@forksworld.de> | 2022-04-11 22:32:10 +0200 |
---|---|---|
committer | Y. Wang <yw05@forksworld.de> | 2022-04-13 15:50:05 +0200 |
commit | d86b03fa18af90dd27b840eca11c2b556a225ca2 (patch) | |
tree | 9d16637fb15cbc4e88d301f538c259b0aa1220af | |
parent | 639a0244d868fc8a6beb7d5866a3c401b71c3f43 (diff) | |
download | advtrains-d86b03fa18af90dd27b840eca11c2b556a225ca2.tar.gz advtrains-d86b03fa18af90dd27b840eca11c2b556a225ca2.tar.bz2 advtrains-d86b03fa18af90dd27b840eca11c2b556a225ca2.zip |
Use dropdown indexing in formspec v4 (breaks MT 5.3 compat)
-rw-r--r-- | advtrains/init.lua | 3 | ||||
-rw-r--r-- | advtrains_interlocking/signal_api.lua | 93 |
2 files changed, 49 insertions, 47 deletions
diff --git a/advtrains/init.lua b/advtrains/init.lua index a7e5764..468623c 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -24,6 +24,9 @@ minetest.log("action", "[advtrains] Loading...") -- There is no need to support 0.4.x anymore given that the compatitability with it is already broken by 1bb1d825f46af3562554c12fba35a31b9f7973ff attrans = minetest.get_translator ("advtrains") +function attrans_formspec(...) + return minetest.formspec_escape(attrans(...)) +end --advtrains advtrains = {trains={}, player_to_train_mapping={}} diff --git a/advtrains_interlocking/signal_api.lua b/advtrains_interlocking/signal_api.lua index 83fae4a..6e862a8 100644 --- a/advtrains_interlocking/signal_api.lua +++ b/advtrains_interlocking/signal_api.lua @@ -423,67 +423,73 @@ purpose: form title string callback: func(pname, aspect) called on form submit isasp: aspect currently set ]] -function advtrains.interlocking.show_signal_aspect_selector(pname, p_suppasp, p_purpose, callback, isasp) - local suppasp = p_suppasp or { - main = {0, -1}, dst = {false}, shunt = false, info = {}, - } - local purpose = p_purpose or "" - - local form = "size[7,7]label[0.5,0.5;Select Signal Aspect:]" - form = form.."label[0.5,1;"..purpose.."]" - - form = form.."label[0.5,1.5;== Main Signal ==]" - local selid = 1 +local function make_signal_aspect_selector_t1(suppasp, purpose, isasp) + local form = {"size[7,7]"} + form[#form+1] = string.format("label[0.5,0.5;%s]", attrans_formspec("Select signal aspect")) + form[#form+1] = string.format("label[0.5,1;%s]", minetest.formspec_escape(purpose)) + + form[#form+1] = string.format("label[0.5,1.5;%s]", attrans_formspec("Main aspect")) local entries = {} + local selid = 1 for idx, spv in ipairs(suppasp.main) do local entry + if isasp and spv == isasp.main then + selid = idx + end if spv == 0 then - entry = "Halt" + entry = attrans("Danger (halt)") elseif spv == -1 then - entry = "Continue at maximum speed" + entry = attrans("Continue at maximum speed") elseif not spv then - entry = "Continue\\, speed limit unchanged (no info)" + entry = attrans("No info: continue with current speed limit") else - entry = "Continue at speed of "..spv - end - -- hack: the crappy formspec system returns the label, not the index. save the index in it. - entries[idx] = idx.."| "..entry - if isasp and spv == (isasp.main or false) then - selid = idx + entry = attrans("Continue with the speed limit of @1", spv) end + entries[idx] = minetest.formspec_escape(entry) end - form = form.."dropdown[0.5,2;6;main;"..table.concat(entries, ",")..";"..selid.."]" + form[#form+1] = string.format("dropdown[0.5,2;6;main;%s;%d;true]", table.concat(entries,","), selid) - - form = form.."label[0.5,3;== Shunting ==]" + form[#form+1] = string.format("label[0.5,3;%s]", attrans_formspec("Shunt aspect")) if suppasp.shunt == nil then local st = 1 - if isasp and isasp.shunt then st=2 end - form = form.."dropdown[0.5,3.5;6;shunt_free;---,allowed;"..st.."]" + if isasp and isasp.shunt then st = 2 end + form[#form+1] = string.format("dropdown[0.5,3.5;6;shunt_free;%s,%s;%d;true]", + attrans_formspec("No shunting"), + attrans_formspec("Shunting allowed"), + st) end - form = form.."label[0.5,4.5;== Distant Signal ==]" - local selid = 1 + form[#form+1] = string.format("label[0.5,4.5;%s]", attrans_formspec("Distant aspect")) local entries = {} + local selid = 1 for idx, spv in ipairs(suppasp.dst) do local entry + if isasp and spv == isasp.dst then + selid = idx + end if spv == 0 then - entry = "Expect to stop at the next signal" + entry = attrans("Expect to stop at the next signal") elseif spv == -1 then - entry = "Expect to pass the next signal at maximum speed" + entry = attrans("Expect to continue at maximum speed") elseif not spv then - entry = "No info" + entry = attrans("No info") else - entry = string.format("Expect to pass the next signal at speed of %d", spv) - end - entries[idx] = idx.."| "..entry - if isasp and spv == (isasp.dst or false) then - selid = idx + entry = attrans("Expect to continue with a speed limit of @1", spv) end + entries[idx] = minetest.formspec_escape(entry) end - form = form.."dropdown[0.5,5;6;dst;"..table.concat(entries, ",")..";"..selid.."]" + form[#form+1] = string.format("dropdown[0.5,5;6;dst;%s;%d;true]", table.concat(entries, ","), selid) + form[#form+1] = string.format("button_exit[0.5,6;5,1;save;%s]", attrans_formspec("Save signal aspect")) + return table.concat(form) +end - form = form.."button_exit[0.5,6;5,1;save;Save signal aspect]" +function advtrains.interlocking.show_signal_aspect_selector(pname, p_suppasp, p_purpose, callback, isasp) + local suppasp = p_suppasp or { + main = {0, -1}, dst = {false}, shunt = false, info = {}, + } + local purpose = p_purpose or "" + + local form = make_signal_aspect_selector_t1(suppasp, purpose, isasp) local token = advtrains.random_id() @@ -506,27 +512,20 @@ local function usebool(sup, val, free) end end --- other side of hack: extract the index -local function ddindex(val) - return tonumber(string.match(val, "^(%d+)|")) -end - --- TODO use non-hacky way to parse outputs - minetest.register_on_player_receive_fields(function(player, formname, fields) local pname = player:get_player_name() local psl = players_aspsel[pname] if psl then if formname == "at_il_sigaspdia_"..psl.token then if fields.save then - local maini = ddindex(fields.main) + local maini = tonumber(fields.main) if not maini then return end - local dsti = ddindex(fields.dst) + local dsti = tonumber(fields.dst) if not dsti then return end local asp = { main = psl.suppasp.main[maini], dst = psl.suppasp.dst[dsti], - shunt = usebool(psl.suppasp.shunt, fields.shunt_free, "allowed"), + shunt = usebool(psl.suppasp.shunt, fields.shunt_free, "2"), info = {} } psl.callback(pname, asp) |