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/game.lua
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000171000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700177100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000177710000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000177771000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700177710000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000171771000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000010171000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

View File

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

View File

@ -1,13 +1,18 @@
function stateMenu()
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()
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
return {
@ -19,8 +24,8 @@ function stateMenu()
end,
_draw = function()
for button in all(buttons) do
button:draw()
for k,button in ipairs(buttons) do
button:draw(selected == k)
end
end
}