|
- function tonum(v, base)
- return tonumber(v, base) or 0
- end
-
- function toint(v)
- return math.round(tonum(v))
- end
-
- function tobool(v)
- return (v ~= nil and v ~= false)
- end
-
- function totable(v)
- if type(v) ~= "table" then v = {} end
- return v
- end
-
- function isset(arr, key)
- local t = type(arr)
- return (t == "table" or t == "userdata") and arr[key] ~= nil
- end
-
- -- 由于C++对象也是table,但是我们不希望这个table被深度拷贝,所以需要额外处理
- local istable;
- if tolua.isnative then
- istable = function(obj)return type(obj) == "table" and not tolua.isnative(obj);end
- else
- istable = function(obj)return type(obj) == "table";end
- end
-
- function clone(object)
- local lookup_table = {}
- local function _copy(object)
- if not istable(object) then
- return object
- elseif lookup_table[object] then
- return lookup_table[object]
- end
- local new_table = {}
- lookup_table[object] = new_table
- for key, value in pairs(object) do
- new_table[_copy(key)] = _copy(value)
- end
- return setmetatable(new_table, getmetatable(object))
- end
- return _copy(object)
- end
-
- --Create an class.
- function class(classname, super)
- local superType = type(super)
- local cls
-
- if superType ~= "function" and superType ~= "table" then
- superType = nil
- super = nil
- end
-
- if superType == "function" or (super and super.__ctype == 1) then
- -- inherited from native C++ Object
- cls = {}
-
- if superType == "table" then
- -- copy fields from super
- for k,v in pairs(super) do cls[k] = v end
- cls.__create = super.__create
- cls.super = super
- else
- cls.__create = super
- end
-
- cls.ctor = function() end
- cls.__cname = classname
- cls.__ctype = 1
-
- function cls:new(...)
- local instance = cls.__create(...)
- -- copy fields from class to native object
- for k,v in pairs(cls) do instance[k] = v end
- instance.class = cls
- instance:ctor(...)
- return instance
- end
-
- else
- -- inherited from Lua Object
- if super then
- cls = clone(super)
- cls.super = super
- else
- cls = {ctor = function() end}
- end
-
- cls.__cname = classname
- cls.__ctype = 2 -- lua
- cls.__index = cls
-
- function cls:new(...)
- local instance = setmetatable({}, cls)
- instance.class = cls
- instance:ctor(...)
- return instance
- end
- end
-
- return cls
- end
-
- function schedule(node, callback, delay)
- local delay = cc.DelayTime:create(delay)
- local sequence = cc.Sequence:create(delay, cc.CallFunc:create(callback))
- local action = cc.RepeatForever:create(sequence)
- node:runAction(action)
- return action
- end
-
- function performWithDelay(node, callback, delay)
- local delay = cc.DelayTime:create(delay)
- local sequence = cc.Sequence:create(delay, cc.CallFunc:create(callback))
- node:runAction(sequence)
- return sequence
- end
-
-
-
- function iskindof(obj, className)
- local t = type(obj)
-
- if t == "table" then
- local mt = getmetatable(obj)
- while mt and mt.__index do
- if mt.__index.__cname == className then
- return true
- end
- mt = mt.super
- end
- return false
-
- elseif t == "userdata" then
-
- else
- return false
- end
- end
-
- function import(moduleName, currentModuleName)
- local currentModuleNameParts
- local moduleFullName = moduleName
- local offset = 1
-
- while true do
- if string.byte(moduleName, offset) ~= 46 then -- .
- moduleFullName = string.sub(moduleName, offset)
- if currentModuleNameParts and #currentModuleNameParts > 0 then
- moduleFullName = table.concat(currentModuleNameParts, ".") .. "." .. moduleFullName
- end
- break
- end
- offset = offset + 1
-
- if not currentModuleNameParts then
- if not currentModuleName then
- local n,v = debug.getlocal(3, 1)
- currentModuleName = v
- end
-
- currentModuleNameParts = string.split(currentModuleName, ".")
- end
- table.remove(currentModuleNameParts, #currentModuleNameParts)
- end
-
- return require(moduleFullName)
- end
-
- function handler(target, method)
- assert(method);
- return function(...)
- return method(target, ...)
- end
- end
-
- -- 弹出一个MessageBox
- -- message 显示的消息,可选参数
- -- title 标题,可选参数
- -- callback 回调,可选参数
- function alert(title , message , callback)
- message = message or "提示信息";
- title = title or "信息";
- app.device.showAlert(message , title , callback);
- end
-
-
- -- 把十六进制转换成字符串
- function hex2bin( hexstr )
- local s = string.gsub(hexstr, "(.)(.)%s", function ( h, l )
- return string.char(h2b[h]*16+h2b[l])
- end)
- return s
- end
-
- -- 把字符串转换成十六进制
- function bin2hex(s)
- s=string.gsub(s,"(.)",function (x) return string.format("%02X ",string.byte(x)) end)
- return s
- end
|