From 490f10c6f8f38cc83264c38dd93e32d471530880 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 23 Aug 2022 09:37:38 -0700 Subject: indent --- button.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index 42c44e3..5042ae6 100644 --- a/button.lua +++ b/button.lua @@ -14,7 +14,9 @@ end function propagate_to_button_handlers(x, y, mouse_button) for _,ev in ipairs(Button_handlers) do if x>ev.x and xev.y and y Date: Tue, 23 Aug 2022 09:40:48 -0700 Subject: allow buttons to interrupt events Most button onpress1 handlers will want to return true. --- button.lua | 2 +- edit.lua | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index 5042ae6..66a2c00 100644 --- a/button.lua +++ b/button.lua @@ -15,7 +15,7 @@ function propagate_to_button_handlers(x, y, mouse_button) for _,ev in ipairs(Button_handlers) do if x>ev.x and xev.y and y Date: Tue, 23 Aug 2022 09:44:16 -0700 Subject: improve explanation for buttons --- button.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index 66a2c00..4cafc86 100644 --- a/button.lua +++ b/button.lua @@ -1,4 +1,7 @@ --- simple immediate-mode buttons +-- Simple immediate-mode buttons with (currently) just an onpress1 handler for +-- the left button. +-- If the handler returns true, it'll prevent any further processing of the +-- event. Button_handlers = {} -- cgit 1.4.1-2-gfad0 From 8057f3e8fe016ea054fa1c878e2c90b5ae32d1b6 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 23 Aug 2022 10:59:58 -0700 Subject: stop putting button state in a global Symptom: a test (test_click_to_create_drawing) started randomly failing after I inserted a `return` 2 commits ago. Cause: my tests call edit.draw, but button handlers only get cleared in app.draw. So my tests weren't clearing button handlers, and every call to edit.draw was accumulating states. Still unclear why those were going to different state objects after the `return`, but anyway. I'm not going to understand every last thing that happens when things go wrong, just guarantee they can't go wrong. And the way to do that is to decentralize button handlers to each state that receives them. The State object in buttons.lua doesn't have to be Editor_state. It just has to be some table that provides a Schelling Point for shared state. --- button.lua | 16 ++++++++++------ edit.lua | 5 +++-- main.lua | 1 - 3 files changed, 13 insertions(+), 9 deletions(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index 4cafc86..4c7fe0b 100644 --- a/button.lua +++ b/button.lua @@ -3,19 +3,23 @@ -- If the handler returns true, it'll prevent any further processing of the -- event. -Button_handlers = {} - -- 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) + 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 propagate_to_button_handlers(State, x, y, mouse_button) + if State.button_handlers == nil then + return + end + for _,ev in ipairs(State.button_handlers) do if x>ev.x and xev.y and y Date: Tue, 23 Aug 2022 11:43:10 -0700 Subject: flip return value of button handlers This is compatible with Javascript, and it also seems like a better default; when people forget to think about return values in click handlers, they should be consumed. --- button.lua | 7 +++---- edit.lua | 3 +-- 2 files changed, 4 insertions(+), 6 deletions(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index 4c7fe0b..748f5ae 100644 --- a/button.lua +++ b/button.lua @@ -1,7 +1,6 @@ -- Simple immediate-mode buttons with (currently) just an onpress1 handler for -- the left button. --- If the handler returns true, it'll prevent any further processing of the --- event. +-- If any applicable button handler returns true, it'll propagate the click to other handlers. -- draw button and queue up event handlers function button(State, name, params) @@ -15,14 +14,14 @@ function button(State, name, params) end -- process button event handlers -function propagate_to_button_handlers(State, x, y, mouse_button) +function mouse_press_consumed_by_any_button_handler(State, x, y, mouse_button) if State.button_handlers == nil then return end for _,ev in ipairs(State.button_handlers) do if x>ev.x and xev.y and y Date: Tue, 23 Aug 2022 11:46:04 -0700 Subject: allow buttons to nest as well --- button.lua | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index 748f5ae..c027b5c 100644 --- a/button.lua +++ b/button.lua @@ -1,6 +1,11 @@ -- Simple immediate-mode buttons with (currently) just an onpress1 handler for -- the left button. --- If any applicable button handler returns true, it'll propagate the click to other handlers. +-- +-- 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(State, name, params) @@ -18,11 +23,15 @@ 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 xev.y and y Date: Tue, 23 Aug 2022 12:13:22 -0700 Subject: pass all button params to the icon --- button.lua | 2 +- icons.lua | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index c027b5c..d8ab601 100644 --- a/button.lua +++ b/button.lua @@ -14,7 +14,7 @@ function button(State, name, params) 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 + if params.icon then params.icon(params) end table.insert(State.button_handlers, params) end diff --git a/icons.lua b/icons.lua index 19939cf..175fb13 100644 --- a/icons.lua +++ b/icons.lua @@ -1,6 +1,7 @@ icon = {} -function icon.insert_drawing(x, y) +function icon.insert_drawing(button_params) + local x,y = button_params.x, button_params.y App.color(Icon_color) love.graphics.rectangle('line', x,y, 12,12) love.graphics.line(4,y+6, 16,y+6) -- cgit 1.4.1-2-gfad0