pico8-0hh1/ui.lua

40 lines
969 B
Lua
Raw Normal View History

2022-06-02 20:18:02 +02:00
function make_button(options)
2022-06-01 21:26:57 +02:00
local state = 0 -- 0 = normal, 1 = hovered, 2 = pressed
2022-07-12 21:47:10 +02:00
local w = print(options.text, 0, -100)
2022-06-01 21:26:57 +02:00
return {
x = options.x,
y = options.y,
2022-07-12 21:47:10 +02:00
w = options.w or w,
2022-06-01 21:26:57 +02:00
h = options.h or 6,
data = options.data,
text = options.text,
2022-06-02 20:18:02 +02:00
on_click = options.on_click,
on_hover = options.on_hover,
2022-06-01 21:26:57 +02:00
ogColor = options.color or 5,
color = options.color or 5,
draw=function(self, selected)
2022-06-02 20:18:02 +02:00
standard_font()
2023-02-22 20:39:24 +01:00
local color = selected and 7 or self.color
print(self.text, self.x+1, self.y+1, color)
2022-06-01 21:26:57 +02:00
end,
update=function(self)
self.color = self.ogColor
if mouse_x >= self.x and mouse_x <= self.x + self.w and
mouse_y >= self.y and mouse_y <= self.y + self.h then
2022-06-02 20:18:02 +02:00
if stat(34)&1 == 0 and state == 2 and self.on_click then
self.on_click(self)
2022-06-01 21:26:57 +02:00
end
if stat(34)&1 == 1 then
state = 2
else
2022-06-02 20:18:02 +02:00
if self.on_hover then self.on_hover(self) end
2022-06-01 21:26:57 +02:00
state = 1
end
else
state = 0
end
end
}
end