cartographer/marker_api.lua

101 lines
2.4 KiB
Lua
Raw Normal View History

2020-06-13 14:01:56 +02:00
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
-- common for ids and has an alternate meaning in formspecs.
2020-06-13 14:50:36 +02:00
--
-- id: The id to format
--
-- Returns the formatted id
local function format_marker_id(id)
return id:gsub(":", "_");
end
-- Find the marker data for a given id
2020-06-13 14:50:36 +02:00
--
-- id: The id to search for
--
-- Returns the marker data, or nil if not found
local function get_marker(id)
if not id then
return nil;
end
id = format_marker_id(id);
for _,marker in pairs(marker_lookup) do
if marker.id == id then
return marker;
end
end
return nil;
end
2020-06-13 14:01:56 +02:00
-- 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
2020-06-13 14:50:36 +02:00
--
-- id: A string containing the id of the marker
-- name: A string containing the displayedname of the marker
-- textures: A table of texture names.
-- These should correspond with detail levels,
-- any detail level past the length of the table will return the last texture
local function add_marker(id, name, textures)
if not id then
return nil;
end
id = format_marker_id(id);
local existing_marker = get_marker(id);
if existing_marker then
existing_marker.name = name;
existing_marker.textures = textures;
else
marker_lookup[#marker_lookup+1] = {
id = id,
name = name,
textures = textures,
};
end
end
-- Get the texture name (minus extension) for the given marker and detail level.
2020-06-13 14:50:36 +02:00
--
-- id: A string containing the marker id
-- detail: The detail level
-- Returns a string with a texture name, or nil if no matching marker was found.
local function get_marker_texture(id, detail)
if not id then
return nil;
end
id = format_marker_id(id);
local marker = get_marker(id);
if marker then
return marker.textures[math.min(detail, #marker.textures)];
end
return nil;
end
return {
add = add_marker,
2020-06-13 14:01:56 +02:00
count = get_marker_count,
get = get_marker,
2020-06-13 14:01:56 +02:00
get_all = get_registered_markers,
get_texture = get_marker_texture,
};