-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtext.lua
More file actions
220 lines (209 loc) · 5.48 KB
/
text.lua
File metadata and controls
220 lines (209 loc) · 5.48 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
218
219
220
if not love.graphics then
LOG("TEXT lib is not loaded (need love.graphics)")
return setmetatable({},{
__index=function(_,k)
error("attempt to use TEXT."..k..", but TEXT lib is not loaded (need love.graphics)")
end,
})
end
---@class Zenitha.TextAnimArg
---@field text? string
---@field x? number
---@field y? number
---@field k? number
---@field kx? number
---@field ky? number
---@field align? Zenitha.TextAlignMode | {[1]:number, [2]:number}
---@field textalign? 'center' | 'left' | 'right'
---@field warplimit? number
---@field r? number
---@field g? number
---@field b? number
---@field a? number
---@field color? number[] | Zenitha.ColorStr
---@field fontSize? number
---@field fontType? string
---@field duration? number
---@field inPoint? number
---@field outPoint? number
---@field style? Zenitha.TextAnimStyle
---@field styleArg? any
local setColor=GC.setColor
local draw=GC.draw
local floor,rnd=math.floor,math.random
local max,min=math.max,math.min
local ins,rem=table.insert,table.remove
---@enum (key) Zenitha.TextAnimStyle
local textFX={
appear=function(T)
setColor(T.r,T.g,T.b,T.a*min(T._t/T.inPoint,1)*min((1-T._t)/T.outPoint,1))
draw(
T.text,T.x,T.y,
nil,
T.kx,T.ky,
T._ox,T._oy
)
end,
fly=function(T)
setColor(T.r,T.g,T.b,T.a*min(T._t/T.inPoint,1)*min((1-T._t)/T.outPoint,1))
draw(
T.text,T.x+(T._t-.5)^3*(T.arg or 300),T.y,
nil,
T.kx,T.ky,
T._ox,T._oy
)
end,
stretch=function(T)
setColor(T.r,T.g,T.b,T.a*min(T._t/T.inPoint,1)*min((1-T._t)/T.outPoint,1))
draw(
T.text,T.x,T.y,
nil,
(max(1-T._t/T.inPoint,0)*(T.arg or 1)+1)*T.kx,T.ky,
T._ox,T._oy
)
end,
drive=function(T)
setColor(T.r,T.g,T.b,T.a*min(T._t/T.inPoint,1)*min((1-T._t)/T.outPoint,1))
draw(
T.text,T.x,T.y,
nil,
T.kx,T.ky,
T._ox,T._oy,
(max(1-T._t/T.inPoint,0)*(T.driveX or 2)) or 0,0
)
end,
spin=function(T)
setColor(T.r,T.g,T.b,T.a*min(T._t/T.inPoint,1)*min((1-T._t)/T.outPoint,1))
draw(
T.text,T.x,T.y,
(max(1-T._t/T.inPoint,0)^2-max(1-(1-T._t)/T.outPoint,0)^2)*(T.arg or .4),
T.kx,T.ky,
T._ox,T._oy
)
end,
flicker=function(T)
setColor(T.r,T.g,T.b,T.a*min(T._t/T.inPoint,1)*min((1-T._t)/T.outPoint,1)*rnd())
draw(
T.text,T.x,T.y,
nil,
T.kx,T.ky,
T._ox,T._oy
)
end,
zoomout=function(T)
setColor(T.r,T.g,T.b,T.a*min(T._t/T.inPoint,1)*min((1-T._t)/T.outPoint,1))
local k=T._t^.5*(T.arg or .1)+1
draw(
T.text,T.x,T.y,
nil,
k*T.kx,k*T.ky,
T._ox,T._oy
)
end,
beat=function(T)
setColor(T.r,T.g,T.b,T.a*min(T._t/T.inPoint,1)*min((1-T._t)/T.outPoint,1))
local k=1+(T.arg or .5)*max(1-T._t/T.inPoint,0)^.6
draw(
T.text,T.x,T.y,
nil,
k*T.kx,k*T.ky,
T._ox,T._oy
)
end,
score=function(T)
setColor(T.r,T.g,T.b,T.a*min(T._t/T.inPoint,1)*min((1-T._t)/T.outPoint,1))
draw(
T.text,T.x,T.y-T._t^.2*(T.arg or 30),
nil,
T.kx,T.ky,
T._ox,T._oy
)
end,
}
---@class Zenitha.Text
local TEXT={_texts={}}
---Clear text container
function TEXT:clear()
self._texts={}
end
---@enum (key) Zenitha.TextAlignMode
local alignModes={
left={0,.5},
right={1,.5},
top={.5,0},
bottom={.5,1},
center={.5,.5},
topleft={0,0},
topright={1,0},
bottomleft={0,1},
bottomright={1,1},
}
---Add text to container
---### Example
---```
---default={
--- text="Example Text",
--- x=0,y=0,
--- align={.5,.5},
--- r=1,g=1,b=1,a=1,
--- fontSize=40,
--- fontType=nil,
---
--- style='appear', -- Check declaration to learn more
--- duration=1,
--- inPoint=0.2,
--- outPoint=0.2,
--- styleArg=...,
---}
---```
---@param data Zenitha.TextAnimArg
function TEXT:add(data)
local T={
text=GC.newText(FONT.get(floor((data.fontSize or 40)/5)*5,data.fontType),data.text or "Example Text"),
x=data.x or 0,y=data.y or 0,
kx=data.k or data.kx or 1,ky=data.k or data.ky or data.kx or 1,
align=alignModes[data.align] or data.align or alignModes.center,
r=data.r,g=data.g,b=data.b,a=data.a,
duration=data.duration or 1,
inPoint=data.inPoint or 0.2,
outPoint=data.outPoint or 0.2,
draw=assertf(textFX[data.style or 'appear'],"No text type: %s",data.style),
arg=data.styleArg,
}
assert(type(T.align)=='table',"Text.add: field align need string or {u,v}")
T._t=0 -- Timer
T.text:setf(data.text or "Example Text",data.warplimit or T.text:getWidth(),data.textalign or 'center')
T._ox,T._oy=T.text:getWidth()*T.align[1],T.text:getHeight()*T.align[2]
if type(data.color)=='string' then data.color=COLOR[data.color] end
if data.color then T.r,T.g,T.b,T.a=data.color[1],data.color[2],data.color[3],data.color[4] end
T.r=T.r or 1
T.g=T.g or 1
T.b=T.b or 1
T.a=T.a or 1
ins(self._texts,T)
end
---Update text container
---@param dt number
function TEXT:update(dt)
local list=self._texts
for i=#list,1,-1 do
local T=list[i]
T._t=T._t+dt/T.duration
if T._t>1 then
rem(list,i).text:release()
end
end
end
---Draw text container
function TEXT:draw()
local list=self._texts
if list[1] then
for i=1,#list do list[i]:draw() end
end
end
---Create new text container
---@return Zenitha.Text
function TEXT.new()
return setmetatable({_texts={}},{__index=TEXT})
end
return TEXT