From 441e0647b886f0d14cba68db85cc3a886a267bed Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Wed, 20 Apr 2016 14:12:32 -0700 Subject: [PATCH] commenting and fixed a bug with mousehandling --- demo/main.lua | 4 +- demo/pop/elements/window.lua | 34 +++++++------- demo/pop/init.lua | 23 ++++++--- lib/pop/elements/window.lua | 34 +++++++------- lib/pop/init.lua | 23 ++++++--- src/pop/init.moon | 91 ++++++++++++++---------------------- 6 files changed, 106 insertions(+), 103 deletions(-) diff --git a/demo/main.lua b/demo/main.lua index 66f2e78..f3b9ac4 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -86,6 +86,7 @@ function love.update(dt) if not videoFile:isPlaying() then videoFile:rewind() + videoFile:play() -- sometimes rewinding at the end of play fails to start a loop end end @@ -94,7 +95,6 @@ function love.draw() if debugDraw then pop.debugDraw() - --w2:debugDraw() end end @@ -131,7 +131,7 @@ function love.keypressed(key) end if (key == "p") and (not handled) then - pop.printElementStack() + pop.printElementTree() end if (key == "escape") and (not handled) then diff --git a/demo/pop/elements/window.lua b/demo/pop/elements/window.lua index 9efb979..68fecd5 100644 --- a/demo/pop/elements/window.lua +++ b/demo/pop/elements/window.lua @@ -54,12 +54,12 @@ do return self end, addChild = function(self, child) - self.window:addChild(child) + self.area:addChild(child) return self end, removeChild = function(self, child) - local result = self.window:removeChild(child) - if result == self.window then + local result = self.area:removeChild(child) + if result == self.area then return self elseif type(result) == "string" then for k, v in ipairs(self.child) do @@ -74,14 +74,14 @@ do end end, getChildren = function(self) - return self.window.child + return self.area.child end, align = function(self, horizontal, vertical, toPixel) _class_0.__parent.__base.align(self, horizontal, vertical, toPixel) for i = 1, #self.child do self.child[i]:align() end - self.window:move(nil, self.head:getHeight()) + self.area:move(nil, self.head:getHeight()) return self end, setSize = function(self, w, h) @@ -99,7 +99,7 @@ do else self.head:setWidth(w) end - self.window:setWidth(w) + self.area:setWidth(w) self.w = w self.x = self.x + x self.title:align() @@ -115,12 +115,12 @@ do elseif "right" == _exp_0 then y = y - (h - self.h) end - self.window:setHeight(h) + self.area:setHeight(h) self.h = h + self.head:getHeight() self.y = self.y + y end self.head:move(x, y) - self.window:move(x, y) + self.area:move(x, y) return self end, setWidth = function(self, w) @@ -136,7 +136,7 @@ do else self.head:setWidth(w) end - self.window:setWidth(w) + self.area:setWidth(w) self.w = w self.x = self.x + x self.title:align() @@ -144,7 +144,7 @@ do self.close:align() end self.head:move(x) - self.window:move(x) + self.area:move(x) return self end, setHeight = function(self, h) @@ -156,12 +156,12 @@ do elseif "right" == _exp_0 then y = y - (h - self.h) end - self.window:setHeight(h) + self.area:setHeight(h) self.h = h + self.head:getHeight() self.y = self.y + y self.head:move(nil, y) self.title:move(nil, y) - self.window:move(nil, y) + self.area:move(nil, y) return self end, setTitle = function(self, title) @@ -250,24 +250,24 @@ do _class_0.__parent.__init(self, parent) self.head = box(self, tBackground) self.title = text(self.head, title, tColor) - self.window = box(self, wBackground) + self.area = box(self, wBackground) self.close = box(self, closeImage) local height = self.title:getHeight() self.head:setSize(self.w - height, height) - self.window:move(nil, height) + self.area:move(nil, height) self.close:align("right"):setSize(height, height) self:setSize(100, 80) self.child = { self.head, self.title, - self.window, + self.area, self.close } self.titleOverflow = "trunicate" - self.window.mousepressed = function() + self.area.mousepressed = function() return true end - self.window.clicked = function() + self.area.clicked = function() return true end self.close.clicked = function() diff --git a/demo/pop/init.lua b/demo/pop/init.lua index 88c3dc8..27eb383 100644 --- a/demo/pop/init.lua +++ b/demo/pop/init.lua @@ -1,3 +1,10 @@ +local pop = { + _VERSION = 'Pop.Box v0.0.0', + _DESCRIPTION = 'GUI library for LOVE, designed for ease of use', + _URL = 'http://github.com/Guard13007/Pop.Box', + _LICENSE = 'The MIT License (MIT)', + _AUTHOR = 'Paul Liverman III' +} if not (love.getVersion) then error("Pop.Box only supports LOVE versions >= 0.9.1") end @@ -11,7 +18,6 @@ insert = table.insert local inheritsFromElement inheritsFromElement = require(tostring(...) .. "/util").inheritsFromElement local path = ... -local pop = { } pop.elements = { } pop.skins = { } pop.screen = false @@ -139,9 +145,11 @@ pop.mousepressed = function(x, y, button, element) local handled = false if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then for i = #element.child, 1, -1 do - handled = pop.mousepressed(x, y, button, element.child[i]) - if handled then - break + do + handled = pop.mousepressed(x, y, button, element.child[i]) + if handled then + return handled + end end end if not (handled) then @@ -165,7 +173,7 @@ pop.mousereleased = function(x, y, button, element) for i = #element.child, 1, -1 do clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.child[i]) if clickedHandled or mousereleasedHandled then - break + return clickedHandled, mousereleasedHandled end end if not (clickedHandled or mousereleasedHandled) then @@ -180,6 +188,9 @@ pop.mousereleased = function(x, y, button, element) end end end + else + print("mousereleased", x, y, button) + pop.mousereleased(x, y, button, pop.screen) end return clickedHandled, mousereleasedHandled end @@ -254,7 +265,7 @@ pop.debugDraw = function(element) pop.debugDraw(element.child[i]) end end -pop.printElementStack = function(element, depth) +pop.printElementTree = function(element, depth) if element == nil then element = pop.screen end diff --git a/lib/pop/elements/window.lua b/lib/pop/elements/window.lua index 9efb979..68fecd5 100644 --- a/lib/pop/elements/window.lua +++ b/lib/pop/elements/window.lua @@ -54,12 +54,12 @@ do return self end, addChild = function(self, child) - self.window:addChild(child) + self.area:addChild(child) return self end, removeChild = function(self, child) - local result = self.window:removeChild(child) - if result == self.window then + local result = self.area:removeChild(child) + if result == self.area then return self elseif type(result) == "string" then for k, v in ipairs(self.child) do @@ -74,14 +74,14 @@ do end end, getChildren = function(self) - return self.window.child + return self.area.child end, align = function(self, horizontal, vertical, toPixel) _class_0.__parent.__base.align(self, horizontal, vertical, toPixel) for i = 1, #self.child do self.child[i]:align() end - self.window:move(nil, self.head:getHeight()) + self.area:move(nil, self.head:getHeight()) return self end, setSize = function(self, w, h) @@ -99,7 +99,7 @@ do else self.head:setWidth(w) end - self.window:setWidth(w) + self.area:setWidth(w) self.w = w self.x = self.x + x self.title:align() @@ -115,12 +115,12 @@ do elseif "right" == _exp_0 then y = y - (h - self.h) end - self.window:setHeight(h) + self.area:setHeight(h) self.h = h + self.head:getHeight() self.y = self.y + y end self.head:move(x, y) - self.window:move(x, y) + self.area:move(x, y) return self end, setWidth = function(self, w) @@ -136,7 +136,7 @@ do else self.head:setWidth(w) end - self.window:setWidth(w) + self.area:setWidth(w) self.w = w self.x = self.x + x self.title:align() @@ -144,7 +144,7 @@ do self.close:align() end self.head:move(x) - self.window:move(x) + self.area:move(x) return self end, setHeight = function(self, h) @@ -156,12 +156,12 @@ do elseif "right" == _exp_0 then y = y - (h - self.h) end - self.window:setHeight(h) + self.area:setHeight(h) self.h = h + self.head:getHeight() self.y = self.y + y self.head:move(nil, y) self.title:move(nil, y) - self.window:move(nil, y) + self.area:move(nil, y) return self end, setTitle = function(self, title) @@ -250,24 +250,24 @@ do _class_0.__parent.__init(self, parent) self.head = box(self, tBackground) self.title = text(self.head, title, tColor) - self.window = box(self, wBackground) + self.area = box(self, wBackground) self.close = box(self, closeImage) local height = self.title:getHeight() self.head:setSize(self.w - height, height) - self.window:move(nil, height) + self.area:move(nil, height) self.close:align("right"):setSize(height, height) self:setSize(100, 80) self.child = { self.head, self.title, - self.window, + self.area, self.close } self.titleOverflow = "trunicate" - self.window.mousepressed = function() + self.area.mousepressed = function() return true end - self.window.clicked = function() + self.area.clicked = function() return true end self.close.clicked = function() diff --git a/lib/pop/init.lua b/lib/pop/init.lua index 88c3dc8..27eb383 100644 --- a/lib/pop/init.lua +++ b/lib/pop/init.lua @@ -1,3 +1,10 @@ +local pop = { + _VERSION = 'Pop.Box v0.0.0', + _DESCRIPTION = 'GUI library for LOVE, designed for ease of use', + _URL = 'http://github.com/Guard13007/Pop.Box', + _LICENSE = 'The MIT License (MIT)', + _AUTHOR = 'Paul Liverman III' +} if not (love.getVersion) then error("Pop.Box only supports LOVE versions >= 0.9.1") end @@ -11,7 +18,6 @@ insert = table.insert local inheritsFromElement inheritsFromElement = require(tostring(...) .. "/util").inheritsFromElement local path = ... -local pop = { } pop.elements = { } pop.skins = { } pop.screen = false @@ -139,9 +145,11 @@ pop.mousepressed = function(x, y, button, element) local handled = false if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then for i = #element.child, 1, -1 do - handled = pop.mousepressed(x, y, button, element.child[i]) - if handled then - break + do + handled = pop.mousepressed(x, y, button, element.child[i]) + if handled then + return handled + end end end if not (handled) then @@ -165,7 +173,7 @@ pop.mousereleased = function(x, y, button, element) for i = #element.child, 1, -1 do clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.child[i]) if clickedHandled or mousereleasedHandled then - break + return clickedHandled, mousereleasedHandled end end if not (clickedHandled or mousereleasedHandled) then @@ -180,6 +188,9 @@ pop.mousereleased = function(x, y, button, element) end end end + else + print("mousereleased", x, y, button) + pop.mousereleased(x, y, button, pop.screen) end return clickedHandled, mousereleasedHandled end @@ -254,7 +265,7 @@ pop.debugDraw = function(element) pop.debugDraw(element.child[i]) end end -pop.printElementStack = function(element, depth) +pop.printElementTree = function(element, depth) if element == nil then element = pop.screen end diff --git a/src/pop/init.moon b/src/pop/init.moon index dfdfa92..fe91402 100644 --- a/src/pop/init.moon +++ b/src/pop/init.moon @@ -1,30 +1,9 @@ pop = { - _VERSION = 'Pop.Box v0.0.0' - _DESCRIPTION = 'A GUI library for LOVE.' - _URL = 'http://github.com/Guard13007/Pop.Box' - _LICENSE = ' - The MIT License (MIT) - - Copyright (c) 2015-2016 Paul Liverman III - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - ' + _VERSION: 'Pop.Box v0.0.0' + _DESCRIPTION: 'GUI library for LOVE, designed for ease of use' + _URL: 'http://github.com/Guard13007/Pop.Box' + _LICENSE: 'The MIT License (MIT)' + _AUTHOR: 'Paul Liverman III' } unless love.getVersion @@ -38,7 +17,6 @@ path = ... pop.elements = {} pop.skins = {} ---pop.events = {} --NOTE leave this commented out for now, as it may be needed again pop.screen = false -- initialized in pop.load() pop.focused = false @@ -71,7 +49,7 @@ pop.load = -> print "wrapper created: \"pop.#{name}()\"" - -- works just like above, except no wrappers + -- works just like above, except no load calls or wrappers skins = filesystem.getDirectoryItems "#{path}/skins" for i = 1, #skins @@ -83,7 +61,7 @@ pop.load = -> print "skin loaded: \"#{name}\"" - -- load extensions by just running them via require + -- (again, similar) load extensions by just running them via require extensions = filesystem.getDirectoryItems "#{path}/extensions" for i = 1, #extensions @@ -95,20 +73,20 @@ pop.load = -> print "extension loaded: \"#{name}\"" - -- main window (called screen because there is a window element class) + -- GUI screen area pop.screen = pop.create("element", false)\setSize(graphics.getWidth!, graphics.getHeight!) print "created \"pop.screen\"" --- creates an element with specified parent (parent can be false or non-existent) +-- creates an element (parent is an element, false, or nil (defaults to pop.screen)) pop.create = (element, parent=pop.screen, ...) -> - -- if valid parent element (includes default of pop.screen when no parent has been passed) + -- if valid parent element if inheritsFromElement parent element = pop.elements[element](parent, ...) insert parent.child, element -- if explicitly no parent elseif parent == false element = pop.elements[element](false, ...) - -- else we use pop.screen, and "parent" is actually first argument + -- else use pop.screen, and "parent" is actually first argument else element = pop.elements[element](pop.screen, parent, ...) insert pop.screen.child, element @@ -129,6 +107,7 @@ pop.draw = (element=pop.screen) -> for i = 1, #element.child pop.draw element.child[i] +--TODO implement a way for an element to attach itself to mousemoved events pop.mousemoved = (x, y, dx, dy) -> if pop.focused and pop.focused.mousemoved return pop.focused\mousemoved x, y, dx, dy @@ -136,68 +115,68 @@ pop.mousemoved = (x, y, dx, dy) -> return false pop.mousepressed = (x, y, button, element) -> + -- start at the screen, print that we received an event unless element print "mousepressed", x, y, button element = pop.screen + -- have we handled the event? handled = false + -- if it was inside the current element.. if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) + -- check its child elements in reverse order, returning if something handles it for i = #element.child, 1, -1 - handled = pop.mousepressed x, y, button, element.child[i] - if handled - break + if handled = pop.mousepressed x, y, button, element.child[i] + return handled + -- if a child hasn't handled it yet unless handled + -- if we can handle it and are visible, try to handle it, and set pop.focused if element.mousepressed and (not element.excludeDraw) if handled = element\mousepressed x - element.x, y - element.y, button pop.focused = element - --NOTE this might end up being needed in the future - -- if it is, add an ability for a mousepressed handler to cancel saving - -- the event, and make sure the window element's area does this - --pop.events[button] = element + -- have we handled the event? return handled pop.mousereleased = (x, y, button, element) -> + -- we are trying to handle a clicked or mousereleased event clickedHandled = false mousereleasedHandled = false + -- if we have an element, and are within its bounds if element if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) + -- check its children in reverse for handling a clicked or mousereleased event for i = #element.child, 1, -1 clickedHandled, mousereleasedHandled = pop.mousereleased x, y, button, element.child[i] if clickedHandled or mousereleasedHandled - break + return clickedHandled, mousereleasedHandled + -- if that doesn't work, we try to handle it ourselves unless clickedHandled or mousereleasedHandled + -- clicked only happens on visible elements, mousereleased happens either way if element.clicked and (not element.excludeDraw) clickedHandled = element\clicked x - element.x, y - element.y, button if element.mousereleased mousereleasedHandled = element\mousereleased x - element.x, y - element.y, button + -- if we clicked, we're focused! if clickedHandled pop.focused = element - --else - -- print "mousereleased", x, y, button - -- 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 clickedHandled = element\clicked x - element.x, y - element.y, button - -- pop.events[button] = nil - - -- if element.mousereleased - -- if mousereleasedHandled = element\mousereleased x - element.x, y - element.y, button - -- pop.events[button] = nil - - -- if (not clickedHandled) and (not mousereleasedHandled) - -- clickedHandled, mousereleasedHandled = pop.mousereleased x, y, button, pop.screen + -- else, default to pop.screen to begin! (and print that we received an event) + else + print "mousereleased", x, y, button + pop.mousereleased x, y, button, pop.screen return clickedHandled, mousereleasedHandled pop.keypressed = (key) -> print "keypressed", key + -- keypressed events must be on visible elements element = pop.focused if element and element.keypressed and (not element.excludeDraw) return element.keypressed key @@ -207,6 +186,7 @@ pop.keypressed = (key) -> pop.keyreleased = (key) -> print "keyreleased", key + -- keyreleased events are always called element = pop.focused if element and element.keyreleased return element.keyreleased key @@ -216,6 +196,7 @@ pop.keyreleased = (key) -> pop.textinput = (text) -> print "textinput", text + -- textinput events must be on visible elements element = pop.focused if element and element.textinput and (not element.excludeDraw) return element.textinput text @@ -258,7 +239,7 @@ pop.debugDraw = (element=pop.screen) -> for i = 1, #element.child pop.debugDraw element.child[i] -pop.printElementStack = (element=pop.screen, depth=0) -> +pop.printElementTree = (element=pop.screen, depth=0) -> cls = element.__class.__name if cls == "text"