Move marker formspec code into items.lua

This commit is contained in:
Hugues Ross 2020-06-08 18:12:05 -04:00
parent 8aba2d8c57
commit 5af69ccd14
3 changed files with 107 additions and 113 deletions

View File

@ -57,7 +57,6 @@ cartographer.gui = loadfile(modpath .. "/formspec.lua") ();
loadfile(modpath .. "/scanner.lua") (map_data, chunk);
loadfile(modpath .. "/map_api.lua") (chunk, biome_lookup, marker_lookup);
loadfile(modpath .. "/items.lua") (chunk, marker_lookup, cartographer.gui, cartographer.skin);
_cartographer.generate_marker_formspec = loadfile(modpath .. "/marker_formspec.lua") (marker_lookup, cartographer.gui);
loadfile(modpath .. "/map_formspec.lua") (map_data);
loadfile(modpath .. "/commands.lua") ();
loadfile(modpath .. "/table.lua") (_cartographer.materials_by_name, _cartographer.materials_by_group, cartographer.gui, cartographer.skin);

109
items.lua
View File

@ -3,6 +3,111 @@ local chunk, marker_lookup, gui, skin = ...
-- The list of players looking at maps, and the map IDs that they're looking at
local player_maps = {};
-- Generates formspec data for the map marker editor
-- selected_id: The id of the currently selected marker, or nil if no marker is
-- selected
-- detail: The map's detail level
-- page: The current page
--
-- Returns a formspec string for use in containers
local function marker_formspec(selected_id, detail, page)
local formspec = {
gui.bg9 {
x = 0,
y = 0,
w = 3.25,
h = 3.875,
skin = skin.marker_bg,
},
gui.style_type {
selector = "button,image_button",
properties = {
border = false,
bgimg = skin.marker_button.texture .. ".png",
bgimg_hovered = skin.marker_button.hovered_texture .. ".png",
bgimg_pressed = skin.marker_button.pressed_texture .. ".png",
bgimg_middle = skin.marker_button.radius,
textcolor = skin.marker_button.font_color,
},
},
gui.button {
x = 0.125,
y = 0.125,
w = 1.125,
h = 0.5,
id = "clear_marker",
text = "Erase",
tooltip = "Remove the selected marker",
},
gui.label {
x = 1.375,
y = 3.5,
text = string.format("%d / %d", page, math.ceil(#marker_lookup / 20)),
},
};
if selected_id then
table.insert(formspec, gui.style {
selector = "marker-" .. selected_id,
properties = {
bgimg = skin.marker_button.selected_texture .. ".png",
bgimg_hovered = skin.marker_button.selected_texture .. ".png",
bgimg_pressed = skin.marker_button.selected_texture .. ".png",
}
});
end
local starting_id = ((page - 1) * 20) + 1;
for i = starting_id,math.min(#marker_lookup,starting_id + 19),1 do
local marker = marker_lookup[i];
table.insert(formspec, gui.image_button {
x = (i - starting_id) % 5 * 0.625 + 0.125,
y = math.floor((i - starting_id) / 5) * 0.625 + 0.75,
w = 0.5,
h = 0.5,
image = marker.textures[math.min(detail, #marker.textures)] .. ".png",
id = "marker-" .. marker.id,
tooltip = marker.name,
});
end
if page > 1 then
table.insert(formspec, gui.button {
x = 0.125,
y = 3.25,
w = 0.5,
h = 0.5,
id = "prev_button",
text = "<"
});
end
if starting_id + 19 < #marker_lookup then
table.insert(formspec, gui.button {
x = 2.625,
y = 3.25,
w = 0.5,
h = 0.5,
id = "next_button",
text = ">"
});
end
return table.concat(formspec);
end
-- Show a map to a player from the ID
-- id: The map ID
-- player_x: The X position (in world coordinates)
@ -42,7 +147,7 @@ local function show_map_id_formspec(id, player_x, player_z, player_name, height_
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),
marker_formspec(cartographer.get_marker(map, player_x, player_z), map.detail, marker_page or 1),
},
gui.container {
x = formspec_width - 0.01,
@ -301,7 +406,7 @@ function cartographer.resize_map_item(meta, size)
local map = cartographer.get_map(id);
cartographer.resize_map(id, size, size);
copy_meta:set_string("description", map_description(id,
meta:set_string("description", map_description(id,
chunk.from(map.x), chunk.from(map.z),
chunk.from(map.w), chunk.from(map.h)));
end

View File

@ -1,110 +0,0 @@
local marker_lookup, gui = ...;
-- Generates formspec data for the map marker editor
-- selected_id: The id of the currently selected marker, or nil if no marker is
-- selected
-- detail: The map's detail level
-- page: The current page
-- bg: A 9-slice background skin table
-- button: A button skin table
--
-- Returns a formspec string for use in containers
local function generate_marker_formspec(selected_id, detail, page, bg, button)
local formspec = {
gui.bg9 {
x = 0,
y = 0,
w = 3.25,
h = 3.875,
skin = bg,
},
gui.style_type {
selector = "button,image_button",
properties = {
border = false,
bgimg = button.texture .. ".png",
bgimg_hovered = button.hovered_texture .. ".png",
bgimg_pressed = button.pressed_texture .. ".png",
bgimg_middle = button.radius,
textcolor = button.font_color,
},
},
gui.button {
x = 0.125,
y = 0.125,
w = 1.125,
h = 0.5,
id = "clear_marker",
text = "Erase",
tooltip = "Remove the selected marker",
},
gui.label {
x = 1.375,
y = 3.5,
text = string.format("%d / %d", page, math.ceil(#marker_lookup / 20)),
},
};
if selected_id then
table.insert(formspec, gui.style {
selector = "marker-" .. selected_id,
properties = {
bgimg = button.selected_texture .. ".png",
bgimg_hovered = button.selected_texture .. ".png",
bgimg_pressed = button.selected_texture .. ".png",
}
});
end
local starting_id = ((page - 1) * 20) + 1;
for i = starting_id,math.min(#marker_lookup,starting_id + 19),1 do
local marker = marker_lookup[i];
table.insert(formspec, gui.image_button {
x = (i - starting_id) % 5 * 0.625 + 0.125,
y = math.floor((i - starting_id) / 5) * 0.625 + 0.75,
w = 0.5,
h = 0.5,
image = marker.textures[math.min(detail, #marker.textures)] .. ".png",
id = "marker-" .. marker.id,
tooltip = marker.name,
});
end
if page > 1 then
table.insert(formspec, gui.button {
x = 0.125,
y = 3.25,
w = 0.5,
h = 0.5,
id = "prev_button",
text = "<"
});
end
if starting_id + 19 < #marker_lookup then
table.insert(formspec, gui.button {
x = 2.625,
y = 3.25,
w = 0.5,
h = 0.5,
id = "next_button",
text = ">"
});
end
return table.concat(formspec);
end
return generate_marker_formspec;