diff --git a/demo/main.lua b/demo/main.lua index e0f79b2..0ea8da3 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -17,6 +17,24 @@ function love.load() pop.skin(pop.text("Here's easier-to-code test text in the center!"):align("center", "center", true)) -- 'true' means align to pixel! w = pop.box(nil, {255, 255, 255, 255}):align(false, "bottom"):setSize(150, 150) b = pop.box(w, {0, 0, 0, 255}):setMargin(5):setSize(100, 100) + + c:move(100) + + w2 = pop.window(nil, "Window") + w2:move(100, 100) + w2:setWidth(500) + w2:move(-50, 80) + w2:setHeight(500) + w2:move(0, -175) + w2.child[2]:align("center") + --w2:align("center") + --w2:setAlignment("center"):align("center") + + --w2.child[1]:setBackground {100, 100, 100, 255} + --w2.child[3]:setBackground {160, 140, 40, 255} + + --TODO make rounding to nearest pixel DEFAULT BEHAVIOR + --TODO make debugdraw better end function love.update(dt) @@ -26,6 +44,7 @@ end function love.draw() pop.draw() --pop.debugDraw() + --w2:debugDraw() end function love.mousepressed(x, y, button) diff --git a/demo/pop/elements/element.lua b/demo/pop/elements/element.lua index 00fb75a..0dbe5c6 100644 --- a/demo/pop/elements/element.lua +++ b/demo/pop/elements/element.lua @@ -105,6 +105,32 @@ do getSize = function(self) return self.w, self.h end, + setWidth = function(self, w) + local _exp_0 = self.horizontal + if "center" == _exp_0 then + self.x = self.x - ((w - self.w) / 2) + elseif "right" == _exp_0 then + self.x = self.x - (w - self.w) + end + self.w = w + return self + end, + getWidth = function(self) + return self.w + end, + setHeight = function(self, h) + local _exp_0 = self.vertical + if "center" == _exp_0 then + self.y = self.y - ((h - self.h) / 2) + elseif "bottom" == _exp_0 then + self.y = self.y - (h - self.h) + end + self.h = h + return self + end, + getHeight = function(self) + return self.h + end, adjustSize = function(self, w, h) local W, H = self:getSize() if w then @@ -136,7 +162,7 @@ do elseif "bottom" == _exp_1 then self.y = self.y + (self.parent.h - self.h - self.margin) end - if toPixel then + if toPixel or (toPixel == nil) then self.x = floor(self.x) self.y = floor(self.y) end @@ -179,8 +205,8 @@ do self.x = 0 self.y = 0 end - self.w = 10 - self.h = 10 + self.w = 20 + self.h = 20 self.horizontal = "left" self.vertical = "top" self.margin = 0 diff --git a/demo/pop/elements/text.lua b/demo/pop/elements/text.lua index ac55175..44ee6a3 100644 --- a/demo/pop/elements/text.lua +++ b/demo/pop/elements/text.lua @@ -56,6 +56,14 @@ do self.h = h return self end, + setWidth = function(self) + self:setSize() + return self + end, + setHeight = function(self) + self:setSize() + return self + end, setText = function(self, text) if text == nil then text = "" diff --git a/demo/pop/elements/window.lua b/demo/pop/elements/window.lua new file mode 100644 index 0000000..f98d497 --- /dev/null +++ b/demo/pop/elements/window.lua @@ -0,0 +1,155 @@ +local graphics +graphics = love.graphics +local sub, len +do + local _obj_0 = string + sub, len = _obj_0.sub, _obj_0.len +end +local path = sub(..., 1, len(...) - len("/window")) +local element = require(tostring(path) .. "/element") +local box = require(tostring(path) .. "/box") +local text = require(tostring(path) .. "/text") +local window +do + local _class_0 + local _parent_0 = element + local _base_0 = { + 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.setColor(200, 0, 200, 200) + graphics.rectangle("line", self.x, self.y, self.w, self.h) + graphics.setColor(255, 200, 255, 255) + graphics.print("w", self.x, self.y) + return self + end, + setSize = function(self, w, h) + local x = 0 + local y = 0 + if w then + local _exp_0 = self.horizontal + if "center" == _exp_0 then + x = x - ((w - self.w) / 2) + elseif "right" == _exp_0 then + x = x - (w - self.w) + end + self.head:setWidth(w) + self.window:setWidth(w) + self.w = w + self.x = self.x + x + end + if h then + h = h - self.head:getHeight() + local _exp_0 = self.vertical + if "center" == _exp_0 then + y = y - ((h - self.h) / 2) + elseif "right" == _exp_0 then + y = y - (h - self.h) + end + self.window:setHeight(h) + self.h = h + self.head:getHeight() + self.y = self.y + y + end + self.head:move(x, y) + self.title:move(x, y) + self.window:move(x, y) + return self + end, + setWidth = function(self, w) + local x = 0 + local _exp_0 = self.horizontal + if "center" == _exp_0 then + x = x - ((w - self.w) / 2) + elseif "right" == _exp_0 then + x = x - (w - self.w) + end + self.head:setWidth(w) + self.window:setWidth(w) + self.w = w + self.x = self.x + x + self.head:move(x) + self.title:move(x) + self.window:move(x) + return self + end, + setHeight = function(self, h) + local y = 0 + h = h - self.head:getHeight() + local _exp_0 = self.vertical + if "center" == _exp_0 then + y = y - ((h - self.h) / 2) + elseif "right" == _exp_0 then + y = y - (h - self.h) + end + self.window:setHeight(h) + self.h = h + self.head:getHeight() + self.y = self.y + y + self.head:move(x, y) + self.title:move(x, y) + self.window:move(x, y) + return self + end + } + _base_0.__index = _base_0 + setmetatable(_base_0, _parent_0.__base) + _class_0 = setmetatable({ + __init = function(self, parent, title, tBackground) + if title == nil then + title = "window" + end + if tBackground == nil then + tBackground = { + 25, + 180, + 230, + 255 + } + end + _class_0.__parent.__init(self, parent) + self.head = box(self, tBackground) + self.title = text(self, title) + self.window = box(self, { + 0, + 0, + 0, + 255 + }) + local height = self.title:getHeight() + self.head:setSize(self.w, height) + self.window:move(nil, height) + self:setSize(100, 80) + self.child = { + self.head, + self.title, + self.window + } + end, + __base = _base_0, + __name = "window", + __parent = _parent_0 + }, { + __index = function(cls, name) + local val = rawget(_base_0, name) + if val == nil then + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end + else + return val + end + end, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + if _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end + window = _class_0 + return _class_0 +end diff --git a/lib/pop/elements/element.lua b/lib/pop/elements/element.lua index 00fb75a..0dbe5c6 100644 --- a/lib/pop/elements/element.lua +++ b/lib/pop/elements/element.lua @@ -105,6 +105,32 @@ do getSize = function(self) return self.w, self.h end, + setWidth = function(self, w) + local _exp_0 = self.horizontal + if "center" == _exp_0 then + self.x = self.x - ((w - self.w) / 2) + elseif "right" == _exp_0 then + self.x = self.x - (w - self.w) + end + self.w = w + return self + end, + getWidth = function(self) + return self.w + end, + setHeight = function(self, h) + local _exp_0 = self.vertical + if "center" == _exp_0 then + self.y = self.y - ((h - self.h) / 2) + elseif "bottom" == _exp_0 then + self.y = self.y - (h - self.h) + end + self.h = h + return self + end, + getHeight = function(self) + return self.h + end, adjustSize = function(self, w, h) local W, H = self:getSize() if w then @@ -136,7 +162,7 @@ do elseif "bottom" == _exp_1 then self.y = self.y + (self.parent.h - self.h - self.margin) end - if toPixel then + if toPixel or (toPixel == nil) then self.x = floor(self.x) self.y = floor(self.y) end @@ -179,8 +205,8 @@ do self.x = 0 self.y = 0 end - self.w = 10 - self.h = 10 + self.w = 20 + self.h = 20 self.horizontal = "left" self.vertical = "top" self.margin = 0 diff --git a/lib/pop/elements/text.lua b/lib/pop/elements/text.lua index ac55175..44ee6a3 100644 --- a/lib/pop/elements/text.lua +++ b/lib/pop/elements/text.lua @@ -56,6 +56,14 @@ do self.h = h return self end, + setWidth = function(self) + self:setSize() + return self + end, + setHeight = function(self) + self:setSize() + return self + end, setText = function(self, text) if text == nil then text = "" diff --git a/lib/pop/elements/window.lua b/lib/pop/elements/window.lua new file mode 100644 index 0000000..f98d497 --- /dev/null +++ b/lib/pop/elements/window.lua @@ -0,0 +1,155 @@ +local graphics +graphics = love.graphics +local sub, len +do + local _obj_0 = string + sub, len = _obj_0.sub, _obj_0.len +end +local path = sub(..., 1, len(...) - len("/window")) +local element = require(tostring(path) .. "/element") +local box = require(tostring(path) .. "/box") +local text = require(tostring(path) .. "/text") +local window +do + local _class_0 + local _parent_0 = element + local _base_0 = { + 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.setColor(200, 0, 200, 200) + graphics.rectangle("line", self.x, self.y, self.w, self.h) + graphics.setColor(255, 200, 255, 255) + graphics.print("w", self.x, self.y) + return self + end, + setSize = function(self, w, h) + local x = 0 + local y = 0 + if w then + local _exp_0 = self.horizontal + if "center" == _exp_0 then + x = x - ((w - self.w) / 2) + elseif "right" == _exp_0 then + x = x - (w - self.w) + end + self.head:setWidth(w) + self.window:setWidth(w) + self.w = w + self.x = self.x + x + end + if h then + h = h - self.head:getHeight() + local _exp_0 = self.vertical + if "center" == _exp_0 then + y = y - ((h - self.h) / 2) + elseif "right" == _exp_0 then + y = y - (h - self.h) + end + self.window:setHeight(h) + self.h = h + self.head:getHeight() + self.y = self.y + y + end + self.head:move(x, y) + self.title:move(x, y) + self.window:move(x, y) + return self + end, + setWidth = function(self, w) + local x = 0 + local _exp_0 = self.horizontal + if "center" == _exp_0 then + x = x - ((w - self.w) / 2) + elseif "right" == _exp_0 then + x = x - (w - self.w) + end + self.head:setWidth(w) + self.window:setWidth(w) + self.w = w + self.x = self.x + x + self.head:move(x) + self.title:move(x) + self.window:move(x) + return self + end, + setHeight = function(self, h) + local y = 0 + h = h - self.head:getHeight() + local _exp_0 = self.vertical + if "center" == _exp_0 then + y = y - ((h - self.h) / 2) + elseif "right" == _exp_0 then + y = y - (h - self.h) + end + self.window:setHeight(h) + self.h = h + self.head:getHeight() + self.y = self.y + y + self.head:move(x, y) + self.title:move(x, y) + self.window:move(x, y) + return self + end + } + _base_0.__index = _base_0 + setmetatable(_base_0, _parent_0.__base) + _class_0 = setmetatable({ + __init = function(self, parent, title, tBackground) + if title == nil then + title = "window" + end + if tBackground == nil then + tBackground = { + 25, + 180, + 230, + 255 + } + end + _class_0.__parent.__init(self, parent) + self.head = box(self, tBackground) + self.title = text(self, title) + self.window = box(self, { + 0, + 0, + 0, + 255 + }) + local height = self.title:getHeight() + self.head:setSize(self.w, height) + self.window:move(nil, height) + self:setSize(100, 80) + self.child = { + self.head, + self.title, + self.window + } + end, + __base = _base_0, + __name = "window", + __parent = _parent_0 + }, { + __index = function(cls, name) + local val = rawget(_base_0, name) + if val == nil then + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end + else + return val + end + end, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + if _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end + window = _class_0 + return _class_0 +end diff --git a/src/pop/elements/element.moon b/src/pop/elements/element.moon index f67942f..c8e48bb 100644 --- a/src/pop/elements/element.moon +++ b/src/pop/elements/element.moon @@ -13,8 +13,8 @@ class element @x = 0 @y = 0 - @w = 10 - @h = 10 + @w = 20 + @h = 20 @horizontal = "left" @vertical = "top" @@ -117,6 +117,34 @@ class element getSize: => return @w, @h + setWidth: (w) => + switch @horizontal + when "center" + @x -= (w - @w)/2 + when "right" + @x -= w - @w + + @w = w + + return @ + + getWidth: => + return @w + + setHeight: (h) => + switch @vertical + when "center" + @y -= (h - @h)/2 + when "bottom" + @y -= h - @h + + @h = h + + return @ + + getHeight: => + return @h + adjustSize: (w, h) => W, H = @getSize! @@ -152,7 +180,7 @@ class element when "bottom" @y += @parent.h - @h - @margin - if toPixel + if toPixel or (toPixel == nil) @x = floor @x @y = floor @y diff --git a/src/pop/elements/text.moon b/src/pop/elements/text.moon index fd90d29..d23833d 100644 --- a/src/pop/elements/text.moon +++ b/src/pop/elements/text.moon @@ -59,6 +59,16 @@ class text extends element return @ + -- cannot set width! + setWidth: => + @setSize! + return @ + + -- cannot set height! + setHeight: => + @setSize! + return @ + setText: (text="") => @text = text @setSize! diff --git a/src/pop/elements/window.moon b/src/pop/elements/window.moon new file mode 100644 index 0000000..c356c95 --- /dev/null +++ b/src/pop/elements/window.moon @@ -0,0 +1,111 @@ +import graphics from love +import sub, len from string + +path = sub ..., 1, len(...) - len "/window" +element = require "#{path}/element" +box = require "#{path}/box" +text = require "#{path}/text" + +class window extends element + new: (parent, title="window", tBackground={25, 180, 230, 255}) => + super parent + + @head = box @, tBackground -- title box at top + @title = text @, title -- text at top + @window = box @, {0,0,0,255} -- main window area + + -- correct placement / sizes of elements + height = @title\getHeight! + @head\setSize @w, height + @window\move nil, height + @setSize 100, 80 + + -- our child elements are still child elements + @child = { + @head, @title, @window + } + + debugDraw: => + graphics.setLineWidth 0.5 + graphics.setColor 0, 0, 0, 100 + graphics.rectangle "fill", @x, @y, @w, @h + graphics.setColor 200, 0, 200, 200 + graphics.rectangle "line", @x, @y, @w, @h + graphics.setColor 255, 200, 255, 255 + graphics.print "w", @x, @y + + return @ + + setSize: (w, h) => + x = 0 + y = 0 + + if w + switch @horizontal + when "center" + x -= (w - @w)/2 + when "right" + x -= w - @w + + @head\setWidth w + @window\setWidth w + @w = w + @x += x + + if h + h = h - @head\getHeight! + switch @vertical + when "center" + y -= (h - @h)/2 + when "right" + y -= h - @h + + @window\setHeight h + @h = h + @head\getHeight! + @y += y + + @head\move x, y + @title\move x, y + @window\move x, y + + return @ + + setWidth: (w) => + x = 0 + + switch @horizontal + when "center" + x -= (w - @w)/2 + when "right" + x -= w - @w + + @head\setWidth w + @window\setWidth w + @w = w + @x += x + + @head\move x + @title\move x + @window\move x + + return @ + + setHeight: (h) => + y = 0 + + h = h - @head\getHeight! + switch @vertical + when "center" + y -= (h - @h)/2 + when "right" + y -= h - @h + + @window\setHeight h + @h = h + @head\getHeight! + @y += y + + @head\move x, y + @title\move x, y + @window\move x, y + + return @ diff --git a/src/pop/init.moon b/src/pop/init.moon index a93d21b..48f0ce6 100644 --- a/src/pop/init.moon +++ b/src/pop/init.moon @@ -14,6 +14,7 @@ pop.focused = false -- loads elements and skins, creates pop.screen (intended to only be called once at the beginning) pop.load = -> elements = filesystem.getDirectoryItems "#{path}/elements" + for i = 1, #elements -- only attempt to load lua files unless elements[i]\sub(-4) == ".lua" @@ -36,11 +37,14 @@ pop.load = -> -- works just like above, except no wrappers skins = filesystem.getDirectoryItems "#{path}/skins" + for i = 1, #skins unless skins[i]\sub(-4) == ".lua" continue + name = skins[i]\sub 1, -5 pop.skins[name] = require "#{path}/skins/#{name}" + print "skin loaded: \"#{name}\"" -- main window (called screen because there will be a window element class) @@ -57,6 +61,7 @@ pop.create = (element, parent=pop.screen, ...) -> return element pop.update = (dt, element=pop.screen) -> + --pop.screen\update dt unless element.excludeUpdate if element.update element\update dt @@ -64,6 +69,7 @@ pop.update = (dt, element=pop.screen) -> pop.update dt, element.child[i] pop.draw = (element=pop.screen) -> + --pop.screen\draw! unless element.excludeDraw if element.draw element\draw!