Small refactor: Add some constants to map_formspec.lua

- Make map formspec calls return the formspec dimensions for more
  accurate marker editor palcement
This commit is contained in:
Hugues Ross 2020-04-17 20:28:15 -04:00
parent c0e1414a88
commit af0a801801
2 changed files with 49 additions and 15 deletions

View File

@ -17,9 +17,9 @@ local function show_map_id_formspec(id, player_x, player_z, player_name, marker_
local map = cartographer.get_map(id);
player_x, player_z = cartographer.to_map_coordinates(map, player_x, player_z)
local formspec = cartographer.get_map_formspec_map(data, map, player_x, player_z);
local formspec, formspec_width, formspec_height = cartographer.get_map_formspec_map(data, map, player_x, player_z);
if #_cartographer.marker_lookup > 0 then
formspec = formspec .. string.format("style_type[button,image_button,label;noclip=true] container[%f,0.5]", map.w * 0.25) -- TODO: Don't assume map.w * 0.25
formspec = formspec .. string.format("style_type[button,image_button,label;noclip=true] container[%f,0.5]", formspec_width - 0.01)
.. _cartographer.generate_marker_formspec(cartographer.get_marker(map, player_x, player_z), map.detail, marker_page or 1, cartographer.skin.marker_bg, cartographer.skin.marker_button)
.. "container_end[]";
end

View File

@ -1,4 +1,5 @@
local scale = 0.25;
local TILE_SIZE = 0.25;
local TILE_OFFSET = 0.24; -- Slightly smaller than TILE_SIZE. We overlap tiles slightly to minimize seams
local variant_odds = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4 };
local function get_variant(random)
@ -16,8 +17,13 @@ local tile = "image[%f,%f;%f,%f;%s]";
local marker = "animated_image[%f,%f;%f,%f;;%s;%d;%d]";
-- Generate formspec markup for an unknown biome tile
local function unknown_biome_tile(x, y, scale, variant)
return tile:format(x, y, scale, scale, cartographer.skin.unknown_biome_texture .. "." .. tostring(variant) .. ".png");
-- x: The x position of the tile
-- y: The y position of the tile
-- variant: The tile variant
--
-- Returns a formspec string
local function unknown_biome_tile(x, y, variant)
return tile:format(x, y, TILE_SIZE, TILE_SIZE, cartographer.skin.unknown_biome_texture .. "." .. tostring(variant) .. ".png");
end
-- Generate formspec markup for a given map
@ -42,12 +48,12 @@ local function generate_map(data, x, y, w, h, player_x, player_y, detail, map_sc
player_y = math.floor(player_y / map_scale + 0.5);
for i = x,x + w,1 do
local fx = (i - x) * (scale - 0.01);
local fx = (i - x) * TILE_OFFSET;
local column = data.generated[i * map_scale];
for j = y + h,y,-1 do
local fy = (y + h - j) * (scale - 0.01);
local fy = (y + h - j) * TILE_OFFSET;
if column == nil or column[j * map_scale] == nil or (is_visible and not is_visible(user, i, j)) then
str = str .. unknown_biome_tile(fx, fy, scale, get_variant(random));
str = str .. unknown_biome_tile(fx, fy, get_variant(random));
else
local name = minetest.get_biome_name(column[j * map_scale].biome);
local height = column[j * map_scale].height;
@ -62,29 +68,29 @@ local function generate_map(data, x, y, w, h, player_x, player_y, detail, map_sc
mod = "^[colorize:white:"..tostring(height * 10)
height = height * 0.05;
str = str..tile:format(fx, fy - height + (scale - 0.01), scale, height + 0.01, cartographer.skin.cliff_texture .. ".png");
str = str..tile:format(fx, fy - height + TILE_OFFSET, TILE_SIZE, height + 0.01, cartographer.skin.cliff_texture .. ".png");
elseif depth > 0 then
mod = "^[colorize:#1f1f34:"..tostring(depth * 10)
end
str = str..tile:format(fx, fy - height, scale, scale, biome .. "." .. tostring(get_variant(random)) .. ".png" .. mod)
str = str..tile:format(fx, fy - height, TILE_SIZE, TILE_SIZE, biome .. "." .. tostring(get_variant(random)) .. ".png" .. mod)
if get_marker then
local marker = cartographer.get_marker_texture(get_marker(user, i, j), detail);
if marker then
str = str..tile:format(fx, fy - height, scale, scale, marker .. ".png")
str = str..tile:format(fx, fy - height, TILE_SIZE, TILE_SIZE, marker .. ".png")
end
end
if i == player_x and j == player_y then
str = str..marker:format(fx, fy - height,
scale, scale,
TILE_SIZE, TILE_SIZE,
cartographer.skin.player_icon.texture .. ".png",
cartographer.skin.player_icon.frame_count,
cartographer.skin.player_icon.frame_duration);
end
else
str = str .. unknown_biome_tile(fx, fy, scale, get_variant(random));
str = str .. unknown_biome_tile(fx, fy, get_variant(random));
end
end
end
@ -93,10 +99,38 @@ local function generate_map(data, x, y, w, h, player_x, player_y, detail, map_sc
return str;
end
-- Get the formspec for a given map segment
-- data: The map data
-- x: The X position of the map, in map coordinates
-- y: The Y position of the map, in map coordinates
-- w: The width of the map, in map coordinates
-- h: The height of the map, in map coordinates
-- detail: The detail level of the map
--
-- Returns a formspec string, the width of the formspec, and the height of the
-- formspec
-- TODO: Remove data argument
function cartographer.get_map_formspec(data, x, y, w, h, detail)
return map_formspec_prefix:format(w * scale, h * scale)..generate_map(data, x - (w * 0.5), y - (h * 0.5), w, h, x, y, detail, 1);
local formspec_width = (w + 1) * TILE_OFFSET + 0.01;
local formspec_height = (h + 1) * TILE_OFFSET + 0.01;
return map_formspec_prefix:format(formspec_width, formspec_height)..generate_map(data, x - (w * 0.5), y - (h * 0.5), w, h, x, y, detail, 1),
formspec_width,
formspec_height;
end
-- Get the formspec for a given map table
-- data: The map data
-- map: The map to use
-- x: The X position of the player marker, in map coordinates
-- y: The Y position of the player marker, in map coordinates
--
-- Returns a formspec string, the width of the formspec, and the height of the
-- formspec
-- TODO: Remove data argument
function cartographer.get_map_formspec_map(data, map, x, y)
return map_formspec_prefix:format(map.w * scale, map.h * scale)..generate_map(data, map.x, map.z, map.w, map.h, x, y, map.detail, map.scale, cartographer.is_filled, cartographer.get_marker, map);
local formspec_width = (map.w + 1) * TILE_OFFSET + 0.01;
local formspec_height = (map.h + 1) * TILE_OFFSET + 0.01;
return map_formspec_prefix:format(formspec_width, formspec_height)..generate_map(data, map.x, map.z, map.w, map.h, x, y, map.detail, map.scale, cartographer.is_filled, cartographer.get_marker, map),
formspec_width,
formspec_height;
end