diff --git a/demo/main.lua b/demo/main.lua index 3b3bc6a..3099637 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -17,7 +17,7 @@ function love.load() pop.box(c, {0, 255, 255}):align("right", "bottom"):setSize(50, 100):move(-50) pop.text(nil, "Here's some test text\n(with newlines)\nin the top left corner!") pop.text(nil, "Here's some test text in the bottom right corner!"):align("right", "bottom") - pop.skin(pop.text("Here's easier-to-code test text in the center!"):align("center", "center", true)) -- 'true' means align to pixel! + --pop.skin(pop.text("Here's easier-to-code test text in the center!"):align("center", "center", true)) -- 'true' means align to pixel! w = pop.box(nil, {255, 255, 255, 255}):align(false, "bottom"):setSize(150, 150) b = pop.box(w, {0, 0, 0, 255}):setMargin(5):setSize(100, 100) --]] diff --git a/demo/pop/elements/box.lua b/demo/pop/elements/box.lua index 6b9fb2f..292164d 100644 --- a/demo/pop/elements/box.lua +++ b/demo/pop/elements/box.lua @@ -71,11 +71,11 @@ do _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ - __init = function(self, parent, background) + __init = function(self, pop, parent, background) if background == nil then background = false end - _class_0.__parent.__init(self, parent) + _class_0.__parent.__init(self, pop, parent) self.w = 20 self.h = 20 self.background = background diff --git a/demo/pop/elements/element.lua b/demo/pop/elements/element.lua index 60ab9df..ed5b479 100644 --- a/demo/pop/elements/element.lua +++ b/demo/pop/elements/element.lua @@ -206,7 +206,8 @@ do } _base_0.__index = _base_0 _class_0 = setmetatable({ - __init = function(self, parent) + __init = function(self, pop, parent) + self.pop = pop self.parent = parent self.child = { } self.w = 0 diff --git a/demo/pop/elements/text.lua b/demo/pop/elements/text.lua index 44ee6a3..eecd15b 100644 --- a/demo/pop/elements/text.lua +++ b/demo/pop/elements/text.lua @@ -106,7 +106,7 @@ do _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ - __init = function(self, parent, text, color) + __init = function(self, pop, parent, text, color) if text == nil then text = "" end @@ -118,7 +118,10 @@ do 255 } end - _class_0.__parent.__init(self, parent) + print("---===---") + print(self, pop, parent, text, color) + print("---===---") + _class_0.__parent.__init(self, pop, parent) self.font = graphics.newFont(14) self:setText(text) self.color = color diff --git a/demo/pop/elements/window.lua b/demo/pop/elements/window.lua index a3b8f47..3e896fd 100644 --- a/demo/pop/elements/window.lua +++ b/demo/pop/elements/window.lua @@ -10,7 +10,7 @@ local element = require(tostring(path) .. "/element") local box = require(tostring(path) .. "/box") local text = require(tostring(path) .. "/text") local left = 1 -local move_event = true +local mousemoved_event = true do local major, minor, revision = love.getVersion() if (major == 0) and (minor == 10) and ((revision == 0) or (revision == 1)) then @@ -19,7 +19,7 @@ do if (major == 0) and (minor == 9) then left = "l" if revision == 1 then - move_event = false + mousemoved_event = false end else print("elements/window: unrecognized LÖVE version: " .. tostring(major) .. "." .. tostring(minor) .. "." .. tostring(revision)) @@ -111,7 +111,7 @@ do _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ - __init = function(self, parent, title, tBackground, tColor, wBackground) + __init = function(self, pop, parent, title, tBackground, tColor, wBackground) if title == nil then title = "window" end @@ -139,8 +139,10 @@ do 255 } end - _class_0.__parent.__init(self, parent) + print((parent == pop.screen), (title == "Window")) + _class_0.__parent.__init(self, pop, parent) self.head = box(self, tBackground) + print(self, title, tColor) self.title = text(self, title, tColor) self.window = box(self, wBackground) local height = self.title:getHeight() @@ -152,6 +154,56 @@ do self.title, self.window } + self.head.selected = false + if mousemoved_event then + self.head.mousemoved = function(self, x, y, dx, dy) + print("mousemoved CALLED!") + if self.selected then + self.parent:move(dx, dy) + return true + end + return false + end + self.head.mousepressed = function(self, x, y, button) + print("mousepressed CALLED!") + if button == left then + print("selected!") + self.selected = true + return true + end + return false + end + self.head.mousereleased = function(self, x, y, button) + print("mousereleased CALLED!") + if button == left then + self.selected = false + self.pop.focused = false + return true + end + print("ERROR FELL THROUGH") + return false + end + else + self.head.mx = 0 + self.head.my = 0 + self.head.update = function(self) + return false + 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 end, __base = _base_0, __name = "window", diff --git a/demo/pop/init.lua b/demo/pop/init.lua index e27518c..37f7aef 100644 --- a/demo/pop/init.lua +++ b/demo/pop/init.lua @@ -87,12 +87,12 @@ pop.create = function(element, parent, ...) parent = pop.screen end if inheritsFromElement(parent) then - element = pop.elements[element](parent, ...) + element = pop.elements[element](pop, parent, ...) insert(parent.child, element) elseif parent == false then - element = pop.elements[element](false, ...) + element = pop.elements[element](pop, false, ...) else - element = pop.elements[element](pop.screen, parent, ...) + element = pop.elements[element](pop, pop.screen, parent, ...) insert(pop.screen.child, element) end return element @@ -140,12 +140,12 @@ pop.mousepressed = function(x, y, button, element) handled = element:mousepressed(x - element.x, y - element.y, button) end if handled then + print("pop.focused has been set!") 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 diff --git a/lib/pop/elements/box.lua b/lib/pop/elements/box.lua index 6b9fb2f..292164d 100644 --- a/lib/pop/elements/box.lua +++ b/lib/pop/elements/box.lua @@ -71,11 +71,11 @@ do _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ - __init = function(self, parent, background) + __init = function(self, pop, parent, background) if background == nil then background = false end - _class_0.__parent.__init(self, parent) + _class_0.__parent.__init(self, pop, parent) self.w = 20 self.h = 20 self.background = background diff --git a/lib/pop/elements/element.lua b/lib/pop/elements/element.lua index 60ab9df..ed5b479 100644 --- a/lib/pop/elements/element.lua +++ b/lib/pop/elements/element.lua @@ -206,7 +206,8 @@ do } _base_0.__index = _base_0 _class_0 = setmetatable({ - __init = function(self, parent) + __init = function(self, pop, parent) + self.pop = pop self.parent = parent self.child = { } self.w = 0 diff --git a/lib/pop/elements/text.lua b/lib/pop/elements/text.lua index 44ee6a3..eecd15b 100644 --- a/lib/pop/elements/text.lua +++ b/lib/pop/elements/text.lua @@ -106,7 +106,7 @@ do _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ - __init = function(self, parent, text, color) + __init = function(self, pop, parent, text, color) if text == nil then text = "" end @@ -118,7 +118,10 @@ do 255 } end - _class_0.__parent.__init(self, parent) + print("---===---") + print(self, pop, parent, text, color) + print("---===---") + _class_0.__parent.__init(self, pop, parent) self.font = graphics.newFont(14) self:setText(text) self.color = color diff --git a/lib/pop/elements/window.lua b/lib/pop/elements/window.lua index a3b8f47..3e896fd 100644 --- a/lib/pop/elements/window.lua +++ b/lib/pop/elements/window.lua @@ -10,7 +10,7 @@ local element = require(tostring(path) .. "/element") local box = require(tostring(path) .. "/box") local text = require(tostring(path) .. "/text") local left = 1 -local move_event = true +local mousemoved_event = true do local major, minor, revision = love.getVersion() if (major == 0) and (minor == 10) and ((revision == 0) or (revision == 1)) then @@ -19,7 +19,7 @@ do if (major == 0) and (minor == 9) then left = "l" if revision == 1 then - move_event = false + mousemoved_event = false end else print("elements/window: unrecognized LÖVE version: " .. tostring(major) .. "." .. tostring(minor) .. "." .. tostring(revision)) @@ -111,7 +111,7 @@ do _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ - __init = function(self, parent, title, tBackground, tColor, wBackground) + __init = function(self, pop, parent, title, tBackground, tColor, wBackground) if title == nil then title = "window" end @@ -139,8 +139,10 @@ do 255 } end - _class_0.__parent.__init(self, parent) + print((parent == pop.screen), (title == "Window")) + _class_0.__parent.__init(self, pop, parent) self.head = box(self, tBackground) + print(self, title, tColor) self.title = text(self, title, tColor) self.window = box(self, wBackground) local height = self.title:getHeight() @@ -152,6 +154,56 @@ do self.title, self.window } + self.head.selected = false + if mousemoved_event then + self.head.mousemoved = function(self, x, y, dx, dy) + print("mousemoved CALLED!") + if self.selected then + self.parent:move(dx, dy) + return true + end + return false + end + self.head.mousepressed = function(self, x, y, button) + print("mousepressed CALLED!") + if button == left then + print("selected!") + self.selected = true + return true + end + return false + end + self.head.mousereleased = function(self, x, y, button) + print("mousereleased CALLED!") + if button == left then + self.selected = false + self.pop.focused = false + return true + end + print("ERROR FELL THROUGH") + return false + end + else + self.head.mx = 0 + self.head.my = 0 + self.head.update = function(self) + return false + 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 end, __base = _base_0, __name = "window", diff --git a/lib/pop/init.lua b/lib/pop/init.lua index e27518c..37f7aef 100644 --- a/lib/pop/init.lua +++ b/lib/pop/init.lua @@ -87,12 +87,12 @@ pop.create = function(element, parent, ...) parent = pop.screen end if inheritsFromElement(parent) then - element = pop.elements[element](parent, ...) + element = pop.elements[element](pop, parent, ...) insert(parent.child, element) elseif parent == false then - element = pop.elements[element](false, ...) + element = pop.elements[element](pop, false, ...) else - element = pop.elements[element](pop.screen, parent, ...) + element = pop.elements[element](pop, pop.screen, parent, ...) insert(pop.screen.child, element) end return element @@ -140,12 +140,12 @@ pop.mousepressed = function(x, y, button, element) handled = element:mousepressed(x - element.x, y - element.y, button) end if handled then + print("pop.focused has been set!") 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 diff --git a/src/pop/elements/box.moon b/src/pop/elements/box.moon index 2450eb3..73421c2 100644 --- a/src/pop/elements/box.moon +++ b/src/pop/elements/box.moon @@ -5,8 +5,8 @@ path = sub ..., 1, len(...) - len "/box" element = require "#{path}/element" class box extends element - new: (parent, background=false) => - super parent + new: (pop, parent, background=false) => + super pop, parent @w = 20 @h = 20 diff --git a/src/pop/elements/element.moon b/src/pop/elements/element.moon index b3f8c83..c37de2b 100644 --- a/src/pop/elements/element.moon +++ b/src/pop/elements/element.moon @@ -2,7 +2,8 @@ import graphics from love import floor from math class element - new: (parent) => + new: (pop, parent) => + @pop = pop @parent = parent @child = {} diff --git a/src/pop/elements/text.moon b/src/pop/elements/text.moon index d23833d..0fa8185 100644 --- a/src/pop/elements/text.moon +++ b/src/pop/elements/text.moon @@ -12,8 +12,12 @@ class text extends element else return pop.create("text", parent, ...) - new: (parent, text="", color={255,255,255,255}) => - super parent + new: (pop, parent, text="", color={255,255,255,255}) => + print("---===---") + print(@, pop, parent, text, color) + print("---===---") + + super pop, parent @font = graphics.newFont 14 @setText text diff --git a/src/pop/elements/window.moon b/src/pop/elements/window.moon index c25a2d2..b635006 100644 --- a/src/pop/elements/window.moon +++ b/src/pop/elements/window.moon @@ -8,7 +8,7 @@ text = require "#{path}/text" -- version compatibility left = 1 -- what is the left mouse button? -move_event = true -- is the mousemoved event available? +mousemoved_event = true -- is the mousemoved event available? do major, minor, revision = love.getVersion! @@ -17,16 +17,18 @@ do if (major == 0) and (minor == 9) left = "l" if revision == 1 - move_event = false + mousemoved_event = false else print "elements/window: unrecognized LÖVE version: #{major}.#{minor}.#{revision}" print " assuming LÖVE version > 0.10.1 (there may be bugs)" class window extends element - new: (parent, title="window", tBackground={25, 180, 230, 255}, tColor={255, 255, 255, 255}, wBackground={200, 200, 210, 255}) => - super parent + new: (pop, parent, title="window", tBackground={25, 180, 230, 255}, tColor={255, 255, 255, 255}, wBackground={200, 200, 210, 255}) => + print (parent == pop.screen), (title == "Window") + super pop, parent @head = box @, tBackground -- title box at top + print @, title, tColor @title = text @, title, tColor -- text at top @window = box @, wBackground -- main window area @@ -44,6 +46,53 @@ class window extends element --@selected = false -- whether or not the window title (and thus, the window) has been selected --NOTE all of these commented out, because I realized these event handlers should be attached to the title element + @head.selected = false -- whether or not the window title (and thus, the window) has been selected + + if mousemoved_event + @head.mousemoved = (x, y, dx, dy) => + print "mousemoved CALLED!" + if @selected + @parent\move dx, dy + return true + return false + + @head.mousepressed = (x, y, button) => + print "mousepressed CALLED!" + if button == left + print "selected!" + @selected = true + return true + return false + + @head.mousereleased = (x, y, button) => + print "mousereleased CALLED!" + if button == left + @selected = false + @pop.focused = false -- we need to have a way to clear + return true + print "ERROR FELL THROUGH" + return false + + else + @head.mx = 0 -- local mouse coordinates when selected + @head.my = 0 + + @head.update = => + --TODO write me! + return false + + @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 + debugDraw: => graphics.setLineWidth 0.5 graphics.setColor 0, 0, 0, 100 diff --git a/src/pop/init.moon b/src/pop/init.moon index 5f7e48b..dfbde72 100644 --- a/src/pop/init.moon +++ b/src/pop/init.moon @@ -71,12 +71,12 @@ pop.load = -> -- creates an element with specified parent (parent can be false or non-existent) pop.create = (element, parent=pop.screen, ...) -> if inheritsFromElement parent - element = pop.elements[element](parent, ...) + element = pop.elements[element](pop, parent, ...) insert parent.child, element elseif parent == false - element = pop.elements[element](false, ...) + element = pop.elements[element](pop, false, ...) else - element = pop.elements[element](pop.screen, parent, ...) + element = pop.elements[element](pop, pop.screen, parent, ...) insert pop.screen.child, element return element @@ -114,12 +114,13 @@ pop.mousepressed = (x, y, button, element) -> if element.mousepressed handled = element\mousepressed x - element.x, y - element.y, button if handled + print "pop.focused has been set!" 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] + --pop.focused = element.child[i] break if handled pop.events[button] = element