Refactor marker_lookup out of init.lua

This commit is contained in:
Hugues Ross 2020-06-13 08:01:56 -04:00
parent 6a3938d996
commit 6187137158
4 changed files with 82 additions and 68 deletions

View File

@ -5,12 +5,16 @@ local CHUNK_SIZE = 16;
return {
-- Convert world coordinates to map coordinates
-- coord: The coordinate value
--
-- Returns a coordinate value in map space
to = function(coord)
return math.floor(coord / CHUNK_SIZE);
end,
-- Convert map coordinates to world coordinates
-- coord: The coordinate value
--
-- Returns a coordinate value in world space
from = function(coord)
return math.floor(coord * CHUNK_SIZE);
end

View File

@ -29,8 +29,6 @@ local function periodic_save()
end
minetest.after(60, periodic_save);
local marker_lookup = {};
-- Includes
local chunk = loadfile(modpath .. "/chunk_api.lua") ();
local skin = loadfile(modpath .. "/skin_api.lua") ();
@ -43,9 +41,9 @@ local biomes = loadfile(modpath .. "/biome_api.lua") (util);
local scanner = loadfile(modpath .. "/scanner.lua") (map_data, chunk);
local maps = loadfile(modpath .. "/map_api.lua") (map_data, chunk);
local materials = loadfile(modpath .. "/material_api.lua") ();
local markers = loadfile(modpath .. "/marker_api.lua") (marker_lookup);
local markers = loadfile(modpath .. "/marker_api.lua") ();
local map_formspec = loadfile(modpath .. "/map_formspec.lua") (map_data, gui, skin, util, biomes, markers);
local map_item = loadfile(modpath .. "/items.lua") (chunk, marker_lookup, gui, skin, audio, maps, map_formspec);
local map_item = loadfile(modpath .. "/items.lua") (chunk, gui, skin, audio, maps, markers, map_formspec);
loadfile(modpath .. "/commands.lua") (chunk, audio, map_formspec);
loadfile(modpath .. "/table.lua") (gui, skin, audio, maps, materials, map_item);
loadfile(modpath .. "/autofill.lua") (chunk, scanner, maps);

122
items.lua
View File

@ -1,4 +1,4 @@
local chunk, marker_lookup, gui, skin, audio, maps, map_formspec = ...;
local chunk, gui, skin, audio, maps, markers, map_formspec = ...;
-- The list of players looking at maps, and the map IDs that they're looking at
local player_maps = {};
@ -11,27 +11,9 @@ local player_maps = {};
--
-- Returns a formspec string for use in containers
local function marker_formspec(selected_id, detail, page)
local marker_lookup = markers.get_all();
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,
@ -131,49 +113,63 @@ local function show_map_id_formspec(id, player_x, player_z, player_name, height_
player_x, player_z = map:to_coordinates(player_x, player_z)
local formspec, formspec_width, _ = map_formspec.from_map(map, player_x, player_z, height_mode);
if #marker_lookup > 0 then
local height_button_texture;
if height_mode then
height_button_texture = skin.height_button_texture .. ".png";
else
height_button_texture = skin.flat_button_texture .. ".png";
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,
marker_formspec(map:get_marker(player_x, player_z), map.detail, marker_page or 1),
},
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);
local height_button_texture;
if height_mode then
height_button_texture = skin.height_button_texture .. ".png";
else
height_button_texture = skin.flat_button_texture .. ".png";
end
local data = {
gui.style_type {
selector = "button,image_button,label",
properties = {
noclip = true,
}
},
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.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",
}
},
};
if markers.count() > 0 then
table.insert(data, gui.container {
x = formspec_width - 0.01,
y = 1,
w = 3.25,
h = 3.875,
bg = skin.marker_bg,
marker_formspec(map:get_marker(player_x, player_z), map.detail, marker_page or 1)});
end
formspec = formspec .. table.concat(data);
minetest.show_formspec(player_name, "cartographer:map", formspec);
end

View File

@ -1,4 +1,4 @@
local marker_lookup = ...;
local marker_lookup = {};
-- Format marker ids to allow their use as formspec element ids.
-- We're mostly concerned with guarding against the : character because it is
@ -29,6 +29,20 @@ local function get_marker(id)
return nil;
end
-- Get the number of registered markers
--
-- Returns the length of the marker table
local function get_marker_count()
return #marker_lookup;
end
-- Get all registered markers
--
-- Returns a copy of the marker table
local function get_registered_markers()
return table.copy(marker_lookup);
end
-- Register a marker with textures to display
-- id: A string containing the id of the marker
-- name: A string containing the displayedname of the marker
@ -75,6 +89,8 @@ end
return {
add = add_marker,
count = get_marker_count,
get = get_marker,
get_all = get_registered_markers,
get_texture = get_marker_texture,
};