text element, fixed object creation

- inheritsFromElement improved
- fixed a couple bugs in element
- added inspect library
This commit is contained in:
Paul Liverman III 2017-01-10 12:57:02 -08:00
parent 3870b20e9f
commit 6255f30fa6
14 changed files with 207 additions and 22 deletions

3
.gitmodules vendored
View File

@ -7,3 +7,6 @@
[submodule "lib/inspect.lua"] [submodule "lib/inspect.lua"]
path = lib/inspect.lua path = lib/inspect.lua
url = https://github.com/kikito/inspect.lua.git url = https://github.com/kikito/inspect.lua.git
[submodule "lib/inspect"]
path = lib/inspect
url = https://github.com/kikito/inspect.lua.git

View File

@ -13,7 +13,8 @@ do
graphics.setColor(150, 255, 150, 150) graphics.setColor(150, 255, 150, 150)
graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h) graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(200, 255, 200, 255) 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, end,
align = function(self, horizontal, vertical, toPixel) align = function(self, horizontal, vertical, toPixel)
if toPixel == nil then if toPixel == nil then
@ -55,6 +56,7 @@ do
if h then if h then
self.data.h = h self.data.h = h
end end
self:align()
return self return self
end, end,
getSize = function(self) getSize = function(self)

View File

@ -43,6 +43,8 @@ class element
graphics.setColor 200, 255, 200, 255 graphics.setColor 200, 255, 200, 255
graphics.print "e", @data.x, @data.y graphics.print "e", @data.x, @data.y
return @
--- @todo doc me --- @todo doc me
align: (horizontal, vertical, toPixel=true) => align: (horizontal, vertical, toPixel=true) =>
unless @data.align return false unless @data.align return false
@ -71,7 +73,7 @@ class element
return @ return @
--- Sets an element's width/height. --- Sets an element's width/height. Fixes alignment if needed.
--- @tparam integer w[opt] Width. --- @tparam integer w[opt] Width.
--- @tparam integer h[opt] Height. --- @tparam integer h[opt] Height.
--- @treturn element self --- @treturn element self
@ -81,6 +83,8 @@ class element
if h if h
@data.h = h @data.h = h
@align!
return @ return @
--- Returns an element's width and height. --- Returns an element's width and height.

86
elements/text.lua Normal file
View File

@ -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

44
elements/text.moon Normal file
View File

@ -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!

View File

@ -3,9 +3,7 @@ local window
do do
local _class_0 local _class_0
local _parent_0 = element local _parent_0 = element
local _base_0 = { local _base_0 = { }
setSize = function(self) end
}
_base_0.__index = _base_0 _base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base) setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({ _class_0 = setmetatable({

View File

@ -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 --- @classmod window
--- @copyright Paul Liverman III (2016) --- @copyright Paul Liverman III (2016)
--- @license The MIT License (MIT) --- @license The MIT License (MIT)
@ -14,5 +15,5 @@ class window extends element
--- @todo if data, do stuff about it --- @todo if data, do stuff about it
setSize: => --setSize: =>
--do more stuff! --do more stuff!

View File

@ -121,21 +121,33 @@ pop.load = function()
pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight()) pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight())
return log("Created \"pop.screen\"") return log("Created \"pop.screen\"")
end end
pop.create = function(element, parent, ...) pop.create = function(element, parent, data, ...)
if parent == nil then if parent == nil then
parent = pop.screen parent = pop.screen
end end
if inheritsFromElement(parent) then 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.child, element)
insert(parent.data.child, element.data) insert(parent.data.child, element.data)
element.data.parent = parent.data element.data.parent = parent.data
elseif parent == false then 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.parent = false
element.data.parent = false element.data.parent = false
else 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.child, element)
insert(pop.screen.data.child, element.data) insert(pop.screen.data.child, element.data)
element.data.parent = pop.screen.data element.data.parent = pop.screen.data

View File

@ -150,22 +150,35 @@ pop.load = ->
--- @see pop --- @see pop
--- @see Element --- @see Element
pop.create = (element, parent=pop.screen, ...) -> pop.create = (element, parent=pop.screen, data, ...) ->
-- if valid parent element, use it -- if valid parent element, use it
if inheritsFromElement parent 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.child, element
insert parent.data.child, element.data insert parent.data.child, element.data
--element.parent = parent --element.parent = parent
element.data.parent = parent.data element.data.parent = parent.data
-- if explicitly no parent, just create the element -- if explicitly no parent, just create the element
elseif parent == false 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.parent = false
element.data.parent = false element.data.parent = false
-- else use pop.screen (and "parent" is actually the first argument) -- else use pop.screen (and "parent" is actually the first argument)
else 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.child, element
insert pop.screen.data.child, element.data insert pop.screen.data.child, element.data
--element.parent = pop.screen --element.parent = pop.screen

1
lib/inspect Submodule

@ -0,0 +1 @@
Subproject commit a384174649e8429cc3270a46cfacc37acaf6e042

View File

@ -1,3 +1,10 @@
local pop = require("") local pop = require("")
local inspect = require("lib/inspect/inspect") pop.text("Hello World!"):align("center", "center")
print(inspect(pop)) love.draw = function()
return pop.draw()
end
love.keypressed = function(key)
if key == "escape" then
return love.event.quit()
end
end

View File

@ -3,11 +3,25 @@
--- @license The MIT License (MIT) --- @license The MIT License (MIT)
pop = require "" 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 -- 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 return --this is to prevent default returning of last statement

View File

@ -1,6 +1,6 @@
local inheritsFromElement local inheritsFromElement
inheritsFromElement = function(object) inheritsFromElement = function(object)
if object and object.__class then if object and type(object) == "table" and object.__class then
local cls = object.__class local cls = object.__class
if cls.__name == "element" then if cls.__name == "element" then
return true return true

View File

@ -12,7 +12,7 @@
--- @see Element --- @see Element
inheritsFromElement = (object) -> inheritsFromElement = (object) ->
if object and object.__class if object and type(object) == "table" and object.__class
cls = object.__class cls = object.__class
if cls.__name == "element" if cls.__name == "element"