4
-- This is a repository table for the current app, view, keys, and mouse.
5
-- You can use this for other objects that are useful to track. This should
6
-- be considered read-only; references here are for convenience and changing
7
-- things here will have no effect.
11
-- If set to true, then Zoetrope will do some extra checking for obvious
12
-- errors at the expense of some performance. Some of these checks will
13
-- throw errors, while others will simply print a warning to the console.
14
-- If you are going to use this, make sure to do so before your project's
15
-- require 'zoetrope' statement.
18
-- If set to true, Zoetrope's debug console will be enabled. If you are
19
-- going to use this, make sure to do so before your project's require
20
-- 'zoetrope' statement.
22
-- Constant: NEARLY_ZERO
23
-- Any number less than this is considered 0 by Zoetrope.
27
-- Directional constant corresponding to up.
31
-- Directional constant corresponding to down.
35
-- Directional constant corresponding to left.
39
-- Directional constant corresponding to right.
44
-- trim() implementation for strings via http://lua-users.org/wiki/stringtrim
47
-- source - source string
50
-- string trimmed of leading and trailing whitespace
52
function trim (source)
53
return source:gsub("^%s*(.-)%s*$", "%1")
57
-- split() implementation for strings via http://lua-users.org/wiki/splitjoin
60
-- source - source string
61
-- pattern - Lua pattern to split on, see http://www.lua.org/pil/20.1.html
64
-- table of split strings
66
function split (source, pattern)
67
assert(type(source) == 'string', 'source must be a string')
68
assert(type(pattern) == 'string', 'pattern must be a string')
72
local splitStart, splitEnd = string.find(source, pattern, searchStart)
75
table.insert(result, string.sub(source, searchStart, splitStart - 1))
76
searchStart = splitEnd + 1
77
splitStart, splitEnd = string.find(source, pattern, searchStart)
80
table.insert(result, string.sub(source, searchStart))
85
-- Coerces, if possible, a string to a boolean or number value.
86
-- If the string cannot be coerced to either of these types, this
87
-- returns the same string passed.
90
-- source - string to coerce
93
-- number, boolean, or string
95
function tovalue (source)
96
if source == 'true' then return true end
97
if source == 'false' then return false end
99
return tonumber(source) or source
103
-- Loads a sound but does not play it. It's important to use the hint property
104
-- appropriately; if set incorrectly, it can cause sound playback to stutter or lag.
107
-- path - string pathname to sound
108
-- hint - either 'short' or 'long', depending on length of sound; default 'short'
111
-- LOVE sound source, see https://love2d.org/wiki/Source
113
function sound (path, hint)
114
return love.audio.newSource(Cached:sound(path, hint or 'short'))
117
-- Function: playSound
118
-- Plays a sound once. This is the easiest way to play a sound. It's important
119
-- to use the hint property appropriately; if set incorrectly, it can cause sound
120
-- playback to stutter or lag.
123
-- path - string pathname to sound
124
-- volume - volume to play at, from 0 to 1; default 1
125
-- hint - either 'short' or 'long', depending on length of sound; default 'short'
128
-- LOVE sound source, see https://love2d.org/wiki/Source
130
function playSound (path, volume, hint)
132
local source = sound(path, hint)
133
source:setVolume(volume)
138
-- Function: searchTable
139
-- Returns the index of a value in a table. If the value
140
-- does not exist in the table, this returns nil.
143
-- table - table to search
144
-- search - value to search for
147
-- integer index or nil
149
function searchTable (table, search)
150
for i, value in ipairs(table) do
151
if value == search then return i end
157
-- Function: copyTable
158
-- Returns a superficial copy of a table. If it contains a
159
-- reference to another table in one of its properties, that
160
-- reference will be copied shallowly.
163
-- source - source table
168
function copyTable (source)
169
assert(type(source) == 'table', "asked to copy a non-table")
172
setmetatable(result, getmetatable(source))
174
for key, value in pairs(source) do
181
-- Function: shuffleTable
182
-- Shuffles the contents of a table in place.
183
-- via http://www.gammon.com.au/forum/?id=9908
186
-- table - table to shuffle
191
function shuffleTable (table)
195
-- n is now the last pertinent index
196
local k = math.random(n) -- 1 <= k <= n
198
table[n], table[k] = table[k], table[n]
206
-- Returns a string representation of a variable in a way
207
-- that can be reconstituted via loadstring(). Yes, this
208
-- is basically a serialization function, but that's so much
209
-- to type :) This ignores any userdata, functions, or circular
211
-- via http://www.lua.org/pil/12.1.2.html
214
-- source - variable to describe
215
-- ignore - a table of references to ignore (to avoid cycles),
216
-- defaults to empty table. This uses the references as keys,
217
-- *not* as values, to speed up lookup.
220
-- string description
222
function dump (source, ignore)
223
ignore = ignore or { source = true }
224
local sourceType = type(source)
226
if sourceType == 'table' then
229
for key, value in pairs(source) do
230
if not ignore[value] then
231
local dumpValue = dump(value, ignore)
233
if dumpValue ~= '' then
234
result = result .. '["' .. key .. '"] = ' .. dumpValue .. ', '
237
if type(value) == 'table' then
243
if result ~= '{ ' then
244
return string.sub(result, 1, -3) .. ' }'
248
elseif sourceType == 'string' then
249
return string.format('%q', source)
250
elseif sourceType ~= 'userdata' and sourceType ~= 'function' then
251
return tostring(source)
257
bind = function (...)
258
return Cached:bind(...)