2022-08-23 17:44:16 +01:00
|
|
|
-- Simple immediate-mode buttons with (currently) just an onpress1 handler for
|
|
|
|
-- the left button.
|
2022-08-23 19:46:04 +01:00
|
|
|
--
|
|
|
|
-- Buttons can nest in principle, though I haven't actually used that yet.
|
|
|
|
--
|
|
|
|
-- Don't rely on the order in which handlers are run. Within any widget, all
|
|
|
|
-- applicable button handlers will run. If _any_ of them returns true, the
|
|
|
|
-- event will continue to propagate elsewhere in the widget.
|
2022-05-18 07:18:56 +01:00
|
|
|
|
2022-05-11 05:17:50 +01:00
|
|
|
-- draw button and queue up event handlers
|
2022-08-23 18:59:58 +01:00
|
|
|
function button(State, name, params)
|
|
|
|
if State.button_handlers == nil then
|
|
|
|
State.button_handlers = {}
|
|
|
|
end
|
2022-05-11 05:17:50 +01:00
|
|
|
love.graphics.setColor(params.color[1], params.color[2], params.color[3])
|
|
|
|
love.graphics.rectangle('fill', params.x,params.y, params.w,params.h, 5,5)
|
2022-08-23 20:13:22 +01:00
|
|
|
if params.icon then params.icon(params) end
|
2022-08-23 18:59:58 +01:00
|
|
|
table.insert(State.button_handlers, params)
|
2022-05-11 05:17:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- process button event handlers
|
2022-08-23 19:43:10 +01:00
|
|
|
function mouse_press_consumed_by_any_button_handler(State, x, y, mouse_button)
|
2022-08-23 18:59:58 +01:00
|
|
|
if State.button_handlers == nil then
|
|
|
|
return
|
|
|
|
end
|
2022-08-24 21:40:36 +01:00
|
|
|
local button_pressed = false
|
|
|
|
local consume_press = true
|
2022-08-23 18:59:58 +01:00
|
|
|
for _,ev in ipairs(State.button_handlers) do
|
2022-05-11 05:17:50 +01:00
|
|
|
if x>ev.x and x<ev.x+ev.w and y>ev.y and y<ev.y+ev.h then
|
2022-08-23 17:37:38 +01:00
|
|
|
if ev.onpress1 and mouse_button == 1 then
|
2022-08-24 21:40:36 +01:00
|
|
|
button_pressed = true
|
|
|
|
if ev.onpress1() then
|
|
|
|
consume_press = false
|
2022-08-23 19:46:04 +01:00
|
|
|
end
|
2022-08-23 17:37:38 +01:00
|
|
|
end
|
2022-05-11 05:17:50 +01:00
|
|
|
end
|
|
|
|
end
|
2022-08-24 21:40:36 +01:00
|
|
|
return button_pressed and consume_press
|
2022-05-11 05:17:50 +01:00
|
|
|
end
|