diff --git a/demo/pop/elements/element.lua b/demo/pop/elements/element.lua index 9f409ee..d94dae6 100644 --- a/demo/pop/elements/element.lua +++ b/demo/pop/elements/element.lua @@ -16,6 +16,11 @@ do graphics.print("e", self.x, self.y) return self end, + addChild = function(self, child) + self.child[#self.child + 1] = child + child.parent = self + return self + end, move = function(self, x, y) if x then self.x = self.x + x diff --git a/demo/pop/init.lua b/demo/pop/init.lua index a86e93b..5275323 100644 --- a/demo/pop/init.lua +++ b/demo/pop/init.lua @@ -63,6 +63,9 @@ pop.create = function(element, parent, ...) if parent == nil then parent = pop.screen end + if parent then + print(parent.__class, parent.__class.__name, parent.__class.__base, parent.__class.__parent) + end element = pop.elements[element](parent, ...) if parent then insert(parent.child, element) diff --git a/lib/pop/elements/element.lua b/lib/pop/elements/element.lua index 9f409ee..d94dae6 100644 --- a/lib/pop/elements/element.lua +++ b/lib/pop/elements/element.lua @@ -16,6 +16,11 @@ do graphics.print("e", self.x, self.y) return self end, + addChild = function(self, child) + self.child[#self.child + 1] = child + child.parent = self + return self + end, move = function(self, x, y) if x then self.x = self.x + x diff --git a/lib/pop/init.lua b/lib/pop/init.lua index a86e93b..5275323 100644 --- a/lib/pop/init.lua +++ b/lib/pop/init.lua @@ -63,6 +63,9 @@ pop.create = function(element, parent, ...) if parent == nil then parent = pop.screen end + if parent then + print(parent.__class, parent.__class.__name, parent.__class.__base, parent.__class.__parent) + end element = pop.elements[element](parent, ...) if parent then insert(parent.child, element) diff --git a/src/pop/init.moon b/src/pop/init.moon index 477faf4..aae4c28 100644 --- a/src/pop/init.moon +++ b/src/pop/init.moon @@ -51,10 +51,30 @@ pop.load = -> pop.screen = pop.create("element", false)\setSize(graphics.getWidth!, graphics.getHeight!) print "created \"pop.screen\"" +instanceOfElement = (object) -> + if object.__class + class = object.__class + + if class.__name == "element" + return true + + while class.__parent + class = class.__parent + if class.__name == "element" + return true + + return false + -- creates an element with specified parent (parent can be false) pop.create = (element, parent=pop.screen, ...) -> - -- 1: if parent is object, use it, 2: if parent is false, use it (use false), 3: if parent is nil, use pop.screen as parent - element = pop.elements[element](parent, ...) + --if parent + -- print parent.__class, parent.__class.__name, parent.__class.__base, parent.__class.__parent + --element = pop.elements[element](parent, ...) + + if instanceOfElement parent + element = pop.elements[element](parent, ...) + else + element = pop.elements[element](pop.screen, parent, ...) if parent insert parent.child, element