From 0b1d48f3c53e833f7fe571fd3ad11b447ec804fa Mon Sep 17 00:00:00 2001 From: Fox Date: Thu, 21 Jan 2016 15:01:15 -0800 Subject: [PATCH] working, but skins suck --- conf.lua | 3 +++ main.lua | 7 ++++-- pop/elements/box.lua | 50 +++++++++++++++++++++++--------------- pop/elements/element.lua | 23 ++++++++++++++++-- pop/init.lua | 16 ++++++++++-- pop/skinclass.lua | 11 +++++++++ pop/skins/blackonwhite.lua | 6 +++++ pop/skins/clear.lua | 4 +-- run here.bat | 2 ++ 9 files changed, 94 insertions(+), 28 deletions(-) create mode 100644 conf.lua create mode 100644 pop/skinclass.lua create mode 100644 pop/skins/blackonwhite.lua create mode 100644 run here.bat diff --git a/conf.lua b/conf.lua new file mode 100644 index 0000000..ed9d1dc --- /dev/null +++ b/conf.lua @@ -0,0 +1,3 @@ +function love.conf(t) + t.console = true +end diff --git a/main.lua b/main.lua index 01bc655..5220177 100644 --- a/main.lua +++ b/main.lua @@ -1,8 +1,9 @@ local pop = require "pop" function love.load() - --pop.box() -- returns the box element - -- or pop.create("box") (this is what is actually called when you call pop.box()) + pop.setSkin("blackonwhite") + local align = pop.box():align("center", "center"):setSize(100, 100):setSkin("blackonwhite") + --print(align.skin) end function love.update(dt) @@ -11,6 +12,8 @@ end function love.draw() pop.draw() + love.graphics.setColor(255, 255, 255, 255) + --love.graphics.rectangle("fill", 0, 0, 100, 100) end function love.textinput(text) diff --git a/pop/elements/box.lua b/pop/elements/box.lua index b89b7a8..f7a6796 100644 --- a/pop/elements/box.lua +++ b/pop/elements/box.lua @@ -11,29 +11,39 @@ function box:initialize(pop, parent, skin) end function box:draw() --TODO these ifs are probably wrong - if type(self.skin.background) == "table" then - lg.setColor(self.skin.background) - lg.rectangle("fill", self.x, self.y, self.w, self.h) - else - lg.setColor(255, 255, 255, 255) - local w, h = self.skin.background:getDimensions() - -- scale! - w = self.w/w - h = self.h/h - lg.draw(self.skin.background, self.x, self.y, 0, w, h) + print("box drawn!") + + if self.skin.background then + if type(self.skin.background) == "table" then + lg.setColor(self.skin.background) + print(self.skin.background[4], self.x, self.y, self.w, self.h) + lg.rectangle("fill", self.x, self.y, self.w, self.h) + print("rect") + else + lg.setColor(255, 255, 255, 255) + local w, h = self.skin.background:getDimensions() + -- scale! + w = self.w/w + h = self.h/h + lg.draw(self.skin.background, self.x, self.y, 0, w, h) + end end - if type(self.skin.foreground) == "table" then - lg.setColor(self.skin.foreground) - lg.rectangle("fill", self.x, self.y, self.w, self.h) - else - lg.setColor(255, 255, 255, 255) - local w, h = self.skin.foreground:getDimensions() - -- scale! - w = self.w/w - h = self.h/h - lg.draw(self.skin.foreground, self.x, self.y, 0, w, h) + if self.skin.foreground then + if type(self.skin.foreground) == "table" then + lg.setColor(self.skin.foreground) + lg.rectangle("fill", self.x, self.y, self.w, self.h) + else + lg.setColor(255, 255, 255, 255) + local w, h = self.skin.foreground:getDimensions() + -- scale! + w = self.w/w + h = self.h/h + lg.draw(self.skin.foreground, self.x, self.y, 0, w, h) + end end + + return self end return box diff --git a/pop/elements/element.lua b/pop/elements/element.lua index 346984e..6e0d92d 100644 --- a/pop/elements/element.lua +++ b/pop/elements/element.lua @@ -4,6 +4,7 @@ local class = require(path .. "/lib/middleclass") local element = class("pop.element") function element:initialize(pop, parent, skin) + self.pop = pop --I hate this -.- self.parent = parent self.child = {} @@ -12,7 +13,11 @@ function element:initialize(pop, parent, skin) self.w = 10 self.h = 10 - self.skin = pop.skins[skin] or pop.skins[pop.currentSkin] + if skin then + self.skin = pop.skins[skin] + else + self.skin = pop.skins[pop.currentSkin] + end self.horizontal = "left" self.vertical = "top" @@ -27,6 +32,8 @@ function element:move(x, y) element.child[i]:move(x - oldX, y - oldY) end end + + return self end function element:setPosition(x, y) @@ -54,6 +61,8 @@ function element:setPosition(x, y) element.child[i]:move(x - oldX, y - oldY) end end + + return self end function element:getPosition() @@ -90,6 +99,8 @@ function element:setSize(w, h) self.w = w self.h = h + + return self end function element:getSize() @@ -113,6 +124,8 @@ function element:align(horizontal, vertical) elseif self.vertical == "bottom" then self.y = self.y + (self.parent.h - self.h) end + + return self end function element:alignTo(element, horizontal, vertical) @@ -122,6 +135,8 @@ function element:alignTo(element, horizontal, vertical) self:align(alignment) self.parent = realParent + + return self end function element:setAlignment(horizontal, vertical) @@ -131,14 +146,18 @@ function element:setAlignment(horizontal, vertical) if vertical then self.vertical = vertical end + + return self end function element:setSkin(skin) if type(skin) == "string" then - self.skin = pop.skins[skin] + self.skin = self.pop.skins[skin] else self.skin = skin end + + return self end return element diff --git a/pop/init.lua b/pop/init.lua index e558f5a..42a5474 100644 --- a/pop/init.lua +++ b/pop/init.lua @@ -5,7 +5,7 @@ local path = ... local pop = {} pop.elementClasses = {} --pop.elements = {} -pop.window = false --top level element, defined in pop.load() +pop.window = {child = {}} --top level element, defined in pop.load() pop.skins = {} pop.currentSkin = "clear" --TODO we need a "focused" element for textinput or whatever @@ -17,10 +17,12 @@ function pop.load() for i=1, #elementList do local name = elementList[i]:sub(1, -5) pop.elementClasses[name] = require(path .. "/elements/" .. name) + print("loaded \"" .. name .. "\" element") -- wrapper to be able to call pop.element() to create elements if not pop[name] then pop[name] = function(...) return pop.create(name, ...) end + print("pop." .. name .. "() created") end end @@ -31,10 +33,12 @@ function pop.load() local name = skinList[i]:sub(1, -5) pop.skins[name] = require(path .. "/skins/" .. name) pop.skins[name].name = name + print("loaded \"" .. name .. "\" skin") end -- set top element pop.window = pop.create("element"):setSize(lg.getWidth(), lg.getHeight()) + print("created pop.window") end function pop.create(elementType, parent, ...) @@ -43,7 +47,7 @@ function pop.create(elementType, parent, ...) end local newElement = pop.elementClasses[elementType](pop, parent, ...) - table.insert(parent.child, newElement) --NOTE pop.window is its own parent! + table.insert(parent.child, newElement) --NOTE pop.window is its own parent? return newElement end @@ -96,6 +100,14 @@ function pop.keyreleased(key) --TODO no idea what to do with this end +function pop.setSkin(skin) + if type(skin) == "string" then + pop.currentSkin = pop.skins[skin] + else + pop.currentSkin = skin + end +end + pop.load() return pop diff --git a/pop/skinclass.lua b/pop/skinclass.lua new file mode 100644 index 0000000..59f2a45 --- /dev/null +++ b/pop/skinclass.lua @@ -0,0 +1,11 @@ +--TODO make skins inherit this, and allow duplication and modification of skins on the fly +local path = string.sub(..., 1, string.len(...) - string.len("/skinclass")) +local class = require(path .. "/lib/middleclass") + +local skinclass = class("pop.skinclass") + +function skinclass:initialize() + -- +end + +return skinclass diff --git a/pop/skins/blackonwhite.lua b/pop/skins/blackonwhite.lua new file mode 100644 index 0000000..bb34560 --- /dev/null +++ b/pop/skins/blackonwhite.lua @@ -0,0 +1,6 @@ +local skin = {} + +skin.background = {255, 255, 255, 255} -- Drawable, color table, or false +skin.foreground = {0, 0, 0, 255} -- Drawable, color table, or false + +return skin diff --git a/pop/skins/clear.lua b/pop/skins/clear.lua index 8a00e9b..1a81a8d 100644 --- a/pop/skins/clear.lua +++ b/pop/skins/clear.lua @@ -1,6 +1,6 @@ local skin = {} -skin.background = false, -- Drawable, color table, or false -skin.foreground = {255,255,255,255} -- Drawable, color table, or false +skin.background = false -- Drawable, color table, or false +skin.foreground = {255, 255, 255, 255} -- Drawable, color table, or false return skin diff --git a/run here.bat b/run here.bat new file mode 100644 index 0000000..320101e --- /dev/null +++ b/run here.bat @@ -0,0 +1,2 @@ +@ECHO OFF +"C:\Program Files\LOVE\love.exe" "%cd%"