diff --git a/.gitmodules b/.gitmodules index ab02186..0fa2828 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "lib/inspect.lua"] path = lib/inspect.lua url = https://github.com/kikito/inspect.lua.git +[submodule "lib/inspect"] + path = lib/inspect + url = https://github.com/kikito/inspect.lua.git diff --git a/elements/element.lua b/elements/element.lua index 2d0953a..ebaeaf3 100644 --- a/elements/element.lua +++ b/elements/element.lua @@ -13,7 +13,8 @@ do graphics.setColor(150, 255, 150, 150) graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h) graphics.setColor(200, 255, 200, 255) - return graphics.print("e", self.data.x, self.data.y) + graphics.print("e", self.data.x, self.data.y) + return self end, align = function(self, horizontal, vertical, toPixel) if toPixel == nil then @@ -55,6 +56,7 @@ do if h then self.data.h = h end + self:align() return self end, getSize = function(self) diff --git a/elements/element.moon b/elements/element.moon index 4291f3c..088c1a4 100644 --- a/elements/element.moon +++ b/elements/element.moon @@ -43,6 +43,8 @@ class element graphics.setColor 200, 255, 200, 255 graphics.print "e", @data.x, @data.y + return @ + --- @todo doc me align: (horizontal, vertical, toPixel=true) => unless @data.align return false @@ -71,7 +73,7 @@ class element return @ - --- Sets an element's width/height. + --- Sets an element's width/height. Fixes alignment if needed. --- @tparam integer w[opt] Width. --- @tparam integer h[opt] Height. --- @treturn element self @@ -81,6 +83,8 @@ class element if h @data.h = h + @align! + return @ --- Returns an element's width and height. diff --git a/elements/text.lua b/elements/text.lua new file mode 100644 index 0000000..e46e46c --- /dev/null +++ b/elements/text.lua @@ -0,0 +1,86 @@ +local graphics +graphics = love.graphics +local element = require(tostring((...):sub(1, -5)) .. "/element") +local text +do + local _class_0 + local _parent_0 = element + local _base_0 = { + draw = function(self) + graphics.setColor(self.data.color) + graphics.setFont(self.font) + graphics.print(self.data.text, self.data.x, self.data.y) + return self + end, + setSize = function(self) + self.data.w = self.font:getWidth(self.data.text) + self.data.h = self.font:getHeight() * (select(2, self.data.text:gsub("\n", "\n")) + 1) + return self + end, + setText = function(self, text) + self.data.text = text + return self:setSize() + end + } + _base_0.__index = _base_0 + setmetatable(_base_0, _parent_0.__base) + _class_0 = setmetatable({ + __init = function(self, parent, data, text, fontFile, fontSize) + if data == nil then + data = { } + end + if text == nil then + text = "" + end + if fontSize == nil then + fontSize = 14 + end + self.parent, self.data = parent, data + _class_0.__parent.__init(self, self.parent, self.data) + self.data.type = "text" + self.data.text = text + self.data.fontFile = fontFile + self.data.fontSize = fontSize + if not (self.data.color) then + self.data.color = { + 255, + 255, + 255, + 255 + } + end + if self.data.fontFile then + self.font = graphics.newFont(self.data.fontFile, self.data.fontSize) + else + self.font = graphics.newFont(self.data.fontSize) + end + return self:setSize() + end, + __base = _base_0, + __name = "text", + __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 + text = _class_0 + return _class_0 +end diff --git a/elements/text.moon b/elements/text.moon new file mode 100644 index 0000000..a96c446 --- /dev/null +++ b/elements/text.moon @@ -0,0 +1,44 @@ +--- A generic text element. Very basic. +--- @classmod text +--- @copyright Paul Liverman III (2016) +--- @license The MIT License (MIT) + +import graphics from love + +element = require "#{(...)\sub 1, -5}/element" + +class text extends element + --- Constructor expects nothing, or a data table describing it. + new: (@parent, @data={}, text="", fontFile, fontSize=14) => + super @parent, @data + + @data.type = "text" + @data.text = text + @data.fontFile = fontFile + @data.fontSize = fontSize + @data.color = {255, 255, 255, 255} unless @data.color + + if @data.fontFile + @font = graphics.newFont(@data.fontFile, @data.fontSize) + else + @font = graphics.newFont(@data.fontSize) + + @setSize! + + draw: => + graphics.setColor(@data.color) + graphics.setFont(@font) + graphics.print(@data.text, @data.x, @data.y) + + return @ + + --- Size is dependant on the text and font, so you cannot specify a size. + setSize: => + @data.w = @font\getWidth @data.text + @data.h = @font\getHeight! * (select(2, @data.text\gsub("\n", "\n")) + 1) --hack to get height of multiple lines + return @ + + --- Text should be set this way, or the object will not be the correct size. + setText: (text) => + @data.text = text + return @setSize! diff --git a/elements/window.lua b/elements/window.lua index c5c9723..e52137e 100644 --- a/elements/window.lua +++ b/elements/window.lua @@ -3,9 +3,7 @@ local window do local _class_0 local _parent_0 = element - local _base_0 = { - setSize = function(self) end - } + local _base_0 = { } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ diff --git a/elements/window.moon b/elements/window.moon index 056209e..d76e91d 100644 --- a/elements/window.moon +++ b/elements/window.moon @@ -1,4 +1,5 @@ ---- A generic window element. Supports resizing, minimizing(?), and closing. +--- A generic window element. Built-in support for minimize, maximize, and close +--- buttons, as well as drag-to-resize and drag-to-move. Title bar customizable. --- @classmod window --- @copyright Paul Liverman III (2016) --- @license The MIT License (MIT) @@ -14,5 +15,5 @@ class window extends element --- @todo if data, do stuff about it - setSize: => + --setSize: => --do more stuff! diff --git a/init.lua b/init.lua index b836bd4..9d33935 100644 --- a/init.lua +++ b/init.lua @@ -121,21 +121,33 @@ pop.load = function() pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight()) return log("Created \"pop.screen\"") end -pop.create = function(element, parent, ...) +pop.create = function(element, parent, data, ...) if parent == nil then parent = pop.screen end if inheritsFromElement(parent) then - element = pop.elements[element](parent, ...) + if type(data) == "table" then + element = pop.elements[element](parent, data, ...) + else + element = pop.elements[element](parent, { }, data, ...) + end insert(parent.child, element) insert(parent.data.child, element.data) element.data.parent = parent.data elseif parent == false then - element = pop.elements[element](false, ...) + if type(data) == "table" then + element = pop.elements[element](false, data, ...) + else + element = pop.elements[element](false, { }, data, ...) + end element.parent = false element.data.parent = false else - element = pop.elements[element](pop.screen, parent, ...) + if type(parent) == "table" then + element = pop.elements[element](pop.screen, parent, data, ...) + else + element = pop.elements[element](pop.screen, { }, parent, data, ...) + end insert(pop.screen.child, element) insert(pop.screen.data.child, element.data) element.data.parent = pop.screen.data diff --git a/init.moon b/init.moon index 6436e6e..5d8cca2 100644 --- a/init.moon +++ b/init.moon @@ -150,22 +150,35 @@ pop.load = -> --- @see pop --- @see Element -pop.create = (element, parent=pop.screen, ...) -> +pop.create = (element, parent=pop.screen, data, ...) -> -- if valid parent element, use it if inheritsFromElement parent - element = pop.elements[element](parent, ...) + if type(data) == "table" + element = pop.elements[element](parent, data, ...) + else + element = pop.elements[element](parent, {}, data, ...) insert parent.child, element insert parent.data.child, element.data --element.parent = parent element.data.parent = parent.data -- if explicitly no parent, just create the element elseif parent == false - element = pop.elements[element](false, ...) + if type(data) == "table" + element = pop.elements[element](false, data, ...) + else + element = pop.elements[element](false, {}, data, ...) element.parent = false element.data.parent = false -- else use pop.screen (and "parent" is actually the first argument) else - element = pop.elements[element](pop.screen, parent, ...) + if type(parent) == "table" -- then parent must be data table + element = pop.elements[element](pop.screen, parent, data, ...) + else -- parent must be an argument + element = pop.elements[element](pop.screen, {}, parent, data, ...) + --if type(data) == "table" + -- element = pop.elements[element](pop.screen, parent, data, ...) + --else + -- element = pop.elements[element](pop.screen, parent, {}, data, ...) insert pop.screen.child, element insert pop.screen.data.child, element.data --element.parent = pop.screen diff --git a/lib/inspect b/lib/inspect new file mode 160000 index 0000000..a384174 --- /dev/null +++ b/lib/inspect @@ -0,0 +1 @@ +Subproject commit a384174649e8429cc3270a46cfacc37acaf6e042 diff --git a/main.lua b/main.lua index 12f26a8..4acc4b1 100644 --- a/main.lua +++ b/main.lua @@ -1,3 +1,10 @@ local pop = require("") -local inspect = require("lib/inspect/inspect") -print(inspect(pop)) +pop.text("Hello World!"):align("center", "center") +love.draw = function() + return pop.draw() +end +love.keypressed = function(key) + if key == "escape" then + return love.event.quit() + end +end diff --git a/main.moon b/main.moon index 0f3d303..2b0cf74 100644 --- a/main.moon +++ b/main.moon @@ -3,11 +3,25 @@ --- @license The MIT License (MIT) pop = require "" ---- @todo write this! + +pop.text("Hello World!")\align "center", "center" + +--- @todo finish writing callbacks! + +love.draw = -> + pop.draw! + --pop.debugDraw! + +love.keypressed = (key) -> + if key == "escape" + love.event.quit! + + -- NOTE TEMPORARY +--inspect = require "lib/inspect/inspect" +--print inspect pop + -inspect = require "lib/inspect/inspect" -print inspect pop return --this is to prevent default returning of last statement diff --git a/util.lua b/util.lua index 0804d8b..4d53f8c 100644 --- a/util.lua +++ b/util.lua @@ -1,6 +1,6 @@ local inheritsFromElement inheritsFromElement = function(object) - if object and object.__class then + if object and type(object) == "table" and object.__class then local cls = object.__class if cls.__name == "element" then return true diff --git a/util.moon b/util.moon index b939336..29be9f4 100644 --- a/util.moon +++ b/util.moon @@ -12,7 +12,7 @@ --- @see Element inheritsFromElement = (object) -> - if object and object.__class + if object and type(object) == "table" and object.__class cls = object.__class if cls.__name == "element"