Better prepared for usage

- added modified debugDraw to element class
- made window element properly inherit from element class
- fixed path detection on initialization
- added better logging and more logging to initialization
- fixed stupid bug with pop.debugDraw
This commit is contained in:
Paul Liverman III 2016-10-30 22:19:24 -07:00
parent fda0a340c9
commit bb2cbba024
12 changed files with 141 additions and 36 deletions

View File

@ -71,6 +71,10 @@
<td class="summary">Constructor expects nothing, or a data table describing it.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#element.debugDraw">element.debugDraw (self)</a></td>
<td class="summary">Slightly modified from pop.debugDraw</td>
</tr>
<tr>
<td class="name" nowrap><a href="#element.setSize">element.setSize (w, h)</a></td>
<td class="summary">Sets an element's width/height.</td>
</tr>
@ -133,6 +137,32 @@
</dd>
<dt>
<a name = "element.debugDraw"></a>
<strong>element.debugDraw (self)</strong>
</dt>
<dd>
Slightly modified from pop.debugDraw
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">self</span>
</li>
</ul>
<h3>See also:</h3>
<ul>
<a href="../modules/pop.html#debugDraw">pop.debugDraw</a>
</ul>
</dd>
<dt>
<a name = "element.setSize"></a>
@ -313,7 +343,7 @@
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.3</a></i>
<i style="float:right;">Last updated 2016-09-07 21:37:53 </i>
<i style="float:right;">Last updated 2016-10-30 22:18:17 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>

View File

@ -79,7 +79,7 @@
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.3</a></i>
<i style="float:right;">Last updated 2016-09-07 21:37:53 </i>
<i style="float:right;">Last updated 2016-10-30 22:18:17 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>

View File

@ -490,7 +490,7 @@
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.3</a></i>
<i style="float:right;">Last updated 2016-09-07 21:37:53 </i>
<i style="float:right;">Last updated 2016-10-30 22:18:17 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>

View File

@ -71,7 +71,7 @@
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.3</a></i>
<i style="float:right;">Last updated 2016-09-07 21:37:53 </i>
<i style="float:right;">Last updated 2016-10-30 22:18:17 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>

View File

@ -665,7 +665,7 @@ table.insert element.parent, element.parent\removeChild(element),
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.3</a></i>
<i style="float:right;">Last updated 2016-09-07 21:37:53 </i>
<i style="float:right;">Last updated 2016-10-30 22:18:17 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>

View File

@ -125,7 +125,7 @@
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.3</a></i>
<i style="float:right;">Last updated 2016-09-07 21:37:53 </i>
<i style="float:right;">Last updated 2016-10-30 22:18:17 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>

View File

@ -1,7 +1,18 @@
local graphics
graphics = love.graphics
local element
do
local _class_0
local _base_0 = {
debugDraw = function(self)
graphics.setLineWidth(1)
graphics.setColor(0, 20, 0, 100)
graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
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)
end,
setSize = function(self, w, h)
if w then
self.data.w = w
@ -63,6 +74,7 @@ do
if self.data.draw == nil then
self.data.draw = true
end
self.child = { }
end,
__base = _base_0,
__name = "element"

View File

@ -3,6 +3,8 @@
--- @copyright Paul Liverman III (2016)
--- @license The MIT License (MIT)
import graphics from love
class element
--- Constructor expects nothing, or a data table describing it.
--- @tparam ?Element|false parent The parent element.
@ -12,7 +14,7 @@ class element
if type @data != "table"
@data = {}
@data.parent = false unless @data.parent --included for correctness
@data.parent = false unless @data.parent
@data.child = {} unless @data.child
@data.x = 0 unless @data.x
@data.y = 0 unless @data.y
@ -21,6 +23,19 @@ class element
@data.update = false if @data.update == nil
@data.draw = true if @data.draw == nil
@child = {}
--- Slightly modified from pop.debugDraw
--- @see pop.debugDraw
debugDraw: =>
graphics.setLineWidth 1
graphics.setColor 0, 20, 0, 100
graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h
graphics.setColor 150, 255, 150, 150
graphics.rectangle "line", @data.x, @data.y, @data.w, @data.h
graphics.setColor 200, 255, 200, 255
graphics.print "e", @data.x, @data.y
--- Sets an element's width/height.
--- @tparam integer w[opt] Width.
--- @tparam integer h[opt] Height.

