pico8-0hh1/ui.lua

42 lines
1.0 KiB
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,
on_draw = options.on_draw,
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()
2022-06-01 21:26:57 +02:00
-- rect2(self.x, self.y, self.w, self.h, 8)
2022-06-02 20:18:02 +02:00
if self.on_draw then self.on_draw(self) end
2022-06-01 21:26:57 +02:00
print(self.text, self.x+1, self.y+1, self.color)
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