-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui.moon
More file actions
190 lines (144 loc) · 3.25 KB
/
ui.moon
File metadata and controls
190 lines (144 loc) · 3.25 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
{graphics: g, :keyboard} = love
ez_approach = (val, target, dt) ->
approach val, target, dt * 10 * math.max 1, math.abs val - target
-- a piece of text that knows its size
class Label extends Box
new: (text, @x=0, @y=0) =>
@set_text text
set_text: (@text) =>
@is_func = type(@text) == "function"
@_set_size @text unless @is_func
@_update_from_fun!
_set_size: (text) =>
font = g.getFont!
@w = font\getWidth text
@h = font\getHeight!
_update_from_fun: =>
if @is_func
@_text = @text!
@_set_size @_text
update: (dt) =>
@_update_from_fun!
@alive
draw: =>
text = @is_func and @_text or @text
g.print text, @x, @y
-- COLOR\push 255,100,100, 200
-- g.rectangle "fill", @x,@y, 2,2
-- g.rectangle "fill", @x+@w,@y+@h, 2,2
-- COLOR\pop!
-- has effect list
class AnimatedLabel extends Label
new: (...) =>
super ...
@effects = EffectList!
update: (dt) =>
@effects\update dt
super dt
draw: =>
text = @is_func and @_text or @text
hw = @w/2
hh = @h/2
g.push!
g.translate @x + hw, @y + hh
@effects\before!
g.print text, -hw, -hh
@effects\after!
g.pop!
class BlinkingLabel extends Label
rate: 1.2
duty: 0.8 -- percent of time visible
elapsed: 0
update: (dt) =>
@elapsed += dt
super dt
draw: =>
scaled = @elapsed / @rate
p = scaled - math.floor scaled
if p <= @duty
super!
class RevealLabel extends Label
rate: 0.03
new: (text, @x, @y, fn) =>
@chr = 0
@seq = Sequence ->
while @chr < #text
@chr += 1
wait @rate
@done = true
@seq = nil
fn @ if fn
@set_text -> text\sub 1, @chr
update: (dt) =>
@seq\update dt if @seq
super dt
class BaseList
padding: 5
xalign: "left"
yalign: "top"
w: 0
h: 0
-- can pass instance properties into items
new: (@x, @y, @items={}) =>
-- not specifying position
if type(@x) == "table"
@items = @x
@x = 0
@y = 0
-- extract props
for k,v in pairs @items
if type(k) == "string"
@items[k] = nil
@[k] = v
update_size: -> error "override me"
update: (dt, ...) =>
for item in *@items
item\update dt, ...
@update_size!
true
class VList extends BaseList
update_size: =>
@w, @h = 0, 0
for item in *@items
@h += item.h + @padding
if item.w > @w
@w = item.w
@h -= @padding if @h > 0
draw: =>
{:x, :y} = @
dy = if @yalign == "bottom"
total_height = 0
for item in *@items
total_height += item.h
if total_height > 0
total_height += @padding * #@items
-total_height
else
0
for item in *@items
item.x = if @xalign == "right"
x - item.w
else
x
item.y = y + dy
y += @padding + item.h
item\draw!
class HList extends BaseList
update_size: =>
@w, @h = 0, 0
for item in *@items
@w += item.w + @padding
if item.h > @h
@h = item.h
@w -= @padding if @w > 0
draw: =>
{:x, :y} = @
for item in *@items
item.x = x
item.y = y
x += @padding + item.w
item\draw!
{
:Label, :AnimatedLabel, :BlinkingLabel, :RevealLabel, :VList, :HList,
:ez_approach
}