forked from info-beamer/package-conference-room
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.lua
More file actions
217 lines (180 loc) · 5.4 KB
/
tools.lua
File metadata and controls
217 lines (180 loc) · 5.4 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
-------------------------------------------------------------------------------
--- generic tools
-- register self
local tools = rawget(_G, "tools")
if not tools then
tools = {}
rawset(_G, "tools", tools)
end
local _autoextendmeta0 =
{
__index = function(t, k)
local ret = {}
t[k] = ret
return ret
end
}
local _autoextendmeta1 =
{
__index = function(t, k)
local ret = setmetatable({}, _autoextendmeta0)
t[k] = ret
return ret
end
}
function tools.newAutoExtendTable()
return setmetatable({}, _autoextendmeta1)
end
function tools.createLookupTable(list)
local lut = {}
for _, element in pairs(list) do
lut[element.id] = element
end
return lut
end
function tools.fixAspect()
gl.scale(1 / (DISPLAY_ASPECT / NATIVE_ASPECT), 1)
end
function tools.RelPosToScreen(x, y)
return x * DISPLAY_WIDTH, y and y * DISPLAY_HEIGHT
end
function tools.RelSizeToScreen(sz)
return sz * DISPLAY_HEIGHT
end
function tools.ScreenPosToRel(x, y)
return x / DISPLAY_WIDTH, y and y / DISPLAY_HEIGHT
end
function tools.ScreenSizeToRel(sz)
return sz / DISPLAY_HEIGHT
end
-- takes sz in resolution-independent coords
-- sz == 0.5 -> half as high as the screen
function tools.textWidth(font, text, sz)
local h = tools.RelSizeToScreen(sz)
local w = font:width(text, h)
return tools.ScreenPosToRel(w)
end
function tools.timeColonOffset(font, time, sz)
local hours, _ = time:match("([-%d]+):([-%d]+)")
if not hours then hours = '--' end
local wh = tools.textWidth(font, hours, sz)
local wc = tools.textWidth(font, ":", sz)
return wh + (wc * 0.5)
end
-- takes x, y, sz in resolution-independent coords
-- (0, 0) = upper left corner, (1, 1) = lower right corner
-- sz == 0.5 -> half as high as the screen
function tools.drawText(font, x, y, text, sz, fgcol, bgcol, padding)
local xS, yS = tools.RelPosToScreen(x, y)
local h = tools.RelSizeToScreen(sz)
if bgcol and bgcol.a > 0 and padding then
local w = font:width(text, h)
local b = tools.RelSizeToScreen(padding)
local bgtex = tools.getColorTex(bgcol)
bgtex:draw(xS-b, yS-b, xS+w+b, yS+h+b)
end
font:write(xS, yS, text, h, fgcol:rgba())
end
local SHADOW = resource.create_colored_texture(0,0,0, 0.1)
-- takes x1, y1, x2, y2 in resolution-independent coords
-- (0, 0) = upper left corner, (1, 1) = lower right corner
function tools.drawResource(res, x1, y1, x2, y2)
gl.pushMatrix()
gl.scale(DISPLAY_WIDTH, DISPLAY_HEIGHT)
-- SHADOW:draw(x1, y1, x2, y2)
res:draw(x1, y1, x2, y2)
gl.popMatrix()
end
function tools.isScheduleActive(schedule)
if type(schedule) == "string" then
if schedule == "always" then
return true
end
return false
end
if schedule['repeat'].count ~= 1 then
tools.debugPrint(1, "WARNING: schedules with repeat.count > 1 are not supported!")
return false
end
local startts = device.getTimestampOfDate(schedule.start)
if startts == nil then
tools.debugPrint(1, "WARNING: no timestamp found for schedule start date: " .. schedule.start)
return false
end
local nowts = math.floor(device.getWallClockTime())
for _, span in ipairs(schedule.spans) do
local begints = startts + (span[1]*60)
local endts = startts + (span[2]*60)
if begints <= nowts and nowts <= endts then
return true
end
end
return false
end
function tools.debugPrint(lvl, ...)
if _DEBUG_ and _DEBUG_ >= lvl then
print(...)
end
end
function tools.debugDraw(lvl, draw, ...)
if _DEBUG_ and _DEBUG_ >= lvl then
draw(...)
end
end
-- return colored single-pixel texture with caching
local _colorTex = setmetatable({}, { __mode = "kv" })
function tools.getColorTex(col)
assert(col, "COLOR MISSING")
local tex = _colorTex[col]
if not tex then
tex = resource.create_colored_texture(col.rgba())
_colorTex[col] = tex
end
assert(tex, "OOPS - TEX MISSING")
return tex
end
function tools.clearColorTex()
table.clear(_colorTex)
end
-------------------------------------------------------------------------------
--- extend string class
function string.fwrap(str, font, sz, max)
local h = tools.RelSizeToScreen(sz)
local wAvail = tools.RelPosToScreen(max)
str = str:match("(.-)%\n*$") -- kill trailing newlines
local x = 0
local x0 = x
-- always allow wrapping after punctuation chars
local wrapped = str:gsub("(%s*)([^%s%-%,%.%;%:%/]*[%-%,%.%;%:%/]*)", function(sp, word)
local ws = font:width(sp, h)
local ww = font:width(word, h)
x = x + ws + ww
if x > wAvail or sp:find("\n", 1, true) then -- always wrap when there's a newline
x = x0 + ww
return "\n"..word
end
end)
local splitted = {}
for token in string.gmatch(wrapped, "[^\n]+") do
splitted[#splitted + 1] = token
end
return splitted
end
-------------------------------------------------------------------------------
--- extend table class
function table.shallowcopy(t)
local tt = {}
for k, v in pairs(t) do
tt[k] = v
end
return tt
end
function table.clear(t)
for k in pairs(t) do
t[k] = nil
end
return t
end
-------------------------------------------------------------------------------
print("tools.lua loaded completely")
return tools