document recent handlers
[view.love.git] / button.lua
blobdf837044c392662a86d2037a14ecdbd68250c160
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 if params.bg then
13 love.graphics.setColor(params.bg.r, params.bg.g, params.bg.b, params.bg.a)
14 love.graphics.rectangle('fill', params.x,params.y, params.w,params.h, 5,5)
15 end
16 if params.icon then params.icon(params) end
17 table.insert(State.button_handlers, params)
18 end
20 -- process button event handlers
21 function mouse_press_consumed_by_any_button(State, x, y, mouse_button)
22 local button_pressed = false
23 local consume_press = true
24 for _,ev in ipairs(State.button_handlers) do
25 if x>ev.x and x<ev.x+ev.w and y>ev.y and y<ev.y+ev.h then
26 if ev.onpress1 and mouse_button == 1 then
27 button_pressed = true
28 if ev.onpress1() then
29 consume_press = false
30 end
31 end
32 end
33 end
34 return button_pressed and consume_press
35 end