-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.lua
135 lines (117 loc) · 4.16 KB
/
node.lua
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
gl.setup(NATIVE_WIDTH, NATIVE_HEIGHT)
--
util.init_hosted()
local json = require "json"
local easing = require "easing"
local font = resource.load_font "Ubuntu-C.ttf"
local content = {["loaded"] = false}
local content_res = {}
util.auto_loader(content_res)
local current_page = 1
local font_size = 100
local margin = 50
local secs_per_page = 10
local logo = resource.create_colored_texture(0,0,0,0)
local white = resource.create_colored_texture(1,1,1,1)
local magenta = resource.create_colored_texture(226/255, 0/255, 116/255, 1) -- #e20074
local background_color = {0.5, 0.5, 0.5, 1}
util.json_watch("config.json", function(config)
font = resource.load_font(config.headline_font.asset_name)
logo = resource.load_image(config.logo.asset_name)
background_color = config.background_color.rgba
end)
util.json_watch("content.json", function(c)
content = c
if content.delay then
secs_per_page = content.delay
end
end)
util.data_mapper{
["device_info"] = function(info)
info = json.decode(info)
location = info.location
description = info.description
end,
["content"] = function(info)
end
}
local function draw_text(text, align)
local l = font:width(text, font_size)
-- horizonal alignment: 50px from the left, then 0, 1, or 2 times the
-- half the remaining width minus the length of the string
local xa = math.floor(math.fmod(align-1, 3)) / 2
local x = margin + xa * (HEIGHT - 2*margin - l)
-- vertical alignment: 50px from top, then 0, 1, or 2 times half the height
local ya = math.floor((align-1) / 3) / 2
local y = margin + ya * (WIDTH - 2*margin - font_size)
font:write(x, y, text, font_size, 1,1,1,1)
-- magenta:draw(x, y, x+2, y+font_size)
-- magenta:draw(x+l, y, x+l-2, y+font_size)
-- magenta:draw(x, y, x+l, y+2)
-- magenta:draw(x, y+font_size-2, x+l, y+font_size)
-- font:write(margin, 150, "align="..align..",x="..tostring(x)..",y="..tostring(y), 50, 1,1,1,1)
-- font:write(margin, 210, "l="..tostring(l)..",xa="..tostring(xa)..",ya="..tostring(ya), 50, 1,1,1,1)
end
local function draw_price(text, tween)
local s = font_size*2
local l = font:width(text, s)
--local x = (HEIGHT+l*2)*(1-tween)-l
local b = HEIGHT
local x = easing.outInCubic(tween, b, -l - b, 1)
--white:draw(x, (WIDTH+s)/3*2, x+10, (WIDTH+s)/3*2+10)
font:write(x, (WIDTH+s)/3*2, text, s, 226/255, 0/255, 116/255, 1)
end
local function draw_page()
if not content["loaded"] then
font:write(50, 50, "Waiting for content...", 50, 1,1,1,1)
return
end
current_page = math.floor(math.fmod(sys.now()/secs_per_page, #content["pages"]) + 1)
tween = math.fmod(sys.now(), secs_per_page) / secs_per_page
--font:write(50, 50, "page " .. tostring(current_page), 50, 1,1,1,1)
local r
if tween <= 0.1 then
r = -90 + tween * 900
elseif tween >= 0.9 then
r = (tween - 0.9) * 900
else
r = 0
end
gl.translate(HEIGHT/2, 0)
gl.rotate(r, 0, 1, 0)
gl.translate(-HEIGHT/2, 0)
if content_res[content["pages"][current_page]["image_name"]] then
content_res[content["pages"][current_page]["image_name"]]:draw(0, 0, HEIGHT, WIDTH)
-- magenta:draw(50, 1400, HEIGHT-350, WIDTH-50, 0.5)
-- magenta:draw(50, 1500, HEIGHT-50, WIDTH-50, 0.5)
-- magenta:draw(50, 1620, HEIGHT-250, WIDTH-50)
local p = content["pages"][current_page]
draw_text(p["teaser_text"], p["align"])
draw_price(p["price"], tween)
-- local i = 0
-- for k, v in string.gmatch(content["pages"][current_page]["teaser_text"], "([^|\n]+)") do
-- font:write(80, 1660 + 90*i, k, 100, 1,1,1,1)
-- i = i + 1
-- end
else
font:write(50, 50, "Image "..content["pages"][current_page]["image_name"].." not yet loaded", 50, 1,1,1,1)
end
end
local function draw_logo()
local w = 480 / 3 * 2
local h = 518 / 3 * 2
local x = 50
local y = (WIDTH - h) / 2
logo:draw(x, y, x+w, y+h)
end
local fov = math.atan2(HEIGHT, WIDTH*2) * 360 / math.pi
function node.render()
gl.clear(unpack(background_color))
gl.perspective(fov, WIDTH/2, HEIGHT/2, -WIDTH,
WIDTH/2, HEIGHT/2, 0)
-- turn to portrait
gl.rotate(270, 0, 0, 1)
gl.translate(-HEIGHT, 0)
draw_logo()
draw_page()
end