isolate some tests from settings
[view.love.git] / button.lua
blob7549169735600db9972f21a323b26210f07cea39
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 State.button_handlers == nil then
13 State.button_handlers = {}
14 end
15 love.graphics.setColor(params.color[1], params.color[2], params.color[3])
16 love.graphics.rectangle('fill', params.x,params.y, params.w,params.h, 5,5)
17 if params.icon then params.icon(params) end
18 table.insert(State.button_handlers, params)
19 end
21 -- process button event handlers
22 function mouse_press_consumed_by_any_button_handler(State, x, y, mouse_button)
23 if State.button_handlers == nil then
24 return
25 end
26 local button_pressed = false
27 local consume_press = true
28 for _,ev in ipairs(State.button_handlers) do
29 if x>ev.x and x<ev.x+ev.w and y>ev.y and y<ev.y+ev.h then
30 if ev.onpress1 and mouse_button == 1 then
31 button_pressed = true
32 if ev.onpress1() then
33 consume_press = false
34 end
35 end
36 end
37 end
38 return button_pressed and consume_press
39 end