streamline button.lua
[view.love.git] / button.lua
blob36923e2dbb15b05ad548f6a6fdcf928e009a0664
1 -- Simple immediate-mode buttons with (currently) just an onpress1 handler for
2 -- the left button.
3 --
4 -- Buttons can nest in principle, though I haven't actually used that yet.
5 --
6 -- Don't rely on the order in which handlers are run. Within any widget, all
7 -- applicable button handlers will run. If _any_ of them returns true, the
8 -- event will continue to propagate elsewhere in the widget.
10 -- draw button and queue up event handlers
11 function button(State, name, params)
12 love.graphics.setColor(params.bg.r, params.bg.g, params.bg.b, params.bg.a)
13 love.graphics.rectangle('fill', params.x,params.y, params.w,params.h, 5,5)
14 if params.icon then params.icon(params) end
15 table.insert(State.button_handlers, params)
16 end
18 -- process button event handlers
19 function mouse_press_consumed_by_any_button(State, x, y, mouse_button)
20 local button_pressed = false
21 local consume_press = true
22 for _,ev in ipairs(State.button_handlers) do
23 if x>ev.x and x<ev.x+ev.w and y>ev.y and y<ev.y+ev.h then
24 if ev.onpress1 and mouse_button == 1 then
25 button_pressed = true
26 if ev.onpress1() then
27 consume_press = false
28 end
29 end
30 end
31 end
32 return button_pressed and consume_press
33 end