EventDispatcher.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. require "Polycode/EventHandler"
  2. class "EventDispatcher" (EventHandler)
  3. function EventDispatcher:EventDispatcher(...)
  4. if type(arg[1]) == "table" and count(arg) == 1 then
  5. if ""..arg[1]:class() == "EventHandler" then
  6. self.__ptr = arg[1].__ptr
  7. return
  8. end
  9. end
  10. for k,v in pairs(arg) do
  11. if type(v) == "table" then
  12. if v.__ptr ~= nil then
  13. arg[k] = v.__ptr
  14. end
  15. end
  16. end
  17. if self.__ptr == nil and arg[1] ~= "__skip_ptr__" then
  18. self.__ptr = Polycore.EventDispatcher(unpack(arg))
  19. Polycore.__ptr_lookup[self.__ptr] = self
  20. end
  21. end
  22. function EventDispatcher:removeAllHandlers()
  23. local retVal = Polycore.EventDispatcher_removeAllHandlers(self.__ptr)
  24. end
  25. function EventDispatcher:removeAllHandlersForListener(listener)
  26. local retVal = Polycore.EventDispatcher_removeAllHandlersForListener(self.__ptr, listener.__ptr)
  27. end
  28. function EventDispatcher:addEventListener(handler, eventCode)
  29. local retVal = Polycore.EventDispatcher_addEventListener(self.__ptr, handler.__ptr, eventCode)
  30. end
  31. function EventDispatcher:removeEventListener(handler, eventCode)
  32. local retVal = Polycore.EventDispatcher_removeEventListener(self.__ptr, handler.__ptr, eventCode)
  33. end
  34. function EventDispatcher:__dispatchEvent(event, eventCode)
  35. local retVal = Polycore.EventDispatcher___dispatchEvent(self.__ptr, event.__ptr, eventCode)
  36. end
  37. function EventDispatcher:dispatchEvent(event, eventCode)
  38. local retVal = Polycore.EventDispatcher_dispatchEvent(self.__ptr, event.__ptr, eventCode)
  39. end
  40. function EventDispatcher:dispatchEventNoDelete(event, eventCode)
  41. local retVal = Polycore.EventDispatcher_dispatchEventNoDelete(self.__ptr, event.__ptr, eventCode)
  42. end
  43. function EventDispatcher:__delete()
  44. Polycore.__ptr_lookup[self.__ptr] = nil
  45. Polycore.delete_EventDispatcher(self.__ptr)
  46. end