mirror of
https://github.com/TangentFoxy/Pop.Box.git
synced 2024-12-15 12:44:20 +00:00
text element, fixed object creation
- inheritsFromElement improved - fixed a couple bugs in element - added inspect library
This commit is contained in:
parent
3870b20e9f
commit
6255f30fa6
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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.
|
||||
|
86
elements/text.lua
Normal file
86
elements/text.lua
Normal 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
44
elements/text.moon
Normal 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!
|
@ -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({
|
||||
|
@ -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!
|
||||
|
20
init.lua
20
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
|
||||
|
21
init.moon
21
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
|
||||
|
1
lib/inspect
Submodule
1
lib/inspect
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit a384174649e8429cc3270a46cfacc37acaf6e042
|
11
main.lua
11
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
|
||||
|
20
main.moon
20
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
|
||||
|
2
util.lua
2
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
|
||||
|
Loading…
Reference in New Issue
Block a user