View File

@ -1,21 +1,36 @@
local element = require(tostring((...):sub(1, -7)) .. "/element")
local window
do
local _class_0
local _parent_0 = element
local _base_0 = {
setSize = function(self) end
}
_base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, parent, data)
if data == nil then
data = { }
end
self.parent, self.data = parent, data
return _class_0.__parent.__init(self, self.parent, self.data)
end,
__base = _base_0,
__name = "window"
__name = "window",
__parent = _parent_0
}, {
__index = _base_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, ...)
@ -23,6 +38,9 @@ do
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

View File

@ -3,9 +3,13 @@
--- @copyright Paul Liverman III (2016)
--- @license The MIT License (MIT)
class window
element = require "#{(...)\sub 1, -7}/element"
class window extends element
--- Constructor expects nothing, or a data table describing it.
new: (@parent, @data={}) =>
super @parent, @data
--- @todo if data, do stuff about it
setSize: =>

View File

@ -5,16 +5,21 @@ local pop = {
_LICENSE = 'The MIT License (MIT)',
_AUTHOR = 'Paul Liverman III'
}
local log
log = function(...)
return print("[Pop.Box]", ...)
end
if not (love.getVersion) then
error("Pop.Box only supports LOVE versions >= 0.9.1")
end
local path = ...
local path = (...):gsub("%.", "/")
if (...):sub(-4) == "init" then
path = (...):sub(1, -5)
if not (path) then
path = "."
end
end
log("Require path detected: \"" .. tostring(path) .. "\"")
local filesystem, graphics
do
local _obj_0 = love
@ -29,21 +34,25 @@ pop.skins = { }
pop.extensions = { }
pop.screen = false
pop.focused = false
pop.log = log
pop.load = function()
log("Loading elements from \"" .. tostring(path) .. "/elements\"")
local elements = filesystem.getDirectoryItems(tostring(path) .. "/elements")
for i = 1, #elements do
local _continue_0 = false
repeat
if not (elements[i]:sub(-4) == ".lua") then
log("Ignored non-Lua file \"" .. tostring(path) .. "/elements/" .. tostring(elements[i]) .. "\"")
_continue_0 = true
break
end
local name = elements[i]:sub(1, -5)
log("Requiring \"" .. tostring(name) .. "\" from \"" .. tostring(path) .. "/elements/" .. tostring(name) .. "\"")
pop.elements[name] = require(tostring(path) .. "/elements/" .. tostring(name))
if pop.elements[name].load then
pop.elements[name].load(pop)
end
print("element loaded: \"" .. tostring(name) .. "\"")
log("Element loaded: \"" .. tostring(name) .. "\"")
if not (pop[name]) then
if pop.elements[name].wrap then
pop[name] = pop.elements[name].wrap(pop)
@ -52,7 +61,7 @@ pop.load = function()
return pop.create(name, ...)
end
end
print("wrapper created: \"pop." .. tostring(name) .. "()\"")
log("Wrapper created: \"pop." .. tostring(name) .. "()\"")
end
_continue_0 = true
until true
@ -65,15 +74,17 @@ pop.load = function()
local _continue_0 = false
repeat
if not (skins[i]:sub(-4) == ".lua") then
log("Ignored non-Lua file \"" .. tostring(path) .. "/skins/" .. tostring(skins[i]) .. "\"")
_continue_0 = true
break
end
local name = skins[i]:sub(1, -5)
log("Requiring \"" .. tostring(name) .. "\" from \"" .. tostring(path) .. "/skins/" .. tostring(name) .. "\"")
pop.skins[name] = require(tostring(path) .. "/skins/" .. tostring(name))
if pop.skins[name].load then
pop.skins[name].load(pop)
end
print("skin loaded: \"" .. tostring(name) .. "\"")
log("Skin loaded: \"" .. tostring(name) .. "\"")
_continue_0 = true
until true
if not _continue_0 then
@ -85,15 +96,17 @@ pop.load = function()
local _continue_0 = false
repeat
if not (extensions[i]:sub(-4) == ".lua") then
log("Ignored non-Lua file \"" .. tostring(path) .. "/extensions/" .. tostring(extensions[i]) .. "\"")
_continue_0 = true
break
end
local name = extensions[i]:sub(1, -5)
log("Requiring \"" .. tostring(name) .. "\" from \"" .. tostring(path) .. "/extensions/" .. tostring(name) .. "\"")
pop.extensions[name] = require(tostring(path) .. "/extensions/" .. tostring(name))
if pop.extensions[name].load then
pop.extensions[name].load(pop)
end
print("extension loaded: \"" .. tostring(name) .. "\"")
log("Extension loaded: \"" .. tostring(name) .. "\"")
_continue_0 = true
until true
if not _continue_0 then
@ -101,7 +114,7 @@ pop.load = function()
end
end
pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight())
return print("created \"pop.screen\"")
return log("Created \"pop.screen\"")
end
pop.create = function(element, parent, ...)
if parent == nil then
@ -160,7 +173,7 @@ pop.mousemoved = function(x, y, dx, dy)
end
pop.mousepressed = function(x, y, button, element)
if not (element) then
print("mousepressed", x, y, button)
log("mousepressed", x, y, button)
element = pop.screen
end
local handled = false
@ -210,13 +223,13 @@ pop.mousereleased = function(x, y, button, element)
end
end
else
print("mousereleased", x, y, button)
log("mousereleased", x, y, button)
pop.mousereleased(x, y, button, pop.screen)
end
return clickedHandled, mousereleasedHandled
end
pop.keypressed = function(key)
print("keypressed", key)
log("keypressed", key)
local element = pop.focused
if element and element.keypressed and element.data.draw then
return element.keypressed(key)
@ -224,7 +237,7 @@ pop.keypressed = function(key)
return false
end
pop.keyreleased = function(key)
print("keyreleased", key)
log("keyreleased", key)
local element = pop.focused
if element and element.keyreleased then
return element.keyreleased(key)
@ -232,7 +245,7 @@ pop.keyreleased = function(key)
return false
end
pop.textinput = function(text)
print("textinput", text)
log("textinput", text)
local element = pop.focused
if element and element.textinput and element.data.draw then
return element.textinput(text)
@ -247,7 +260,7 @@ pop.debugDraw = function(element)
element:debugDraw()
else
graphics.setLineWidth(1)
graphics.setLineColor(0, 0, 0, 100)
graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", element.data.x, element.data.y, element.data.w, element.data.h)
graphics.setColor(150, 150, 150, 150)
graphics.rectangle("line", element.data.x, element.data.y, element.data.w, element.data.h)
@ -275,7 +288,7 @@ pop.printElementTree = function(element, depth)
end
cls = cls .. " (" .. tostring(bg) .. ")"
end
print(string.rep("-", depth) .. " " .. tostring(cls))
log(string.rep("-", depth) .. " " .. tostring(cls))
for i = 1, #element.child do
pop.printElementTree(element.child[i], depth + 1)
end

