Fixed some buttons stuff

This commit is contained in:
Simon Cambier 2023-02-23 08:14:29 +01:00
parent 3432f5d36e
commit c1484655cd
3 changed files with 28 additions and 20 deletions

View File

@ -132,5 +132,6 @@ function _draw()
for overlay in all(overlays) do for overlay in all(overlays) do
overlay:_draw() overlay:_draw()
end end
spr(1, stat(32), stat(33)) -- mouse cursor
spr(1, mouse_x, mouse_y)
end end

View File

@ -1,26 +1,28 @@
function state_menu() function state_menu()
local selected = 1 local selected = 1
local buttons = { local buttons = {}
make_button({x=10, y=10, w=30, text="pLAY", data={i=1}, -- local buttons = {
on_click=function() set_state(states.loading) end, -- make_button({x=10, y=10, w=30, text="pLAY", data={i=1},
on_hover=function(btn) selected=1 end, -- on_click=function() set_state(states.loading) end,
}), -- on_hover=function(btn) selected=1 end,
} -- }),
-- }
local game_sizes = { local game_sizes = {
{"mINI bOARD - 4x4", 4}, {"mINI bOARD - 4X4", 4},
{"sMALL bOARD - 6x6", 6}, {"sMALL bOARD - 6X6", 6},
{"mEDIUM bOARD - 8x8", 8}, {"mEDIUM bOARD - 8X8", 8},
{"lARGE bOARD - 10x10", 10} {"lARGE bOARD - 10X10", 10}
} }
for k, item in ipairs(game_sizes) do for k, item in ipairs(game_sizes) do
add(buttons, add(buttons,
make_button({ make_button({
x=10, y=10+k*10, w=200, x=10, y=10+k*10,
text=item[1], data={i=2}, selected_color=7,
text=item[1],
on_click=function() board_size=item[2] set_state(states.loading) end, on_click=function() board_size=item[2] set_state(states.loading) end,
on_hover=function(btn) selected=k+1 end, on_hover=function() selected=k end,
}) })
) )
end end
@ -28,9 +30,10 @@ function state_menu()
local rulesId = #buttons+1 local rulesId = #buttons+1
add(buttons, add(buttons,
make_button({x=10, y=10+(#game_sizes+1)*10, w=30, text="rULES", data={i=2}, make_button({x=10, y=20+(#game_sizes+1)*10, w=30, text="rULES",
color=13, selected_color=12,
on_click=function() set_state(states.rules) end, on_click=function() set_state(states.rules) end,
on_hover=function(btn) selected=rulesId end, on_hover=function() selected=rulesId end,
}) })
) )
@ -42,7 +45,9 @@ function state_menu()
local function _draw() local function _draw()
cls() cls()
draw_bg_menu() draw_bg_menu()
-- printh(selected)
print("pLAY", 10, 8, 8)
for k,button in ipairs(buttons) do for k,button in ipairs(buttons) do
button:draw(selected == k) button:draw(selected == k)
end end

8
ui.lua
View File

@ -11,12 +11,14 @@ function make_button(options)
on_click = options.on_click, on_click = options.on_click,
on_hover = options.on_hover, on_hover = options.on_hover,
ogColor = options.color or 5, ogColor = options.color or 5,
selected_color = options.selected_color or 5,
color = options.color or 5, color = options.color or 5,
draw=function(self, selected) draw=function(self, selected)
standard_font() standard_font()
local color = selected and 7 or self.color local color = selected and self.selected_color or self.color
print(self.text, self.x+1, self.y+1, color) print(self.text, self.x, self.y, color)
-- rect(self.x, self.y, self.x+self.w, self.y+self.h, color)
end, end,
update=function(self) update=function(self)
@ -29,7 +31,7 @@ function make_button(options)
if stat(34)&1 == 1 then if stat(34)&1 == 1 then
state = 2 state = 2
else else
if self.on_hover then self.on_hover(self) end if self.on_hover then self:on_hover() end
state = 1 state = 1
end end
else else