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.

58 lines
1.8 KiB

  1. local BaseCommon = BaseCommon or {}
  2. -- event
  3. function BaseCommon.DispatchCustomEvent(__name, __data, __cmd)
  4. if (not __name) then
  5. assert(false, "BaseCommon.DispatchCustomEvent __name is null")
  6. end
  7. local event = cc.EventCustom:new(__name)
  8. event._usedata = __data
  9. event._cmd = __cmd
  10. cc.Director:getInstance():getEventDispatcher():dispatchEvent(event)
  11. end
  12. function BaseCommon.AddCustomEvent(__node, __eventName, __handler)
  13. if (not __eventName) then
  14. assert(false, "BaseCommon.AddCustomEvent __eventName is null")
  15. end
  16. local listener = cc.EventListenerCustom:create(__eventName, __handler)
  17. cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener, __node)
  18. return listener
  19. end
  20. function BaseCommon.AddFixedCustomEvent(__eventName, __handler)
  21. local listener = cc.EventListenerCustom:create(__eventName, __handler)
  22. cc.Director:getInstance():getEventDispatcher():addEventListenerWithFixedPriority(listener, 1)
  23. return listener
  24. end
  25. function BaseCommon.RemoveFixedCustomEvent(__listener)
  26. cc.Director:getInstance():getEventDispatcher():removeEventListener(__listener)
  27. end
  28. -- const table
  29. function BaseCommon.NewConst( const_table )
  30. local function Const( const_table )
  31. local mt =
  32. {
  33. __index = function (t,k)
  34. if type(const_table[k])=="table" then
  35. const_table[k] = BaseCommon.NewConst(const_table[k])
  36. end
  37. return const_table[k]
  38. end,
  39. __newindex = function (t,k,v)
  40. error("can't update " .. tostring(t) .."[" .. tostring(k) .."] = " .. tostring(v))
  41. -- dump(t)
  42. end
  43. }
  44. return mt
  45. end
  46. local t = {}
  47. setmetatable(t, Const(const_table))
  48. return t
  49. end
  50. return BaseCommon