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 { return {
-- Convert world coordinates to map coordinates -- Convert world coordinates to map coordinates
-- coord: The coordinate value -- coord: The coordinate value
--
-- Returns a coordinate value in map space
to = function(coord) to = function(coord)
return math.floor(coord / CHUNK_SIZE); return math.floor(coord / CHUNK_SIZE);
end, end,
-- Convert map coordinates to world coordinates -- Convert map coordinates to world coordinates
-- coord: The coordinate value -- coord: The coordinate value
--
-- Returns a coordinate value in world space
from = function(coord) from = function(coord)
return math.floor(coord * CHUNK_SIZE); return math.floor(coord * CHUNK_SIZE);
end end

View File

@ -29,8 +29,6 @@ local function periodic_save()
end end
minetest.after(60, periodic_save); minetest.after(60, periodic_save);
local marker_lookup = {};
-- Includes -- Includes
local chunk = loadfile(modpath .. "/chunk_api.lua") (); local chunk = loadfile(modpath .. "/chunk_api.lua") ();
local skin = loadfile(modpath .. "/skin_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 scanner = loadfile(modpath .. "/scanner.lua") (map_data, chunk);
local maps = loadfile(modpath .. "/map_api.lua") (map_data, chunk); local maps = loadfile(modpath .. "/map_api.lua") (map_data, chunk);
local materials = loadfile(modpath .. "/material_api.lua") (); 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_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 .. "/commands.lua") (chunk, audio, map_formspec);
loadfile(modpath .. "/table.lua") (gui, skin, audio, maps, materials, map_item); loadfile(modpath .. "/table.lua") (gui, skin, audio, maps, materials, map_item);
loadfile(modpath .. "/autofill.lua") (chunk, scanner, maps); loadfile(modpath .. "/autofill.lua") (chunk, scanner, maps);

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 -- The list of players looking at maps, and the map IDs that they're looking at
local player_maps = {}; local player_maps = {};
@ -11,27 +11,9 @@ local player_maps = {};
-- --
-- Returns a formspec string for use in containers -- Returns a formspec string for use in containers
local function marker_formspec(selected_id, detail, page) local function marker_formspec(selected_id, detail, page)
local marker_lookup = markers.get_all();
local formspec = { 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 { gui.button {
x = 0.125, x = 0.125,
y = 0.125, y = 0.125,
@ -131,7 +113,6 @@ 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) 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); local formspec, formspec_width, _ = map_formspec.from_map(map, player_x, player_z, height_mode);
if #marker_lookup > 0 then
local height_button_texture; local height_button_texture;
if height_mode then if height_mode then
height_button_texture = skin.height_button_texture .. ".png"; height_button_texture = skin.height_button_texture .. ".png";
@ -146,12 +127,16 @@ local function show_map_id_formspec(id, player_x, player_z, player_name, height_
noclip = true, noclip = true,
} }
}, },
gui.container { gui.style_type {
x = formspec_width - 0.01, selector = "button,image_button",
y = 1, properties = {
bg = skin.marker_bg, border = false,
bgimg = skin.marker_button.texture .. ".png",
marker_formspec(map:get_marker(player_x, player_z), map.detail, marker_page or 1), 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 { gui.container {
x = formspec_width - 0.01, x = formspec_width - 0.01,
@ -172,8 +157,19 @@ local function show_map_id_formspec(id, player_x, player_z, player_name, height_
} }
}, },
}; };
formspec = formspec .. table.concat(data);
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 end
formspec = formspec .. table.concat(data);
minetest.show_formspec(player_name, "cartographer:map", formspec); minetest.show_formspec(player_name, "cartographer:map", formspec);
end end

View File

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