fix #54 event-handlers require explicit false to fail event handling

This commit is contained in:
Paul Liverman III 2017-04-18 23:03:24 -07:00
parent 3fb4b15241
commit ecc1f5a84f
2 changed files with 14 additions and 16 deletions

View File

@ -239,19 +239,15 @@ pop.mousepressed = function(x, y, button, element)
local handled = false local handled = false
if element.data.draw and (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) then if element.data.draw and (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) then
for i = #element.child, 1, -1 do for i = #element.child, 1, -1 do
do handled = pop.mousepressed(x, y, button, element.child[i])
handled = pop.mousepressed(x, y, button, element.child[i]) if handled ~= false then
if handled then return handled
return handled
end
end end
end end
if element.mousepressed then if element.mousepressed then
do handled = element:mousepressed(x - element.data.x, y - element.data.y, button)
handled = element:mousepressed(x - element.data.x, y - element.data.y, button) if handled ~= false then
if handled then pop.focused = element
pop.focused = element
end
end end
end end
end end
@ -264,7 +260,7 @@ pop.mousereleased = function(x, y, button, element)
if element.data.draw and (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) then if element.data.draw and (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) then
for i = #element.child, 1, -1 do for i = #element.child, 1, -1 do
clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.child[i]) clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.child[i])
if clickedHandled or mousereleasedHandled then if clickedHandled ~= false or mousereleasedHandled ~= false then
return clickedHandled, mousereleasedHandled return clickedHandled, mousereleasedHandled
end end
end end
@ -274,7 +270,7 @@ pop.mousereleased = function(x, y, button, element)
if element.mousereleased then if element.mousereleased then
mousereleasedHandled = element:mousereleased(x - element.data.x, y - element.data.y, button) mousereleasedHandled = element:mousereleased(x - element.data.x, y - element.data.y, button)
end end
if clickedHandled then if clickedHandled ~= false then
pop.focused = element pop.focused = element
end end
end end

View File

@ -315,12 +315,14 @@ pop.mousepressed = (x, y, button, element) ->
if element.data.draw and (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) if element.data.draw and (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h)
-- check its child elements in reverse order, returning if something handles it -- check its child elements in reverse order, returning if something handles it
for i = #element.child, 1, -1 for i = #element.child, 1, -1
if handled = pop.mousepressed x, y, button, element.child[i] handled = pop.mousepressed x, y, button, element.child[i]
if handled != false
return handled return handled
-- if a child hasn't handled it yet, try to handle it, and set pop.focused -- if a child hasn't handled it yet, try to handle it, and set pop.focused
if element.mousepressed if element.mousepressed
if handled = element\mousepressed x - element.data.x, y - element.data.y, button handled = element\mousepressed x - element.data.x, y - element.data.y, button
if handled != false
pop.focused = element pop.focused = element
-- return whether or not we have handled the event -- return whether or not we have handled the event
@ -351,7 +353,7 @@ pop.mousereleased = (x, y, button, element) ->
-- check its children in reverse for handling a clicked or mousereleased event -- check its children in reverse for handling a clicked or mousereleased event
for i = #element.child, 1, -1 for i = #element.child, 1, -1
clickedHandled, mousereleasedHandled = pop.mousereleased x, y, button, element.child[i] clickedHandled, mousereleasedHandled = pop.mousereleased x, y, button, element.child[i]
if clickedHandled or mousereleasedHandled if clickedHandled != false or mousereleasedHandled != false
return clickedHandled, mousereleasedHandled return clickedHandled, mousereleasedHandled
-- if that doesn't work, we try to handle it ourselves -- if that doesn't work, we try to handle it ourselves
@ -361,7 +363,7 @@ pop.mousereleased = (x, y, button, element) ->
mousereleasedHandled = element\mousereleased x - element.data.x, y - element.data.y, button mousereleasedHandled = element\mousereleased x - element.data.x, y - element.data.y, button
-- if we clicked, we're focused! -- if we clicked, we're focused!
if clickedHandled if clickedHandled != false
pop.focused = element pop.focused = element
--- @todo Figure out how to bring a focused element to the front of view (aka the first element in its parent's children). --- @todo Figure out how to bring a focused element to the front of view (aka the first element in its parent's children).
--- (If I do it right here, the for loop above may break! I need to test/figure this out.) --- (If I do it right here, the for loop above may break! I need to test/figure this out.)