This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
wait.lua
128 lines (113 loc) · 3.59 KB
/
wait.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
local WAIT={
state=false,
timer=false,
totalTimer=false,
enterTime=.2,
leaveTime=.2,
timeout=6,
coverColor={.1,.1,.1},
coverAlpha=.6,
arg=false,
}
local arcAlpha={1,.6,.4,.3}
local function defaultDraw(a,t)
GC.setLineWidth(SCR.h/26)
t=t*2.6
for i=1,4 do
GC.setColor(1,1,1,a*arcAlpha[i])
GC.arc('line','open',SCR.w/2,SCR.h/2,SCR.h/5,t+MATH.tau*(i/4),t+MATH.tau*((i+1)/4))
end
end
function WAIT.new(arg)
if WAIT.state then return end
assert(type(arg)=='table',"arg must be table")
assert(arg.init==nil or type(arg.init) =='function',"Field 'enter' must be function")
assert(arg.update==nil or type(arg.update) =='function',"Field 'update' must be function")
assert(arg.quit==nil or type(arg.quit) =='function',"Field 'leave' must be function")
assert(arg.draw==nil or type(arg.draw) =='function',"Field 'draw' must be function")
assert(arg.escapable==nil or type(arg.escapable) =='boolean', "Field 'escapable' must be boolean")
if arg.init then arg.init() end
WAIT.arg=arg
WAIT.state='enter'
WAIT.timer=0
WAIT.totalTimer=0
end
function WAIT.interrupt()
if WAIT.state and WAIT.state~='leave' then
WAIT.state='leave'
WAIT.timer=WAIT.leaveTime*WAIT.timer/WAIT.enterTime
end
end
function WAIT.update(dt)
if WAIT.state then
WAIT.totalTimer=WAIT.totalTimer+dt
if WAIT.arg.update then WAIT.arg.update(dt) end
if WAIT.state~='leave' and WAIT.totalTimer>=(WAIT.arg.timeout or WAIT.timeout) then
WAIT.interrupt()
end
if WAIT.state=='enter' then
WAIT.timer=math.min(WAIT.timer+dt,WAIT.enterTime)
if WAIT.timer>=WAIT.enterTime then WAIT.state='wait' end
elseif WAIT.state=='leave' then
WAIT.timer=WAIT.timer-dt
if WAIT.timer<=0 then
WAIT.state=false
if WAIT.arg.quit then WAIT.arg.quit() end
end
end
end
end
function WAIT.draw()
if WAIT.state then
local alpha=(
WAIT.state=='enter' and WAIT.timer/WAIT.enterTime or
WAIT.state=='wait' and 1 or
WAIT.state=='leave' and WAIT.timer/WAIT.leaveTime
)
GC.setColor(
WAIT.coverColor[1],
WAIT.coverColor[2],
WAIT.coverColor[3],
alpha*WAIT.coverAlpha
)
GC.rectangle('fill',0,0,SCR.w,SCR.h);
(WAIT.arg.draw or defaultDraw)(alpha,WAIT.totalTimer)
end
end
function WAIT.setEnterTime(t)
assert(type(t)=='number' and t>0,"Arg #1 must be number larger then 0")
WAIT.enterTime=t
end
function WAIT.setLeaveTime(t)
assert(type(t)=='number' and t>0,"Arg #1 must be number larger then 0")
WAIT.leaveTime=t
end
function WAIT.setTimeout(t)
assert(type(t)=='number' and t>0,"Arg #1 must be number larger then 0")
WAIT.timeout=t
end
function WAIT.setCoverColor(r,g,b)
if type(r)=='table' then
r,g,b=r[1],r[2],r[3]
end
if
type(r)=='number' and r>=0 and r<=1 and
type(g)=='number' and g>=0 and g<=1 and
type(b)=='number' and b>=0 and b<=1
then
WAIT.coverColor[1],WAIT.coverColor[2],WAIT.coverColor[3]=r,g,b
else
error("Arg must be r,g,b or {r,g,b}")
end
end
function WAIT.setCoverAlpha(a)
assert(type(a)=='number',"Arg #1 must be number between 0~1")
end
function WAIT.setDefaultDraw(f)
assert(type(f)=='function',"Arg #1 must be function")
defaultDraw=f
end
setmetatable(WAIT,{__call=function(self,arg)
self.new(arg)
end,__metatable=true})
return WAIT