streamline button.lua

This commit is contained in:
Kartik K. Agaram 2023-12-16 23:37:32 -08:00
parent 961f296131
commit c29be0ffce
6 changed files with 7 additions and 11 deletions

View File

@ -9,9 +9,6 @@
-- draw button and queue up event handlers
function button(State, name, params)
if State.button_handlers == nil then
State.button_handlers = {}
end
love.graphics.setColor(params.bg.r, params.bg.g, params.bg.b, params.bg.a)
love.graphics.rectangle('fill', params.x,params.y, params.w,params.h, 5,5)
if params.icon then params.icon(params) end
@ -19,10 +16,7 @@ function button(State, name, params)
end
-- process button event handlers
function mouse_press_consumed_by_any_button_handler(State, x, y, mouse_button)
if State.button_handlers == nil then
return
end
function mouse_press_consumed_by_any_button(State, x, y, mouse_button)
local button_pressed = false
local consume_press = true
for _,ev in ipairs(State.button_handlers) do

View File

@ -235,7 +235,7 @@ function edit.mouse_press(State, x,y, mouse_button)
if State.search_term then return end
State.mouse_down = mouse_button
--? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then
if mouse_press_consumed_by_any_button(State, x,y, mouse_button) then
-- press on a button and it returned 'true' to short-circuit
return
end

View File

@ -294,7 +294,7 @@ The following facilities help set these things up:
everything about a button in one place. Create as many buttons as you like
within a single shared `state`.
* `mouse_press_consumed_by_any_button_handler(state, x,y, mouse_button)`
* `mouse_press_consumed_by_any_button(state, x,y, mouse_button)`
Call this either directly or indirectly from `App.mousepressed`. It will
pass on a click to any button registered in `state`. It's also helpful to
@ -302,7 +302,7 @@ The following facilities help set these things up:
following boilerplate early in `mousepressed`:
```
if mouse_press_consumed_by_any_button_handler(state, x,y, mouse_button) then
if mouse_press_consumed_by_any_button(state, x,y, mouse_button) then
return
end
```

View File

@ -239,7 +239,7 @@ function edit.mouse_press(State, x,y, mouse_button)
if State.search_term then return end
State.mouse_down = mouse_button
--? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then
if mouse_press_consumed_by_any_button(State, x,y, mouse_button) then
-- press on a button and it returned 'true' to short-circuit
return
end

View File

@ -881,6 +881,7 @@ function test_select_text_using_mouse_starting_above_text_wrapping_line()
Editor_state.screen_top1 = {line=2, pos=3}
Editor_state.screen_bottom1 = {}
-- press mouse above first line of text
edit.draw(Editor_state)
edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1)
-- selection is at screen top
check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')

View File

@ -881,6 +881,7 @@ function test_select_text_using_mouse_starting_above_text_wrapping_line()
Editor_state.screen_top1 = {line=2, pos=3}
Editor_state.screen_bottom1 = {}
-- press mouse above first line of text
edit.draw(Editor_state)
edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1)
-- selection is at screen top
check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')