From 93b6ab6ee822490f590e61fd6acdbd4c97c2afbe Mon Sep 17 00:00:00 2001 From: Hugues Ross Date: Sun, 7 Jun 2020 20:35:17 -0400 Subject: [PATCH] items.lua: Start formspec cleanup and refactor map description into a function --- formspec.lua | 12 +++++++++ init.lua | 2 +- items.lua | 70 +++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 71 insertions(+), 13 deletions(-) diff --git a/formspec.lua b/formspec.lua index ee24b31..83b11b4 100644 --- a/formspec.lua +++ b/formspec.lua @@ -41,6 +41,18 @@ end function gui.container(args) local data = string.format("container[%f,%f]", args.x, args.y); + + if args.bg then + data = data .. gui.bg9 { + x = 0, + y = 0, + w = args.w, + h = args.h, + + skin = args.bg, + }; + end + for _,element in ipairs(args) do data = data .. element; end diff --git a/init.lua b/init.lua index 615b611..d228e3e 100644 --- a/init.lua +++ b/init.lua @@ -56,7 +56,7 @@ cartographer.skin = loadfile(modpath .. "/skin_api.lua") (); cartographer.gui = loadfile(modpath .. "/formspec.lua") (); loadfile(modpath .. "/scanner.lua") (map_data, chunk); loadfile(modpath .. "/map_api.lua") (chunk); -loadfile(modpath .. "/items.lua") (chunk); +loadfile(modpath .. "/items.lua") (chunk, cartographer.gui, cartographer.skin); _cartographer.generate_marker_formspec = loadfile(modpath .. "/marker_formspec.lua") (_cartographer.marker_lookup, cartographer.gui); loadfile(modpath .. "/map_formspec.lua") (map_data); loadfile(modpath .. "/commands.lua") (); diff --git a/items.lua b/items.lua index 4440aad..d115dbd 100644 --- a/items.lua +++ b/items.lua @@ -1,4 +1,4 @@ -local chunk = ... +local chunk, gui, skin = ... -- The list of players looking at maps, and the map IDs that they're looking at local player_maps = {}; @@ -23,22 +23,64 @@ local function show_map_id_formspec(id, player_x, player_z, player_name, height_ player_x, player_z = cartographer.to_map_coordinates(map, player_x, player_z) local formspec, formspec_width, _ = cartographer.get_map_formspec_map(map, player_x, player_z, height_mode); if #_cartographer.marker_lookup > 0 then - formspec = formspec .. string.format("style_type[button,image_button,label;noclip=true] container[%f,1.0]", formspec_width - 0.01) - .. _cartographer.generate_marker_formspec(cartographer.get_marker(map, player_x, player_z), map.detail, marker_page or 1, cartographer.skin.marker_bg, cartographer.skin.marker_button) - .. "container_end[]" - .. string.format("container[%f,0.125]", formspec_width - 0.01) - .. string.format("background9[0,0;0.75,0.75;%s.png;false;%s]", cartographer.skin.marker_bg.texture, tostring(cartographer.skin.marker_bg.radius)); + local height_button_texture; if height_mode then - formspec = formspec .. string.format("image_button[0.125,0.125;0.5,0.5;%s.png;height_button;]", cartographer.skin.height_button_texture) + height_button_texture = skin.height_button_texture .. ".png"; else - formspec = formspec .. string.format("image_button[0.125,0.125;0.5,0.5;%s.png;height_button;]", cartographer.skin.flat_button_texture) + height_button_texture = skin.flat_button_texture .. ".png"; end - formspec = formspec .. "tooltip[height_button;Toggle height view] container_end[]"; + local data = { + gui.style_type { + selector = "button,image_button,label", + properties = { + noclip = true, + } + }, + gui.container { + x = formspec_width - 0.01, + y = 1, + bg = skin.marker_bg, + + _cartographer.generate_marker_formspec(cartographer.get_marker(map, player_x, player_z), map.detail, marker_page or 1, skin.marker_bg, skin.marker_button), + }, + gui.container { + x = formspec_width - 0.01, + y = 0.125, + w = 0.75, + h = 0.75, + bg = skin.marker_bg, + + gui.image_button { + x = 0.125, + y = 0.125, + w = 0.5, + h = 0.5, + + id = "height_button", + image = height_button_texture, + tooltip = "Toggle height view", + } + }, + }; + formspec = formspec .. table.concat(data); end minetest.show_formspec(player_name, "cartographer:map", formspec); end +-- Get the description text for a map ID and dimensions +-- id: The map ID +-- from_x: The x coordinate of the top-left corner of the map, in world coordinates +-- from_z: The z coordinate of the top-left corner of the map, in world coordinates +-- w: The width, in world coordinates +-- h: The height, in world coordinates +local function map_description(id, from_x, from_z, w, h) + return string.format("Map #%d\n[%d,%d] - [%d,%d]", + id, + chunk.from(from_x), chunk.from(from_z), + chunk.from(from_x + w), chunk.from(from_z + h)); +end + -- Create a map from metadata, and assign the ID to the metadata -- meta: A metadata object containing the map ID -- player_x: The X position (in map coordinates) @@ -56,7 +98,7 @@ local function map_from_meta(meta, player_x, player_z) local id = cartographer.create_map(map_x, map_y, size, size, false, detail, scale); meta:set_int("cartographer:map_id", id); - meta:set_string("description", "Map #" .. tostring(id) .. "\n[" .. tostring(chunk.from(map_x)) .. "," .. tostring(chunk.from(map_y)) .. "] - [" .. tostring(chunk.from(map_x + total_size)) .. "," .. tostring(chunk.from(map_y + total_size)) .. "]") + meta:set_string("description", map_description(id, map_x, map_y, total_size, total_size)); return id; end @@ -237,7 +279,9 @@ function cartographer.copy_map_item(stack) end copy_meta:set_int("cartographer:map_id", new_id); - copy_meta:set_string("description", "Map #" .. tostring(new_id) .. "\n[" .. tostring(chunk.from(dest.x)) .. "," .. tostring(chunk.from(dest.z)) .. "] - [" .. tostring(chunk.from(dest.x + dest.w)) .. "," .. tostring(chunk.from(dest.z + dest.h)) .. "]") + copy_meta:set_string("description", map_description(new_id, + dest.x, dest.z, + dest.w, dest.h)); end return copy; @@ -257,6 +301,8 @@ function cartographer.resize_map_item(meta, size) local map = cartographer.get_map(id); cartographer.resize_map(id, size, size); - meta:set_string("description", "Map #" .. tostring(id) .. "\n[" .. tostring(chunk.from(map.x)) .. "," .. tostring(chunk.from(map.z)) .. "] - [" .. tostring(chunk.from(map.x + map.w)) .. "," .. tostring(chunk.from(map.z + map.h)) .. "]") + copy_meta:set_string("description", map_description(id, + chunk.from(map.x), chunk.from(map.z), + chunk.from(map.w), chunk.from(map.h))); end end