diff --git a/demo/pop/init.lua b/demo/pop/init.lua index d77f6cc..3e8b968 100644 --- a/demo/pop/init.lua +++ b/demo/pop/init.lua @@ -10,6 +10,7 @@ local pop = { } pop.elements = { } pop.skins = { } pop.screen = false +pop.focused = false pop.load = function() local elements = filesystem.getDirectoryItems(tostring(path) .. "/elements") for i = 1, #elements do @@ -99,7 +100,24 @@ pop.mousepressed = function(x, y, button, element) element = pop.screen end print("mousepressed", x, y, button, element) - return false + local handled = false + if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then + if element.mousepressed then + handled = element:mousepressed(x - element.x, y - element.y, button) + end + if handled then + pop.focused = element + else + for i = 1, #element.child do + handled = pop.mousepressed(x, y, button, element.child[i]) + if handled then + pop.focused = element.child[i] + break + end + end + end + end + return handled end pop.mousereleased = function(x, y, button, element) if element == nil then diff --git a/lib/pop/init.lua b/lib/pop/init.lua index d77f6cc..3e8b968 100644 --- a/lib/pop/init.lua +++ b/lib/pop/init.lua @@ -10,6 +10,7 @@ local pop = { } pop.elements = { } pop.skins = { } pop.screen = false +pop.focused = false pop.load = function() local elements = filesystem.getDirectoryItems(tostring(path) .. "/elements") for i = 1, #elements do @@ -99,7 +100,24 @@ pop.mousepressed = function(x, y, button, element) element = pop.screen end print("mousepressed", x, y, button, element) - return false + local handled = false + if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then + if element.mousepressed then + handled = element:mousepressed(x - element.x, y - element.y, button) + end + if handled then + pop.focused = element + else + for i = 1, #element.child do + handled = pop.mousepressed(x, y, button, element.child[i]) + if handled then + pop.focused = element.child[i] + break + end + end + end + end + return handled end pop.mousereleased = function(x, y, button, element) if element == nil then diff --git a/src/pop/init.moon b/src/pop/init.moon index 8420818..c688ae2 100644 --- a/src/pop/init.moon +++ b/src/pop/init.moon @@ -9,7 +9,7 @@ pop.elements = {} pop.skins = {} pop.screen = false -- initialized in pop.load() ---pop.focused ? +pop.focused = false -- loads elements and skins, creates pop.screen (intended to only be called once at the beginning) pop.load = -> @@ -72,10 +72,27 @@ pop.draw = (element=pop.screen) -> pop.mousepressed = (x, y, button, element=pop.screen) -> print "mousepressed", x, y, button, element - return false --TODO event handlers return if they have handled the event! + handled = false + if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) + if element.mousepressed + handled = element\mousepressed x - element.x, y - element.y, button + if handled + pop.focused = element + else + for i = 1, #element.child + handled = pop.mousepressed x, y, button, element.child[i] + if handled + pop.focused = element.child[i] + break + return handled pop.mousereleased = (x, y, button, element=pop.screen) -> print "mousereleased", x, y, button, element + --clickHandled = false + --mouseReleaseHandled = false + --if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) + -- efw + -- return false --TODO event handlers return if they have handled the event! pop.keypressed = (key) ->