summaryrefslogtreecommitdiff
path: root/init.lua
blob: 338800f72e6267c482722ff87a6b4d51ad35ac72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
local sdl = require("SDL")
sdl.init({sdl.flags.video})
local windows = {}
local renderers = {}

local function drawrect(renderer,x,y,color,scale)
	x = x - 1
	y = y - 1
	local rect = {x=x*scale,y=y*scale,w=scale,h=scale}
	if string.sub(color,1,1) == "#" then color = string.sub(color,2,-1) end
	renderer:setDrawColor(tonumber(color,16))
	renderer:fillRect(rect)
end

minetest.register_node("sdl_display:display",{
	description = "SDL Display",
	groups = {dig_immediate=2},
	tiles = {"sdl_display_sides.png"},
	digiline = 
	{
		receptor = {},
		effector = {
			action = function(pos,node,channel,msg)
				local meta = minetest.get_meta(pos)
				if (not msg) or channel ~= meta:get_string("channel") then
					return
				end
				local w = meta:get_int("width")
				local h = meta:get_int("height")
				local scale = meta:get_int("scale")
				local hash = minetest.hash_node_position(pos)
				if not windows[hash] then
					windows[hash] = sdl.createWindow({
						title = meta:get_string("windowtitle"),
						width = w*scale,
						height = h*scale,
					})
					renderers[hash] = sdl.createRenderer(windows[hash],0,0)
				end
				renderers[hash]:clear()
				for y=1,h,1 do
					if type(msg[y]) == "table" then
						for x=1,w,1 do
							if type(msg[y][x]) =="string" and string.len(msg[y][x]) >= 6 then
								drawrect(renderers[hash],x,y,msg[y][x],scale)
							end
						end
					end
				end
				renderers[hash]:present()
			end,
		},
	},
	on_destruct = function(pos)
		local hash = minetest.hash_node_position(pos)
		if windows[hash] then windows[hash]:hide() end
		windows[hash] = nil
		renderers[hash] = nil
	end,
	after_place_node = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("channel","display")
		meta:set_int("width",10)
		meta:set_int("height",10)
		meta:set_string("windowtitle","Display")
		meta:set_int("scale",40)
		local fs = "size[3,6.5]"
		fs = fs.."field[0,0.5;3,1;channel;Channel;${channel}]"
		fs = fs.."field[0,1.5;3,1;title;Window Title;${windowtitle}]"
		fs = fs.."field[0,2.5;3,1;width;Width;${width}]"
		fs = fs.."field[0,3.5;3,1;height;Height;${height}]"
		fs = fs.."field[0,4.5;3,1;scale;Scale;${scale}]"
		fs = fs.."button[0,5.5;3,1;save;Save]"
		meta:set_string("formspec",fs)
		meta:set_string("infotext","SDL Display (\"Display\")")
	end,
	on_receive_fields = function(pos,_,fields)
		if not fields.save then return end
		local meta = minetest.get_meta(pos)
		meta:set_string("channel",fields.channel)
		meta:set_string("windowtitle",fields.title)
		meta:set_int("width",fields.width)
		meta:set_int("height",fields.height)
		meta:set_int("scale",fields.scale)
		meta:set_string("infotext",string.format("SDL Display (\"%s\")",fields.title))
		local hash = minetest.hash_node_position(pos)
		if windows[hash] then windows[hash]:hide() end
		windows[hash] = nil
		renderers[hash] = nil
	end,
})