You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.1 KiB

  1. function tonum(v, base)
  2. return tonumber(v, base) or 0
  3. end
  4. function toint(v)
  5. return math.round(tonum(v))
  6. end
  7. function tobool(v)
  8. return (v ~= nil and v ~= false)
  9. end
  10. function totable(v)
  11. if type(v) ~= "table" then v = {} end
  12. return v
  13. end
  14. function isset(arr, key)
  15. local t = type(arr)
  16. return (t == "table" or t == "userdata") and arr[key] ~= nil
  17. end
  18. -- 由于C++对象也是table,但是我们不希望这个table被深度拷贝,所以需要额外处理
  19. local istable;
  20. if tolua.isnative then
  21. istable = function(obj)return type(obj) == "table" and not tolua.isnative(obj);end
  22. else
  23. istable = function(obj)return type(obj) == "table";end
  24. end
  25. function clone(object)
  26. local lookup_table = {}
  27. local function _copy(object)
  28. if not istable(object) then
  29. return object
  30. elseif lookup_table[object] then
  31. return lookup_table[object]
  32. end
  33. local new_table = {}
  34. lookup_table[object] = new_table
  35. for key, value in pairs(object) do
  36. new_table[_copy(key)] = _copy(value)
  37. end
  38. return setmetatable(new_table, getmetatable(object))
  39. end
  40. return _copy(object)
  41. end
  42. --Create an class.
  43. function class(classname, super)
  44. local superType = type(super)
  45. local cls
  46. if superType ~= "function" and superType ~= "table" then
  47. superType = nil
  48. super = nil
  49. end
  50. if superType == "function" or (super and super.__ctype == 1) then
  51. -- inherited from native C++ Object
  52. cls = {}
  53. if superType == "table" then
  54. -- copy fields from super
  55. for k,v in pairs(super) do cls[k] = v end
  56. cls.__create = super.__create
  57. cls.super = super
  58. else
  59. cls.__create = super
  60. end
  61. cls.ctor = function() end
  62. cls.__cname = classname
  63. cls.__ctype = 1
  64. function cls:new(...)
  65. local instance = cls.__create(...)
  66. -- copy fields from class to native object
  67. for k,v in pairs(cls) do instance[k] = v end
  68. instance.class = cls
  69. instance:ctor(...)
  70. return instance
  71. end
  72. else
  73. -- inherited from Lua Object
  74. if super then
  75. cls = clone(super)
  76. cls.super = super
  77. else
  78. cls = {ctor = function() end}
  79. end
  80. cls.__cname = classname
  81. cls.__ctype = 2 -- lua
  82. cls.__index = cls
  83. function cls:new(...)
  84. local instance = setmetatable({}, cls)
  85. instance.class = cls
  86. instance:ctor(...)
  87. return instance
  88. end
  89. end
  90. return cls
  91. end
  92. function schedule(node, callback, delay)
  93. local delay = cc.DelayTime:create(delay)
  94. local sequence = cc.Sequence:create(delay, cc.CallFunc:create(callback))
  95. local action = cc.RepeatForever:create(sequence)
  96. node:runAction(action)
  97. return action
  98. end
  99. function performWithDelay(node, callback, delay)
  100. local delay = cc.DelayTime:create(delay)
  101. local sequence = cc.Sequence:create(delay, cc.CallFunc:create(callback))
  102. node:runAction(sequence)
  103. return sequence
  104. end
  105. function iskindof(obj, className)
  106. local t = type(obj)
  107. if t == "table" then
  108. local mt = getmetatable(obj)
  109. while mt and mt.__index do
  110. if mt.__index.__cname == className then
  111. return true
  112. end
  113. mt = mt.super
  114. end
  115. return false
  116. elseif t == "userdata" then
  117. else
  118. return false
  119. end
  120. end
  121. function import(moduleName, currentModuleName)
  122. local currentModuleNameParts
  123. local moduleFullName = moduleName
  124. local offset = 1
  125. while true do
  126. if string.byte(moduleName, offset) ~= 46 then -- .
  127. moduleFullName = string.sub(moduleName, offset)
  128. if currentModuleNameParts and #currentModuleNameParts > 0 then
  129. moduleFullName = table.concat(currentModuleNameParts, ".") .. "." .. moduleFullName
  130. end
  131. break
  132. end
  133. offset = offset + 1
  134. if not currentModuleNameParts then
  135. if not currentModuleName then
  136. local n,v = debug.getlocal(3, 1)
  137. currentModuleName = v
  138. end
  139. currentModuleNameParts = string.split(currentModuleName, ".")
  140. end
  141. table.remove(currentModuleNameParts, #currentModuleNameParts)
  142. end
  143. return require(moduleFullName)
  144. end
  145. function handler(target, method)
  146. assert(method);
  147. return function(...)
  148. return method(target, ...)
  149. end
  150. end
  151. -- 弹出一个MessageBox
  152. -- message 显示的消息,可选参数
  153. -- title 标题,可选参数
  154. -- callback 回调,可选参数
  155. function alert(title , message , callback)
  156. message = message or "提示信息";
  157. title = title or "信息";
  158. app.device.showAlert(message , title , callback);
  159. end
  160. -- 把十六进制转换成字符串
  161. function hex2bin( hexstr )
  162. local s = string.gsub(hexstr, "(.)(.)%s", function ( h, l )
  163. return string.char(h2b[h]*16+h2b[l])
  164. end)
  165. return s
  166. end
  167. -- 把字符串转换成十六进制
  168. function bin2hex(s)
  169. s=string.gsub(s,"(.)",function (x) return string.format("%02X ",string.byte(x)) end)
  170. return s
  171. end