From bd158e00992e21648433f796485815a9bfc423e5 Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Sun, 1 May 2016 01:55:50 -0700 Subject: [PATCH] close #42 data-driven! --- demo/main.lua | 12 +- demo/pop/elements/box.lua | 40 +-- demo/pop/elements/element.lua | 205 +++++++-------- demo/pop/elements/text.lua | 48 ++-- demo/pop/elements/window.lua | 237 +++++++++--------- demo/pop/init.lua | 76 +++--- lib/pop/elements/box.lua | 40 +-- lib/pop/elements/element.lua | 205 +++++++-------- lib/pop/elements/text.lua | 48 ++-- lib/pop/elements/window.lua | 237 +++++++++--------- lib/pop/init.lua | 76 +++--- src/pop/elements/box.moon | 40 +-- src/pop/elements/element.moon | 223 +++++++++-------- src/pop/elements/text.moon | 48 ++-- src/pop/elements/window.moon | 260 ++++++++++---------- src/pop/extensions/streamlined_get_set.moon | 7 + src/pop/init.moon | 85 ++++--- src/pop/skins/default.moon | 3 + 18 files changed, 956 insertions(+), 934 deletions(-) diff --git a/demo/main.lua b/demo/main.lua index e4247d1..a83f29f 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -37,7 +37,7 @@ function love.load() w2:move(-50, 80) w2:setHeight(500) w2:move(0, -175) - w2.title:align("center") + w2.data.title:align("center") w2:position(0, 0) w2:size(200, 120):position(90, 70) w2:setClose(false) @@ -54,9 +54,9 @@ function love.load() b = pop.box(G, test):align("right"):move(-25):setWidth(40) c = pop.box(G, test):align("right"):move(0, 25):setHeight(40) - print(a.horizontal, a.vertical) - print(b.horizontal, b.vertical) - print(c.horizontal, c.vertical) + print(a.data.horizontal, a.data.vertical) + print(b.data.horizontal, b.data.vertical) + print(c.data.horizontal, c.data.vertical) local window = pop.window():align("center", "center"):setTitle("Welcome! This title is far too big!") @@ -114,9 +114,10 @@ function love.keypressed(key) if (key == "w") and (not handled) then local w = pop.window() - w.title:align("center") + w.data.title:align("center") end + --[[ if (key == "e") and (not handled) then print("EVENTS:") for k,v in pairs(pop.events) do @@ -124,6 +125,7 @@ function love.keypressed(key) end print("END") end + --]] if (key == "p") and (not handled) then pop.printElementTree() diff --git a/demo/pop/elements/box.lua b/demo/pop/elements/box.lua index 2128ddf..91ec7c2 100644 --- a/demo/pop/elements/box.lua +++ b/demo/pop/elements/box.lua @@ -13,16 +13,16 @@ do local _parent_0 = element local _base_0 = { draw = function(self) - if self.background then - if type(self.background) == "table" then - graphics.setColor(self.background) - graphics.rectangle("fill", self.x, self.y, self.w, self.h) + if self.data.background then + if type(self.data.background) == "table" then + graphics.setColor(self.data.background) + graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h) else - local w, h = self.background:getDimensions() - w = self.w / w - h = self.h / h + local w, h = self.data.background:getDimensions() + w = self.data.w / w + h = self.data.h / h graphics.setColor(255, 255, 255, 255) - graphics.draw(self.background, self.x, self.y, 0, w, h) + graphics.draw(self.data.background, self.data.x, self.data.y, 0, w, h) end end return self @@ -30,28 +30,28 @@ do debugDraw = function(self) graphics.setLineWidth(0.5) graphics.setColor(0, 0, 0, 100) - graphics.rectangle("fill", self.x, self.y, self.w, self.h) + graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(0, 0, 200, 200) - graphics.rectangle("line", self.x, self.y, self.w, self.h) + graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(200, 200, 255, 255) - graphics.print("b", self.x, self.y) + graphics.print("b", self.data.x, self.data.y) return self end, setBackground = function(self, background) - self.background = background + self.data.background = background return self end, getBackground = function(self) - return self.background + return self.data.background end, setColor = function(self, r, g, b, a) if a == nil then a = 255 end if type(r) == "table" then - self.background = r + self.data.background = r else - self.background = { + self.data.background = { r, g, b, @@ -61,8 +61,8 @@ do return self end, getColor = function(self) - if type(self.background) == "table" then - return unpack(self.background) + if type(self.data.background) == "table" then + return unpack(self.data.background) else return error("Box \"" .. tostring(self) .. "\" doesn't have a color.") end @@ -76,9 +76,9 @@ do background = false end _class_0.__parent.__init(self, parent) - self.w = 20 - self.h = 20 - self.background = background + self.data.w = 20 + self.data.h = 20 + self.data.background = background end, __base = _base_0, __name = "box", diff --git a/demo/pop/elements/element.lua b/demo/pop/elements/element.lua index 0532455..5a679ca 100644 --- a/demo/pop/elements/element.lua +++ b/demo/pop/elements/element.lua @@ -15,150 +15,150 @@ do debugDraw = function(self) graphics.setLineWidth(0.5) graphics.setColor(0, 0, 0, 100) - graphics.rectangle("fill", self.x, self.y, self.w, self.h) + graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(0, 200, 0, 200) - graphics.rectangle("line", self.x, self.y, self.w, self.h) + graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(200, 255, 200, 255) - graphics.print("e", self.x, self.y) + graphics.print("e", self.data.x, self.data.y) return self end, addChild = function(self, child) - if child.parent then - child.parent:removeChild(child) + if child.data.parent then + child.data.parent:removeChild(child) end - insert(self.child, child) - child.parent = self + insert(self.data.child, child) + child.data.parent = self child:align() return self end, removeChild = function(self, child) if tonumber(child) == child then - self.child[child].parent = false - return remove(self.child, child) + self.data.child[child].data.parent = false + return remove(self.data.child, child) else - for k, v in ipairs(self.child) do + for k, v in ipairs(self.data.child) do if v == child then - return remove(self.child, k) + return remove(self.data.child, k) end end return "Element \"" .. tostring(child) .. "\" is not a child of element \"" .. tostring(self) .. "\". Cannot remove it." end end, getChildren = function(self) - return self.child + return self.data.child end, move = function(self, x, y) if x then - self.x = self.x + x + self.data.x = self.data.x + x end if y then - self.y = self.y + y + self.data.y = self.data.y + y end - for i = 1, #self.child do - if not (self.child[i].excludeMovement) then - self.child[i]:move(x, y) + for i = 1, #self.data.child do + if self.data.child[i].data.move then + self.data.child[i]:move(x, y) end end return self end, setPosition = function(self, x, y) - local oldX = self.x - local oldY = self.y + local oldX = self.data.x + local oldY = self.data.y if x then - local _exp_0 = self.horizontal + local _exp_0 = self.data.horizontal if "left" == _exp_0 then - self.x = x + self.data.x = x elseif "center" == _exp_0 then - self.x = x - self.w / 2 + self.data.x = x - self.data.w / 2 elseif "right" == _exp_0 then - self.x = x - self.w + self.data.x = x - self.data.w end else x = oldX end if y then - local _exp_0 = self.vertical + local _exp_0 = self.data.vertical if "top" == _exp_0 then - self.y = y + self.data.y = y elseif "center" == _exp_0 then - self.y = y - self.h / 2 + self.data.y = y - self.data.h / 2 elseif "bottom" == _exp_0 then - self.y = y - self.h + self.data.y = y - self.data.h end else y = oldY end - for i = 1, #self.child do - self.child[i]:move(x - oldX, y - oldY) + for i = 1, #self.data.child do + self.data.child[i]:move(x - oldX, y - oldY) end return self end, getPosition = function(self) - local resultX = self.x - local resultY = self.y - local _exp_0 = self.horizontal + local resultX = self.data.x + local resultY = self.data.y + local _exp_0 = self.data.horizontal if "center" == _exp_0 then - resultX = resultX + (self.w / 2) + resultX = resultX + (self.data.w / 2) elseif "right" == _exp_0 then - resultY = resultY + self.w + resultY = resultY + self.data.w end - local _exp_1 = self.vertical + local _exp_1 = self.data.vertical if "center" == _exp_1 then - resultY = resultY + (self.h / 2) + resultY = resultY + (self.data.h / 2) elseif "bottom" == _exp_1 then - resultY = resultY + self.h + resultY = resultY + self.data.h end return resultX, resultY end, setSize = function(self, w, h) if w then - local _exp_0 = self.horizontal + local _exp_0 = self.data.horizontal if "center" == _exp_0 then - self.x = self.x - ((w - self.w) / 2) + self.data.x = self.data.x - ((w - self.data.w) / 2) elseif "right" == _exp_0 then - self.x = self.x - (w - self.w) + self.data.x = self.data.x - (w - self.data.w) end - self.w = w + self.data.w = w end if h then - local _exp_0 = self.vertical + local _exp_0 = self.data.vertical if "center" == _exp_0 then - self.y = self.y - ((h - self.h) / 2) + self.data.y = self.data.y - ((h - self.data.h) / 2) elseif "bottom" == _exp_0 then - self.y = self.y - (h - self.h) + self.data.y = self.data.y - (h - self.data.h) end - self.h = h + self.data.h = h end return self end, getSize = function(self) - return self.w, self.h + return self.data.w, self.data.h end, setWidth = function(self, w) - local _exp_0 = self.horizontal + local _exp_0 = self.data.horizontal if "center" == _exp_0 then - self.x = self.x - ((w - self.w) / 2) + self.data.x = self.data.x - ((w - self.data.w) / 2) elseif "right" == _exp_0 then - self.x = self.x - (w - self.w) + self.data.x = self.data.x - (w - self.data.w) end - self.w = w + self.data.w = w return self end, getWidth = function(self) - return self.w + return self.data.w end, setHeight = function(self, h) - local _exp_0 = self.vertical + local _exp_0 = self.data.vertical if "center" == _exp_0 then - self.y = self.y - ((h - self.h) / 2) + self.data.y = self.data.y - ((h - self.data.h) / 2) elseif "bottom" == _exp_0 then - self.y = self.y - (h - self.h) + self.data.y = self.data.y - (h - self.data.h) end - self.h = h + self.data.h = h return self end, getHeight = function(self) - return self.h + return self.data.h end, adjustSize = function(self, w, h) local W, H = self:getSize() @@ -176,27 +176,27 @@ do toPixel = true end self:setAlignment(horizontal, vertical) - self.x = self.parent.x - self.y = self.parent.y - local _exp_0 = self.horizontal + self.data.x = self.data.parent.data.x + self.data.y = self.data.parent.data.y + local _exp_0 = self.data.horizontal if "left" == _exp_0 then - self.x = self.x + self.spacing + self.data.x = self.data.x + self.data.margin elseif "center" == _exp_0 then - self.x = self.x + ((self.parent.w - self.w) / 2) + self.data.x = self.data.x + ((self.data.parent.data.w - self.data.w) / 2) elseif "right" == _exp_0 then - self.x = self.x + (self.parent.w - self.w - self.spacing) + self.data.x = self.data.x + (self.data.parent.data.w - self.data.w - self.data.margin) end - local _exp_1 = self.vertical + local _exp_1 = self.data.vertical if "top" == _exp_1 then - self.y = self.y + self.spacing + self.data.y = self.data.y + self.data.margin elseif "center" == _exp_1 then - self.y = self.y + ((self.parent.h - self.h) / 2) + self.data.y = self.data.y + ((self.data.parent.data.h - self.data.h) / 2) elseif "bottom" == _exp_1 then - self.y = self.y + (self.parent.h - self.h - self.spacing) + self.data.y = self.data.y + (self.data.parent.data.h - self.data.h - self.data.margin) end if toPixel then - self.x = floor(self.x) - self.y = floor(self.y) + self.data.x = floor(self.data.x) + self.data.y = floor(self.data.y) end return self end, @@ -204,81 +204,82 @@ do if toPixel == nil then toPixel = true end - local parent = self.parent - self.parent = element + local parent = self.data.parent + self.data.parent = element self:align(horizontal, vertical, toPixel) - self.parent = parent + self.data.parent = parent return self end, setAlignment = function(self, horizontal, vertical) if horizontal then - self.horizontal = horizontal + self.data.horizontal = horizontal end if vertical then - self.vertical = vertical + self.data.vertical = vertical end return self end, getAlignment = function(self) - return self.horizontal, self.vertical + return self.data.horizontal, self.data.vertical end, - setMargin = function(self, spacing) - self.spacing = spacing + setMargin = function(self, margin) + self.data.margin = margin self:align() return self end, getMargin = function(self) - return self.spacing + return self.data.margin end, fill = function(self) - self.x = self.parent.x + self.spacing - self.y = self.parent.y + self.spacing - self.w = self.parent.w - self.spacing * 2 - self.h = self.parent.h - self.spacing * 2 + self.data.x = self.data.parent.data.x + self.data.margin + self.data.y = self.data.parent.data.y + self.data.margin + self.data.w = self.data.parent.data.w - self.data.margin * 2 + self.data.h = self.data.parent.data.h - self.data.margin * 2 end, delete = function(self) - for k, v in ipairs(self.child) do + for k, v in ipairs(self.data.child) do v:delete() end - self.parent:removeChild(self) + self.data.parent:removeChild(self) self = nil return nil end, getVisibility = function(self) - return (not self.excludeDraw) + return self.data.draw end, setVisibility = function(self, isVisible) - self.excludeDraw = (not isVisible) + self.data.draw = isVisible return self end, getStatic = function(self) - return self.excludeMovement + return (not self.data.move) end, setStatic = function(self, isStatic) - self.excludeMovement = isStatic + self.data.move = (not isStatic) return self end } _base_0.__index = _base_0 _class_0 = setmetatable({ __init = function(self, parent) - self.parent = parent - self.child = { } - self.w = 0 - self.h = 0 - self.spacing = 0 + self.data = { + parent = parent, + child = { }, + w = 0, + h = 0, + x = 0, + y = 0, + horizontal = "left", + vertical = "top", + margin = 0, + draw = true, + update = true, + move = true + } if parent then - self.x = parent.x - self.y = parent.y - else - self.x = 0 - self.y = 0 + self.data.x = parent.data.x + self.data.y = parent.data.y end - self.horizontal = "left" - self.vertical = "top" - self.excludeDraw = false - self.excludeUpdate = false - self.excludeMovement = false end, __base = _base_0, __name = "element" diff --git a/demo/pop/elements/text.lua b/demo/pop/elements/text.lua index a81af09..5645df9 100644 --- a/demo/pop/elements/text.lua +++ b/demo/pop/elements/text.lua @@ -13,38 +13,38 @@ do local _parent_0 = element local _base_0 = { draw = function(self) - graphics.setColor(self.color) - graphics.setFont(self.font) - graphics.print(self.txt, self.x, self.y) + graphics.setColor(self.data.color) + graphics.setFont(self.data.font) + graphics.print(self.data.text, self.data.x, self.data.y) return self end, debugDraw = function(self) graphics.setLineWidth(0.5) graphics.setColor(0, 0, 0, 100) - graphics.rectangle("fill", self.x, self.y, self.w, self.h) + graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(200, 0, 0, 200) - graphics.rectangle("line", self.x, self.y, self.w, self.h) + graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(255, 200, 200, 255) - graphics.print("t", self.x, self.y) + graphics.print("t", self.data.x, self.data.y) return self end, setSize = function(self) - local w = self.font:getWidth(self.txt) - local h = self.font:getHeight() * (select(2, self.txt:gsub("\n", "\n")) + 1) - local _exp_0 = self.horizontal + local w = self.data.font:getWidth(self.data.text) + local h = self.data.font:getHeight() * (select(2, self.data.text:gsub("\n", "\n")) + 1) + local _exp_0 = self.data.horizontal if "center" == _exp_0 then - self.x = self.x - ((w - self.w) / 2) + self.data.x = self.data.x - ((w - self.data.w) / 2) elseif "right" == _exp_0 then - self.x = self.x - (w - self.w - self.spacing) + self.data.x = self.data.x - (w - self.data.w - self.data.margin) end local _exp_1 = self.vertical if "center" == _exp_1 then - self.y = self.y - ((h - self.h) / 2) + self.data.y = self.data.y - ((h - self.data.h) / 2) elseif "bottom" == _exp_1 then - self.y = self.y - (h - self.h - self.spacing) + self.data.y = self.data.y - (h - self.data.h - self.data.margin) end - self.w = w - self.h = h + self.data.w = w + self.data.h = h return self end, setWidth = function(self) @@ -59,29 +59,29 @@ do if text == nil then text = "" end - self.txt = text + self.data.text = text self:setSize() return self end, getText = function(self) - return self.txt + return self.data.text end, setFont = function(self, font) - self.font = font + self.data.font = font self:setSize() return self end, getFont = function(self) - return self.font + return self.data.font end, setColor = function(self, r, g, b, a) if a == nil then a = 255 end if type(r) == "table" then - self.color = r + self.data.color = r else - self.color = { + self.data.color = { r, g, b, @@ -91,7 +91,7 @@ do return self end, getColor = function(self) - return unpack(self.color) + return unpack(self.data.color) end } _base_0.__index = _base_0 @@ -110,9 +110,9 @@ do } end _class_0.__parent.__init(self, parent) - self.font = graphics.newFont(14) + self.data.font = graphics.newFont(14) self:setText(text) - self.color = color + self.data.color = color end, __base = _base_0, __name = "text", diff --git a/demo/pop/elements/window.lua b/demo/pop/elements/window.lua index cd4cb31..4a3f2d8 100644 --- a/demo/pop/elements/window.lua +++ b/demo/pop/elements/window.lua @@ -34,37 +34,33 @@ do print(" assuming LOVE version > 0.10.1 (there may be bugs)") end end -local pop_ref = false local window do local _class_0 local _parent_0 = element local _base_0 = { - load = function(pop) - pop_ref = pop - end, debugDraw = function(self) graphics.setLineWidth(0.5) graphics.setColor(0, 0, 0, 100) - graphics.rectangle("fill", self.x, self.y, self.w, self.h) + graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(200, 0, 200, 200) - graphics.rectangle("line", self.x, self.y, self.w, self.h) + graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(255, 200, 255, 255) - graphics.print("w", self.x, self.y) + graphics.print("w", self.data.x, self.data.y) return self end, addChild = function(self, child) - self.area:addChild(child) + self.data.area:addChild(child) return self end, removeChild = function(self, child) - local result = self.area:removeChild(child) - if result == self.area then + local result = self.data.area:removeChild(child) + if result == self.data.area then return self elseif type(result) == "string" then - for k, v in ipairs(self.child) do + for k, v in ipairs(self.data.child) do if v == child then - remove(self.child, k) + remove(self.data.child, k) return self end end @@ -74,142 +70,142 @@ do end end, getChildren = function(self) - return self.area.child + return self.data.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() + for i = 1, #self.data.child do + self.data.child[i]:align() end - self.area:move(nil, self.head:getHeight()) + self.data.area:move(nil, self.data.head:getHeight()) return self end, setSize = function(self, w, h) local x = 0 local y = 0 if w then - local _exp_0 = self.horizontal + local _exp_0 = self.data.horizontal if "center" == _exp_0 then - x = x - ((w - self.w) / 2) + x = x - ((w - self.data.w) / 2) elseif "right" == _exp_0 then - x = x - (w - self.w) + x = x - (w - self.data.w) end - if self.close then - self.head:setWidth(w - self.head:getHeight()) + if self.data.close then + self.data.head:setWidth(w - self.data.head:getHeight()) else - self.head:setWidth(w) + self.data.head:setWidth(w) end - self.area:setWidth(w) - self.w = w - self.x = self.x + x - self.title:align() - if self.close then - self.close:align() + self.data.area:setWidth(w) + self.data.w = w + self.data.x = self.data.x + x + self.data.title:align() + if self.data.close then + self.data.close:align() end end if h then - h = h - self.head:getHeight() - local _exp_0 = self.vertical + h = h - self.data.head:getHeight() + local _exp_0 = self.data.vertical if "center" == _exp_0 then - y = y - ((h - self.h) / 2) + y = y - ((h - self.data.h) / 2) elseif "right" == _exp_0 then - y = y - (h - self.h) + y = y - (h - self.data.h) end - self.area:setHeight(h) - self.h = h + self.head:getHeight() - self.y = self.y + y + self.data.area:setHeight(h) + self.data.h = h + self.data.head:getHeight() + self.data.y = self.data.y + y end - self.head:move(x, y) - self.area:move(x, y) + self.data.head:move(x, y) + self.data.area:move(x, y) return self end, setWidth = function(self, w) local x = 0 - local _exp_0 = self.horizontal + local _exp_0 = self.data.horizontal if "center" == _exp_0 then - x = x - ((w - self.w) / 2) + x = x - ((w - self.data.w) / 2) elseif "right" == _exp_0 then - x = x - (w - self.w) + x = x - (w - self.data.w) end - if self.close then - self.head:setWidth(w - self.head:getHeight()) + if self.data.close then + self.data.head:setWidth(w - self.data.head:getHeight()) else - self.head:setWidth(w) + self.data.head:setWidth(w) end - self.area:setWidth(w) - self.w = w - self.x = self.x + x - self.title:align() - if self.close then - self.close:align() + self.data.area:setWidth(w) + self.data.w = w + self.data.x = self.data.x + x + self.data.title:align() + if self.data.close then + self.data.close:align() end - self.head:move(x) - self.area:move(x) + self.data.head:move(x) + self.data.area:move(x) return self end, setHeight = function(self, h) local y = 0 - h = h - self.head:getHeight() - local _exp_0 = self.vertical + h = h - self.data.head:getHeight() + local _exp_0 = self.data.vertical if "center" == _exp_0 then - y = y - ((h - self.h) / 2) + y = y - ((h - self.data.h) / 2) elseif "right" == _exp_0 then - y = y - (h - self.h) + y = y - (h - self.data.h) end - 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.area:move(nil, y) + self.data.area:setHeight(h) + self.data.h = h + self.data.head:getHeight() + self.data.y = self.data.y + y + self.data.head:move(nil, y) + self.data.title:move(nil, y) + self.data.area:move(nil, y) return self end, setTitle = function(self, title) - self.title:setText(title) - if self.titleOverflow == "trunicate" then - while self.title:getWidth() > self.head:getWidth() do + self.data.title:setText(title) + if self.data.overflow == "trunicate" then + while self.data.title:getWidth() > self.data.head:getWidth() do title = title:sub(1, -3) - self.title:setText(title .. "…") + self.data.title:setText(title .. "…") end - elseif self.titleOverflow == "resize" then - if self.title:getWidth() > self.head:getWidth() then - self:setWidth(self.title:getWidth()) + elseif self.data.overflow == "resize" then + if self.data.title:getWidth() > self.data.head:getWidth() then + self:setWidth(self.data.title:getWidth()) end end return self end, getTitle = function(self) - return self.title:getText() + return self.data.title:getText() end, setTitleOverflow = function(self, method) - self.titleOverflow = method + self.data.overflow = method return self end, getTitleOverflow = function(self) - return self.titleOverflow + return self.data.overflow end, setClose = function(self, enabled) if enabled then - self.close = box(self, closeImage) - self.close.clicked = function() + self.data.close = box(self, closeImage) + self.data.close.clicked = function() self:delete() return true end - local height = self.head:getHeight() - self.close:align("right"):setSize(height, height) - self.head:setWidth(self.w - height) - self.title:align() - insert(self.child, self.close) + local height = self.data.head:getHeight() + self.data.close:align("right"):setSize(height, height) + self.data.head:setWidth(self.data.w - height) + self.data.title:align() + insert(self.data.child, self.data.close) else - self.close:delete() - self.head:setWidth(self.w) - self.title:align() - self.close = false + self.data.close:delete() + self.data.head:setWidth(self.data.w) + self.data.title:align() + self.data.close = false end return self end, hasClose = function(self) - if self.close then + if self.data.close then return true else return false @@ -217,10 +213,10 @@ do end, delete = function(self) _class_0.__parent.__base.delete(self) - self.head = nil - self.title = nil - self.area = nil - self.close = nil + self.data.head = nil + self.data.title = nil + self.data.area = nil + self.data.close = nil end } _base_0.__index = _base_0 @@ -255,69 +251,68 @@ do } end _class_0.__parent.__init(self, parent) - self.head = box(self, tBackground) - self.title = text(self.head, title, tColor) - self.area = box(self, wBackground) - self.close = box(self, closeImage) - local height = self.title:getHeight() - self.head:setSize(self.w - height, height) - self.area:move(nil, height) - self.close:align("right"):setSize(height, height) + self.data.head = box(self, tBackground) + self.data.title = text(self.data.head, title, tColor) + self.data.area = box(self, wBackground) + self.data.close = box(self, closeImage) + local height = self.data.title:getHeight() + self.data.head:setSize(self.data.w - height, height) + self.data.area:move(nil, height) + self.data.close:align("right"):setSize(height, height) self:setSize(100, 80) - self.child = { - self.head, - self.title, - self.area, - self.close + self.data.child = { + self.data.head, + self.data.title, + self.data.area, + self.data.close } - self.titleOverflow = "trunicate" - self.area.mousepressed = function() + self.data.overflow = "trunicate" + self.data.area.mousepressed = function() return true end - self.area.clicked = function() + self.data.area.clicked = function() return true end - self.close.clicked = function() + self.data.close.clicked = function() self:delete() return true end - self.head.selected = false + self.data.head.data.selected = false if mousemoved_event then - self.head.mousemoved = function(self, x, y, dx, dy) - if self.selected then - self.parent:move(dx, dy) + self.data.head.mousemoved = function(self, x, y, dx, dy) + if self.data.selected then + self.data.parent:move(dx, dy) return true end return false end - self.head.mousepressed = function(self, x, y, button) + self.data.head.mousepressed = function(self, x, y, button) if button == left then - self.selected = true + self.data.selected = true return true end return false end else - self.head.mx = 0 - self.head.my = 0 - self.head.update = function(self) + self.data.head.data.mx = 0 + self.data.head.data.my = 0 + self.data.head.update = function(self) local x, y = mouse.getPosition() - return self:setPosition(x - mx, y - my) + return self:setPosition(x - self.data.mx, y - self.data.my) end - self.head.mousepressed = function(self, x, y, button) + self.data.head.mousepressed = function(self, x, y, button) if button == left then - self.selected = true - self.mx = x - self.my = y + self.data.selected = true + self.data.mx = x + self.data.my = y return true end return false end end - self.head.mousereleased = function(self, x, y, button) + self.data.head.mousereleased = function(self, x, y, button) if button == left then - self.selected = false - pop_ref.focused = false + self.data.selected = false return true end return false diff --git a/demo/pop/init.lua b/demo/pop/init.lua index 27eb383..60260bd 100644 --- a/demo/pop/init.lua +++ b/demo/pop/init.lua @@ -96,12 +96,12 @@ pop.create = function(element, parent, ...) end if inheritsFromElement(parent) then element = pop.elements[element](parent, ...) - insert(parent.child, element) + insert(parent.data.child, element) elseif parent == false then element = pop.elements[element](false, ...) else element = pop.elements[element](pop.screen, parent, ...) - insert(pop.screen.child, element) + insert(pop.screen.data.child, element) end return element end @@ -109,12 +109,12 @@ pop.update = function(dt, element) if element == nil then element = pop.screen end - if not (element.excludeUpdate) then + if element.data.update then if element.update then element:update(dt) end - for i = 1, #element.child do - pop.update(dt, element.child[i]) + for i = 1, #element.data.child do + pop.update(dt, element.data.child[i]) end end end @@ -122,12 +122,12 @@ pop.draw = function(element) if element == nil then element = pop.screen end - if not (element.excludeDraw) then + if element.data.draw then if element.draw then element:draw() end - for i = 1, #element.child do - pop.draw(element.child[i]) + for i = 1, #element.data.child do + pop.draw(element.data.child[i]) end end end @@ -143,19 +143,19 @@ pop.mousepressed = function(x, y, button, element) element = pop.screen end 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 + if (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) then + for i = #element.data.child, 1, -1 do do - handled = pop.mousepressed(x, y, button, element.child[i]) + handled = pop.mousepressed(x, y, button, element.data.child[i]) if handled then return handled end end end if not (handled) then - if element.mousepressed and (not element.excludeDraw) then + if element.mousepressed and element.data.draw then do - handled = element:mousepressed(x - element.x, y - element.y, button) + handled = element:mousepressed(x - element.data.x, y - element.data.y, button) if handled then pop.focused = element end @@ -169,19 +169,19 @@ pop.mousereleased = function(x, y, button, element) local clickedHandled = false local mousereleasedHandled = false if element then - 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 - clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.child[i]) + if (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) then + for i = #element.data.child, 1, -1 do + clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.data.child[i]) if clickedHandled or mousereleasedHandled then return clickedHandled, mousereleasedHandled end end if not (clickedHandled or mousereleasedHandled) then - if element.clicked and (not element.excludeDraw) then - clickedHandled = element:clicked(x - element.x, y - element.y, button) + if element.clicked and element.data.draw then + clickedHandled = element:clicked(x - element.data.x, y - element.data.y, button) end if element.mousereleased then - mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button) + mousereleasedHandled = element:mousereleased(x - element.data.x, y - element.data.y, button) end if clickedHandled then pop.focused = element @@ -197,7 +197,7 @@ end pop.keypressed = function(key) print("keypressed", key) local element = pop.focused - if element and element.keypressed and (not element.excludeDraw) then + if element and element.keypressed and element.data.draw then return element.keypressed(key) end return false @@ -213,7 +213,7 @@ end pop.textinput = function(text) print("textinput", text) local element = pop.focused - if element and element.textinput and (not element.excludeDraw) then + if element and element.textinput and element.data.draw then return element.textinput(text) end return false @@ -225,23 +225,23 @@ pop.skin = function(element, skin, depth) if skin == nil then skin = pop.skins.default end - if element.background and skin.background then - element.background = skin.background + if element.data.background and skin.background then + element.data.background = skin.background end - if element.color and skin.color then - element.color = skin.color + if element.data.color and skin.color then + element.data.color = skin.color end - if element.font and skin.font then - element.font = skin.font + if element.data.font and skin.font then + element.data.font = skin.font end if not (depth or (depth == 0)) then if depth == tonumber(depth) then - for i = 1, #element.child do - pop.skin(element.child[i], skin, depth - 1) + for i = 1, #element.data.child do + pop.skin(element.data.child[i], skin, depth - 1) end else - for i = 1, #element.child do - pop.skin(element.child[i], skin, false) + for i = 1, #element.data.child do + pop.skin(element.data.child[i], skin, false) end end end @@ -255,14 +255,14 @@ pop.debugDraw = function(element) else graphics.setLineWidth(1) graphics.setLineColor(0, 0, 0, 100) - graphics.rectangle("fill", element.x, element.y, element.w, element.h) + graphics.rectangle("fill", element.data.x, element.data.y, element.data.w, element.data.h) graphics.setColor(150, 150, 150, 150) - graphics.rectangle("line", element.x, element.y, element.w, element.h) + graphics.rectangle("line", element.data.x, element.data.y, element.data.w, element.data.h) graphics.setColor(200, 200, 200, 255) - graphics.print(".", element.x, element.y) + graphics.print(".", element.data.x, element.data.y) end - for i = 1, #element.child do - pop.debugDraw(element.child[i]) + for i = 1, #element.data.child do + pop.debugDraw(element.data.child[i]) end end pop.printElementTree = function(element, depth) @@ -283,8 +283,8 @@ pop.printElementTree = function(element, depth) cls = cls .. " (" .. tostring(bg) .. ")" end print(string.rep("-", depth) .. " " .. tostring(cls)) - for i = 1, #element.child do - pop.printElementStack(element.child[i], depth + 1) + for i = 1, #element.data.child do + pop.printElementStack(element.data.child[i], depth + 1) end end pop.load() diff --git a/lib/pop/elements/box.lua b/lib/pop/elements/box.lua index 2128ddf..91ec7c2 100644 --- a/lib/pop/elements/box.lua +++ b/lib/pop/elements/box.lua @@ -13,16 +13,16 @@ do local _parent_0 = element local _base_0 = { draw = function(self) - if self.background then - if type(self.background) == "table" then - graphics.setColor(self.background) - graphics.rectangle("fill", self.x, self.y, self.w, self.h) + if self.data.background then + if type(self.data.background) == "table" then + graphics.setColor(self.data.background) + graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h) else - local w, h = self.background:getDimensions() - w = self.w / w - h = self.h / h + local w, h = self.data.background:getDimensions() + w = self.data.w / w + h = self.data.h / h graphics.setColor(255, 255, 255, 255) - graphics.draw(self.background, self.x, self.y, 0, w, h) + graphics.draw(self.data.background, self.data.x, self.data.y, 0, w, h) end end return self @@ -30,28 +30,28 @@ do debugDraw = function(self) graphics.setLineWidth(0.5) graphics.setColor(0, 0, 0, 100) - graphics.rectangle("fill", self.x, self.y, self.w, self.h) + graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(0, 0, 200, 200) - graphics.rectangle("line", self.x, self.y, self.w, self.h) + graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(200, 200, 255, 255) - graphics.print("b", self.x, self.y) + graphics.print("b", self.data.x, self.data.y) return self end, setBackground = function(self, background) - self.background = background + self.data.background = background return self end, getBackground = function(self) - return self.background + return self.data.background end, setColor = function(self, r, g, b, a) if a == nil then a = 255 end if type(r) == "table" then - self.background = r + self.data.background = r else - self.background = { + self.data.background = { r, g, b, @@ -61,8 +61,8 @@ do return self end, getColor = function(self) - if type(self.background) == "table" then - return unpack(self.background) + if type(self.data.background) == "table" then + return unpack(self.data.background) else return error("Box \"" .. tostring(self) .. "\" doesn't have a color.") end @@ -76,9 +76,9 @@ do background = false end _class_0.__parent.__init(self, parent) - self.w = 20 - self.h = 20 - self.background = background + self.data.w = 20 + self.data.h = 20 + self.data.background = background end, __base = _base_0, __name = "box", diff --git a/lib/pop/elements/element.lua b/lib/pop/elements/element.lua index 0532455..5a679ca 100644 --- a/lib/pop/elements/element.lua +++ b/lib/pop/elements/element.lua @@ -15,150 +15,150 @@ do debugDraw = function(self) graphics.setLineWidth(0.5) graphics.setColor(0, 0, 0, 100) - graphics.rectangle("fill", self.x, self.y, self.w, self.h) + graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(0, 200, 0, 200) - graphics.rectangle("line", self.x, self.y, self.w, self.h) + graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(200, 255, 200, 255) - graphics.print("e", self.x, self.y) + graphics.print("e", self.data.x, self.data.y) return self end, addChild = function(self, child) - if child.parent then - child.parent:removeChild(child) + if child.data.parent then + child.data.parent:removeChild(child) end - insert(self.child, child) - child.parent = self + insert(self.data.child, child) + child.data.parent = self child:align() return self end, removeChild = function(self, child) if tonumber(child) == child then - self.child[child].parent = false - return remove(self.child, child) + self.data.child[child].data.parent = false + return remove(self.data.child, child) else - for k, v in ipairs(self.child) do + for k, v in ipairs(self.data.child) do if v == child then - return remove(self.child, k) + return remove(self.data.child, k) end end return "Element \"" .. tostring(child) .. "\" is not a child of element \"" .. tostring(self) .. "\". Cannot remove it." end end, getChildren = function(self) - return self.child + return self.data.child end, move = function(self, x, y) if x then - self.x = self.x + x + self.data.x = self.data.x + x end if y then - self.y = self.y + y + self.data.y = self.data.y + y end - for i = 1, #self.child do - if not (self.child[i].excludeMovement) then - self.child[i]:move(x, y) + for i = 1, #self.data.child do + if self.data.child[i].data.move then + self.data.child[i]:move(x, y) end end return self end, setPosition = function(self, x, y) - local oldX = self.x - local oldY = self.y + local oldX = self.data.x + local oldY = self.data.y if x then - local _exp_0 = self.horizontal + local _exp_0 = self.data.horizontal if "left" == _exp_0 then - self.x = x + self.data.x = x elseif "center" == _exp_0 then - self.x = x - self.w / 2 + self.data.x = x - self.data.w / 2 elseif "right" == _exp_0 then - self.x = x - self.w + self.data.x = x - self.data.w end else x = oldX end if y then - local _exp_0 = self.vertical + local _exp_0 = self.data.vertical if "top" == _exp_0 then - self.y = y + self.data.y = y elseif "center" == _exp_0 then - self.y = y - self.h / 2 + self.data.y = y - self.data.h / 2 elseif "bottom" == _exp_0 then - self.y = y - self.h + self.data.y = y - self.data.h end else y = oldY end - for i = 1, #self.child do - self.child[i]:move(x - oldX, y - oldY) + for i = 1, #self.data.child do + self.data.child[i]:move(x - oldX, y - oldY) end return self end, getPosition = function(self) - local resultX = self.x - local resultY = self.y - local _exp_0 = self.horizontal + local resultX = self.data.x + local resultY = self.data.y + local _exp_0 = self.data.horizontal if "center" == _exp_0 then - resultX = resultX + (self.w / 2) + resultX = resultX + (self.data.w / 2) elseif "right" == _exp_0 then - resultY = resultY + self.w + resultY = resultY + self.data.w end - local _exp_1 = self.vertical + local _exp_1 = self.data.vertical if "center" == _exp_1 then - resultY = resultY + (self.h / 2) + resultY = resultY + (self.data.h / 2) elseif "bottom" == _exp_1 then - resultY = resultY + self.h + resultY = resultY + self.data.h end return resultX, resultY end, setSize = function(self, w, h) if w then - local _exp_0 = self.horizontal + local _exp_0 = self.data.horizontal if "center" == _exp_0 then - self.x = self.x - ((w - self.w) / 2) + self.data.x = self.data.x - ((w - self.data.w) / 2) elseif "right" == _exp_0 then - self.x = self.x - (w - self.w) + self.data.x = self.data.x - (w - self.data.w) end - self.w = w + self.data.w = w end if h then - local _exp_0 = self.vertical + local _exp_0 = self.data.vertical if "center" == _exp_0 then - self.y = self.y - ((h - self.h) / 2) + self.data.y = self.data.y - ((h - self.data.h) / 2) elseif "bottom" == _exp_0 then - self.y = self.y - (h - self.h) + self.data.y = self.data.y - (h - self.data.h) end - self.h = h + self.data.h = h end return self end, getSize = function(self) - return self.w, self.h + return self.data.w, self.data.h end, setWidth = function(self, w) - local _exp_0 = self.horizontal + local _exp_0 = self.data.horizontal if "center" == _exp_0 then - self.x = self.x - ((w - self.w) / 2) + self.data.x = self.data.x - ((w - self.data.w) / 2) elseif "right" == _exp_0 then - self.x = self.x - (w - self.w) + self.data.x = self.data.x - (w - self.data.w) end - self.w = w + self.data.w = w return self end, getWidth = function(self) - return self.w + return self.data.w end, setHeight = function(self, h) - local _exp_0 = self.vertical + local _exp_0 = self.data.vertical if "center" == _exp_0 then - self.y = self.y - ((h - self.h) / 2) + self.data.y = self.data.y - ((h - self.data.h) / 2) elseif "bottom" == _exp_0 then - self.y = self.y - (h - self.h) + self.data.y = self.data.y - (h - self.data.h) end - self.h = h + self.data.h = h return self end, getHeight = function(self) - return self.h + return self.data.h end, adjustSize = function(self, w, h) local W, H = self:getSize() @@ -176,27 +176,27 @@ do toPixel = true end self:setAlignment(horizontal, vertical) - self.x = self.parent.x - self.y = self.parent.y - local _exp_0 = self.horizontal + self.data.x = self.data.parent.data.x + self.data.y = self.data.parent.data.y + local _exp_0 = self.data.horizontal if "left" == _exp_0 then - self.x = self.x + self.spacing + self.data.x = self.data.x + self.data.margin elseif "center" == _exp_0 then - self.x = self.x + ((self.parent.w - self.w) / 2) + self.data.x = self.data.x + ((self.data.parent.data.w - self.data.w) / 2) elseif "right" == _exp_0 then - self.x = self.x + (self.parent.w - self.w - self.spacing) + self.data.x = self.data.x + (self.data.parent.data.w - self.data.w - self.data.margin) end - local _exp_1 = self.vertical + local _exp_1 = self.data.vertical if "top" == _exp_1 then - self.y = self.y + self.spacing + self.data.y = self.data.y + self.data.margin elseif "center" == _exp_1 then - self.y = self.y + ((self.parent.h - self.h) / 2) + self.data.y = self.data.y + ((self.data.parent.data.h - self.data.h) / 2) elseif "bottom" == _exp_1 then - self.y = self.y + (self.parent.h - self.h - self.spacing) + self.data.y = self.data.y + (self.data.parent.data.h - self.data.h - self.data.margin) end if toPixel then - self.x = floor(self.x) - self.y = floor(self.y) + self.data.x = floor(self.data.x) + self.data.y = floor(self.data.y) end return self end, @@ -204,81 +204,82 @@ do if toPixel == nil then toPixel = true end - local parent = self.parent - self.parent = element + local parent = self.data.parent + self.data.parent = element self:align(horizontal, vertical, toPixel) - self.parent = parent + self.data.parent = parent return self end, setAlignment = function(self, horizontal, vertical) if horizontal then - self.horizontal = horizontal + self.data.horizontal = horizontal end if vertical then - self.vertical = vertical + self.data.vertical = vertical end return self end, getAlignment = function(self) - return self.horizontal, self.vertical + return self.data.horizontal, self.data.vertical end, - setMargin = function(self, spacing) - self.spacing = spacing + setMargin = function(self, margin) + self.data.margin = margin self:align() return self end, getMargin = function(self) - return self.spacing + return self.data.margin end, fill = function(self) - self.x = self.parent.x + self.spacing - self.y = self.parent.y + self.spacing - self.w = self.parent.w - self.spacing * 2 - self.h = self.parent.h - self.spacing * 2 + self.data.x = self.data.parent.data.x + self.data.margin + self.data.y = self.data.parent.data.y + self.data.margin + self.data.w = self.data.parent.data.w - self.data.margin * 2 + self.data.h = self.data.parent.data.h - self.data.margin * 2 end, delete = function(self) - for k, v in ipairs(self.child) do + for k, v in ipairs(self.data.child) do v:delete() end - self.parent:removeChild(self) + self.data.parent:removeChild(self) self = nil return nil end, getVisibility = function(self) - return (not self.excludeDraw) + return self.data.draw end, setVisibility = function(self, isVisible) - self.excludeDraw = (not isVisible) + self.data.draw = isVisible return self end, getStatic = function(self) - return self.excludeMovement + return (not self.data.move) end, setStatic = function(self, isStatic) - self.excludeMovement = isStatic + self.data.move = (not isStatic) return self end } _base_0.__index = _base_0 _class_0 = setmetatable({ __init = function(self, parent) - self.parent = parent - self.child = { } - self.w = 0 - self.h = 0 - self.spacing = 0 + self.data = { + parent = parent, + child = { }, + w = 0, + h = 0, + x = 0, + y = 0, + horizontal = "left", + vertical = "top", + margin = 0, + draw = true, + update = true, + move = true + } if parent then - self.x = parent.x - self.y = parent.y - else - self.x = 0 - self.y = 0 + self.data.x = parent.data.x + self.data.y = parent.data.y end - self.horizontal = "left" - self.vertical = "top" - self.excludeDraw = false - self.excludeUpdate = false - self.excludeMovement = false end, __base = _base_0, __name = "element" diff --git a/lib/pop/elements/text.lua b/lib/pop/elements/text.lua index a81af09..5645df9 100644 --- a/lib/pop/elements/text.lua +++ b/lib/pop/elements/text.lua @@ -13,38 +13,38 @@ do local _parent_0 = element local _base_0 = { draw = function(self) - graphics.setColor(self.color) - graphics.setFont(self.font) - graphics.print(self.txt, self.x, self.y) + graphics.setColor(self.data.color) + graphics.setFont(self.data.font) + graphics.print(self.data.text, self.data.x, self.data.y) return self end, debugDraw = function(self) graphics.setLineWidth(0.5) graphics.setColor(0, 0, 0, 100) - graphics.rectangle("fill", self.x, self.y, self.w, self.h) + graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(200, 0, 0, 200) - graphics.rectangle("line", self.x, self.y, self.w, self.h) + graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(255, 200, 200, 255) - graphics.print("t", self.x, self.y) + graphics.print("t", self.data.x, self.data.y) return self end, setSize = function(self) - local w = self.font:getWidth(self.txt) - local h = self.font:getHeight() * (select(2, self.txt:gsub("\n", "\n")) + 1) - local _exp_0 = self.horizontal + local w = self.data.font:getWidth(self.data.text) + local h = self.data.font:getHeight() * (select(2, self.data.text:gsub("\n", "\n")) + 1) + local _exp_0 = self.data.horizontal if "center" == _exp_0 then - self.x = self.x - ((w - self.w) / 2) + self.data.x = self.data.x - ((w - self.data.w) / 2) elseif "right" == _exp_0 then - self.x = self.x - (w - self.w - self.spacing) + self.data.x = self.data.x - (w - self.data.w - self.data.margin) end local _exp_1 = self.vertical if "center" == _exp_1 then - self.y = self.y - ((h - self.h) / 2) + self.data.y = self.data.y - ((h - self.data.h) / 2) elseif "bottom" == _exp_1 then - self.y = self.y - (h - self.h - self.spacing) + self.data.y = self.data.y - (h - self.data.h - self.data.margin) end - self.w = w - self.h = h + self.data.w = w + self.data.h = h return self end, setWidth = function(self) @@ -59,29 +59,29 @@ do if text == nil then text = "" end - self.txt = text + self.data.text = text self:setSize() return self end, getText = function(self) - return self.txt + return self.data.text end, setFont = function(self, font) - self.font = font + self.data.font = font self:setSize() return self end, getFont = function(self) - return self.font + return self.data.font end, setColor = function(self, r, g, b, a) if a == nil then a = 255 end if type(r) == "table" then - self.color = r + self.data.color = r else - self.color = { + self.data.color = { r, g, b, @@ -91,7 +91,7 @@ do return self end, getColor = function(self) - return unpack(self.color) + return unpack(self.data.color) end } _base_0.__index = _base_0 @@ -110,9 +110,9 @@ do } end _class_0.__parent.__init(self, parent) - self.font = graphics.newFont(14) + self.data.font = graphics.newFont(14) self:setText(text) - self.color = color + self.data.color = color end, __base = _base_0, __name = "text", diff --git a/lib/pop/elements/window.lua b/lib/pop/elements/window.lua index cd4cb31..4a3f2d8 100644 --- a/lib/pop/elements/window.lua +++ b/lib/pop/elements/window.lua @@ -34,37 +34,33 @@ do print(" assuming LOVE version > 0.10.1 (there may be bugs)") end end -local pop_ref = false local window do local _class_0 local _parent_0 = element local _base_0 = { - load = function(pop) - pop_ref = pop - end, debugDraw = function(self) graphics.setLineWidth(0.5) graphics.setColor(0, 0, 0, 100) - graphics.rectangle("fill", self.x, self.y, self.w, self.h) + graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(200, 0, 200, 200) - graphics.rectangle("line", self.x, self.y, self.w, self.h) + graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(255, 200, 255, 255) - graphics.print("w", self.x, self.y) + graphics.print("w", self.data.x, self.data.y) return self end, addChild = function(self, child) - self.area:addChild(child) + self.data.area:addChild(child) return self end, removeChild = function(self, child) - local result = self.area:removeChild(child) - if result == self.area then + local result = self.data.area:removeChild(child) + if result == self.data.area then return self elseif type(result) == "string" then - for k, v in ipairs(self.child) do + for k, v in ipairs(self.data.child) do if v == child then - remove(self.child, k) + remove(self.data.child, k) return self end end @@ -74,142 +70,142 @@ do end end, getChildren = function(self) - return self.area.child + return self.data.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() + for i = 1, #self.data.child do + self.data.child[i]:align() end - self.area:move(nil, self.head:getHeight()) + self.data.area:move(nil, self.data.head:getHeight()) return self end, setSize = function(self, w, h) local x = 0 local y = 0 if w then - local _exp_0 = self.horizontal + local _exp_0 = self.data.horizontal if "center" == _exp_0 then - x = x - ((w - self.w) / 2) + x = x - ((w - self.data.w) / 2) elseif "right" == _exp_0 then - x = x - (w - self.w) + x = x - (w - self.data.w) end - if self.close then - self.head:setWidth(w - self.head:getHeight()) + if self.data.close then + self.data.head:setWidth(w - self.data.head:getHeight()) else - self.head:setWidth(w) + self.data.head:setWidth(w) end - self.area:setWidth(w) - self.w = w - self.x = self.x + x - self.title:align() - if self.close then - self.close:align() + self.data.area:setWidth(w) + self.data.w = w + self.data.x = self.data.x + x + self.data.title:align() + if self.data.close then + self.data.close:align() end end if h then - h = h - self.head:getHeight() - local _exp_0 = self.vertical + h = h - self.data.head:getHeight() + local _exp_0 = self.data.vertical if "center" == _exp_0 then - y = y - ((h - self.h) / 2) + y = y - ((h - self.data.h) / 2) elseif "right" == _exp_0 then - y = y - (h - self.h) + y = y - (h - self.data.h) end - self.area:setHeight(h) - self.h = h + self.head:getHeight() - self.y = self.y + y + self.data.area:setHeight(h) + self.data.h = h + self.data.head:getHeight() + self.data.y = self.data.y + y end - self.head:move(x, y) - self.area:move(x, y) + self.data.head:move(x, y) + self.data.area:move(x, y) return self end, setWidth = function(self, w) local x = 0 - local _exp_0 = self.horizontal + local _exp_0 = self.data.horizontal if "center" == _exp_0 then - x = x - ((w - self.w) / 2) + x = x - ((w - self.data.w) / 2) elseif "right" == _exp_0 then - x = x - (w - self.w) + x = x - (w - self.data.w) end - if self.close then - self.head:setWidth(w - self.head:getHeight()) + if self.data.close then + self.data.head:setWidth(w - self.data.head:getHeight()) else - self.head:setWidth(w) + self.data.head:setWidth(w) end - self.area:setWidth(w) - self.w = w - self.x = self.x + x - self.title:align() - if self.close then - self.close:align() + self.data.area:setWidth(w) + self.data.w = w + self.data.x = self.data.x + x + self.data.title:align() + if self.data.close then + self.data.close:align() end - self.head:move(x) - self.area:move(x) + self.data.head:move(x) + self.data.area:move(x) return self end, setHeight = function(self, h) local y = 0 - h = h - self.head:getHeight() - local _exp_0 = self.vertical + h = h - self.data.head:getHeight() + local _exp_0 = self.data.vertical if "center" == _exp_0 then - y = y - ((h - self.h) / 2) + y = y - ((h - self.data.h) / 2) elseif "right" == _exp_0 then - y = y - (h - self.h) + y = y - (h - self.data.h) end - 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.area:move(nil, y) + self.data.area:setHeight(h) + self.data.h = h + self.data.head:getHeight() + self.data.y = self.data.y + y + self.data.head:move(nil, y) + self.data.title:move(nil, y) + self.data.area:move(nil, y) return self end, setTitle = function(self, title) - self.title:setText(title) - if self.titleOverflow == "trunicate" then - while self.title:getWidth() > self.head:getWidth() do + self.data.title:setText(title) + if self.data.overflow == "trunicate" then + while self.data.title:getWidth() > self.data.head:getWidth() do title = title:sub(1, -3) - self.title:setText(title .. "…") + self.data.title:setText(title .. "…") end - elseif self.titleOverflow == "resize" then - if self.title:getWidth() > self.head:getWidth() then - self:setWidth(self.title:getWidth()) + elseif self.data.overflow == "resize" then + if self.data.title:getWidth() > self.data.head:getWidth() then + self:setWidth(self.data.title:getWidth()) end end return self end, getTitle = function(self) - return self.title:getText() + return self.data.title:getText() end, setTitleOverflow = function(self, method) - self.titleOverflow = method + self.data.overflow = method return self end, getTitleOverflow = function(self) - return self.titleOverflow + return self.data.overflow end, setClose = function(self, enabled) if enabled then - self.close = box(self, closeImage) - self.close.clicked = function() + self.data.close = box(self, closeImage) + self.data.close.clicked = function() self:delete() return true end - local height = self.head:getHeight() - self.close:align("right"):setSize(height, height) - self.head:setWidth(self.w - height) - self.title:align() - insert(self.child, self.close) + local height = self.data.head:getHeight() + self.data.close:align("right"):setSize(height, height) + self.data.head:setWidth(self.data.w - height) + self.data.title:align() + insert(self.data.child, self.data.close) else - self.close:delete() - self.head:setWidth(self.w) - self.title:align() - self.close = false + self.data.close:delete() + self.data.head:setWidth(self.data.w) + self.data.title:align() + self.data.close = false end return self end, hasClose = function(self) - if self.close then + if self.data.close then return true else return false @@ -217,10 +213,10 @@ do end, delete = function(self) _class_0.__parent.__base.delete(self) - self.head = nil - self.title = nil - self.area = nil - self.close = nil + self.data.head = nil + self.data.title = nil + self.data.area = nil + self.data.close = nil end } _base_0.__index = _base_0 @@ -255,69 +251,68 @@ do } end _class_0.__parent.__init(self, parent) - self.head = box(self, tBackground) - self.title = text(self.head, title, tColor) - self.area = box(self, wBackground) - self.close = box(self, closeImage) - local height = self.title:getHeight() - self.head:setSize(self.w - height, height) - self.area:move(nil, height) - self.close:align("right"):setSize(height, height) + self.data.head = box(self, tBackground) + self.data.title = text(self.data.head, title, tColor) + self.data.area = box(self, wBackground) + self.data.close = box(self, closeImage) + local height = self.data.title:getHeight() + self.data.head:setSize(self.data.w - height, height) + self.data.area:move(nil, height) + self.data.close:align("right"):setSize(height, height) self:setSize(100, 80) - self.child = { - self.head, - self.title, - self.area, - self.close + self.data.child = { + self.data.head, + self.data.title, + self.data.area, + self.data.close } - self.titleOverflow = "trunicate" - self.area.mousepressed = function() + self.data.overflow = "trunicate" + self.data.area.mousepressed = function() return true end - self.area.clicked = function() + self.data.area.clicked = function() return true end - self.close.clicked = function() + self.data.close.clicked = function() self:delete() return true end - self.head.selected = false + self.data.head.data.selected = false if mousemoved_event then - self.head.mousemoved = function(self, x, y, dx, dy) - if self.selected then - self.parent:move(dx, dy) + self.data.head.mousemoved = function(self, x, y, dx, dy) + if self.data.selected then + self.data.parent:move(dx, dy) return true end return false end - self.head.mousepressed = function(self, x, y, button) + self.data.head.mousepressed = function(self, x, y, button) if button == left then - self.selected = true + self.data.selected = true return true end return false end else - self.head.mx = 0 - self.head.my = 0 - self.head.update = function(self) + self.data.head.data.mx = 0 + self.data.head.data.my = 0 + self.data.head.update = function(self) local x, y = mouse.getPosition() - return self:setPosition(x - mx, y - my) + return self:setPosition(x - self.data.mx, y - self.data.my) end - self.head.mousepressed = function(self, x, y, button) + self.data.head.mousepressed = function(self, x, y, button) if button == left then - self.selected = true - self.mx = x - self.my = y + self.data.selected = true + self.data.mx = x + self.data.my = y return true end return false end end - self.head.mousereleased = function(self, x, y, button) + self.data.head.mousereleased = function(self, x, y, button) if button == left then - self.selected = false - pop_ref.focused = false + self.data.selected = false return true end return false diff --git a/lib/pop/init.lua b/lib/pop/init.lua index 27eb383..60260bd 100644 --- a/lib/pop/init.lua +++ b/lib/pop/init.lua @@ -96,12 +96,12 @@ pop.create = function(element, parent, ...) end if inheritsFromElement(parent) then element = pop.elements[element](parent, ...) - insert(parent.child, element) + insert(parent.data.child, element) elseif parent == false then element = pop.elements[element](false, ...) else element = pop.elements[element](pop.screen, parent, ...) - insert(pop.screen.child, element) + insert(pop.screen.data.child, element) end return element end @@ -109,12 +109,12 @@ pop.update = function(dt, element) if element == nil then element = pop.screen end - if not (element.excludeUpdate) then + if element.data.update then if element.update then element:update(dt) end - for i = 1, #element.child do - pop.update(dt, element.child[i]) + for i = 1, #element.data.child do + pop.update(dt, element.data.child[i]) end end end @@ -122,12 +122,12 @@ pop.draw = function(element) if element == nil then element = pop.screen end - if not (element.excludeDraw) then + if element.data.draw then if element.draw then element:draw() end - for i = 1, #element.child do - pop.draw(element.child[i]) + for i = 1, #element.data.child do + pop.draw(element.data.child[i]) end end end @@ -143,19 +143,19 @@ pop.mousepressed = function(x, y, button, element) element = pop.screen end 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 + if (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) then + for i = #element.data.child, 1, -1 do do - handled = pop.mousepressed(x, y, button, element.child[i]) + handled = pop.mousepressed(x, y, button, element.data.child[i]) if handled then return handled end end end if not (handled) then - if element.mousepressed and (not element.excludeDraw) then + if element.mousepressed and element.data.draw then do - handled = element:mousepressed(x - element.x, y - element.y, button) + handled = element:mousepressed(x - element.data.x, y - element.data.y, button) if handled then pop.focused = element end @@ -169,19 +169,19 @@ pop.mousereleased = function(x, y, button, element) local clickedHandled = false local mousereleasedHandled = false if element then - 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 - clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.child[i]) + if (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) then + for i = #element.data.child, 1, -1 do + clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.data.child[i]) if clickedHandled or mousereleasedHandled then return clickedHandled, mousereleasedHandled end end if not (clickedHandled or mousereleasedHandled) then - if element.clicked and (not element.excludeDraw) then - clickedHandled = element:clicked(x - element.x, y - element.y, button) + if element.clicked and element.data.draw then + clickedHandled = element:clicked(x - element.data.x, y - element.data.y, button) end if element.mousereleased then - mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button) + mousereleasedHandled = element:mousereleased(x - element.data.x, y - element.data.y, button) end if clickedHandled then pop.focused = element @@ -197,7 +197,7 @@ end pop.keypressed = function(key) print("keypressed", key) local element = pop.focused - if element and element.keypressed and (not element.excludeDraw) then + if element and element.keypressed and element.data.draw then return element.keypressed(key) end return false @@ -213,7 +213,7 @@ end pop.textinput = function(text) print("textinput", text) local element = pop.focused - if element and element.textinput and (not element.excludeDraw) then + if element and element.textinput and element.data.draw then return element.textinput(text) end return false @@ -225,23 +225,23 @@ pop.skin = function(element, skin, depth) if skin == nil then skin = pop.skins.default end - if element.background and skin.background then - element.background = skin.background + if element.data.background and skin.background then + element.data.background = skin.background end - if element.color and skin.color then - element.color = skin.color + if element.data.color and skin.color then + element.data.color = skin.color end - if element.font and skin.font then - element.font = skin.font + if element.data.font and skin.font then + element.data.font = skin.font end if not (depth or (depth == 0)) then if depth == tonumber(depth) then - for i = 1, #element.child do - pop.skin(element.child[i], skin, depth - 1) + for i = 1, #element.data.child do + pop.skin(element.data.child[i], skin, depth - 1) end else - for i = 1, #element.child do - pop.skin(element.child[i], skin, false) + for i = 1, #element.data.child do + pop.skin(element.data.child[i], skin, false) end end end @@ -255,14 +255,14 @@ pop.debugDraw = function(element) else graphics.setLineWidth(1) graphics.setLineColor(0, 0, 0, 100) - graphics.rectangle("fill", element.x, element.y, element.w, element.h) + graphics.rectangle("fill", element.data.x, element.data.y, element.data.w, element.data.h) graphics.setColor(150, 150, 150, 150) - graphics.rectangle("line", element.x, element.y, element.w, element.h) + graphics.rectangle("line", element.data.x, element.data.y, element.data.w, element.data.h) graphics.setColor(200, 200, 200, 255) - graphics.print(".", element.x, element.y) + graphics.print(".", element.data.x, element.data.y) end - for i = 1, #element.child do - pop.debugDraw(element.child[i]) + for i = 1, #element.data.child do + pop.debugDraw(element.data.child[i]) end end pop.printElementTree = function(element, depth) @@ -283,8 +283,8 @@ pop.printElementTree = function(element, depth) cls = cls .. " (" .. tostring(bg) .. ")" end print(string.rep("-", depth) .. " " .. tostring(cls)) - for i = 1, #element.child do - pop.printElementStack(element.child[i], depth + 1) + for i = 1, #element.data.child do + pop.printElementStack(element.data.child[i], depth + 1) end end pop.load() diff --git a/src/pop/elements/box.moon b/src/pop/elements/box.moon index 5c79ee2..a67ad8f 100644 --- a/src/pop/elements/box.moon +++ b/src/pop/elements/box.moon @@ -8,53 +8,53 @@ class box extends element new: (parent, background=false) => super parent - @w = 20 - @h = 20 + @data.w = 20 + @data.h = 20 - @background = background + @data.background = background draw: => - if @background - if type(@background) == "table" - graphics.setColor @background - graphics.rectangle "fill", @x, @y, @w, @h + if @data.background + if type(@data.background) == "table" + graphics.setColor @data.background + graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h else - w, h = @background\getDimensions! - w = @w / w - h = @h / h + w, h = @data.background\getDimensions! + w = @data.w / w + h = @data.h / h graphics.setColor 255, 255, 255, 255 - graphics.draw @background, @x, @y, 0, w, h + graphics.draw @data.background, @data.x, @data.y, 0, w, h return @ debugDraw: => graphics.setLineWidth 0.5 graphics.setColor 0, 0, 0, 100 - graphics.rectangle "fill", @x, @y, @w, @h + graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h graphics.setColor 0, 0, 200, 200 - graphics.rectangle "line", @x, @y, @w, @h + graphics.rectangle "line", @data.x, @data.y, @data.w, @data.h graphics.setColor 200, 200, 255, 255 - graphics.print "b", @x, @y + graphics.print "b", @data.x, @data.y return @ setBackground: (background) => - @background = background + @data.background = background return @ getBackground: => - return @background + return @data.background setColor: (r, g, b, a=255) => if type(r) == "table" - @background = r + @data.background = r else - @background = {r, g, b, a} + @data.background = {r, g, b, a} return @ getColor: => - if type(@background) == "table" - return unpack @background + if type(@data.background) == "table" + return unpack @data.background else error "Box \"#{@}\" doesn't have a color." --might be a bad idea diff --git a/src/pop/elements/element.moon b/src/pop/elements/element.moon index 0dd7023..a3c08a2 100644 --- a/src/pop/elements/element.moon +++ b/src/pop/elements/element.moon @@ -5,51 +5,50 @@ tonumber = tonumber class element new: (parent) => - @parent = parent - @child = {} - @w = 0 - @h = 0 + @data = { + parent: parent + child: {} - @spacing = 0 + w: 0 + h: 0 + x: 0 + y: 0 + + horizontal: "left" + vertical: "top" + margin: 0 + + draw: true + update: true + move: true + } if parent - @x = parent.x - @y = parent.y - --@horizontal = parent.horizontal - --@vertical = parent.vertical + @data.x = parent.data.x + @data.y = parent.data.y + --@data.horizontal = parent.data.horizontal + --@data.vertical = parent.data.vertical --@align! - else - @x = 0 - @y = 0 - --@horizontal = "left" - --@vertical = "top" - - @horizontal = "left" - @vertical = "top" - - @excludeDraw = false - @excludeUpdate = false - @excludeMovement = false debugDraw: => graphics.setLineWidth 0.5 graphics.setColor 0, 0, 0, 100 - graphics.rectangle "fill", @x, @y, @w, @h + graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h graphics.setColor 0, 200, 0, 200 - graphics.rectangle "line", @x, @y, @w, @h + graphics.rectangle "line", @data.x, @data.y, @data.w, @data.h graphics.setColor 200, 255, 200, 255 - graphics.print "e", @x, @y + graphics.print "e", @data.x, @data.y return @ addChild: (child) => -- make sure we don't duplicate references - if child.parent - child.parent\removeChild child + if child.data.parent + child.data.parent\removeChild child - insert @child, child - child.parent = @ + insert @data.child, child + child.data.parent = @ child\align! --NOTE not 100% sure if this is a good idea @@ -60,133 +59,135 @@ class element removeChild: (child) => if tonumber(child) == child -- remove indexed child, return it - @child[child].parent = false - return remove @child, child + @data.child[child].data.parent = false + return remove @data.child, child else - for k,v in ipairs @child + for k,v in ipairs @data.child if v == child - return remove @child, k + return remove @data.child, k --NOTE might break due to modifying table while indexing it? shouldn't as it returns... return "Element \"#{child}\" is not a child of element \"#{@}\". Cannot remove it." + -- returning an error string instead of erroring is kind of bad, but needed for window to function... getChildren: => - return @child + return @data.child --focusChild: (child) => - -- insert @child, 1, @removeChild(child) + -- insert @data.child, 1, @removeChild(child) -- return @ move: (x, y) => if x - @x = @x + x + @data.x = @data.x + x if y - @y = @y + y + @data.y = @data.y + y - for i = 1, #@child - unless @child[i].excludeMovement - @child[i]\move x, y + for i = 1, #@data.child + --unless @data.child[i].excludeMovement + if @data.child[i].data.move + @data.child[i]\move x, y return @ setPosition: (x, y) => - oldX = @x - oldY = @y + oldX = @data.x + oldY = @data.y if x - switch @horizontal + switch @data.horizontal when "left" - @x = x + @data.x = x when "center" - @x = x - @w/2 + @data.x = x - @data.w/2 when "right" - @x = x - @w + @data.x = x - @data.w else x = oldX if y - switch @vertical + switch @data.vertical when "top" - @y = y + @data.y = y when "center" - @y = y - @h/2 + @data.y = y - @data.h/2 when "bottom" - @y = y - @h + @data.y = y - @data.h else y = oldY - for i = 1, #@child - @child[i]\move x - oldX, y - oldY + for i = 1, #@data.child + @data.child[i]\move x - oldX, y - oldY return @ getPosition: => - resultX = @x - resultY = @y + resultX = @data.x + resultY = @data.y - switch @horizontal + switch @data.horizontal when "center" - resultX += @w/2 + resultX += @data.w/2 when "right" - resultY += @w + resultY += @data.w - switch @vertical + switch @data.vertical when "center" - resultY += @h/2 + resultY += @data.h/2 when "bottom" - resultY += @h + resultY += @data.h return resultX, resultY setSize: (w, h) => if w - switch @horizontal + switch @data.horizontal when "center" - @x -= (w - @w)/2 + @data.x -= (w - @data.w)/2 when "right" - @x -= w - @w + @data.x -= w - @data.w - @w = w + @data.w = w if h - switch @vertical + switch @data.vertical when "center" - @y -= (h - @h)/2 + @data.y -= (h - @data.h)/2 when "bottom" - @y -= h - @h + @data.y -= h - @data.h - @h = h + @data.h = h return @ getSize: => - return @w, @h + return @data.w, @data.h setWidth: (w) => - switch @horizontal + switch @data.horizontal when "center" - @x -= (w - @w)/2 + @data.x -= (w - @data.w)/2 when "right" - @x -= w - @w + @data.x -= w - @data.w - @w = w + @data.w = w return @ getWidth: => - return @w + return @data.w setHeight: (h) => - switch @vertical + switch @data.vertical when "center" - @y -= (h - @h)/2 + @data.y -= (h - @data.h)/2 when "bottom" - @y -= h - @h + @data.y -= h - @data.h - @h = h + @data.h = h return @ getHeight: => - return @h + return @data.h adjustSize: (w, h) => W, H = @getSize! @@ -204,84 +205,88 @@ class element align: (horizontal, vertical, toPixel=true) => @setAlignment horizontal, vertical - @x = @parent.x - @y = @parent.y + @data.x = @data.parent.data.x + @data.y = @data.parent.data.y - switch @horizontal + switch @data.horizontal when "left" - @x += @spacing + @data.x += @data.margin when "center" - @x += (@parent.w - @w)/2 + @data.x += (@data.parent.data.w - @data.w)/2 when "right" - @x += @parent.w - @w - @spacing + @data.x += @data.parent.data.w - @data.w - @data.margin - switch @vertical + switch @data.vertical when "top" - @y += @spacing + @data.y += @data.margin when "center" - @y += (@parent.h - @h)/2 + @data.y += (@data.parent.data.h - @data.h)/2 when "bottom" - @y += @parent.h - @h - @spacing + @data.y += @data.parent.data.h - @data.h - @data.margin if toPixel - @x = floor @x - @y = floor @y + @data.x = floor @data.x + @data.y = floor @data.y + + --NOTE why does align not move or align children? maybe it's good that it doesn't, maybe it's bad return @ alignTo: (element, horizontal, vertical, toPixel=true) => - parent = @parent - @parent = element + parent = @data.parent + @data.parent = element @align horizontal, vertical, toPixel - @parent = parent + @data.parent = parent return @ setAlignment: (horizontal, vertical) => if horizontal - @horizontal = horizontal + @data.horizontal = horizontal if vertical - @vertical = vertical + @data.vertical = vertical return @ getAlignment: => - return @horizontal, @vertical + return @data.horizontal, @data.vertical - setMargin: (spacing) => - @spacing = spacing + setMargin: (margin) => + @data.margin = margin @align! return @ getMargin: => - return @spacing + return @data.margin fill: => - @x = @parent.x + @spacing - @y = @parent.y + @spacing - @w = @parent.w - @spacing*2 - @h = @parent.h - @spacing*2 + @data.x = @data.parent.data.x + @data.margin + @data.y = @data.parent.data.y + @data.margin + @data.w = @data.parent.data.w - @data.margin*2 + @data.h = @data.parent.data.h - @data.margin*2 delete: => - for k,v in ipairs @child + for k,v in ipairs @data.child v\delete! - @parent\removeChild @ + @data.parent\removeChild @ + --@data.child = nil --not 100% sure if this is needed + --@data = nil -- pretty sure this .. actually has no effect for why I added it @ = nil return nil getVisibility: => - return (not @excludeDraw) + return @data.draw setVisibility: (isVisible) => - @excludeDraw = (not isVisible) + @data.draw = isVisible return @ getStatic: => - return @excludeMovement + return (not @data.move) setStatic: (isStatic) => - @excludeMovement = isStatic + @data.move = (not isStatic) return @ diff --git a/src/pop/elements/text.moon b/src/pop/elements/text.moon index 9736595..28e60ff 100644 --- a/src/pop/elements/text.moon +++ b/src/pop/elements/text.moon @@ -16,47 +16,47 @@ class text extends element new: (parent, text="", color={255,255,255,255}) => super parent - @font = graphics.newFont 14 + @data.font = graphics.newFont 14 @setText text - @color = color + @data.color = color draw: => - graphics.setColor @color - graphics.setFont @font - graphics.print @txt, @x, @y + graphics.setColor @data.color + graphics.setFont @data.font + graphics.print @data.text, @data.x, @data.y return @ debugDraw: => graphics.setLineWidth 0.5 graphics.setColor 0, 0, 0, 100 - graphics.rectangle "fill", @x, @y, @w, @h + graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h graphics.setColor 200, 0, 0, 200 - graphics.rectangle "line", @x, @y, @w, @h + graphics.rectangle "line", @data.x, @data.y, @data.w, @data.h graphics.setColor 255, 200, 200, 255 - graphics.print "t", @x, @y + graphics.print "t", @data.x, @data.y return @ -- unlike most elements, you cannot set a size for text elements setSize: => - w = @font\getWidth @txt - h = @font\getHeight! * (select(2, @txt\gsub("\n", "\n")) + 1) --hack to get height of multiple lines + w = @data.font\getWidth @data.text + h = @data.font\getHeight! * (select(2, @data.text\gsub("\n", "\n")) + 1) --hack to get height of multiple lines - switch @horizontal + switch @data.horizontal when "center" - @x -= (w - @w)/2 + @data.x -= (w - @data.w)/2 when "right" - @x -= w - @w - @spacing + @data.x -= w - @data.w - @data.margin switch @vertical when "center" - @y -= (h - @h)/2 + @data.y -= (h - @data.h)/2 when "bottom" - @y -= h - @h - @spacing + @data.y -= h - @data.h - @data.margin - @w = w - @h = h + @data.w = w + @data.h = h return @ @@ -71,28 +71,28 @@ class text extends element return @ setText: (text="") => - @txt = text + @data.text = text @setSize! return @ getText: => - return @txt + return @data.text setFont: (font) => - @font = font + @data.font = font @setSize! return @ getFont: => - return @font + return @data.font setColor: (r, g, b, a=255) => if type(r) == "table" - @color = r + @data.color = r else - @color = {r, g, b, a} + @data.color = {r, g, b, a} return @ getColor: => - return unpack @color + return unpack @data.color diff --git a/src/pop/elements/window.moon b/src/pop/elements/window.moon index e734a1c..f6323e2 100644 --- a/src/pop/elements/window.moon +++ b/src/pop/elements/window.moon @@ -25,134 +25,139 @@ do print "elements/window: unrecognized LOVE version: #{major}.#{minor}.#{revision}" print " assuming LOVE version > 0.10.1 (there may be bugs)" -pop_ref = false -- reference to pop, loaded by pop.load! +--pop_ref = false -- reference to pop, loaded by pop.load! +-- turns out we don't need it...? class window extends element - load: (pop) -> - pop_ref = pop + --TODO IMPLEMENT THIS COMMENTED OUT CHANGE. IT SHOULD WORK THE SAME EXCEPT BE BETTER. + --@load = (pop) -> + --load: (pop) -> + -- pop_ref = pop new: (parent, title="window", tBackground={25, 180, 230, 255}, tColor={255, 255, 255, 255}, wBackground={200, 200, 210, 255}) => super parent - -- NOTE @title having @head as its parent might break things horribly - @head = box @, tBackground -- title box at top - @title = text @head, title, tColor -- text at top - @area = box @, wBackground -- main window area - @close = box @, closeImage -- close button + -- NOTE @data.title having @data.head as its parent might break things horribly + @data.head = box @, tBackground -- title box at top + @data.title = text @data.head, title, tColor -- text at top + @data.area = box @, wBackground -- main window area + @data.close = box @, closeImage -- close button -- correct placement / sizes of elements - height = @title\getHeight! - @head\setSize @w - height, height - @area\move nil, height - @close\align("right")\setSize height, height + height = @data.title\getHeight! + @data.head\setSize @data.w - height, height + @data.area\move nil, height + @data.close\align("right")\setSize height, height @setSize 100, 80 -- our child elements are still child elements - --TODO change title to be a child of head ? - @child = { - @head, @title, @area, @close + --NOTE this is needed anywhere an element creates its own children (aka is NOT called through pop.create) + -- TODO put this note in the dev docs! + --TODO change title to be a child of head ? title already thinks that head is its parent (look above!) + @data.child = { + @data.head, @data.title, @data.area, @data.close } - @titleOverflow = "trunicate" -- defaults to trunicating title to fit in window + @data.overflow = "trunicate" -- defaults to trunicating title to fit in window -- window area steals mouse events to keep them from propagating under it - @area.mousepressed = -> + @data.area.mousepressed = -> return true - @area.clicked = -> + @data.area.clicked = -> return true - @close.clicked = -> + @data.close.clicked = -> @delete! return true - @head.selected = false -- whether or not the window title (and thus, the window) has been selected + @data.head.data.selected = false -- whether or not the window title (and thus, the window) has been selected if mousemoved_event - @head.mousemoved = (x, y, dx, dy) => - if @selected - @parent\move dx, dy + @data.head.mousemoved = (x, y, dx, dy) => + if @data.selected + @data.parent\move dx, dy return true return false - @head.mousepressed = (x, y, button) => + @data.head.mousepressed = (x, y, button) => if button == left - @selected = true + @data.selected = true return true return false else - @head.mx = 0 -- local mouse coordinates when selected - @head.my = 0 + @data.head.data.mx = 0 -- local mouse coordinates when selected + @data.head.data.my = 0 - @head.update = => + @data.head.update = => x, y = mouse.getPosition! - @setPosition x - mx, y - my - --return false -- why? + @setPosition x - @data.mx, y - @data.my - @head.mousepressed = (x, y, button) => + @data.head.mousepressed = (x, y, button) => if button == left - @selected = true - @mx = x - @my = y + @data.selected = true + @data.mx = x + @data.my = y return true return false - @head.mousereleased = (x, y, button) => + @data.head.mousereleased = (x, y, button) => if button == left - @selected = false - pop_ref.focused = false -- clear our focus + @data.selected = false + --pop_ref.focused = false -- clear our focus (should this be done? I think not) return true return false - --@head.focusChild = => - -- @parent\focusChild @ -- nope + --@data.head.focusChild = => + -- @data.parent\focusChild @ -- nope -- return @ debugDraw: => graphics.setLineWidth 0.5 graphics.setColor 0, 0, 0, 100 - graphics.rectangle "fill", @x, @y, @w, @h + graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h graphics.setColor 200, 0, 200, 200 - graphics.rectangle "line", @x, @y, @w, @h + graphics.rectangle "line", @data.x, @data.y, @data.w, @data.h graphics.setColor 255, 200, 255, 255 - graphics.print "w", @x, @y + graphics.print "w", @data.x, @data.y return @ addChild: (child) => - @area\addChild child + @data.area\addChild child return @ - -- pass through to window, but return us if window returns itself + -- pass through to window, but return us if @data.area returns itself + --TODO check if that's even possible, with the API change to removeChild, it shouldn't be happening EVER removeChild: (child) => - result = @area\removeChild child - if result == @area + result = @data.area\removeChild child + if result == @data.area return @ elseif type(result) == "string" - for k,v in ipairs @child + for k,v in ipairs @data.child if v == child - remove @child, k + remove @data.child, k return @ return "Element \"#{child}\" is not a child of window \"#{@}\". Cannot remove it." else return result getChildren: => - return @area.child + return @data.area.child --focusChild: => - -- @parent\focusChild @ + -- @data.parent\focusChild @ -- --NOTE might need to also actually focus the sub-element -- return @ align: (horizontal, vertical, toPixel) => super horizontal, vertical, toPixel - for i = 1, #@child - @child[i]\align! + for i = 1, #@data.child + @data.child[i]\align! - @area\move nil, @head\getHeight! + @data.area\move nil, @data.head\getHeight! return @ @@ -161,147 +166,150 @@ class window extends element y = 0 if w - switch @horizontal + switch @data.horizontal when "center" - x -= (w - @w)/2 + x -= (w - @data.w)/2 when "right" - x -= w - @w + x -= w - @data.w - if @close - @head\setWidth w - @head\getHeight! + if @data.close + @data.head\setWidth w - @data.head\getHeight! else - @head\setWidth w + @data.head\setWidth w - @area\setWidth w - @w = w - @x += x + @data.area\setWidth w + @data.w = w + @data.x += x - @title\align! + @data.title\align! - if @close - @close\align! + if @data.close + @data.close\align! if h - h = h - @head\getHeight! - switch @vertical + h = h - @data.head\getHeight! + switch @data.vertical when "center" - y -= (h - @h)/2 + y -= (h - @data.h)/2 when "right" - y -= h - @h + y -= h - @data.h - @area\setHeight h - @h = h + @head\getHeight! - @y += y + @data.area\setHeight h + @data.h = h + @data.head\getHeight! + @data.y += y - @head\move x, y - --@title\move x, y - @area\move x, y + @data.head\move x, y + --@data.title\move x, y + @data.area\move x, y return @ setWidth: (w) => x = 0 - switch @horizontal + switch @data.horizontal when "center" - x -= (w - @w)/2 + x -= (w - @data.w)/2 when "right" - x -= w - @w + x -= w - @data.w - if @close - @head\setWidth w - @head\getHeight! + if @data.close + @data.head\setWidth w - @data.head\getHeight! else - @head\setWidth w + @data.head\setWidth w - @area\setWidth w - @w = w - @x += x + @data.area\setWidth w + @data.w = w + @data.x += x - @title\align! + @data.title\align! - if @close - @close\align! + if @data.close + @data.close\align! - @head\move x - --@title\move x - @area\move x + @data.head\move x + --@data.title\move x + @data.area\move x return @ setHeight: (h) => y = 0 - h = h - @head\getHeight! - switch @vertical + h = h - @data.head\getHeight! + switch @data.vertical when "center" - y -= (h - @h)/2 + y -= (h - @data.h)/2 when "right" - y -= h - @h + y -= h - @data.h - @area\setHeight h - @h = h + @head\getHeight! - @y += y + @data.area\setHeight h + @data.h = h + @data.head\getHeight! + @data.y += y - @head\move nil, y - @title\move nil, y - @area\move nil, y + @data.head\move nil, y + @data.title\move nil, y + @data.area\move nil, y return @ setTitle: (title) => - @title\setText title + @data.title\setText title - if @titleOverflow == "trunicate" - while @title\getWidth! > @head\getWidth! + --NOTE when the entire window is resized, these checks do not get performed..this is probably a bad thing + --TODO make sure they always get checked, and that a trunicated title is saved so it can be un-trunicated when able + if @data.overflow == "trunicate" + while @data.title\getWidth! > @data.head\getWidth! title = title\sub 1, -3 - @title\setText title .. "…" + @data.title\setText title .. "…" - elseif @titleOverflow == "resize" - if @title\getWidth! > @head\getWidth! - @setWidth @title\getWidth! + elseif @data.overflow == "resize" + if @data.title\getWidth! > @data.head\getWidth! + @setWidth @data.title\getWidth! return @ getTitle: => - return @title\getText! + return @data.title\getText! setTitleOverflow: (method) => - @titleOverflow = method + @data.overflow = method return @ getTitleOverflow: => - return @titleOverflow + return @data.overflow + --TODO make the constructor call this instead of having these methods defined twice... setClose: (enabled) => if enabled - @close = box @, closeImage - @close.clicked = -> + @data.close = box @, closeImage + @data.close.clicked = -> @delete! return true - height = @head\getHeight! - @close\align("right")\setSize height, height - @head\setWidth @w - height - @title\align! - insert @child, @close + height = @data.head\getHeight! + @data.close\align("right")\setSize height, height + @data.head\setWidth @data.w - height + @data.title\align! + insert @data.child, @data.close else - @close\delete! - @head\setWidth @w - @title\align! - @close = false + @data.close\delete! + @data.head\setWidth @data.w + @data.title\align! + @data.close = false return @ hasClose: => - if @close + if @data.close return true else return false delete: => super! - @head = nil - @title = nil - @area = nil - @close = nil + @data.head = nil + @data.title = nil + @data.area = nil + @data.close = nil return diff --git a/src/pop/extensions/streamlined_get_set.moon b/src/pop/extensions/streamlined_get_set.moon index 0e8353c..abe8f40 100644 --- a/src/pop/extensions/streamlined_get_set.moon +++ b/src/pop/extensions/streamlined_get_set.moon @@ -65,14 +65,19 @@ element.__base.static = (s) => else return @getStatic! +--TODO implement this, as it now can be done! -- box.__base.background -- can't be done! +--TODO implement anything else / more than could/should be implemented on element class! + box.__base.color = (r, g, b, a) => if r or g or b or a return @setColor r, g, b, a else return @getColor! +--TODO see if anything else should be done for boxes! + text.__base.text = (text) => if text return @setText text @@ -90,3 +95,5 @@ text.__base.color = (r, g, b, a) => return @setColor r, g, b, a else return @getColor! + +--TODO see if anything else should be done for boxes! diff --git a/src/pop/init.moon b/src/pop/init.moon index 85452c5..597ded9 100644 --- a/src/pop/init.moon +++ b/src/pop/init.moon @@ -56,6 +56,8 @@ pop.load = -> unless skins[i]\sub(-4) == ".lua" continue + --NOTE why not allow skins to have a load function that is passed a reference to pop? + name = skins[i]\sub 1, -5 pop.skins[name] = require "#{path}/skins/#{name}" @@ -70,6 +72,7 @@ pop.load = -> name = extensions[i]\sub 1, -5 require "#{path}/extensions/#{name}" + --TODO find out if there is a use case for actually having a reference to one of these when loaded print "extension loaded: \"#{name}\"" @@ -82,30 +85,32 @@ pop.create = (element, parent=pop.screen, ...) -> -- if valid parent element if inheritsFromElement parent element = pop.elements[element](parent, ...) - insert parent.child, element + insert parent.data.child, element -- if explicitly no parent elseif parent == false element = pop.elements[element](false, ...) -- else use pop.screen, and "parent" is actually first argument else element = pop.elements[element](pop.screen, parent, ...) - insert pop.screen.child, element + insert pop.screen.data.child, element return element pop.update = (dt, element=pop.screen) -> - unless element.excludeUpdate + if element.data.update if element.update element\update dt - for i = 1, #element.child - pop.update dt, element.child[i] + for i = 1, #element.data.child + pop.update dt, element.data.child[i] + --for child in *element\getChildren! + -- pop.update dt, child pop.draw = (element=pop.screen) -> - unless element.excludeDraw + if element.data.draw if element.draw element\draw! - for i = 1, #element.child - pop.draw element.child[i] + for i = 1, #element.data.child + pop.draw element.data.child[i] --TODO implement a way for an element to attach itself to mousemoved events pop.mousemoved = (x, y, dx, dy) -> @@ -124,17 +129,17 @@ pop.mousepressed = (x, y, button, element) -> 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) + if (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) -- check its child elements in reverse order, returning if something handles it - for i = #element.child, 1, -1 - if handled = pop.mousepressed x, y, button, element.child[i] + for i = #element.data.child, 1, -1 + if handled = pop.mousepressed x, y, button, element.data.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 + if element.mousepressed and element.data.draw + if handled = element\mousepressed x - element.data.x, y - element.data.y, button pop.focused = element -- return whether or not we have handled the event @@ -147,28 +152,28 @@ pop.mousereleased = (x, y, button, element) -> -- 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) + if (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.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] + for i = #element.data.child, 1, -1 + clickedHandled, mousereleasedHandled = pop.mousereleased x, y, button, element.data.child[i] if clickedHandled or mousereleasedHandled 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.clicked and element.data.draw + clickedHandled = element\clicked x - element.data.x, y - element.data.y, button if element.mousereleased - mousereleasedHandled = element\mousereleased x - element.x, y - element.y, button + mousereleasedHandled = element\mousereleased x - element.data.x, y - element.data.y, button -- if we clicked, we're focused! if clickedHandled pop.focused = element --NOTE this might cause an error in the above for loop! -- basically, move focused element to front of its parent's child - --element.parent\focusChild element - --table.insert element.parent, element.parent\removeChild(element), + --element.data.parent\focusChild element + --table.insert element.data.parent, element.parent\removeChild(element), -- else, default to pop.screen to begin! (and print that we received an event) else @@ -182,7 +187,7 @@ pop.keypressed = (key) -> -- keypressed events must be on visible elements element = pop.focused - if element and element.keypressed and (not element.excludeDraw) + if element and element.keypressed and element.data.draw return element.keypressed key return false @@ -202,7 +207,7 @@ pop.textinput = (text) -> -- textinput events must be on visible elements element = pop.focused - if element and element.textinput and (not element.excludeDraw) + if element and element.textinput and element.data.draw return element.textinput text return false @@ -213,20 +218,20 @@ pop.textinput = (text) -> -- depth can be an integer for how many levels to go down when skinning -- defaults to pop.screen and the default skin pop.skin = (element=pop.screen, skin=pop.skins.default, depth) -> - if element.background and skin.background - element.background = skin.background - if element.color and skin.color - element.color = skin.color - if element.font and skin.font - element.font = skin.font + if element.data.background and skin.background + element.data.background = skin.background + if element.data.color and skin.color + element.data.color = skin.color + if element.data.font and skin.font + element.data.font = skin.font unless depth or (depth == 0) if depth == tonumber depth - for i = 1, #element.child - pop.skin element.child[i], skin, depth - 1 + for i = 1, #element.data.child + pop.skin element.data.child[i], skin, depth - 1 else - for i = 1, #element.child - pop.skin element.child[i], skin, false + for i = 1, #element.data.child + pop.skin element.data.child[i], skin, false pop.debugDraw = (element=pop.screen) -> if element.debugDraw @@ -234,14 +239,14 @@ pop.debugDraw = (element=pop.screen) -> else graphics.setLineWidth 1 graphics.setLineColor 0, 0, 0, 100 - graphics.rectangle "fill", element.x, element.y, element.w, element.h + graphics.rectangle "fill", element.data.x, element.data.y, element.data.w, element.data.h graphics.setColor 150, 150, 150, 150 - graphics.rectangle "line", element.x, element.y, element.w, element.h + graphics.rectangle "line", element.data.x, element.data.y, element.data.w, element.data.h graphics.setColor 200, 200, 200, 255 - graphics.print ".", element.x, element.y + graphics.print ".", element.data.x, element.data.y - for i = 1, #element.child - pop.debugDraw element.child[i] + for i = 1, #element.data.child + pop.debugDraw element.data.child[i] pop.printElementTree = (element=pop.screen, depth=0) -> cls = element.__class.__name @@ -258,8 +263,8 @@ pop.printElementTree = (element=pop.screen, depth=0) -> print string.rep("-", depth) .. " #{cls}" - for i = 1, #element.child - pop.printElementStack element.child[i], depth + 1 + for i = 1, #element.data.child + pop.printElementStack element.data.child[i], depth + 1 pop.load! diff --git a/src/pop/skins/default.moon b/src/pop/skins/default.moon index 197849e..f474673 100644 --- a/src/pop/skins/default.moon +++ b/src/pop/skins/default.moon @@ -1,6 +1,9 @@ -- Note that the "default" name is a bit of a misnomer, as this does not -- specify the defaults used in Pop.Box elements (they define their own) +--TODO make this actually specify defaults and be used (along with redoing the +-- skinning system entirely) + import graphics from love return {