MultiHandlers.lua 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ----------------------------------------------------------------------------------------------------
  2. --
  3. -- Copyright (c) Contributors to the Open 3D Engine Project.
  4. -- For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. --
  6. -- SPDX-License-Identifier: Apache-2.0 OR MIT
  7. --
  8. --
  9. --
  10. ----------------------------------------------------------------------------------------------------
  11. --This utility function allows you to easily create handlers when needing to handle multiple IDs.
  12. --Common examples are inputs (where you have multiple key presses/actions you need to handle) and
  13. --gameplay notifications (where there's several gameplay events your component needs to handle).
  14. --See GameplayUtils.lua and InputUtils.lua as examples.
  15. -- require with:
  16. -- local multiHandlers = require('scripts.utils.components.multihandlers')
  17. -- create multihandlers like:
  18. -- return {
  19. -- ConnectMultiHandlers = multiHandlers(GameplayNotificationBus, GameplayEventNotificationId),
  20. -- }
  21. local function ConnectMultiHandlers(bus, idType)
  22. -- Generate listener constructor
  23. return function(eventTable)
  24. -- Our object emulating an ebus handler
  25. local proxyHandler = {
  26. handlers = { }
  27. }
  28. -- Connect to the bus for each id/handler passed
  29. for busId, handlers in pairs(eventTable) do
  30. Debug.Error(typeid(busId) == typeid(idType), "Wrong event id type, expected " .. tostring(typeid(busId)) .. " but got " .. tostring(typeid(idType)))
  31. -- Setup proxy table
  32. proxyHandler.handlers[busId] = {
  33. originalHandlerTable = handlers; -- Reference to original handler table passed
  34. handlerTable = { } -- Table containing actual handler functions
  35. }
  36. local currentProxy = proxyHandler.handlers[busId]
  37. -- For each event handler, wrap and copy to proxy handlerTable
  38. for eventName, eventHandler in pairs(handlers) do
  39. if (type(eventHandler) == 'function') then
  40. -- drop the original handler table for the callback
  41. currentProxy.handlerTable[eventName] = function(origHandlerTable, ...)
  42. eventHandler(...)
  43. end
  44. else
  45. Debug.Warning(false, string.format("Invalid value passed to multihandler for key %s: %s. Function expected.", eventName, type(eventHandler)))
  46. end
  47. end
  48. -- add a handler keyed on busid. Store the handler and callback by strong reference
  49. currentProxy.busHandler = bus.Connect(currentProxy.handlerTable, busId)
  50. end
  51. -- Setup disconnect
  52. function proxyHandler:Disconnect()
  53. for busId, handlers in pairs(self.handlers) do
  54. handlers.busHandler:Disconnect()
  55. handlers.handlerTable = nil
  56. handlers.origHandlerTable = nil
  57. end
  58. self.handlers = nil
  59. end
  60. return proxyHandler
  61. end
  62. end
  63. return ConnectMultiHandlers