about summary refs log tree commit diff stats
path: root/button.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-08-23 13:25:55 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-08-23 13:25:55 -0700
commitd011c0ce325b69ed413666a5adb31156c1a26c41 (patch)
tree73f480975dce88114623f5fe9502248eaf578f92 /button.lua
parenteca4a725d1a723a2a476ca6d399dfa99619a5445 (diff)
parent019a8292793077106b9b6927b9ca727918fad9ec (diff)
downloadview.love-d011c0ce325b69ed413666a5adb31156c1a26c41.tar.gz
Merge lines.love
Diffstat (limited to 'button.lua')
-rw-r--r--button.lua35
1 files changed, 26 insertions, 9 deletions
diff --git a/button.lua b/button.lua
index 42c44e3..d8ab601 100644
--- a/button.lua
+++ b/button.lua
@@ -1,20 +1,37 @@
--- simple immediate-mode buttons
-
-Button_handlers = {}
+-- Simple immediate-mode buttons with (currently) just an onpress1 handler for
+-- the left button.
+--
+-- 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.
 
 -- draw button and queue up event handlers
-function button(name, params)
+function button(State, name, params)
+  if State.button_handlers == nil then
+    State.button_handlers = {}
+  end
   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)
-  if params.icon then params.icon(params.x, params.y) end
-  table.insert(Button_handlers, params)
+  if params.icon then params.icon(params) end
+  table.insert(State.button_handlers, params)
 end
 
 -- process button event handlers
-function propagate_to_button_handlers(x, y, mouse_button)
-  for _,ev in ipairs(Button_handlers) do
+function mouse_press_consumed_by_any_button_handler(State, x, y, mouse_button)
+  if State.button_handlers == nil then
+    return
+  end
+  local result = false
+  for _,ev in ipairs(State.button_handlers) do
     if x>ev.x and x<ev.x+ev.w and y>ev.y and y<ev.y+ev.h then
-      if ev.onpress1 and mouse_button == 1 then ev.onpress1() end
+      if ev.onpress1 and mouse_button == 1 then
+        if not ev.onpress1() then
+          result = true
+        end
+      end
     end
   end
+  return result
 end