-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgui.lua
220 lines (171 loc) · 4.87 KB
/
imgui.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
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
local imgui = {
mouse_clicked = false,
mouse_released = false,
mouse_down = false,
current = nil,
list = {} --List of objects
}
menuselect = la.newSource("sounds/bing1.ogg", "static")
menuselect:setVolume(.8)
local list = imgui.list
local pool = {} --Empty tables
local function getTable(id)
for i, obj in ipairs(list) do
if obj.id == id then
imgui.current = obj
return obj
end
end
local obj = nil
if pool[1] then
obj = pool[1]
table.remove(pool, 1)
else
obj = {}
end
obj.id = id
imgui.current = obj
toLast(list, obj)
return obj
end
local function deleteTable(index)
local obj = list[index]
lume.clear(obj)
table.remove(list, index)
toLast(pool, obj)
end
function imgui.update()
local old_mouse_down = imgui.mouse_down
imgui.current = nil
imgui.mouse_clicked = false
imgui.mouse_released = false
imgui.mouse_down = false
if lm.isDown(1) then
imgui.mouse_down = true
if not old_mouse_down then
imgui.mouse_clicked = true
end
elseif old_mouse_down then
imgui.mouse_released = true
end
for i, obj in ipairs(list) do
local old_hovered = obj.hovered
obj.hovered = false
obj.mouse_enter = false
obj.mouse_exit = false
assert(obj.x, #list)
if mouseOver(obj.x, obj.y, obj.w, obj.h) then
if (not isMobile or lm.isDown(1)) then
obj.hovered = true
if not old_hovered then
obj.mouse_enter = true
end
end
elseif old_mouse_down then
obj.mouse_exit = true
end
if obj.not_rendering then
deleteTable(i)
else
obj.not_rendering = true
end
end
imgui._info = string.format("Imgui:\npool: %d\nobjects: %d", #pool, #list)
end
function imgui.draw()
local r, g, b, a = lg.getColor()
for i, obj in ipairs(list) do
if not obj.not_rendering then
if obj.type == "scrollbutton" then
local scroll = obj.scroll
lg.setColor(r, scroll*g, scroll*b, a)
local addPos = 30 - scroll * 30
lg.draw(obj.img, obj.x + addPos * boolto(obj.dir == "left", 1, -1), obj.y)
end
if obj.type == "button" then
local scroll = obj.scroll
lg.setColor(r, scroll*g, scroll*b, a)
lg.draw(obj.img, obj.x, obj.y)
end
end
end
lg.setColor(1,1,1,1)
setFont(20)
end
local imagecache = {}
function imgui.button(id, img, x, y, w, h)
obj = getTable(id)
obj.type = "button"
obj.x = x
obj.y = y
obj.img = img
if type(img) == "string" then
if imagecache[img] then
obj.img = imagecache[img]
else
obj.img = lg.newImage("assets/icons/"..img)
imagecache[img] = obj.img
end
end
obj.w = w or obj.img:getWidth()
obj.h = h or obj.img:getHeight()
obj.scroll = obj.scroll or 1
if obj.hovered then
obj.scroll = max(obj.scroll - 5 * dt, 0)
else
obj.scroll = min(obj.scroll + 5 * dt, 1)
end
obj.not_rendering = false
if obj.mouse_enter then
menuselect:setPitch(lume.random(.8, 1))
menuselect:stop()
menuselect:play()
end
if isMobile then
if obj.hovered and imgui.mouse_clicked then
obj.pressed = true
end
if obj.mouse_exit then
obj.pressed = false
end
return imgui.mouse_released and obj.pressed
else
return obj.hovered and imgui.mouse_clicked
end
end
function imgui.scrollbutton(id, img, dir, x, y, w, h)
local rt = imgui.button(id, img, x, y, w, h)
imgui.current.type = "scrollbutton"
imgui.current.dir = dir
return rt
end
function imgui.textbutton(id, text, dir, x, y, w, h)
local obj = getTable(id)
local img = obj.img
local cachepointer = string.format("%s_%s_%s", lang.code, currFont, text)
if imagecache[cachepointer] then
img = imagecache[cachepointer]
else
local w, h = fontWidth(text) + 2.5, fonts[currFont]:getHeight() + 5
img = lg.newCanvas(w, h)
lg.clear()
lg.setCanvas(img)
lg.push()
lg.origin()
lg.setBlendMode("alpha", "premultiplied")
lg.setColor(1,0,1,1)
blur(lg.print, text, 2.5)
lg.setBlendMode("alpha")
lg.setColor(1,1,1,1)
lg.print(text, 2.5)
lg.pop()
lg.setCanvas()
imagecache[cachepointer] = img
block_dt = true
end
return imgui.scrollbutton(id, img, dir, x, y, w, h)
end
function imgui.clearcachedimages()
imagecache = {}
end
return imgui