View File

@ -12,16 +12,21 @@ pop = {
_AUTHOR: 'Paul Liverman III'
}
log = (...) ->
print "[Pop.Box]", ...
unless love.getVersion
error "Pop.Box only supports LOVE versions >= 0.9.1"
path = ...
path = (...)\gsub "%.", "/"
if (...)\sub(-4) == "init"
path = (...)\sub 1, -5
unless path
path = "."
log "Require path detected: \"#{path}\""
import filesystem, graphics from love
import insert from table
import inheritsFromElement from require "#{path}/util"
@ -42,6 +47,7 @@ pop.skins = {}
pop.extensions = {}
pop.screen = false
pop.focused = false
pop.log = log
@ -56,21 +62,24 @@ pop.focused = false
pop.load = ->
--@todo @ see Skins
--@todo @ see Extensions
log "Loading elements from \"#{path}/elements\""
elements = filesystem.getDirectoryItems "#{path}/elements"
for i = 1, #elements
-- ignore non-Lua files
unless elements[i]\sub(-4) == ".lua"
log "Ignored non-Lua file \"#{path}/elements/#{elements[i]}\""
continue
-- require into pop.elements table by filename
name = elements[i]\sub 1, -5
log "Requiring \"#{name}\" from \"#{path}/elements/#{name}\""
pop.elements[name] = require "#{path}/elements/#{name}"
-- call the element's load function if it exists
if pop.elements[name].load
pop.elements[name].load pop
print "element loaded: \"#{name}\""
log "Element loaded: \"#{name}\""
-- create "pop.element()" function wrapper if possible
unless pop[name]
@ -80,46 +89,50 @@ pop.load = ->
pop[name] = (...) ->
return pop.create(name, ...)
print "wrapper created: \"pop.#{name}()\""
log "Wrapper created: \"pop.#{name}()\""
skins = filesystem.getDirectoryItems "#{path}/skins"
for i = 1, #skins
-- ignore non-Lua files
unless skins[i]\sub(-4) == ".lua"
log "Ignored non-Lua file \"#{path}/skins/#{skins[i]}\""
continue
-- require into pop.skins table by filename
name = skins[i]\sub 1, -5
log "Requiring \"#{name}\" from \"#{path}/skins/#{name}\""
pop.skins[name] = require "#{path}/skins/#{name}"
-- call the skin's load function if it exists
if pop.skins[name].load
pop.skins[name].load pop
print "skin loaded: \"#{name}\""
log "Skin loaded: \"#{name}\""
extensions = filesystem.getDirectoryItems "#{path}/extensions"
for i = 1, #extensions
-- ignore non-Lua files
unless extensions[i]\sub(-4) == ".lua"
log "Ignored non-Lua file \"#{path}/extensions/#{extensions[i]}\""
continue
-- require into pop.extensions by filename
name = extensions[i]\sub 1, -5
log "Requiring \"#{name}\" from \"#{path}/extensions/#{name}\""
pop.extensions[name] = require "#{path}/extensions/#{name}"
-- call the extension's load function if it exists
if pop.extensions[name].load
pop.extensions[name].load pop
print "extension loaded: \"#{name}\""
log "Extension loaded: \"#{name}\""
-- Initialize pop.screen (top element, GUI area)
pop.screen = pop.create("element", false)\setSize(graphics.getWidth!, graphics.getHeight!)
print "created \"pop.screen\""
log "Created \"pop.screen\""
@ -227,7 +240,7 @@ pop.mousemoved = (x, y, dx, dy) ->
pop.mousepressed = (x, y, button, element) ->
-- start at the screen, print that we received an event
unless element
print "mousepressed", x, y, button
log "mousepressed", x, y, button
element = pop.screen
-- have we handled the event?
@ -298,7 +311,7 @@ pop.mousereleased = (x, y, button, element) ->
-- else, default to pop.screen to begin! (and print that we received an event)
else
print "mousereleased", x, y, button
log "mousereleased", x, y, button
pop.mousereleased x, y, button, pop.screen
return clickedHandled, mousereleasedHandled
@ -311,7 +324,7 @@ pop.mousereleased = (x, y, button, element) ->
--- @treturn boolean Was the event handled?
pop.keypressed = (key) ->
print "keypressed", key
log "keypressed", key
-- keypressed events must be on visible elements
element = pop.focused
@ -328,7 +341,7 @@ pop.keypressed = (key) ->
--- @treturn boolean Was the event handled?
pop.keyreleased = (key) ->
print "keyreleased", key
log "keyreleased", key
-- keyreleased events are always called
element = pop.focused
@ -345,7 +358,7 @@ pop.keyreleased = (key) ->
--- @treturn boolean Was the text input handled?
pop.textinput = (text) ->
print "textinput", text
log "textinput", text
-- textinput events must be on visible elements
element = pop.focused
@ -368,7 +381,7 @@ pop.debugDraw = (element=pop.screen) ->
element\debugDraw!
else
graphics.setLineWidth 1
graphics.setLineColor 0, 0, 0, 100
graphics.setColor 0, 0, 0, 100
graphics.rectangle "fill", element.data.x, element.data.y, element.data.w, element.data.h
graphics.setColor 150, 150, 150, 150
graphics.rectangle "line", element.data.x, element.data.y, element.data.w, element.data.h
@ -400,7 +413,7 @@ pop.printElementTree = (element=pop.screen, depth=0) ->
cls = cls .. " (#{bg})"
print string.rep("-", depth) .. " #{cls}"
log string.rep("-", depth) .. " #{cls}"
for i = 1, #element.child
pop.printElementTree element.child[i], depth + 1