1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: |
---|
14 | |
---|
15 | This code is inspired by Christian Meyers CommandNode code (in Orxonox v0.2.3) |
---|
16 | */ |
---|
17 | |
---|
18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT |
---|
19 | |
---|
20 | #include "event_handler.h" |
---|
21 | #include "event_listener.h" |
---|
22 | #include "event.h" |
---|
23 | |
---|
24 | #include "compiler.h" |
---|
25 | #include "debug.h" |
---|
26 | |
---|
27 | using namespace std; |
---|
28 | |
---|
29 | |
---|
30 | /** |
---|
31 | \brief standard constructor |
---|
32 | */ |
---|
33 | EventHandler::EventHandler () |
---|
34 | { |
---|
35 | this->setClassID(CL_EVENT_HANDLER, "EventHandler"); |
---|
36 | |
---|
37 | this->listeners = new EventListener**[ES_NUMBER]; |
---|
38 | for(int i = 0; i < ES_NUMBER; ++i) |
---|
39 | this->listeners[i] = new EventListener*[EV_NUMBER]; |
---|
40 | |
---|
41 | /* now initialize them all to zero */ |
---|
42 | for(int i = 0; i < ES_NUMBER; ++i) |
---|
43 | { |
---|
44 | for(int j = 0; j < SDLK_LAST; ++j) |
---|
45 | { |
---|
46 | this->listeners[i][j] = NULL; |
---|
47 | } |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | \brief the singleton reference to this class |
---|
53 | */ |
---|
54 | EventHandler* EventHandler::singletonRef = NULL; |
---|
55 | |
---|
56 | /** |
---|
57 | \returns a Pointer to this Class |
---|
58 | */ |
---|
59 | EventHandler* EventHandler::getInstance(void) |
---|
60 | { |
---|
61 | if (!EventHandler::singletonRef) |
---|
62 | EventHandler::singletonRef = new EventHandler(); |
---|
63 | return EventHandler::singletonRef; |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | \brief standard deconstructor |
---|
68 | |
---|
69 | */ |
---|
70 | EventHandler::~EventHandler () |
---|
71 | { |
---|
72 | EventHandler::singletonRef = NULL; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | void EventHandler::setState(elState state) |
---|
78 | { |
---|
79 | this->state = state; |
---|
80 | } |
---|
81 | |
---|
82 | void EventHandler::subscribeListener(EventListener* el, elState state, int eventType) |
---|
83 | { |
---|
84 | if( likely(this->listeners[state][eventType] == NULL)) |
---|
85 | this->listeners[state][eventType] = el; |
---|
86 | else |
---|
87 | PRINTF(0)("Someone tried to subscribe to event %i @ state %i but this event has already been subscribed\n", eventType, state); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | void EventHandler::unsubscribeListener(int eventType, elState state) |
---|
92 | { |
---|
93 | this->listeners[state][eventType] = NULL; |
---|
94 | } |
---|
95 | |
---|
96 | void EventHandler::flush(elState state) |
---|
97 | { |
---|
98 | if( state == ES_ALL) |
---|
99 | { |
---|
100 | for(int i = 0; i < ES_NUMBER; ++i) |
---|
101 | { |
---|
102 | for(int j = 0; j < SDLK_LAST; ++j) |
---|
103 | { |
---|
104 | this->listeners[i][j] = NULL; |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | else |
---|
109 | { |
---|
110 | for(int j = 0; j < SDLK_LAST; ++j) |
---|
111 | { |
---|
112 | this->listeners[state][j] = NULL; |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | void EventHandler::process() |
---|
120 | { |
---|
121 | SDL_Event event; |
---|
122 | Event ev; |
---|
123 | EventListener* listener; |
---|
124 | while( SDL_PollEvent (&event)) |
---|
125 | { |
---|
126 | switch( event.type) |
---|
127 | { |
---|
128 | case SDL_KEYDOWN: |
---|
129 | ev.bPressed = true; |
---|
130 | ev.type = event.key.keysym.sym; |
---|
131 | break; |
---|
132 | case SDL_KEYUP: |
---|
133 | ev.bPressed = false; |
---|
134 | ev.type = event.key.keysym.sym; |
---|
135 | break; |
---|
136 | case SDL_MOUSEMOTION: |
---|
137 | ev.bPressed = false; |
---|
138 | ev.type = EV_MOUSE_MOTION; |
---|
139 | ev.x = event.motion.x; |
---|
140 | ev.y = event.motion.y; |
---|
141 | ev.xRel = event.motion.xrel; |
---|
142 | ev.yRel = event.motion.yrel; |
---|
143 | break; |
---|
144 | case SDL_MOUSEBUTTONUP: |
---|
145 | ev.bPressed = false; |
---|
146 | ev.type = EV_MOUSE_BUTTON; |
---|
147 | break; |
---|
148 | case SDL_MOUSEBUTTONDOWN: |
---|
149 | ev.bPressed = true; |
---|
150 | ev.type = EV_MOUSE_BUTTON; |
---|
151 | break; |
---|
152 | case SDL_JOYAXISMOTION: |
---|
153 | ev.bPressed = false; |
---|
154 | ev.type = EV_JOY_AXIS_MOTION; |
---|
155 | break; |
---|
156 | case SDL_JOYBALLMOTION: |
---|
157 | ev.bPressed = false; |
---|
158 | ev.type = EV_JOY_BALL_MOTION; |
---|
159 | break; |
---|
160 | case SDL_JOYHATMOTION: |
---|
161 | ev.bPressed = false; |
---|
162 | ev.type = EV_JOY_HAT_MOTION; |
---|
163 | break; |
---|
164 | case SDL_JOYBUTTONDOWN: |
---|
165 | ev.bPressed = true; |
---|
166 | ev.type = EV_JOY_BUTTON; |
---|
167 | break; |
---|
168 | case SDL_JOYBUTTONUP: |
---|
169 | ev.bPressed = true; |
---|
170 | ev.type = EV_JOY_BUTTON; |
---|
171 | break; |
---|
172 | default: |
---|
173 | break; |
---|
174 | } |
---|
175 | |
---|
176 | /* small debug routine: shows alle events dispatched by the event handler */ |
---|
177 | PRINT(0)("\n==========================| EventHandler::process () |===\n"); |
---|
178 | PRINT(0)("= Got Event nr%i\n, for state %i", event.type, this->state); |
---|
179 | |
---|
180 | listener = this->listeners[this->state][event.key.keysym.sym]; |
---|
181 | if( listener != NULL) |
---|
182 | { |
---|
183 | listener->process(ev); |
---|
184 | PRINTF(0)("= Event dispatcher msg: This event has been consumed\n"); |
---|
185 | } |
---|
186 | else |
---|
187 | { |
---|
188 | PRINTF(0)("= Event dispatcher msg: This event has NOT been consumed\n"); |
---|
189 | } |
---|
190 | |
---|
191 | PRINT(0)("=======================================================\n"); |
---|
192 | } |
---|
193 | } |
---|