aboutsummaryrefslogtreecommitdiff
path: root/advtrains_trackmap
diff options
context:
space:
mode:
authororwell96 <orwell@bleipb.de>2023-05-24 20:35:18 +0200
committerorwell96 <orwell@bleipb.de>2023-05-27 12:26:16 +0200
commit7ca8ac8d00ed8b6e707f932cc012c1eb4ace1932 (patch)
tree7889e29bb728d162ea59096127f0a0b66a10ecec /advtrains_trackmap
parent8371d8496a0423d992c29f38c59265a5d6f8517a (diff)
downloadadvtrains-7ca8ac8d00ed8b6e707f932cc012c1eb4ace1932.tar.gz
advtrains-7ca8ac8d00ed8b6e707f932cc012c1eb4ace1932.tar.bz2
advtrains-7ca8ac8d00ed8b6e707f932cc012c1eb4ace1932.zip
Improve map formspec rendering
Diffstat (limited to 'advtrains_trackmap')
-rw-r--r--advtrains_trackmap/fsrender.lua24
-rw-r--r--advtrains_trackmap/grid.lua22
-rw-r--r--advtrains_trackmap/viewer.lua9
3 files changed, 45 insertions, 10 deletions
diff --git a/advtrains_trackmap/fsrender.lua b/advtrains_trackmap/fsrender.lua
index 607f3cd..340b08c 100644
--- a/advtrains_trackmap/fsrender.lua
+++ b/advtrains_trackmap/fsrender.lua
@@ -3,15 +3,32 @@
local tm = advtrains.trackmap
-function tm.render_grid(grid, origin_pos, width, height)
- local s = {"label[0,0;"}
+function tm.render_grid_formspec(formsize_x, formsize_y, gridtbl, origin_pos, width, height)
+ local grid = gridtbl.grid
+ local gcolor = gridtbl.gcolor
+ local s = {
+ "formspec_version[3]",
+ "size["..formsize_x..","..formsize_y.."]",
+ "no_prepend[]",
+ "bgcolor[white;false;white]",
+ "style_type[label;font=mono]",
+ "label[0,0;",
+ minetest.get_color_escape_sequence("black")
+ }
+ local last_color = nil
for z=height-1, 0, -1 do
-- render a row
for x=0,width-1 do
local apos_x = origin_pos.x + x
local apos_z = origin_pos.z + z
- local chr = "░"
+ local chr = " "
if grid[apos_x] and grid[apos_x][apos_z] then
+ local color = gcolor[apos_x][apos_z]
+ if color ~= last_color then
+ -- change the color of the text
+ table.insert(s, minetest.get_color_escape_sequence(color or "black"))
+ last_color = color
+ end
chr = grid[apos_x][apos_z]
end
table.insert(s, chr)
@@ -19,5 +36,6 @@ function tm.render_grid(grid, origin_pos, width, height)
table.insert(s,"\n")
end
table.insert(s, "]")
+ table.insert(s, "style_type[label;font=]") -- reset font style
return table.concat(s)
end \ No newline at end of file
diff --git a/advtrains_trackmap/grid.lua b/advtrains_trackmap/grid.lua
index d679bd6..4c98b2f 100644
--- a/advtrains_trackmap/grid.lua
+++ b/advtrains_trackmap/grid.lua
@@ -33,7 +33,7 @@ local dir_to_charmap = {
[14] = 4,
}
function tm.rotate_char_class_by_conn(chr, conndir)
- atdebug("rotatechar", chr, conndir, "dircharmap", dir_to_charmap[conndir], "charequiv", char_equiv[chr])
+ --atdebug("rotatechar", chr, conndir, "dircharmap", dir_to_charmap[conndir], "charequiv", char_equiv[chr])
return char_equiv[chr][dir_to_charmap[conndir]]
end
@@ -43,6 +43,7 @@ end
-- should return a table as follows:
-- {
-- char = "X" -- the character to use in this place, defaults to guessing a fitting frame character
+-- color = "colorstring" or nil -- the color to render the character with
-- stop = false -- if true, the iterator will stop following this branch
-- }
-- Returning nil will assume defaults.
@@ -55,6 +56,8 @@ end
function tm.generate_grid_map(start_pos, node_callback)
local ti = advtrains.get_track_iterator(start_pos, nil, TS_MAX_SCAN, true)
local grid = {} -- grid[x][y]
+ local gcolor = {} -- grid[x][y]
+ local gylev = {} -- grid[x][y]
local gminx, gmaxx, gminz, gmaxz = start_pos.x, start_pos.x, start_pos.z, start_pos.z
while ti:has_next_branch() do
@@ -63,7 +66,7 @@ function tm.generate_grid_map(start_pos, node_callback)
local ok, conns = advtrains.get_rail_info_at(pos)
local c = node_callback(pos, conns, connid)
local chr = c and c.char
- atdebug("init",pos.x,pos.z,chr,"")
+ --atdebug("init",pos.x,pos.z,chr,"")
if not chr then
chr = tm.rotate_char_class_by_conn("=", conns[connid].c)
end
@@ -71,9 +74,19 @@ function tm.generate_grid_map(start_pos, node_callback)
-- add the char to the grid
if not grid[pos.x] then
grid[pos.x] = {}
+ gcolor[pos.x] = {}
+ gylev[pos.x] = {}
+ end
+
+ -- ensure that higher rails are rendered on top
+ local prev_ylev = gylev[pos.x][pos.z]
+ if not prev_ylev or prev_ylev < pos.y then
+ grid[pos.x][pos.z] = chr
+ gylev[pos.x][pos.z] = pos.y
+ if c and c.color then
+ gcolor[pos.x][pos.z] = c.color
+ end
end
- atdebug("final",pos.x,pos.z,chr,"")
- grid[pos.x][pos.z] = chr
gminx = math.min(gminx, pos.x)
gmaxx = math.max(gmaxx, pos.x)
@@ -86,6 +99,7 @@ function tm.generate_grid_map(start_pos, node_callback)
end
return {
grid = grid,
+ gcolor = gcolor,
min_pos = {x=gminx, z=gminz},
max_pos = {x=gmaxx, z=gmaxz},
}
diff --git a/advtrains_trackmap/viewer.lua b/advtrains_trackmap/viewer.lua
index 7752991..b7205c8 100644
--- a/advtrains_trackmap/viewer.lua
+++ b/advtrains_trackmap/viewer.lua
@@ -10,14 +10,17 @@ local function node_left_click(pos, pname)
return
end
- local function node_callback()
+ local function node_callback(npos, conns, connid)
+ if vector.equals(pos, npos) then
+ return {color = "red"}
+ end
return nil
end
local gridtbl = tm.generate_grid_map(pos, node_callback)
- local fslabel = tm.render_grid(gridtbl.grid, {x=pos.x-30, z=pos.z-25}, 60, 50)
+ local fslabel = tm.render_grid_formspec(20, 20, gridtbl, {x=pos.x-35, z=pos.z-22}, 70, 44)
- minetest.show_formspec(pname, "advtrains_trackmap:test", "size[20,20]"..fslabel)
+ minetest.show_formspec(pname, "advtrains_trackmap:test", fslabel)
end