From 723997fe7a3220df55b567cbcc4d27d8fc324d4d Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Fri, 20 Nov 2015 03:30:40 -0800 Subject: [PATCH] mid-redesign --- README.md | 2 +- main.lua | 8 +++++++- pop/elements/box.lua | 6 +++--- pop/elements/element.lua | 40 +++++++++++++++++++++++++----------- pop/init.lua | 44 ++++++++++++++++++++++++++++------------ pop/skins/clearspace.lua | 10 +++++++++ 6 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 pop/skins/clearspace.lua diff --git a/README.md b/README.md index 9850a5d..e67f5dc 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ local pop = require "pop" local box = pop.box() ``` -* `box` is a box for containing things. +* `box` is a box (class) for containing things. * `text` is a class for handling text. * Nothing else! Is alpha, just started. diff --git a/main.lua b/main.lua index 4d2fd85..8d1e34c 100644 --- a/main.lua +++ b/main.lua @@ -1,4 +1,4 @@ -local pop = require "pop" +local pop = require "pop" --TODO tell user that pop must be required with SLASHES function love.load() pop.box() -- returns the box element @@ -16,3 +16,9 @@ end function love.mousereleased(button, x, y) pop.mousereleased(button, x, y) end + +function love.keypressed(key) + if key == "escape" then + love.event.quit() + end +end diff --git a/pop/elements/box.lua b/pop/elements/box.lua index 2de59f8..a7e57cf 100644 --- a/pop/elements/box.lua +++ b/pop/elements/box.lua @@ -9,11 +9,11 @@ function box:initialize(pop, parent) self.sizeControl = "specified" self.outerWidth = 300 self.outerHeight = 250 - self.innerWidth = self.outerWidth - self.style.borderSize - self.innerHeight = self.outerHeight - self.style.borderSize + self.innerWidth = self.outerWidth - self.skin.style.borderSize + self.innerHeight = self.outerHeight - self.skin.style.borderSize end -function box:update(pop) +function box:update() -- end diff --git a/pop/elements/element.lua b/pop/elements/element.lua index 09b0fb9..1df6888 100644 --- a/pop/elements/element.lua +++ b/pop/elements/element.lua @@ -1,15 +1,14 @@ local path = string.sub(..., 1, string.len(...) - string.len(".elements.element")) local class = require(path .. ".lib.middleclass") +--TODO determine if these requires can break because of slashes / subdirectories -local element = class("pop.element") --NOTE are periods allowed in middleclass class names? - ---TODO setting widths and heights need to call update() ---TODO setters and getters for just width/height, aliases for outerWidth/outerHeight +local element = class("pop.element") function element:initialize(pop, parent) - self.x = 0 - self.y = 0 - self.alignPoint = 1 -- 1 to 9, how aligned relative to x/y, see docs + self.ax = 0 -- absolute locations + self.ay = 0 + self.rx = 0 -- relative to parent locations + self.ry = 0 self.sizeControl = "fromInner" -- fromInner, fromOuter, specified self.outerWidth = 0 @@ -17,7 +16,7 @@ function element:initialize(pop, parent) self.innerWidth = 0 self.innerHeight = 0 - self.style = pop.style + self.skin = pop.skins[pop.currentSkin] self.visible = true self.parent = parent @@ -26,11 +25,20 @@ function element:initialize(pop, parent) parent.child[self] = self -- add ourselves to the parent's children end -function element:getAlignPoint() - return self.alignPoint +--TODO completely redefine interface based on what we should expect users to do +-- REMEMBER the goal is minimal effort on their part +-- THEREFORE, we should reduce this interface, they should rely on skins for borderSize (and thus, differences in outer/inner sizes) +-- all calls should be based on sizing the outside, and update() should update inners (including children!) based on outers + +function element:getSize() + return self.outerWidth, self.outerHeight end -function element:setAlignPoint(point) - self.alignPoint = point +function element:setSize(width, height) + assert(width > 0, "width must be above 0") + assert(height > 0, "height must be above 0") + self.outerWidth = width + self.outerHeight = height + self.sizeControl = "specified" end function element:getOuterWidth() @@ -39,6 +47,8 @@ end function element:setOuterWidth(width) assert(width > 0, "width must be above 0") self.outerWidth = width + self.sizeControl = "specified" + --TODO needs to update() to update inner size based on borderSize ??? end function element:getOuterHeight() @@ -47,6 +57,8 @@ end function element:setOuterHeight(height) assert(height > 0, "height must be above 0") self.outerHeight = height + self.sizeControl = "specified" + --TODO needs to update() to update inner size based on borderSize ??? end function element:getInnerWidth() @@ -55,6 +67,8 @@ end function element:setInnerWidth(width) assert(width > 0, "width must be above 0") self.innerWidth = width + self.sizeControl = "specified" + --TODO needs to update outerWidth ??? end function element:getInnerHeight() @@ -63,6 +77,8 @@ end function element:setInnerHeight(height) assert(height > 0, "height must be above 0") self.innerHeight = height + self.sizeControl = "specified" + --TODO needs to update outerHeight ?? end --[[ TODO determine how to write these better (consistency motherfucker) diff --git a/pop/init.lua b/pop/init.lua index 0b2274d..41bf3e3 100644 --- a/pop/init.lua +++ b/pop/init.lua @@ -1,12 +1,23 @@ +local lf = love.filesystem +local lg = love.graphics + local pop = {} -local path = ... -- this only works as long as the require() does't specify init.lua..which it shouldn't +local path = ... --NOTE Pop.Box must be required as its directory name (w SLASHES)! -- elements are local +--TODO require these how skins are required local box = require(path .. ".elements.box") local text = require(path .. ".elements.text") --- style defines how elements are drawn -pop.style = require(path .. ".skins.clear") +-- skins define how elements are drawn +pop.skins = {} +local skins = lf.getDirectoryItems(path .. "/skins") --NOTE Pop.Box must be required with SLASHES! +for _, v in ipairs(skins) do + local name = v:sub(1,-5) + pop.skins[name] = require(path .. "/skins/" .. name) + pop.skins[name].name = name +end +pop.currentSkin = "clearspace" --default skin -- everything has one parent element (initialized at the end) pop.parentElement = false @@ -30,34 +41,41 @@ function pop.create(elementType, parent, ...) end -- pretty wrappers to call pop.element() instead of pop.create("element") -pop.box = function() return pop.create("box", ...) end -pop.text = function() return pop.create("text", ...) end +pop.box = function(...) return pop.create("box", ...) end +pop.text = function(...) return pop.create("text", ...) end function pop.draw(element) if not element then element = pop.parentElement end - for _, childElements in pairs(element.child) do - --pop.draw(childElements, element.x, element.y) - pop.draw(childElements) - end - element:draw() + + for _, childElement in pairs(element.child) do + --pop.draw(childElement, element.x, element.y) + pop.draw(childElement) + end end -- TODO decide if we should track mouse movement +function pop.onMousePress(button, x, y) end +function pop.onMouseRelease(button, x, y) end + function pop.mousepressed(button, x, y) - -- + --TODO find which element it belongs to and if that element has a callback set, + -- if it does, use that, else, use the global callback (defined above..as nil) end function pop.mousereleased(button, x, y) - -- + --TODO find which element it belongs to and if that element has a callback set, + -- if it does, use that, else, use the global callback (defined above..as nil) end -- initialize the top element -pop.parentElement = box(pop, nil) -- nil because it has no parent +pop.parentElement = box(pop, {child={}}) -- dummy object because it has no parent +--pop.parentElement:setSizeControl("specified") -- make it not try to update itself (TODO or based on outro, and custom code to check top parent against screen size) +--pop.parentElement:setSize(lg.getWidth(), lg.getHeight()) -- fill window size --pop.parentElement:setVisible(false) -- uneeded since its clear... return pop diff --git a/pop/skins/clearspace.lua b/pop/skins/clearspace.lua new file mode 100644 index 0000000..1147ff5 --- /dev/null +++ b/pop/skins/clearspace.lua @@ -0,0 +1,10 @@ +local skin = {} + +skin.style = { + background = false, -- color table, image, or false + foreground = {0,0,0,1}, -- color table + borderSize = 2, -- integer (minimum 0) + borderStyle = false -- color table, image, or false +} + +return skin