2016-06-21 05:52:44 +00:00
|
|
|
--- The Pop.Box GUI itself.
|
|
|
|
--- @module pop
|
|
|
|
--- @copyright Paul Liverman III (2015-2016)
|
|
|
|
--- @license The MIT License (MIT)
|
2016-08-02 23:30:59 +00:00
|
|
|
--- @release 0.0.0
|
2016-06-21 05:52:44 +00:00
|
|
|
|
2016-04-18 06:22:40 +00:00
|
|
|
pop = {
|
2016-08-02 23:30:59 +00:00
|
|
|
_VERSION: '0.0.0'
|
2016-04-20 21:12:32 +00:00
|
|
|
_DESCRIPTION: 'GUI library for LOVE, designed for ease of use'
|
|
|
|
_URL: 'http://github.com/Guard13007/Pop.Box'
|
|
|
|
_LICENSE: 'The MIT License (MIT)'
|
|
|
|
_AUTHOR: 'Paul Liverman III'
|
2016-04-18 06:22:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-31 05:19:24 +00:00
|
|
|
log = (...) ->
|
|
|
|
print "[Pop.Box]", ...
|
|
|
|
|
2016-04-02 03:45:43 +00:00
|
|
|
unless love.getVersion
|
2016-04-05 03:59:42 +00:00
|
|
|
error "Pop.Box only supports LOVE versions >= 0.9.1"
|
2016-04-02 03:45:43 +00:00
|
|
|
|
2016-10-31 05:19:24 +00:00
|
|
|
path = (...)\gsub "%.", "/"
|
2016-06-21 05:52:44 +00:00
|
|
|
|
2016-08-21 05:29:16 +00:00
|
|
|
if (...)\sub(-4) == "init"
|
|
|
|
path = (...)\sub 1, -5
|
|
|
|
unless path
|
|
|
|
path = "."
|
|
|
|
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Require path detected: \"#{path}\""
|
|
|
|
|
2016-02-23 03:55:05 +00:00
|
|
|
import filesystem, graphics from love
|
|
|
|
import insert from table
|
2016-08-02 23:30:59 +00:00
|
|
|
import inheritsFromElement from require "#{path}/util"
|
2016-10-31 06:40:40 +00:00
|
|
|
import dumps, loads from require "#{path}/lib/bitser/bitser"
|
2016-02-23 03:55:05 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
--- @table pop
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @tfield table elements All GUI classes are stored here.
|
|
|
|
--- @tfield table skins All skins are stored here.
|
|
|
|
--- @tfield table extensions All extensions are loaded here.
|
|
|
|
--- @tfield Element screen The top level GUI element. Represents the game
|
|
|
|
--- screen. Initialized in `pop.load()`
|
|
|
|
--- @tfield ?Element|false focused The currently focused GUI element (or `false`
|
|
|
|
--- if none is focused).
|
2016-06-21 05:52:44 +00:00
|
|
|
--- @see pop.load
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @see Element
|
2016-06-21 05:52:44 +00:00
|
|
|
|
2016-02-25 01:02:56 +00:00
|
|
|
pop.elements = {}
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.skins = {}
|
2016-08-21 05:29:16 +00:00
|
|
|
pop.extensions = {}
|
2016-06-21 05:52:44 +00:00
|
|
|
pop.screen = false
|
2016-03-30 17:50:33 +00:00
|
|
|
pop.focused = false
|
2016-10-31 05:19:24 +00:00
|
|
|
pop.log = log
|
2016-03-29 00:59:12 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
|
|
|
|
|
2016-08-21 05:29:16 +00:00
|
|
|
--- Loads elements, skins, extensions, and initializes `pop.screen`.
|
|
|
|
---
|
2016-08-21 07:49:16 +00:00
|
|
|
--- **IMPORTANT**: Intended to only be called once, and is automatically called
|
|
|
|
--- when you require Pop.Box.
|
2016-06-21 05:52:44 +00:00
|
|
|
--- @function load
|
|
|
|
--- @see pop
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @see Element
|
2016-06-21 05:52:44 +00:00
|
|
|
|
2016-02-25 01:02:56 +00:00
|
|
|
pop.load = ->
|
2016-08-21 05:29:16 +00:00
|
|
|
--@todo @ see Skins
|
|
|
|
--@todo @ see Extensions
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Loading elements from \"#{path}/elements\""
|
2016-02-25 01:02:56 +00:00
|
|
|
elements = filesystem.getDirectoryItems "#{path}/elements"
|
|
|
|
for i = 1, #elements
|
2016-06-21 05:52:44 +00:00
|
|
|
-- ignore non-Lua files
|
2016-03-29 00:59:12 +00:00
|
|
|
unless elements[i]\sub(-4) == ".lua"
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Ignored non-Lua file \"#{path}/elements/#{elements[i]}\""
|
2016-03-29 00:59:12 +00:00
|
|
|
continue
|
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
-- require into pop.elements table by filename
|
2016-02-25 01:02:56 +00:00
|
|
|
name = elements[i]\sub 1, -5
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Requiring \"#{name}\" from \"#{path}/elements/#{name}\""
|
2016-02-25 01:02:56 +00:00
|
|
|
pop.elements[name] = require "#{path}/elements/#{name}"
|
2016-04-03 07:27:15 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
-- call the element's load function if it exists
|
2016-04-03 07:27:15 +00:00
|
|
|
if pop.elements[name].load
|
|
|
|
pop.elements[name].load pop
|
|
|
|
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Element loaded: \"#{name}\""
|
2016-03-29 00:59:12 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
-- create "pop.element()" function wrapper if possible
|
2016-03-29 00:59:12 +00:00
|
|
|
unless pop[name]
|
|
|
|
if pop.elements[name].wrap
|
|
|
|
pop[name] = pop.elements[name].wrap pop
|
|
|
|
else
|
|
|
|
pop[name] = (...) ->
|
|
|
|
return pop.create(name, ...)
|
2016-02-25 01:02:56 +00:00
|
|
|
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Wrapper created: \"pop.#{name}()\""
|
2016-02-25 01:02:56 +00:00
|
|
|
|
2016-03-30 21:01:15 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
skins = filesystem.getDirectoryItems "#{path}/skins"
|
2016-03-29 00:59:12 +00:00
|
|
|
for i = 1, #skins
|
2016-06-21 05:52:44 +00:00
|
|
|
-- ignore non-Lua files
|
2016-03-29 00:59:12 +00:00
|
|
|
unless skins[i]\sub(-4) == ".lua"
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Ignored non-Lua file \"#{path}/skins/#{skins[i]}\""
|
2016-03-29 00:59:12 +00:00
|
|
|
continue
|
2016-03-30 21:01:15 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
-- require into pop.skins table by filename
|
2016-03-29 00:59:12 +00:00
|
|
|
name = skins[i]\sub 1, -5
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Requiring \"#{name}\" from \"#{path}/skins/#{name}\""
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.skins[name] = require "#{path}/skins/#{name}"
|
2016-03-30 21:01:15 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
-- call the skin's load function if it exists
|
|
|
|
if pop.skins[name].load
|
|
|
|
pop.skins[name].load pop
|
|
|
|
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Skin loaded: \"#{name}\""
|
2016-02-25 01:02:56 +00:00
|
|
|
|
2016-04-01 21:14:24 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
extensions = filesystem.getDirectoryItems "#{path}/extensions"
|
2016-04-01 21:14:24 +00:00
|
|
|
for i = 1, #extensions
|
2016-06-21 05:52:44 +00:00
|
|
|
-- ignore non-Lua files
|
2016-04-01 21:14:24 +00:00
|
|
|
unless extensions[i]\sub(-4) == ".lua"
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Ignored non-Lua file \"#{path}/extensions/#{extensions[i]}\""
|
2016-04-01 21:14:24 +00:00
|
|
|
continue
|
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
-- require into pop.extensions by filename
|
2016-04-01 21:14:24 +00:00
|
|
|
name = extensions[i]\sub 1, -5
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Requiring \"#{name}\" from \"#{path}/extensions/#{name}\""
|
2016-08-21 05:29:16 +00:00
|
|
|
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
|
2016-04-01 21:14:24 +00:00
|
|
|
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Extension loaded: \"#{name}\""
|
2016-04-02 03:45:43 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
|
|
|
|
-- Initialize pop.screen (top element, GUI area)
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.screen = pop.create("element", false)\setSize(graphics.getWidth!, graphics.getHeight!)
|
2016-10-31 05:19:24 +00:00
|
|
|
log "Created \"pop.screen\""
|
2016-02-25 01:02:56 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
--- Creates an element.
|
|
|
|
--- @function create
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @tparam string element The element class to use.
|
|
|
|
--- @tparam ?Element|false|nil parent[opt] The parent element. If `false`, an
|
|
|
|
--- element is created with no parent. If `nil`, defaults to `pop.screen`.
|
|
|
|
--- @param ...[opt] Any number of parameters can be passed to the constructor
|
|
|
|
--- for the element.
|
2016-08-21 05:29:16 +00:00
|
|
|
---
|
2016-08-21 07:49:16 +00:00
|
|
|
--- (**Note**: An element with no parent will not be handled by Pop.Box's event
|
|
|
|
--- handlers unless you handle it explicitly.)
|
2016-06-21 05:52:44 +00:00
|
|
|
--- @see pop
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @see Element
|
2016-06-21 05:52:44 +00:00
|
|
|
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.create = (element, parent=pop.screen, ...) ->
|
2016-06-21 05:52:44 +00:00
|
|
|
-- if valid parent element, use it
|
2016-04-02 03:45:43 +00:00
|
|
|
if inheritsFromElement parent
|
2016-04-03 06:33:18 +00:00
|
|
|
element = pop.elements[element](parent, ...)
|
2016-04-01 22:48:58 +00:00
|
|
|
insert parent.child, element
|
2016-06-21 05:52:44 +00:00
|
|
|
insert parent.data.child, element.data
|
2016-10-31 06:40:40 +00:00
|
|
|
--element.parent = parent
|
2016-08-21 07:49:16 +00:00
|
|
|
element.data.parent = parent.data
|
2016-06-21 05:52:44 +00:00
|
|
|
-- if explicitly no parent, just create the element
|
2016-04-02 03:45:43 +00:00
|
|
|
elseif parent == false
|
2016-04-03 06:33:18 +00:00
|
|
|
element = pop.elements[element](false, ...)
|
2016-08-21 07:49:16 +00:00
|
|
|
element.parent = false
|
|
|
|
element.data.parent = false
|
2016-06-21 05:52:44 +00:00
|
|
|
-- else use pop.screen (and "parent" is actually the first argument)
|
2016-04-01 01:59:16 +00:00
|
|
|
else
|
2016-04-03 06:33:18 +00:00
|
|
|
element = pop.elements[element](pop.screen, parent, ...)
|
2016-04-01 22:48:58 +00:00
|
|
|
insert pop.screen.child, element
|
2016-06-21 05:52:44 +00:00
|
|
|
insert pop.screen.data.child, element.data
|
2016-10-31 06:40:40 +00:00
|
|
|
--element.parent = pop.screen
|
2016-08-21 07:49:16 +00:00
|
|
|
element.data.parent = pop.screen.data
|
2016-03-29 00:59:12 +00:00
|
|
|
|
|
|
|
return element
|
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
|
|
|
|
|
2016-06-21 07:58:08 +00:00
|
|
|
--- Event handler for `love.update()`.
|
2016-06-21 05:52:44 +00:00
|
|
|
--- @function update
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @tparam number dt The amount of time passed since the last call to update,
|
|
|
|
--- in seconds.
|
|
|
|
--- @tparam Element element[opt] The element to update (will update all its
|
|
|
|
--- children as well). Defaults to `pop.screen`.
|
|
|
|
--- @see Element
|
2016-06-21 05:52:44 +00:00
|
|
|
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.update = (dt, element=pop.screen) ->
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @todo Define Elements and @ see that documentation from here. Generic documentation, not specifically element!
|
2016-06-21 05:52:44 +00:00
|
|
|
-- data.update boolean controls an element and its children being updated
|
|
|
|
if element.data.update
|
2016-02-25 01:02:56 +00:00
|
|
|
if element.update
|
|
|
|
element\update dt
|
|
|
|
for i = 1, #element.child
|
|
|
|
pop.update dt, element.child[i]
|
2016-02-23 03:55:05 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
|
|
|
|
|
2016-06-21 07:58:08 +00:00
|
|
|
--- Event handler for `love.draw()`.
|
2016-06-21 05:52:44 +00:00
|
|
|
--- @function draw
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @tparam Element element[opt] The element to draw (will draw all its children
|
|
|
|
--- as well). Defaults to `pop.screen`.
|
|
|
|
--- @see Element
|
2016-06-21 05:52:44 +00:00
|
|
|
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.draw = (element=pop.screen) ->
|
2016-06-21 05:52:44 +00:00
|
|
|
-- data.draw boolean controls an element and its children being drawn
|
|
|
|
if element.data.draw
|
2016-02-25 01:02:56 +00:00
|
|
|
if element.draw
|
2016-03-29 00:59:12 +00:00
|
|
|
element\draw!
|
2016-02-25 01:02:56 +00:00
|
|
|
for i = 1, #element.child
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.draw element.child[i]
|
2016-02-25 01:02:56 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
|
|
|
|
|
2016-08-21 07:49:16 +00:00
|
|
|
--- Event handler for `love.mousemoved()`. (LÖVE >= 0.10.0)
|
2016-06-21 05:52:44 +00:00
|
|
|
--- @function mousemoved
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @tparam integer x The x coordinate of the mouse.
|
|
|
|
--- @tparam integer y The y coordinate of the mouse.
|
|
|
|
--- @tparam number dx The distance on the x axis the mouse was moved.
|
|
|
|
--- @tparam number dy The distance on the y axis the mouse was moved.
|
|
|
|
--- @treturn boolean Was the event handled?
|
2016-06-21 05:52:44 +00:00
|
|
|
|
2016-04-17 08:27:06 +00:00
|
|
|
pop.mousemoved = (x, y, dx, dy) ->
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @todo Implement a way for an element to attach itself to `love.mousemoved()` events?
|
2016-04-02 03:45:43 +00:00
|
|
|
if pop.focused and pop.focused.mousemoved
|
2016-09-08 04:42:19 +00:00
|
|
|
return pop.focused\mousemoved x - pop.focused.data.x, y - pop.focused.data.y, dx, dy
|
2016-04-02 03:45:43 +00:00
|
|
|
|
|
|
|
return false
|
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
|
|
|
|
|
2016-06-21 07:58:08 +00:00
|
|
|
--- Event handler for `love.mousepressed()`.
|
2016-06-21 05:52:44 +00:00
|
|
|
--- @function mousepressed
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @tparam integer x The x coordinate of the mouse press.
|
|
|
|
--- @tparam integer y The y coordinate of the mouse press.
|
|
|
|
--- @tparam ?string|integer button The mouse button pressed. (Type varies by
|
|
|
|
--- LÖVE version.)
|
|
|
|
--- @tparam Element element[opt] The element to check for event handling (will
|
|
|
|
--- check its children as well). Defaults to `pop.screen`.
|
|
|
|
--- @treturn boolean Was the event handled?
|
|
|
|
--- @see Element
|
2016-06-21 05:52:44 +00:00
|
|
|
|
2016-04-02 03:45:43 +00:00
|
|
|
pop.mousepressed = (x, y, button, element) ->
|
2016-04-20 21:12:32 +00:00
|
|
|
-- start at the screen, print that we received an event
|
2016-04-02 03:45:43 +00:00
|
|
|
unless element
|
2016-10-31 05:19:24 +00:00
|
|
|
log "mousepressed", x, y, button
|
2016-04-02 03:45:43 +00:00
|
|
|
element = pop.screen
|
2016-03-30 21:38:46 +00:00
|
|
|
|
2016-04-20 21:12:32 +00:00
|
|
|
-- have we handled the event?
|
2016-03-30 17:50:33 +00:00
|
|
|
handled = false
|
2016-04-02 03:45:43 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
-- if it is inside the current element..
|
|
|
|
if (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h)
|
2016-04-20 21:12:32 +00:00
|
|
|
-- check its child elements in reverse order, returning if something handles it
|
2016-04-18 04:57:32 +00:00
|
|
|
for i = #element.child, 1, -1
|
2016-04-20 21:12:32 +00:00
|
|
|
if handled = pop.mousepressed x, y, button, element.child[i]
|
|
|
|
return handled
|
2016-04-18 04:57:32 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
-- if a child hasn't handled it yet (note: this check doesn't seem neccessary)
|
2016-04-18 04:57:32 +00:00
|
|
|
unless handled
|
2016-04-20 21:12:32 +00:00
|
|
|
-- if we can handle it and are visible, try to handle it, and set pop.focused
|
2016-06-21 05:52:44 +00:00
|
|
|
if element.mousepressed and element.data.draw
|
|
|
|
if handled = element\mousepressed x - element.data.x, y - element.data.y, button
|
2016-04-18 04:57:32 +00:00
|
|
|
pop.focused = element
|
2016-02-25 01:02:56 +00:00
|
|
|
|
2016-04-27 17:53:19 +00:00
|
|
|
-- return whether or not we have handled the event
|
2016-04-02 03:45:43 +00:00
|
|
|
return handled
|
2016-03-30 21:38:46 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
|
|
|
|
|
2016-06-21 07:58:08 +00:00
|
|
|
--- Event handler for `love.mousereleased()`.
|
2016-06-21 05:52:44 +00:00
|
|
|
--- @function mousereleased
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @tparam integer x The x coordinate of the mouse release.
|
|
|
|
--- @tparam integer y The y coordinate of the mouse release.
|
|
|
|
--- @tparam ?string|integer button The mouse button released. (Type varies by
|
|
|
|
--- LÖVE version.)
|
|
|
|
--- @tparam Element element[opt] The element to check for event handling (will
|
|
|
|
--- check its children as well). Defaults to `pop.screen`.
|
|
|
|
--- @treturn boolean Was a click handled?
|
|
|
|
--- @treturn boolean Was a mouse release handled?
|
|
|
|
--- @see Element
|
2016-06-21 05:52:44 +00:00
|
|
|
|
2016-04-18 03:37:55 +00:00
|
|
|
pop.mousereleased = (x, y, button, element) ->
|
2016-04-20 21:12:32 +00:00
|
|
|
-- we are trying to handle a clicked or mousereleased event
|
2016-04-02 03:45:43 +00:00
|
|
|
clickedHandled = false
|
|
|
|
mousereleasedHandled = false
|
2016-03-30 21:38:46 +00:00
|
|
|
|
2016-04-20 21:12:32 +00:00
|
|
|
-- if we have an element, and are within its bounds
|
2016-04-18 03:37:55 +00:00
|
|
|
if element
|
2016-06-21 05:52:44 +00:00
|
|
|
if (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h)
|
2016-04-20 21:12:32 +00:00
|
|
|
-- check its children in reverse for handling a clicked or mousereleased event
|
2016-04-18 04:57:32 +00:00
|
|
|
for i = #element.child, 1, -1
|
|
|
|
clickedHandled, mousereleasedHandled = pop.mousereleased x, y, button, element.child[i]
|
|
|
|
if clickedHandled or mousereleasedHandled
|
2016-04-20 21:12:32 +00:00
|
|
|
return clickedHandled, mousereleasedHandled
|
2016-04-18 03:37:55 +00:00
|
|
|
|
2016-06-21 05:52:44 +00:00
|
|
|
-- if that doesn't work, we try to handle it ourselves (note: again, this check seems unneccessary)
|
2016-04-18 04:57:32 +00:00
|
|
|
unless clickedHandled or mousereleasedHandled
|
2016-04-20 21:12:32 +00:00
|
|
|
-- clicked only happens on visible elements, mousereleased happens either way
|
2016-06-21 05:52:44 +00:00
|
|
|
if element.clicked and element.data.draw
|
|
|
|
clickedHandled = element\clicked x - element.data.x, y - element.data.y, button
|
2016-04-18 04:57:32 +00:00
|
|
|
if element.mousereleased
|
2016-06-21 05:52:44 +00:00
|
|
|
mousereleasedHandled = element\mousereleased x - element.data.x, y - element.data.y, button
|
2016-03-30 21:38:46 +00:00
|
|
|
|
2016-04-20 21:12:32 +00:00
|
|
|
-- if we clicked, we're focused!
|
2016-04-18 04:57:32 +00:00
|
|
|
if clickedHandled
|
|
|
|
pop.focused = element
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @todo Figure out how to bring a focused element to the front of view (aka the first element in its parent's children).
|
2016-06-21 05:52:44 +00:00
|
|
|
--- (If I do it right here, the for loop above may break! I need to test/figure this out.)
|
2016-04-27 17:53:19 +00:00
|
|
|
--NOTE this might cause an error in the above for loop!
|
|
|
|
-- basically, move focused element to front of its parent's child
|
|
|
|
--element.parent\focusChild element
|
|
|
|
--table.insert element.parent, element.parent\removeChild(element),
|
2016-04-18 03:37:55 +00:00
|
|
|
|
2016-04-20 21:12:32 +00:00
|
|
|
-- else, default to pop.screen to begin! (and print that we received an event)
|
|
|
|
else
|
2016-10-31 05:19:24 +00:00
|
|
|
log "mousereleased", x, y, button
|
2016-04-20 21:12:32 +00:00
|
|
|
pop.mousereleased x, y, button, pop.screen
|
2016-03-30 21:38:46 +00:00
|
|
|
|
2016-04-02 03:45:43 +00:00
|
|
|
return clickedHandled, mousereleasedHandled
|
2016-02-25 01:02:56 +00:00
|
|
|
|
2016-06-21 07:58:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
--- Event handler for `love.keypressed()`.
|
|
|
|
--- @function keypressed
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @tparam string key The key that was pressed.
|
|
|
|
--- @treturn boolean Was the event handled?
|
2016-06-21 07:58:08 +00:00
|
|
|
|
2016-02-25 01:02:56 +00:00
|
|
|
pop.keypressed = (key) ->
|
2016-10-31 05:19:24 +00:00
|
|
|
log "keypressed", key
|
2016-04-17 07:29:27 +00:00
|
|
|
|
2016-04-20 21:12:32 +00:00
|
|
|
-- keypressed events must be on visible elements
|
2016-04-17 07:29:27 +00:00
|
|
|
element = pop.focused
|
2016-06-21 07:58:08 +00:00
|
|
|
if element and element.keypressed and element.data.draw
|
2016-04-17 07:29:27 +00:00
|
|
|
return element.keypressed key
|
|
|
|
|
|
|
|
return false
|
2016-02-25 01:02:56 +00:00
|
|
|
|
2016-06-21 07:58:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
--- Event handler for `love.keyreleased()`.
|
|
|
|
--- @function keyreleased
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @tparam string key The key that was released.
|
|
|
|
--- @treturn boolean Was the event handled?
|
2016-06-21 07:58:08 +00:00
|
|
|
|
2016-02-25 01:02:56 +00:00
|
|
|
pop.keyreleased = (key) ->
|
2016-10-31 05:19:24 +00:00
|
|
|
log "keyreleased", key
|
2016-04-17 07:29:27 +00:00
|
|
|
|
2016-04-20 21:12:32 +00:00
|
|
|
-- keyreleased events are always called
|
2016-04-17 07:29:27 +00:00
|
|
|
element = pop.focused
|
|
|
|
if element and element.keyreleased
|
|
|
|
return element.keyreleased key
|
|
|
|
|
|
|
|
return false
|
2016-02-25 01:02:56 +00:00
|
|
|
|
2016-06-21 07:58:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
--- Event handler for `love.textinput()`.
|
|
|
|
--- @function textinput
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @tparam string text The text that was typed.
|
|
|
|
--- @treturn boolean Was the text input handled?
|
2016-06-21 07:58:08 +00:00
|
|
|
|
2016-02-25 01:02:56 +00:00
|
|
|
pop.textinput = (text) ->
|
2016-10-31 05:19:24 +00:00
|
|
|
log "textinput", text
|
2016-04-17 07:29:27 +00:00
|
|
|
|
2016-04-20 21:12:32 +00:00
|
|
|
-- textinput events must be on visible elements
|
2016-04-17 07:29:27 +00:00
|
|
|
element = pop.focused
|
2016-06-21 07:58:08 +00:00
|
|
|
if element and element.textinput and element.data.draw
|
2016-04-17 07:29:27 +00:00
|
|
|
return element.textinput text
|
|
|
|
|
|
|
|
return false
|
2016-03-29 00:59:12 +00:00
|
|
|
|
2016-06-21 07:58:08 +00:00
|
|
|
|
|
|
|
|
2016-10-31 06:40:40 +00:00
|
|
|
--- @todo doc me
|
|
|
|
|
|
|
|
pop.import = (data, parent=pop.screen) ->
|
|
|
|
local element
|
|
|
|
if type(data) == "string"
|
|
|
|
data = loads(data)
|
|
|
|
element = pop.create(data.type, parent, data)
|
|
|
|
else
|
|
|
|
element = pop.elements[data.type](parent, data)
|
|
|
|
insert parent.child, element
|
|
|
|
|
|
|
|
for i = 1, #data.child
|
|
|
|
pop.import data.child[i], element
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--- @todo doc me
|
|
|
|
|
|
|
|
pop.export = (element=pop.screen) ->
|
|
|
|
return dumps(element.data)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-21 19:31:42 +00:00
|
|
|
--- Draws simple rectangle outlines to debug placement of elements.
|
|
|
|
--- @function debugDraw
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @tparam Element element[opt] The element to draw (will draw its children as
|
|
|
|
--- well). Defaults to `pop.screen`.
|
|
|
|
--- @see Element
|
2016-06-21 19:31:42 +00:00
|
|
|
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.debugDraw = (element=pop.screen) ->
|
2016-08-21 05:29:16 +00:00
|
|
|
--@todo Make this better in the future when different element types have been created and whatnot.
|
2016-02-25 01:02:56 +00:00
|
|
|
if element.debugDraw
|
|
|
|
element\debugDraw!
|
|
|
|
else
|
|
|
|
graphics.setLineWidth 1
|
2016-10-31 05:19:24 +00:00
|
|
|
graphics.setColor 0, 0, 0, 100
|
2016-08-23 01:19:58 +00:00
|
|
|
graphics.rectangle "fill", element.data.x, element.data.y, element.data.w, element.data.h
|
2016-02-25 01:02:56 +00:00
|
|
|
graphics.setColor 150, 150, 150, 150
|
2016-08-23 01:19:58 +00:00
|
|
|
graphics.rectangle "line", element.data.x, element.data.y, element.data.w, element.data.h
|
2016-02-25 01:02:56 +00:00
|
|
|
graphics.setColor 200, 200, 200, 255
|
2016-08-23 01:19:58 +00:00
|
|
|
graphics.print ".", element.data.x, element.data.y
|
2016-02-25 01:02:56 +00:00
|
|
|
|
|
|
|
for i = 1, #element.child
|
|
|
|
pop.debugDraw element.child[i]
|
|
|
|
|
2016-06-21 19:31:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
--- Prints a basic structure of GUI elements with minimal info.
|
|
|
|
--- @function printElementTree
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @tparam Element element[opt] The element to start at. Defaults to
|
|
|
|
--- `pop.screen`.
|
|
|
|
--- @see Element
|
2016-06-21 19:31:42 +00:00
|
|
|
|
2016-04-20 21:12:32 +00:00
|
|
|
pop.printElementTree = (element=pop.screen, depth=0) ->
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @todo Correct this once elements are reimplemented if it needs correction.
|
2016-04-18 04:57:32 +00:00
|
|
|
cls = element.__class.__name
|
|
|
|
|
|
|
|
if cls == "text"
|
|
|
|
cls = cls .. " (\"#{element\getText!\gsub "\n", "\\n"}\")"
|
|
|
|
elseif cls == "box"
|
|
|
|
bg = element\getBackground!
|
|
|
|
|
|
|
|
if type(bg) == "table"
|
|
|
|
bg = "#{bg[1]}, #{bg[2]}, #{bg[3]}, #{bg[4]}"
|
|
|
|
|
|
|
|
cls = cls .. " (#{bg})"
|
|
|
|
|
2016-10-31 05:19:24 +00:00
|
|
|
log string.rep("-", depth) .. " #{cls}"
|
2016-04-18 04:57:32 +00:00
|
|
|
|
|
|
|
for i = 1, #element.child
|
2016-06-21 19:31:42 +00:00
|
|
|
pop.printElementTree element.child[i], depth + 1
|
2016-04-18 04:57:32 +00:00
|
|
|
|
2016-02-25 01:02:56 +00:00
|
|
|
|
2016-06-21 19:31:42 +00:00
|
|
|
|
|
|
|
-- finally, load is called and pop returned
|
|
|
|
pop.load!
|
2016-02-25 01:02:56 +00:00
|
|
|
return pop
|