Template repo for tiny cross-platform apps that can be modified on phone, tablet or computer.
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.
9
10-- draw button and queue up event handlers
11function button(state, name, params)
12 if params.bg then
13 if params.bg.r then
14 -- old interface for compatibility
15 love.graphics.setColor(params.bg.r, params.bg.g, params.bg.b, params.bg.a)
16 else
17 love.graphics.setColor(params.bg)
18 end
19 love.graphics.rectangle('fill', params.x,params.y, params.w,params.h, 5,5)
20 end
21 if params.icon then params.icon(params) end
22 table.insert(state.button_handlers, params)
23end
24
25function mouse_hover_on_any_button(state, x, y)
26 for _,ev in ipairs(state.button_handlers) do
27 if x>ev.x and x<ev.x+ev.w and y>ev.y and y<ev.y+ev.h then
28 if ev.onpress1 then
29 return true
30 end
31 end
32 end
33end
34
35-- process button event handlers
36function mouse_press_consumed_by_any_button(state, x, y, mouse_button)
37 local button_pressed = false
38 local consume_press = true
39 for _,ev in ipairs(state.button_handlers) do
40 if x>ev.x and x<ev.x+ev.w and y>ev.y and y<ev.y+ev.h then
41 if ev.onpress1 and mouse_button == 1 then
42 button_pressed = true
43 if ev.onpress1() then
44 consume_press = false
45 end
46 end
47 end
48 end
49 return button_pressed and consume_press
50end
51
52-- I _could_ just do this inside button(), but then every button on screen
53-- would independently query mouse position and focus..
54function draw_button_tooltips(state)
55 if App.mouse_down(1) then return end
56 local x,y = love.mouse.getPosition()
57 for _,ev in ipairs(state.button_handlers) do
58 if ev.tooltip then
59 if x>ev.x and x<ev.x+ev.w and y>ev.y and y<ev.y+ev.h then
60 ev.tooltip(x,y)
61 end
62 end
63 end
64end