events.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. --==Contribution Guide==--
  2. --[[
  3. Events System, used for wiring love events to liko12s peripherals.
  4. Event Name:
  5. * It's highly recommended to name event as "parent:funcName", for ex: "love:update".
  6. Love2D Hooks:
  7. * All love2d callbacks are triggered with names "love:callbackName", ex: "love:mousepressed".
  8. * There is 2 special love2d events:
  9. - "love:graphics" called then the love.graphics module is active and it's turn to draw in love.run.
  10. - "love:reboot" Trigger this event with a table of args to pass after SOFT rebooting.
  11. - "love:quit" if any registered function returns true the quit will be cancelled.
  12. NormBIOS:
  13. * It does automatically switch the group name for each peripheral with name "peripheralName:mountName".
  14. * When unmounting a peripheral it does automatically unregisters the events of it.
  15. events.reg:
  16. - It's a table that contains table, the keys of this table is the name of register function, and the value is a table containing all the registered function with the same name.
  17. events.groups:
  18. - It's a table, where the index is the group name, and the value is the group table:
  19. - Each group table contains tables, where each index is the function name, and the value is a table which contains the ids of each registered function.
  20. - Inorder to find a function in the events.reg, you may do: events.reg[funcname][id]()
  21. How events works:
  22. * Registering a function: registering means adding a function to be called when a specific event is triggered.
  23. * Triggering an event: will call all the registered functions with a specific event name.
  24. * Groups are like sorting the registered events to their "owner name", so they can be registered togather instead of each one itself.
  25. * Calling event:group with not arguments will set to the default group which is called "Unsorted"
  26. * Groups name must be a string.
  27. ==Contributers to this file==
  28. (Add your name when contributing to this file)
  29. - Rami Sabbagh (RamiLego4Game)
  30. ]]
  31. local events = {reg={},groups={}}
  32. --Adds functions when registered to a specific group.
  33. --When called by nil it will register them unsorted.
  34. function events:group(name)
  35. local name = name or "Unsorted"
  36. if not self.groups[name] then
  37. self.groups[name] = {} --Create a new group
  38. end
  39. self.activeGroup = self.groups[name]
  40. return self
  41. end
  42. events:group() --Setup the Unlisted Group.
  43. --Register a function to an event.
  44. function events:register(name,func)
  45. if type(name) ~= "string" then return error("Name should be a string value. Passed "..type(name).." instead !") end
  46. if type(func) ~= "function" then return error("func should be a function value. Passed "..type(func).." instead !") end
  47. if not self.reg[name] then self.reg[name] = {} end
  48. if not self.activeGroup[name] then self.activeGroup[name] = {} end
  49. table.insert(self.reg[name],func)
  50. table.insert(self.activeGroup[name],#self.reg[name])
  51. return func
  52. end
  53. --Unregister a function from the events system.
  54. function events:unregister(func,name) --Name is optional
  55. if type(name) ~= "string" and type(name) ~= "nil" then return error("Name should be a string value. Passed "..type(name).." instead !") end
  56. if type(func) ~= "function" then return error("func should be a function value. Passed "..type(func).." instead !") end
  57. if name and self.reg[name] then
  58. for k,v in ipairs(self.reg[name]) do
  59. if v == func then
  60. table.remove(self.reg[name],k)
  61. end
  62. end
  63. --Clear the gaps
  64. local newList = {}
  65. local gapCount = 0
  66. for k,v in ipairs(self.reg[name]) do
  67. if type(v) == "nil" then
  68. gapCount = gapCount+1
  69. else
  70. table.insert(newList,k-gapCount,v)
  71. end
  72. end
  73. self.reg[name] = newList
  74. else --Search through all events
  75. for rk,rv in pairs(self.reg) do
  76. for k,v in ipairs(rv) do
  77. if v == func then
  78. table.remove(rv,k)
  79. end
  80. end
  81. --Clear the gaps
  82. local newList = {}
  83. local gapCount = 0
  84. for k,v in ipairs(rv) do
  85. if type(v) == "nil" then
  86. gapCount = gapCount+1
  87. else
  88. table.insert(newList,k-gapCount,v)
  89. end
  90. end
  91. self.reg[rk] = newList
  92. end
  93. end
  94. return self
  95. end
  96. function events:unregisterGroup(gname)
  97. if not self.groups[gname] then return self end
  98. for name,funcs in pairs(self.groups[gname]) do
  99. for k,id in ipairs(funcs) do
  100. if self.reg[name][id] then
  101. table.remove(self.reg[name],id)
  102. end
  103. end
  104. --Clear the gaps
  105. local newList = {}
  106. local gapCount = 0
  107. for k,v in ipairs(self.reg[name]) do
  108. if type(v) == "nil" then
  109. gapCount = gapCount+1
  110. else
  111. table.insert(newList,k-gapCount,v)
  112. end
  113. end
  114. self.reg[name] = newList
  115. end
  116. self.groups[gname] = nil
  117. return self
  118. end
  119. --Call an event functions
  120. --Returns a table with the responds of the functions, with functions as the keyvalue.
  121. function events:trigger(name,...)
  122. if type(name) ~= "string" then return error("Name should be a string value. Passed "..type(name).." instead !") end
  123. if not self.reg[name] then return {} end
  124. local r = {}
  125. for k,f in pairs(self.reg[name]) do
  126. r[f] = {f(...)}
  127. end
  128. return r
  129. end
  130. return events