Advertisement
theTANCO

Events.lua

Jun 22nd, 2022 (edited)
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.34 KB | None | 0 0
  1. -- This is an event handler for ComputerCraft. It takes event arguments returned
  2. --   by os.pullEvent or os.pullEventRaw and processes them into an object with
  3. --   methods for better accessibility and usability.
  4.  
  5. -- Key presses and mouse clicks are also tracked so that it's easier to tell if
  6. --   a button is currently pressed or not. This is useful for multi-key events
  7. --   such as pressing Ctrl+C, for example.
  8.  
  9. -- You can get this program from https://pastebin.com/KA2dK07y
  10. -- This program requires two additional files to work. To get this program
  11. --   run the following commands:
  12. --     pastebin get Rac6Jxjg "/API/LibAppend.lua"
  13. --     pastebin get t2TvSiSU "/API/Class.lua"
  14. --     pastebin get KA2dK07y "/API/events.lua"
  15.  
  16. -- Make sure that all of these files are saved to "/API" in the root directory
  17. --   in order for all programs that use this one to work properly.
  18.  
  19. -- Add this to your program using 'dofile("/API/events.lua")'' and call 'event =
  20. --   Events()' to create an events object.
  21.  
  22. -- Call events.getEvent() for os.pullEvent(), and events.getRawEvent() for
  23. --   os.pullEventRaw() and events.queueEvent() for os.queueEvent().
  24.  
  25. -- Event parameters are automatically stored in events[n]. All you have to do is
  26. --   call events[1] for the event name, events[2] for its first parameter, etc.
  27.  
  28. -- Call events.isPressed() to check if a key or mouse click is currently being
  29. --   pressed. Scroll down to learn the function arguments.
  30.  
  31. -- Call events.mousePos() to get the position of a mouse button if it is
  32. --   currently being pressed. Pass the mouse button id as this function's
  33. --   argument.
  34.  
  35. dofile("/API/LibAppend.lua")
  36. dofile("/API/Class.lua")
  37.  
  38. Events = Class(function()
  39. local readOnly = {}
  40. local event = {}
  41. local eventSize = 0
  42. local pressed = {key = {}, click = {}}
  43. local mousePos = {
  44.     [1] = vector.new(0, 0),
  45.     [2] = vector.new(0, 0),
  46.     [3] = vector.new(0, 0),
  47.     ["left"] = vector.new(0, 0),
  48.     ["right"] = vector.new(0, 0),
  49.     ["middle"] = vector.new(0, 0)
  50. }
  51.  
  52. local clearEvents = function()
  53.     for a = 1, eventSize do
  54.         readOnly[a] = nil
  55.     end
  56.     eventSize = 0
  57. end
  58. local refreshEvents = function()
  59.     clearEvents()
  60.     for a, b in ipairs(event) do
  61.         readOnly[a] = b
  62.     end
  63.     eventSize = #event
  64. end
  65. local pressButton = function(t, k, x, y)
  66.     local click = {[1] = "left", [2] = "right", [3] = "middle"}
  67.     if t == "mouse_click" then
  68.         pressed.click[k] = true
  69.         pressed.click[click[k]] = true
  70.         mousePos[k] = vector.new(x, y)
  71.         mousePos[click[k]] = vector.new(x, y)
  72.     elseif t == "mouse_up" then
  73.         pressed.click[k] = false
  74.         pressed.click[click[k]] = false
  75.         mousePos[k] = vector.new(0, 0)
  76.         mousePos[click[k]] = vector.new(0, 0)
  77.     elseif t == "key" then
  78.         pressed.key[k] = true
  79.         pressed.key[keys.getName(k)] = true
  80.     elseif t == "key_up" then
  81.         pressed.key[k] = false
  82.         pressed.key[keys.getName(k)] = false
  83.     end
  84.     pressed.key.shift = pressed.key.leftShift or pressed.key.rightShift
  85.     pressed.key.ctrl = pressed.key.leftCtrl or pressed.key.rightCtrl
  86.     pressed.key.alt = pressed.key.leftAlt or pressed.key.rightAlt
  87.     pressed.key.enterKey = pressed.key.enter or pressed.key.numPadEnter
  88. end
  89.  
  90. -- Call events[n] to get the nth parameter in the event.
  91. readOnly.getEvent = function(...)
  92.     event = {os.pullEvent(...)}
  93.     refreshEvents()
  94.     pressButton(event[1], event[2], event[3], event[4])
  95.     return event
  96. end
  97. readOnly.getRawEvent = function(...)
  98.     event = {os.pullEventRaw(...)}
  99.     refreshEvents()
  100.     pressButton(event[1], event[2], event[3], event[4])
  101.     return event
  102. end
  103. readOnly.queueEvent = function(...)
  104.     os.queueEvent(...)
  105. end
  106.  
  107. -- t = "key" or "click"
  108. -- k = the mouse/key id or name.
  109. -- Example 1: events.isPressed("key", keys.leftShift) -> true or false if left shift is pressed.
  110. -- Example 2: events.isPressed("key", "rightShift") -> true or false if right shift is pressed.
  111. -- Example 3: events.isPressed("click", 1) -> true or false if left mouse click is pressed.
  112. -- Example 4: events.isPressed("click", "right") -> true or false if right mouse click is pressed.
  113. -- Look up the keys API for specific key names and id numbers.
  114. readOnly.isPressed = function(t, k)
  115.     if not OR(t, "click", "key") then
  116.         error('bad argument #1 (expected "click" or "key")', 2)
  117.     end
  118.     if pressed[t][k] == nil then return false
  119.     else return pressed[t][k] end
  120. end
  121.  
  122. -- If specified mouse button is pressed, returns the x,y coordinate, else returns nil.
  123. readOnly.mousePos = function(k)
  124.     if pressed.click[k] then return mousePos[k].x, mousePos[k].y
  125.     else return nil end
  126. end
  127.  
  128. return {
  129.     protected = true,
  130.     readOnly = readOnly,
  131.     meta = {
  132.         __len = function() return #event end
  133.     }
  134. }
  135. end)
  136.  
  137. --[[ Changelog
  138. 2024/01/03:
  139. • Added general identifiers for keys that have duplicates on the keyboard:
  140.   'shift', 'ctrl', 'alt', and 'enterkey'
  141.  
  142. 2023/12/03:
  143. • Removed 'sleep' and 'sleepRaw' due to redundancy. They worked perfectly for
  144.   one specific situation, but they didn't work as intended for anything else.
  145.  
  146. 2023/12/02:
  147. • 'sleep' and 'sleepRaw' now return the timer value.
  148.  
  149. 2023/12/01:
  150. • Added comment to 'isPressed' for checking mouse clicks with strings.
  151. • Added additional return values for 'isPressed' and 'mousePos' if the values
  152.   being checked are nil.
  153.  
  154. 2023/07/08:
  155. • Removed event.eventIs() due to lack of use as well as it not always working as
  156.   intended. Normal comparison operators are more practical, as well as the OR()
  157.   and isBetween() functions from LibAppend.
  158. • Changed comments at the top from saying to use 'require' to saying to use
  159.   'dofile', which I forgot to do two updates ago.
  160.  
  161. 2023/05/08:
  162. • Added 'sleep' and 'sleepRaw'. Read the comments above the methods for info.
  163.  
  164. 2023/05/06:
  165. • Changed 'require' to 'dofile' which does the same thing but makes this API
  166.   backwards compatible.
  167. • 'mousePos' now uses the vector API from 'LibAppend'.
  168.  
  169. 2023/03/09:
  170. • Changed events class to a class header to reflect the changes made to
  171.   "Class.lua".
  172.  
  173. 2023/02/27:
  174. • Changed 'protected' to 'readOnly' to reflect the changes made to "Class.lua".
  175.  
  176. 2023/02/20:
  177. • protected.isPressed() - swapped the 'or' comparison with the 'OR()' function
  178.   from LibAppend.
  179.  
  180. 2023/02/08:
  181. • Version 2.0. The program has been completely revamped.
  182. • Instead of setting a table with keys based on specific events, which was not
  183.   actually that helpful, this helps streamline event processing.
  184. • events.listen() has been replaced with events.getEvent().
  185. • events.rawListen() has been replaced with events.getRawEvent().
  186. • events.queue() has been replaced with events.queueEvent().
  187. • Call events[n] to get the nth parameter of the event.
  188. • events.eventIs() has been added in an attempt to help greatly reduce the amount
  189.   of code needed for conditional statements revolving specifically around event
  190.   parameters.
  191. • Mouse button and key presses are now being tracked. Use events.isPressed() to
  192.   check if a button is currently being pressed.
  193. • Use events.mousePos() to get the current position of the specified mouse button,
  194.   or nil if the specified mouse button is not pressed.
  195.  
  196. 2022/06/29:
  197. • Moved all functions into a table.
  198.  
  199. 2022/06/26:
  200. • Added eventQueue() to handle queuing events with os.queueEvent().
  201. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement