diff --git a/demo/pop/elements/window.lua b/demo/pop/elements/window.lua index dd247e2..f81d5a3 100644 --- a/demo/pop/elements/window.lua +++ b/demo/pop/elements/window.lua @@ -1,5 +1,8 @@ -local graphics -graphics = love.graphics +local graphics, mouse +do + local _obj_0 = love + graphics, mouse = _obj_0.graphics, _obj_0.mouse +end local insert, remove do local _obj_0 = table @@ -141,6 +144,9 @@ do setTitle = function(self, title) self.title:setText(title) return self + end, + getTitle = function(self) + return self.title:getText() end } _base_0.__index = _base_0 @@ -203,35 +209,31 @@ do end return false end - self.head.mousereleased = function(self, x, y, button) - if button == left then - self.selected = false - pop_ref.focused = false - return true - end - return false - end else self.head.mx = 0 self.head.my = 0 self.head.update = function(self) - return false + local x, y = mouse.getPosition() + return self:setPosition(x - mx, y - my) end self.head.mousepressed = function(self, x, y, button) if button == left then self.selected = true self.mx = x self.my = y - end - end - self.head.mousereleased = function(self, x, y, button) - if button == left then - self.selected = false return true end return false end end + self.head.mousereleased = function(self, x, y, button) + if button == left then + self.selected = false + pop_ref.focused = false + return true + end + return false + end end, __base = _base_0, __name = "window", diff --git a/demo/pop/init.lua b/demo/pop/init.lua index e8b8655..5cd150b 100644 --- a/demo/pop/init.lua +++ b/demo/pop/init.lua @@ -163,7 +163,7 @@ pop.mousereleased = function(x, y, button) do local element = pop.events[button] if element then - if element.clicked and (not element.excludeDraw) and (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then + if element.clicked and (not element.excludeDraw) then do clickedHandled = element:clicked(x - element.x, y - element.y, button) if clickedHandled then @@ -171,7 +171,7 @@ pop.mousereleased = function(x, y, button) end end end - if element.mousereleased and (not element.excludeDraw) then + if element.mousereleased then do mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button) if mousereleasedHandled then @@ -185,14 +185,26 @@ pop.mousereleased = function(x, y, button) end pop.keypressed = function(key) print("keypressed", key) + local element = pop.focused + if element and element.keypressed and (not element.excludeDraw) then + return element.keypressed(key) + end return false end pop.keyreleased = function(key) print("keyreleased", key) + local element = pop.focused + if element and element.keyreleased then + return element.keyreleased(key) + end return false end pop.textinput = function(text) print("textinput", text) + local element = pop.focused + if element and element.textinput and (not element.excludeDraw) then + return element.textinput(text) + end return false end pop.skin = function(element, skin, depth) diff --git a/docs/Excludes.md b/docs/Excludes.md index 1460964..d31e27d 100644 --- a/docs/Excludes.md +++ b/docs/Excludes.md @@ -10,5 +10,14 @@ Note that any element using one of these excludes its children as well. - `excludeUpdate` Excludes an element from being updated (by `pop.update()`). - `excludeDraw` Excludes being rendered (by `pop.draw()`). -**Note**: `excludeDraw` also excludes an element from accepting events (it -wouldn't make sense to have an invisible element capturing text input). +**Note**: `excludeDraw` also excludes an element from accepting the following +events: + +- `mousepressed` +- `clicked` +- `keypressed` +- `textinput` + +The reason for this is that it wouldn't make sense for an invisible element to +be capturing input. However, some events are passed through in case an element +becomes invisible while processing input. diff --git a/docs/dev/Pop.md b/docs/dev/Pop.md index 55ea328..63e6666 100644 --- a/docs/dev/Pop.md +++ b/docs/dev/Pop.md @@ -10,6 +10,6 @@ TODO: Write me. - `pop.mousereleased()` Handling a click maybe should *not* check bounds. Handling a mouse release should maybe *not* check for `excludeDraw`. If an element was already selected and then went invisible, I'm probably breaking - things worse by doing this. + things worse by doing this. This has been changed. [1]: ../Pop.md diff --git a/lib/pop/elements/window.lua b/lib/pop/elements/window.lua index dd247e2..f81d5a3 100644 --- a/lib/pop/elements/window.lua +++ b/lib/pop/elements/window.lua @@ -1,5 +1,8 @@ -local graphics -graphics = love.graphics +local graphics, mouse +do + local _obj_0 = love + graphics, mouse = _obj_0.graphics, _obj_0.mouse +end local insert, remove do local _obj_0 = table @@ -141,6 +144,9 @@ do setTitle = function(self, title) self.title:setText(title) return self + end, + getTitle = function(self) + return self.title:getText() end } _base_0.__index = _base_0 @@ -203,35 +209,31 @@ do end return false end - self.head.mousereleased = function(self, x, y, button) - if button == left then - self.selected = false - pop_ref.focused = false - return true - end - return false - end else self.head.mx = 0 self.head.my = 0 self.head.update = function(self) - return false + local x, y = mouse.getPosition() + return self:setPosition(x - mx, y - my) end self.head.mousepressed = function(self, x, y, button) if button == left then self.selected = true self.mx = x self.my = y - end - end - self.head.mousereleased = function(self, x, y, button) - if button == left then - self.selected = false return true end return false end end + self.head.mousereleased = function(self, x, y, button) + if button == left then + self.selected = false + pop_ref.focused = false + return true + end + return false + end end, __base = _base_0, __name = "window", diff --git a/lib/pop/init.lua b/lib/pop/init.lua index e8b8655..5cd150b 100644 --- a/lib/pop/init.lua +++ b/lib/pop/init.lua @@ -163,7 +163,7 @@ pop.mousereleased = function(x, y, button) do local element = pop.events[button] if element then - if element.clicked and (not element.excludeDraw) and (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then + if element.clicked and (not element.excludeDraw) then do clickedHandled = element:clicked(x - element.x, y - element.y, button) if clickedHandled then @@ -171,7 +171,7 @@ pop.mousereleased = function(x, y, button) end end end - if element.mousereleased and (not element.excludeDraw) then + if element.mousereleased then do mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button) if mousereleasedHandled then @@ -185,14 +185,26 @@ pop.mousereleased = function(x, y, button) end pop.keypressed = function(key) print("keypressed", key) + local element = pop.focused + if element and element.keypressed and (not element.excludeDraw) then + return element.keypressed(key) + end return false end pop.keyreleased = function(key) print("keyreleased", key) + local element = pop.focused + if element and element.keyreleased then + return element.keyreleased(key) + end return false end pop.textinput = function(text) print("textinput", text) + local element = pop.focused + if element and element.textinput and (not element.excludeDraw) then + return element.textinput(text) + end return false end pop.skin = function(element, skin, depth) diff --git a/src/pop/elements/window.moon b/src/pop/elements/window.moon index b1e2049..663c0b3 100644 --- a/src/pop/elements/window.moon +++ b/src/pop/elements/window.moon @@ -1,4 +1,4 @@ -import graphics from love +import graphics, mouse from love import insert, remove from table import sub, len from string @@ -64,33 +64,30 @@ class window extends element return true return false - @head.mousereleased = (x, y, button) => - if button == left - @selected = false - pop_ref.focused = false -- clear our focus - return true - return false - else @head.mx = 0 -- local mouse coordinates when selected @head.my = 0 @head.update = => - --TODO write me! - return false + x, y = mouse.getPosition! + @setPosition x - mx, y - my + --return false -- why? @head.mousepressed = (x, y, button) => if button == left @selected = true @mx = x @my = y - - @head.mousereleased = (x, y, button) => -- this is actually the same for both versions... - if button == left - @selected = false return true return false + @head.mousereleased = (x, y, button) => + if button == left + @selected = false + pop_ref.focused = false -- clear our focus + return true + return false + debugDraw: => graphics.setLineWidth 0.5 graphics.setColor 0, 0, 0, 100 @@ -128,18 +125,6 @@ class window extends element return @ - --update: => - -- if selected, set position based on current mouse position relative to position it was when mousepressed - - --mousemoved: (x, y, dx, dy) => - -- if selected, set position based on new mouse position relative to position it was when mousepressed - - --mousepressed: (x, y, button) => - -- if button == "l" -> selected = true, mouse position saved - - --mousereleased: (x, y, button) => - -- if button == "l" -> set position based on position relative to when mousepressed, selected == false - setSize: (w, h) => x = 0 y = 0 @@ -221,3 +206,6 @@ class window extends element setTitle: (title) => @title\setText title return @ + + getTitle: => + return @title\getText! diff --git a/src/pop/init.moon b/src/pop/init.moon index c925c67..be1028b 100644 --- a/src/pop/init.moon +++ b/src/pop/init.moon @@ -136,11 +136,11 @@ pop.mousereleased = (x, y, button) -> mousereleasedHandled = false if element = pop.events[button] - if element.clicked and (not element.excludeDraw) and (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) + if element.clicked and (not element.excludeDraw) --and (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) if clickedHandled = element\clicked x - element.x, y - element.y, button pop.events[button] = nil - if element.mousereleased and (not element.excludeDraw) + if element.mousereleased if mousereleasedHandled = element\mousereleased x - element.x, y - element.y, button pop.events[button] = nil @@ -148,15 +148,30 @@ pop.mousereleased = (x, y, button) -> pop.keypressed = (key) -> print "keypressed", key - return false --TODO event handlers return if they have handled the event! + + element = pop.focused + if element and element.keypressed and (not element.excludeDraw) + return element.keypressed key + + return false pop.keyreleased = (key) -> print "keyreleased", key - return false --TODO event handlers return if they have handled the event! + + element = pop.focused + if element and element.keyreleased + return element.keyreleased key + + return false pop.textinput = (text) -> print "textinput", text - return false --TODO event handlers return if they have handled the event! + + element = pop.focused + if element and element.textinput and (not element.excludeDraw) + return element.textinput text + + return false --TODO rewrite skin system to not rely on knowing internals of elements, -- instead call functions like setColor and setBackground