First commit of LuaGame with an OpenGL backend.
[luagame.git] / base / EventManager.lua
blob189b8eeb304aca4cdfe191132ea4c3bfb321bbba
1 --this handles the events
2 EventManager = {}
5 --create the resource managers
6 EventManager.keyboard = {}
7 EventManager.keyboard.pressed = {}
8 EventManager.keyboard.released = {}
11 EventManager.mouse = {}
12 EventManager.mouse.motion = nil
13 EventManager.mouse.pressed = {}
14 EventManager.mouse.released = {}
17 --joystick handling is inherently difficult because there can be multiple joysticks
18 --thus, instead of having it be that the axis/button/etc... is the index for each,
19 -- the index is the joystick that the event originates from.
20 EventManager.joystick = {}
21 EventManager.joystick.axis_motion = {}
22 EventManager.joystick.ball_motion = {}
23 EventManager.joystick.hat_motion = {}
24 EventManager.joystick.pressed = {}
25 EventManager.joystick.released = {}
27 --misc events
28 EventManager.quit = nil
31 --state variables
32 EventManager.type = 0
33 EventManager.state = 0
34 EventManager.arg1 = 0
35 EventManager.arg2 = 0
36 EventManager.arg3 = 0
37 EventManager.arg4 = 0
40 --constructor
41 function EventManager:new(o)
42 o = o or {}
43 setmetatable(o, self)
44 self.__index = self
45 return o
46 end
49 --gets and evaluates events
50 function EventManager:gather_events()
52 self.type, self.state, self.arg1, self.arg2, self.arg3, self.arg4 = get_event()
54 while (self.type ~= 0) do
56 --keyboard handling
57 if self.type == "keyboard" then
58 if self.keyboard[self.state][self.arg1] then self.keyboard[self.state][self.arg1]() end
60 --mouse handling
61 elseif self.type == "mouse" then
62 if self.state == "motion" then
63 if self.mouse.motion then self.mouse.motion(self.arg1, self.arg2, self.arg3, self.arg4) end
64 else
65 if self.mouse[self.state][self.arg1] then self.mouse[self.state][self.arg1](self.arg2, self.arg3) end
66 end
68 --joystick handling
69 elseif self.type == "joystick" then
70 if self.state == "axis_motion" then
71 if self.joystick.axis_motion[self.arg1] then self.joystick.axis_motion[self.arg1](self.arg2, self.arg3) end
72 elseif self.state == "ball_motion" then
73 if self.joystick.ball_motion[self.arg1] then self.joystick.ball_motion[self.arg1](self.arg2, self.arg3, self.arg4) end
74 elseif self.state == "hat_motion" then
75 if self.joystick.hat_motion[self.arg1] then self.joystick.hat_motion[self.arg1](self.arg2, self.arg3) end
76 else
77 if self.joystick[self.state][self.arg1] then self.joystick[self.state][self.arg1](self.arg2) end
78 end
80 --misc events
81 elseif self.type == "quit" then
82 if self.quit then self.quit() end
83 end
85 self.type, self.state, self.arg1, self.arg2, self.arg3, self.arg4 = get_event()
86 end
88 end