forked from info-beamer/package-scheduled-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.lua
100 lines (85 loc) · 2.23 KB
/
helper.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
local M = {}
local easing = require "easing"
local min, max, abs, floor = math.min, math.max, math.abs, math.floor
function M.in_epsilon(a, b, e)
return abs(a - b) <= e
end
function M.ramp(t_s, t_e, t_c, ramp_time)
if ramp_time == 0 then return 1 end
local delta_s = t_c - t_s
local delta_e = t_e - t_c
return min(1, delta_s * 1/ramp_time, delta_e * 1/ramp_time)
end
function M.wait_frame()
return coroutine.yield(true)
end
function M.wait_t(t)
local now = sys.now()
if now >= t then
return now
end
while true do
local now = M.wait_frame()
if now >= t then
return now
end
end
end
function M.frame_between(starts, ends)
return function()
local now
while true do
now = M.wait_frame()
if now >= starts then
break
end
end
if now < ends then
return now
end
end
end
function M.mktween(fn)
return function(sx1, sy1, sx2, sy2, ex1, ey1, ex2, ey2, progress)
return fn(progress, sx1, ex1-sx1, 1),
fn(progress, sy1, ey1-sy1, 1),
fn(progress, sx2, ex2-sx2, 1),
fn(progress, sy2, ey2-sy2, 1)
end
end
M.movements = {
linear = M.mktween(easing.linear),
smooth = M.mktween(easing.inOutQuint),
}
function M.trim(s)
return s:match "^%s*(.-)%s*$"
end
function M.split(str, delim)
local result, pat, last = {}, "(.-)" .. delim .. "()", 1
for part, pos in string.gmatch(str, pat) do
result[#result+1] = part
last = pos
end
result[#result+1] = string.sub(str, last)
return result
end
function M.wrap(str, limit, indent, indent1)
limit = limit or 72
local here = 1
local wrapped = str:gsub("(%s+)()(%S+)()", function(sp, st, word, fi)
if fi-here > limit then
here = st
return "\n"..word
end
end)
local splitted = {}
for token in string.gmatch(wrapped, "[^\n]+") do
splitted[#splitted + 1] = token
end
return splitted
end
function M.parse_rgb(hex)
hex = hex:gsub("#","")
return tonumber("0x"..hex:sub(1,2))/255, tonumber("0x"..hex:sub(3,4))/255, tonumber("0x"..hex:sub(5,6))/255
end
return M