Mouse hover/click

This commit is contained in:
Simon Cambier 2022-05-29 19:49:59 +02:00
parent 977d2c342b
commit f793c3e17a
3 changed files with 31 additions and 21 deletions

14
0hh1.p8
View File

@ -6,9 +6,11 @@ __lua__
#include states/menu.lua #include states/menu.lua
#include states/game.lua #include states/game.lua
__gfx__ __gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000171000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00700700177100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00077000177710000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00077000177771000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00700700177710000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000171771000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000010171000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

View File

@ -104,7 +104,7 @@ end
-- return str.."}" -- return str.."}"
-- end -- end
function makeButton(x, y, w, h, text, f) function makeButton(x, y, w, h, text, onClick, onHover)
local state = 0 -- 0 = normal, 1 = hovered, 2 = pressed local state = 0 -- 0 = normal, 1 = hovered, 2 = pressed
return { return {
x = x, x = x,
@ -112,21 +112,25 @@ function makeButton(x, y, w, h, text, f)
w = w, w = w,
h = h, h = h,
text = text, text = text,
onClick = onClick,
onHover = onHover,
draw=function(self) draw=function(self, selected)
rectfill(self.x, self.y, self.w, self.h, 8) -- rectfill(self.x, self.y, self.w, self.h, 8)
print(self.text.." "..state, self.x+1, self.y+1, 6) local color = selected and 7 or 5
print(self.text, self.x+1, self.y+1, color)
end, end,
update=function(self) update=function(self)
if mouse_x >= self.x and mouse_x <= self.x + self.w and 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 mouse_y >= self.y and mouse_y <= self.y + self.h then
if stat(34)&1 == 0 and state == 2 and f then if stat(34)&1 == 0 and state == 2 and self.onClick then
f() self.onClick()
end end
if stat(34)&1 == 1 then if stat(34)&1 == 1 then
state = 2 state = 2
else else
if onHover then onHover() end
state = 1 state = 1
end end
else else
@ -179,6 +183,5 @@ end
function _draw() function _draw()
cls() cls()
gs._draw() gs._draw()
circfill(stat(32), stat(33), 0, 7) spr(1, stat(32), stat(33))
circ(stat(32), stat(33), 1, 0)
end end

View File

@ -1,13 +1,18 @@
function stateMenu() function stateMenu()
local items = {"play", "rules"} local items = {"play", "rules"}
local buttons = {} local selected = 1
local buttons = {
makeButton(10, 10, 30, 6, "play",
function() setState(states.game) end,
function() selected = 1 end),
makeButton(10, 20, 30, 6, "rules",
function() setState(states.rules) end,
function() selected = 2 end)
}
function _enter() function _enter()
for k,v in ipairs(items) do
local button = makeButton(10, k*10, 30, 6, v, function() printh("click") end)
add(buttons, button)
end
end end
return { return {
@ -19,8 +24,8 @@ function stateMenu()
end, end,
_draw = function() _draw = function()
for button in all(buttons) do for k,button in ipairs(buttons) do
button:draw() button:draw(selected == k)
end end
end end
} }