LuaExtend.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. -- add empty ccslog function to avoid function not defined error.
  2. -- if you log something, add what you want in it.
  3. if cc.exports then
  4. cc.exports.ccslog = function(...) end
  5. else
  6. ccslog = function(...) end
  7. end
  8. -- used as metaTable
  9. local luaExtend = {}
  10. local function getChildInSubNodes(nodeTable, key)
  11. if #nodeTable == 0 then
  12. return nil
  13. end
  14. local child = nil
  15. local subNodeTable = {}
  16. for _, v in ipairs(nodeTable) do
  17. child = v:getChildByName(key)
  18. if (child) then
  19. return child
  20. end
  21. end
  22. for _, v in ipairs(nodeTable) do
  23. local subNodes = v:getChildren()
  24. if #subNodes ~= 0 then
  25. for _, v1 in ipairs(subNodes) do
  26. table.insert(subNodeTable, v1)
  27. end
  28. end
  29. end
  30. return getChildInSubNodes(subNodeTable, key)
  31. end
  32. luaExtend.__index = function(table, key)
  33. local root = table.root
  34. local child = root[key]
  35. if child then
  36. return child
  37. end
  38. child = root:getChildByName(key)
  39. if child then
  40. root[key] = child
  41. return child
  42. end
  43. child = getChildInSubNodes(root:getChildren(), key)
  44. if child then root[key] = child end
  45. return child
  46. end
  47. -- used as metaTable
  48. luaExtend.__newindex = function(table, key, value)
  49. if key == "root" and nil == rawget(table, key) then
  50. local function checkChildren(children)
  51. if #children == 0 then
  52. return nil
  53. end
  54. for _, child in pairs(children) do
  55. if child.getDescription and (child:getDescription() == "ListView" or child:getDescription() == "ScrollView") then
  56. if child:getDirection() == 1 and (nil == child.UserData or (tonumber(child.UserData[1]) and tonumber(child.UserData[1]) > 0)) then
  57. local speed = nil
  58. if child.UserData and tonumber(child.UserData[1]) and tonumber(child.UserData[1]) > 0 then
  59. speed = tonumber(child.UserData[1])
  60. end
  61. child:addMouseScrollPercent(speed)
  62. end
  63. end
  64. checkChildren(child:getChildren())
  65. end
  66. end
  67. checkChildren(value:getChildren())
  68. end
  69. rawset(table, key, value)
  70. end
  71. return luaExtend