2012-06-26 00:13:30 +02:00
|
|
|
-- @docfuncs @{
|
|
|
|
|
2011-08-20 22:30:41 +02:00
|
|
|
function print(...)
|
|
|
|
local msg = ""
|
2013-01-23 18:35:43 +01:00
|
|
|
local args = {...}
|
|
|
|
local appendSpace = #args > 1
|
|
|
|
for i,v in ipairs(args) do
|
|
|
|
msg = msg .. tostring(v)
|
|
|
|
if appendSpace and i < #args then
|
|
|
|
msg = msg .. ' '
|
|
|
|
end
|
2011-08-20 22:30:41 +02:00
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
g_logger.log(LogInfo, msg)
|
2011-08-21 03:01:46 +02:00
|
|
|
end
|
|
|
|
|
2012-06-20 02:15:56 +02:00
|
|
|
function pinfo(msg)
|
2012-04-29 00:02:57 +02:00
|
|
|
g_logger.log(LogInfo, msg)
|
|
|
|
end
|
|
|
|
|
2012-06-20 02:15:56 +02:00
|
|
|
function perror(msg)
|
|
|
|
g_logger.log(LogError, msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
function pwarning(msg)
|
2012-04-26 21:54:16 +02:00
|
|
|
g_logger.log(LogWarning, msg)
|
|
|
|
end
|
|
|
|
|
2012-06-20 02:15:56 +02:00
|
|
|
function pdebug(msg)
|
|
|
|
g_logger.log(LogDebug, msg)
|
|
|
|
end
|
|
|
|
|
2011-12-07 01:31:55 +01:00
|
|
|
function fatal(msg)
|
2012-02-20 03:27:08 +01:00
|
|
|
g_logger.log(LogFatal, msg)
|
2011-08-29 21:35:58 +02:00
|
|
|
end
|
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
function exit()
|
|
|
|
g_app.exit()
|
|
|
|
end
|
2012-06-20 02:15:56 +02:00
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
function quit()
|
|
|
|
g_app.quit()
|
|
|
|
end
|
2012-01-07 06:35:50 +01:00
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function connect(object, arg1, arg2, arg3)
|
|
|
|
local signalsAndSlots
|
|
|
|
local pushFront
|
|
|
|
if type(arg1) == 'string' then
|
|
|
|
signalsAndSlots = { [arg1] = arg2 }
|
|
|
|
pushFront = arg3
|
|
|
|
else
|
|
|
|
signalsAndSlots = arg1
|
|
|
|
pushFront = arg2
|
|
|
|
end
|
|
|
|
|
2011-08-29 21:35:58 +02:00
|
|
|
for signal,slot in pairs(signalsAndSlots) do
|
2012-02-08 23:58:27 +01:00
|
|
|
if not object[signal] then
|
|
|
|
local mt = getmetatable(object)
|
2012-02-09 07:42:07 +01:00
|
|
|
if mt and type(object) == 'userdata' then
|
2012-02-08 23:58:27 +01:00
|
|
|
object[signal] = function(...)
|
|
|
|
return signalcall(mt[signal], ...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-29 21:35:58 +02:00
|
|
|
if not object[signal] then
|
2012-01-24 23:22:56 +01:00
|
|
|
object[signal] = slot
|
2011-08-29 21:35:58 +02:00
|
|
|
elseif type(object[signal]) == 'function' then
|
2012-01-24 23:22:56 +01:00
|
|
|
object[signal] = { object[signal] }
|
|
|
|
end
|
2013-02-18 17:16:22 +01:00
|
|
|
|
|
|
|
if type(slot) ~= 'function' then
|
|
|
|
perror(debug.traceback('unable to connect a non function value'))
|
|
|
|
end
|
|
|
|
|
2012-01-24 23:22:56 +01:00
|
|
|
if type(object[signal]) == 'table' then
|
|
|
|
if pushFront then
|
|
|
|
table.insert(object[signal], 1, slot)
|
|
|
|
else
|
|
|
|
table.insert(object[signal], #object[signal]+1, slot)
|
|
|
|
end
|
2011-08-29 21:35:58 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-11-01 19:32:48 +01:00
|
|
|
|
2012-07-26 08:10:28 +02:00
|
|
|
function disconnect(object, arg1, arg2)
|
|
|
|
local signalsAndSlots
|
|
|
|
if type(arg1) == 'string' then
|
2013-02-18 17:16:22 +01:00
|
|
|
if arg2 == nil then
|
|
|
|
object[arg1] = nil
|
|
|
|
return
|
|
|
|
end
|
2012-07-26 08:10:28 +02:00
|
|
|
signalsAndSlots = { [arg1] = arg2 }
|
2013-02-18 17:16:22 +01:00
|
|
|
elseif type(arg1) == 'table' then
|
2012-07-26 08:10:28 +02:00
|
|
|
signalsAndSlots = arg1
|
2013-02-18 17:16:22 +01:00
|
|
|
else
|
|
|
|
perror(debug.traceback('unable to disconnect'))
|
2012-07-26 08:10:28 +02:00
|
|
|
end
|
|
|
|
|
2012-02-06 20:19:47 +01:00
|
|
|
for signal,slot in pairs(signalsAndSlots) do
|
|
|
|
if not object[signal] then
|
|
|
|
elseif type(object[signal]) == 'function' then
|
|
|
|
if object[signal] == slot then
|
|
|
|
object[signal] = nil
|
|
|
|
end
|
|
|
|
elseif type(object[signal]) == 'table' then
|
|
|
|
for k,func in pairs(object[signal]) do
|
|
|
|
if func == slot then
|
|
|
|
table.remove(object[signal], k)
|
|
|
|
|
|
|
|
if #object[signal] == 1 then
|
|
|
|
object[signal] = object[signal][1]
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-06 18:10:14 +02:00
|
|
|
function newclass(name)
|
|
|
|
if not name then
|
|
|
|
perror(debug.traceback('new class has no name.'))
|
|
|
|
end
|
|
|
|
|
2012-02-08 00:06:52 +01:00
|
|
|
local class = {}
|
|
|
|
function class.internalCreate()
|
|
|
|
local instance = {}
|
|
|
|
for k,v in pairs(class) do
|
|
|
|
instance[k] = v
|
|
|
|
end
|
|
|
|
return instance
|
|
|
|
end
|
|
|
|
class.create = class.internalCreate
|
2014-06-06 18:10:14 +02:00
|
|
|
class.__class = name
|
|
|
|
class.getClassName = function() return name end
|
2012-02-08 00:06:52 +01:00
|
|
|
return class
|
|
|
|
end
|
|
|
|
|
2014-06-06 18:10:14 +02:00
|
|
|
function extends(base, name)
|
|
|
|
if not name then
|
|
|
|
perror(debug.traceback('extended class has no name.'))
|
|
|
|
end
|
|
|
|
|
2012-01-05 02:28:29 +01:00
|
|
|
local derived = {}
|
|
|
|
function derived.internalCreate()
|
|
|
|
local instance = base.create()
|
|
|
|
for k,v in pairs(derived) do
|
|
|
|
instance[k] = v
|
|
|
|
end
|
|
|
|
return instance
|
|
|
|
end
|
|
|
|
derived.create = derived.internalCreate
|
2014-06-06 18:10:14 +02:00
|
|
|
derived.__class = name
|
|
|
|
derived.getClassName = function() return name end
|
2012-01-05 02:28:29 +01:00
|
|
|
return derived
|
|
|
|
end
|
|
|
|
|
2012-07-24 02:22:38 +02:00
|
|
|
function runinsandbox(func, ...)
|
|
|
|
if type(func) == 'string' then
|
|
|
|
func, err = loadfile(resolvepath(func, 2))
|
|
|
|
if not func then
|
|
|
|
error(err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local env = { }
|
|
|
|
local oldenv = getfenv(0)
|
|
|
|
setmetatable(env, { __index = oldenv } )
|
|
|
|
setfenv(0, env)
|
|
|
|
func(...)
|
|
|
|
setfenv(0, oldenv)
|
|
|
|
return env
|
|
|
|
end
|
|
|
|
|
|
|
|
function loadasmodule(name, file)
|
|
|
|
file = file or resolvepath(name, 2)
|
|
|
|
if package.loaded[name] then
|
|
|
|
return package.loaded[name]
|
|
|
|
end
|
|
|
|
local env = runinsandbox(file)
|
|
|
|
package.loaded[name] = env
|
|
|
|
return env
|
|
|
|
end
|
|
|
|
|
|
|
|
local function module_loader(modname)
|
|
|
|
local module = g_modules.getModule(modname)
|
|
|
|
if not module then
|
|
|
|
return '\n\tno module \'' .. modname .. '\''
|
|
|
|
end
|
|
|
|
return function()
|
|
|
|
if not module:load() then
|
|
|
|
error('unable to load required module ' .. modname)
|
|
|
|
end
|
|
|
|
return module:getSandbox()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.insert(package.loaders, 1, module_loader)
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function import(table)
|
|
|
|
assert(type(table) == 'table')
|
|
|
|
local env = getfenv(2)
|
|
|
|
for k,v in pairs(table) do
|
|
|
|
env[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-19 11:12:17 +02:00
|
|
|
function export(what, key)
|
|
|
|
if key ~= nil then
|
|
|
|
_G[key] = what
|
|
|
|
else
|
|
|
|
for k,v in pairs(what) do
|
|
|
|
_G[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function unexport(key)
|
|
|
|
if type(key) == 'table' then
|
|
|
|
for _k,v in pairs(key) do
|
|
|
|
_G[v] = nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
_G[key] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-05 02:28:29 +01:00
|
|
|
function getfsrcpath(depth)
|
2011-12-30 07:05:32 +01:00
|
|
|
depth = depth or 2
|
|
|
|
local info = debug.getinfo(1+depth, "Sn")
|
|
|
|
local path
|
|
|
|
if info.short_src then
|
|
|
|
path = info.short_src:match("(.*)/.*")
|
|
|
|
end
|
|
|
|
if not path then
|
|
|
|
path = '/'
|
|
|
|
elseif path:sub(0, 1) ~= '/' then
|
|
|
|
path = '/' .. path
|
|
|
|
end
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
|
2012-01-05 02:28:29 +01:00
|
|
|
function resolvepath(filePath, depth)
|
2012-07-18 01:49:21 +02:00
|
|
|
if not filePath then return nil end
|
2011-11-17 21:40:31 +01:00
|
|
|
depth = depth or 1
|
2012-01-07 22:10:06 +01:00
|
|
|
if filePath then
|
|
|
|
if filePath:sub(0, 1) ~= '/' then
|
|
|
|
local basepath = getfsrcpath(depth+1)
|
|
|
|
if basepath:sub(#basepath) ~= '/' then basepath = basepath .. '/' end
|
|
|
|
return basepath .. filePath
|
|
|
|
else
|
|
|
|
return filePath
|
|
|
|
end
|
2011-11-17 21:40:31 +01:00
|
|
|
else
|
2012-01-07 22:10:06 +01:00
|
|
|
local basepath = getfsrcpath(depth+1)
|
|
|
|
if basepath:sub(#basepath) ~= '/' then basepath = basepath .. '/' end
|
|
|
|
return basepath
|
2011-11-17 21:40:31 +01:00
|
|
|
end
|
|
|
|
end
|
2012-01-06 04:29:26 +01:00
|
|
|
|
2013-01-16 17:20:17 +01:00
|
|
|
function toboolean(v)
|
|
|
|
if type(v) == 'string' then
|
|
|
|
v = v:trim():lower()
|
|
|
|
if v == '1' or v == 'true' then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
elseif type(v) == 'number' then
|
|
|
|
if v == 1 then
|
|
|
|
return true
|
|
|
|
end
|
2013-01-16 18:41:12 +01:00
|
|
|
elseif type(v) == 'boolean' then
|
|
|
|
return v
|
2012-01-06 04:29:26 +01:00
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
2012-01-07 01:46:41 +01:00
|
|
|
|
2012-08-22 10:51:31 +02:00
|
|
|
function fromboolean(boolean)
|
|
|
|
if boolean then
|
|
|
|
return 'true'
|
|
|
|
else
|
|
|
|
return 'false'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-30 07:59:10 +02:00
|
|
|
function booleantonumber(boolean)
|
|
|
|
if boolean then
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function numbertoboolean(number)
|
|
|
|
if number ~= 0 then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-03 17:03:49 +02:00
|
|
|
function protectedcall(func, ...)
|
|
|
|
local status, ret = pcall(func, ...)
|
|
|
|
if status then
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
perror(ret)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2012-01-11 00:13:38 +01:00
|
|
|
function signalcall(param, ...)
|
|
|
|
if type(param) == 'function' then
|
2012-07-26 08:10:28 +02:00
|
|
|
local status, ret = pcall(param, ...)
|
|
|
|
if status then
|
|
|
|
return ret
|
|
|
|
else
|
|
|
|
perror(ret)
|
|
|
|
end
|
2012-01-11 00:13:38 +01:00
|
|
|
elseif type(param) == 'table' then
|
|
|
|
for k,v in pairs(param) do
|
2012-07-26 08:10:28 +02:00
|
|
|
local status, ret = pcall(v, ...)
|
|
|
|
if status then
|
|
|
|
if ret then return true end
|
|
|
|
else
|
|
|
|
perror(ret)
|
2012-01-11 00:13:38 +01:00
|
|
|
end
|
|
|
|
end
|
2015-01-25 21:20:48 +01:00
|
|
|
elseif param ~= nil then
|
2012-01-11 00:13:38 +01:00
|
|
|
error('attempt to call a non function value')
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
2012-06-20 02:15:56 +02:00
|
|
|
|
2012-07-28 07:29:26 +02:00
|
|
|
function tr(s, ...)
|
|
|
|
return string.format(s, ...)
|
2012-06-20 02:15:56 +02:00
|
|
|
end
|
2012-06-26 00:13:30 +02:00
|
|
|
|
2012-08-13 01:27:41 +02:00
|
|
|
function getOppositeAnchor(anchor)
|
|
|
|
if anchor == AnchorLeft then
|
|
|
|
return AnchorRight
|
|
|
|
elseif anchor == AnchorRight then
|
|
|
|
return AnchorLeft
|
|
|
|
elseif anchor == AnchorTop then
|
|
|
|
return AnchorBottom
|
|
|
|
elseif anchor == AnchorBottom then
|
|
|
|
return AnchorTop
|
|
|
|
elseif anchor == AnchorVerticalCenter then
|
|
|
|
return AnchorHorizontalCenter
|
|
|
|
elseif anchor == AnchorHorizontalCenter then
|
|
|
|
return AnchorVerticalCenter
|
|
|
|
end
|
|
|
|
return anchor
|
|
|
|
end
|
|
|
|
|
2014-04-01 06:08:07 +02:00
|
|
|
function makesingleton(obj)
|
|
|
|
local singleton = {}
|
|
|
|
if obj.getClassName then
|
|
|
|
for key,value in pairs(_G[obj:getClassName()]) do
|
|
|
|
if type(value) == 'function' then
|
2014-04-01 06:22:00 +02:00
|
|
|
singleton[key] = function(...) return value(obj, ...) end
|
2014-04-01 06:08:07 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return singleton
|
|
|
|
end
|
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
-- @}
|