cartographer/marker_formspec.lua
Hugues Ross 2461c66f99 Add experimental declarative GUI API
- Refactor marker UI to use the new API
2020-06-02 08:01:11 -04:00

111 lines
2.9 KiB
Lua

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;