diff --git a/demo/pop/skins/default.lua b/demo/pop/skins/default.lua index ecdf85c..299adb2 100644 --- a/demo/pop/skins/default.lua +++ b/demo/pop/skins/default.lua @@ -1,4 +1,5 @@ -local lg = love.graphics +local graphics +graphics = love.graphics return { background = { 0, @@ -12,5 +13,5 @@ return { 255, 250 }, - font = lg.newFont(14) + font = graphics.newFont(14) } diff --git a/lib/pop/elements/box.lua b/lib/pop/elements/box.lua index f1cc1b6..bd95152 100644 --- a/lib/pop/elements/box.lua +++ b/lib/pop/elements/box.lua @@ -16,8 +16,8 @@ do 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) else - graphics.setColor(self.background) local w, h = self.background:getDimensions() w = self.w / w h = self.h / h @@ -47,12 +47,16 @@ do if a == nil then a = 255 end - self.background = { - r, - g, - b, - a - } + if type(r) == "table" then + self.background = r + else + self.background = { + r, + g, + b, + a + } + end return self end, getColor = function(self) diff --git a/lib/pop/elements/element.lua b/lib/pop/elements/element.lua index 12bcfe5..163d7a3 100644 --- a/lib/pop/elements/element.lua +++ b/lib/pop/elements/element.lua @@ -1,5 +1,7 @@ local graphics graphics = love.graphics +local floor +floor = math.floor local element do local _class_0 @@ -15,10 +17,14 @@ do return self end, move = function(self, x, y) - self.x = self.x + x - self.y = self.y + y + if x then + self.x = self.x + x + end + if y then + self.y = self.y + y + end for i = 1, #self.child do - if not self.child[i].excludeMovement then + if not (self.child[i].excludeMovement) then self.child[i]:move(x, y) end end @@ -27,24 +33,32 @@ do setPosition = function(self, x, y) local oldX = self.x local oldY = self.y - local _exp_0 = self.horizontal - if "left" == _exp_0 then - self.x = x - elseif "center" == _exp_0 then - self.x = x - self.w / 2 - elseif "right" == _exp_0 then - self.x = x - self.w + if x then + local _exp_0 = self.horizontal + if "left" == _exp_0 then + self.x = x + elseif "center" == _exp_0 then + self.x = x - self.w / 2 + elseif "right" == _exp_0 then + self.x = x - self.w + end + else + x = oldX end - local _exp_1 = self.vertical - if "top" == _exp_1 then - self.y = y - elseif "center" == _exp_1 then - self.y = y - self.h / 2 - elseif "bottom" == _exp_1 then - self.y = y - self.h + if y then + local _exp_0 = self.vertical + if "top" == _exp_0 then + self.y = y + elseif "center" == _exp_0 then + self.y = y - self.h / 2 + elseif "bottom" == _exp_0 then + self.y = y - self.h + end + else + y = oldY end for i = 1, #self.child do - if not self.child[i].excludeMovement then + if not (self.child[i].excludeMovement) then self.child[i]:move(x - oldX, y - oldY) end end @@ -57,7 +71,7 @@ do if "center" == _exp_0 then resultX = resultX + (self.w / 2) elseif "right" == _exp_0 then - resultX = resultX + self.w + resultY = resultY + self.w end local _exp_1 = self.vertical if "center" == _exp_1 then @@ -102,7 +116,7 @@ do self:setSize(W, H) return self end, - align = function(self, horizontal, vertical) + align = function(self, horizontal, vertical, toPixel) self:setAlignment(horizontal, vertical) self.x = self.parent.x self.y = self.parent.y @@ -122,13 +136,17 @@ do elseif "bottom" == _exp_1 then self.y = self.y + (self.parent.h - self.h - self.margin) end + if toPixel then + self.x = floor(self.x) + self.y = floor(self.y) + end return self end, alignTo = function(self, element, horizontal, vertical) - local realParent = self.parent + local parent = self.parent self.parent = element self:align(horizontal, vertical) - self.parent = realParent + self.parent = parent return self end, setAlignment = function(self, horizontal, vertical) @@ -139,6 +157,14 @@ do self.vertical = vertical end return self + end, + setMargin = function(self, margin) + self.margin = margin + self:align() + return self + end, + getMargin = function(self) + return self.margin end } _base_0.__index = _base_0 @@ -146,8 +172,13 @@ do __init = function(self, pop, parent) self.parent = parent self.child = { } - self.x = parent.x or 0 - self.y = parent.y or 0 + if parent then + self.x = parent.x or 0 + self.y = parent.y or 0 + else + self.x = 0 + self.y = 0 + end self.w = 10 self.h = 10 self.horizontal = "left" diff --git a/lib/pop/elements/text.lua b/lib/pop/elements/text.lua index cebc19f..ba1cd78 100644 --- a/lib/pop/elements/text.lua +++ b/lib/pop/elements/text.lua @@ -5,13 +5,22 @@ do local _obj_0 = string sub, len = _obj_0.sub, _obj_0.len end -local path = sub(..., 1, len(...) - len("/text")) +local path = sub(..., 1, len(...) - len("/box")) local element = require(tostring(path) .. "/element") local text do local _class_0 local _parent_0 = element local _base_0 = { + wrap = function(pop) + return function(parent, ...) + if type(parent) == "string" then + return pop.create("text", nil, parent, ...) + else + return pop.create("text", parent, ...) + end + end + end, draw = function(self) graphics.setColor(self.color) graphics.setFont(self.font) @@ -40,7 +49,7 @@ do local _exp_1 = self.vertical if "center" == _exp_1 then self.y = self.y - ((h - self.h) / 2) - elseif "right" == _exp_1 then + elseif "bottom" == _exp_1 then self.y = self.y - (h - self.h - self.margin) end self.w = w @@ -70,12 +79,16 @@ do if a == nil then a = 255 end - self.color = { - r, - g, - b, - a - } + if type(r) == "table" then + self.color = r + else + self.color = { + r, + g, + b, + a + } + end return self end, getColor = function(self) diff --git a/lib/pop/init.lua b/lib/pop/init.lua index 9c6375b..d77f6cc 100644 --- a/lib/pop/init.lua +++ b/lib/pop/init.lua @@ -8,38 +8,71 @@ insert = table.insert local path = ... local pop = { } pop.elements = { } -pop.window = { - child = { } -} +pop.skins = { } +pop.screen = false pop.load = function() local elements = filesystem.getDirectoryItems(tostring(path) .. "/elements") for i = 1, #elements do - local name = elements[i]:sub(1, -5) - pop.elements[name] = require(tostring(path) .. "/elements/" .. tostring(name)) - print("loaded element: " .. tostring(name)) - if not pop[name] then - pop[name] = function(...) - return pop.create(name, ...) + local _continue_0 = false + repeat + if not (elements[i]:sub(-4) == ".lua") then + _continue_0 = true + break end - print("wrapper created: " .. tostring(name) .. "()") + local name = elements[i]:sub(1, -5) + pop.elements[name] = require(tostring(path) .. "/elements/" .. tostring(name)) + print("element loaded: \"" .. tostring(name) .. "\"") + if not (pop[name]) then + if pop.elements[name].wrap then + pop[name] = pop.elements[name].wrap(pop) + else + pop[name] = function(...) + return pop.create(name, ...) + end + end + print("wrapper created: \"pop." .. tostring(name) .. "()\"") + end + _continue_0 = true + until true + if not _continue_0 then + break end end - pop.window = pop.create("element"):setSize(graphics.getWidth(), graphics.getHeight()) - return print("created window") -end -pop.create = function(elementType, parent, ...) - if parent == nil then - parent = pop.window + local skins = filesystem.getDirectoryItems(tostring(path) .. "/skins") + for i = 1, #skins do + local _continue_0 = false + repeat + if not (skins[i]:sub(-4) == ".lua") then + _continue_0 = true + break + end + local name = skins[i]:sub(1, -5) + pop.skins[name] = require(tostring(path) .. "/skins/" .. tostring(name)) + print("skin loaded: \"" .. tostring(name) .. "\"") + _continue_0 = true + until true + if not _continue_0 then + break + end end - local newElement = pop.elements[elementType](parent, ...) - insert(parent.child, newElement) - return newElement + pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight()) + return print("created \"pop.screen\"") +end +pop.create = function(element, parent, ...) + if parent == nil then + parent = pop.screen + end + element = pop.elements[element](pop, parent, ...) + if parent then + insert(parent.child, element) + end + return element end pop.update = function(dt, element) if element == nil then - element = pop.window + element = pop.screen end - if not element.excludeUpdating then + if not (element.excludeUpdate) then if element.update then element:update(dt) end @@ -50,86 +83,80 @@ pop.update = function(dt, element) end pop.draw = function(element) if element == nil then - element = pop.window + element = pop.screen end - if not element.excludeRendering then + if not (element.excludeDraw) then if element.draw then - local _ - do - local _base_0 = element - local _fn_0 = _base_0.draw - _ = function(...) - return _fn_0(_base_0, ...) - end - end + element:draw() end for i = 1, #element.child do - pop.draw(element.child) + pop.draw(element.child[i]) end end end -pop.mousepressed = function(button, x, y, element) +pop.mousepressed = function(x, y, button, element) if element == nil then - element = pop.window - end - if (x >= element.x) and (x <= (element.x + element.w)) then - if (y >= element.y) and (y <= (element.y + element.h)) then - for i = 1, #element.child do - if pop.mousepressed(button, x, y, element.child[i]) then - return true - end - end - if element.mousepressed then - return element:mousepressed(button, x - element.x, y - element.y) - else - return false - end - end + element = pop.screen end + print("mousepressed", x, y, button, element) + return false end -pop.mousereleased = function(button, x, y, element) +pop.mousereleased = function(x, y, button, element) if element == nil then - element = pop.window + element = pop.screen end + print("mousereleased", x, y, button, element) + return false end pop.keypressed = function(key) - return print("pop.keypressed() is unimplemented.") + print("keypressed", key) + return false end pop.keyreleased = function(key) - return print("pop.keyreleased() is unimplemented.") + print("keyreleased", key) + return false end pop.textinput = function(text) - return print("pop.textinput() is unimplemented.") + print("textinput", text) + return false end -pop.skin = function(element, skin, apply_to_children) - if apply_to_children == nil then - apply_to_children = true +pop.skin = function(element, skin, depth) + if element == nil then + element = pop.screen end - element.margin = skin.margin - if element.background then + if skin == nil then + skin = pop.skins.default + end + if element.background and skin.background then element.background = skin.background end - if element.color then + if element.color and skin.color then element.color = skin.color end - if element.font then + if element.font and skin.font then element.font = skin.font end - if apply_to_children then - for i = 1, #element.child do - pop.skin(element.child[i], skin) + 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) + end + else + for i = 1, #element.child do + pop.skin(element.child[i], skin, false) + end end end end pop.debugDraw = function(element) if element == nil then - element = pop.window + element = pop.screen end if element.debugDraw then element:debugDraw() else graphics.setLineWidth(1) - graphics.setColor(0, 0, 0, 100) + graphics.setLineColor(0, 0, 0, 100) graphics.rectangle("fill", element.x, element.y, element.w, element.h) graphics.setColor(150, 150, 150, 150) graphics.rectangle("line", element.x, element.y, element.w, element.h) diff --git a/lib/pop/skins/default.lua b/lib/pop/skins/default.lua new file mode 100644 index 0000000..299adb2 --- /dev/null +++ b/lib/pop/skins/default.lua @@ -0,0 +1,17 @@ +local graphics +graphics = love.graphics +return { + background = { + 0, + 0, + 0, + 220 + }, + color = { + 255, + 255, + 255, + 250 + }, + font = graphics.newFont(14) +}