fix bug w implementation of hovered callback

This commit is contained in:
Paul Liverman III 2017-08-13 18:42:53 -07:00
parent 3ab37d3e6a
commit ab232862cb
2 changed files with 25 additions and 16 deletions

View File

@ -229,12 +229,20 @@ pop.mousemoved = function(x, y, dx, dy, element)
pop.mousemoved(x, y, dx, dy, element.child[i]) pop.mousemoved(x, y, dx, dy, element.child[i])
end end
end end
if element == pop.screen and pop.hovered ~= previously_hovered then if element == pop.screen then
if pop.hovered ~= previously_hovered then
log(" pop.hovered: " .. tostring(pop.hovered) .. " (" .. tostring(pop.hovered.data.type) .. ")") log(" pop.hovered: " .. tostring(pop.hovered) .. " (" .. tostring(pop.hovered.data.type) .. ")")
if previously_hovered and previously_hovered.hovered then
previously_hovered:hovered(false)
end end
if pop.focused and pop.focused.mousemoved and element == pop.screen then if pop.hovered.hovered then
pop.hovered:hovered(true)
end
end
if pop.focused and pop.focused.mousemoved then
return pop.focused:mousemoved(x - pop.focused.data.x, y - pop.focused.data.y, dx, dy) return pop.focused:mousemoved(x - pop.focused.data.x, y - pop.focused.data.y, dx, dy)
end end
end
return false return false
end end
pop.mousepressed = function(x, y, button, element) pop.mousepressed = function(x, y, button, element)

View File

@ -269,7 +269,7 @@ pop.draw = (element=pop.screen) ->
pop.mousemoved = (x, y, dx, dy, element=pop.screen) -> pop.mousemoved = (x, y, dx, dy, element=pop.screen) ->
local previously_hovered local previously_hovered
if element == pop.screen if element == pop.screen
previously_hovered = pop.hovered previously_hovered = pop.hovered -- we save it because the loop below will change it
-- first we find out if we're hovering over anything and set pop.hovered -- first we find out if we're hovering over anything and set pop.hovered
if element.data.draw and element.data.hoverable 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 element.data.hoverable 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)
@ -279,16 +279,17 @@ pop.mousemoved = (x, y, dx, dy, element=pop.screen) ->
for i = 1, #element.child for i = 1, #element.child
pop.mousemoved x, y, dx, dy, element.child[i] pop.mousemoved x, y, dx, dy, element.child[i]
-- check element == pop.screen so this is only called once at the end
if element == pop.screen
-- if we're hovering over something different, log it, and call handlers if relevant -- if we're hovering over something different, log it, and call handlers if relevant
if element == pop.screen and pop.hovered != previously_hovered if pop.hovered != previously_hovered
log " pop.hovered: #{pop.hovered} (#{pop.hovered.data.type})" log " pop.hovered: #{pop.hovered} (#{pop.hovered.data.type})"
if previously_hovered.hovered if previously_hovered and previously_hovered.hovered -- previously_hovered can be a false boolean
previously_hovered\hovered false previously_hovered\hovered false
if pop.hovered.hovered if pop.hovered.hovered
pop.hovered\hovered true pop.hovered\hovered true
-- checks element == pop.screen so this only gets called once at the end of recursion if pop.focused and pop.focused.mousemoved
if pop.focused and pop.focused.mousemoved and element == pop.screen
return pop.focused\mousemoved x - pop.focused.data.x, y - pop.focused.data.y, dx, dy return pop.focused\mousemoved x - pop.focused.data.x, y - pop.focused.data.y, dx, dy
return false return false