From f793c3e17aba9a4ba5720595bf16ace8070b8dc7 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Sun, 29 May 2022 19:49:59 +0200 Subject: [PATCH] Mouse hover/click --- 0hh1.p8 | 14 ++++++++------ main.lua | 19 +++++++++++-------- states/menu.lua | 19 ++++++++++++------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/0hh1.p8 b/0hh1.p8 index af86cd3..872d28d 100644 --- a/0hh1.p8 +++ b/0hh1.p8 @@ -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 diff --git a/main.lua b/main.lua index a500b17..c4868e6 100644 --- a/main.lua +++ b/main.lua @@ -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 diff --git a/states/menu.lua b/states/menu.lua index e44b682..98d3ef8 100644 --- a/states/menu.lua +++ b/states/menu.lua @@ -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 }