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 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT |
---|
17 | |
---|
18 | #include "event_handler.h" |
---|
19 | #include "event_listener.h" |
---|
20 | #include "event.h" |
---|
21 | |
---|
22 | #include "ini_parser.h" |
---|
23 | #include "keynames.h" |
---|
24 | |
---|
25 | #include "SDL_keysym.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*[SDLK_LAST]; |
---|
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 | \brief loads new key bindings from a file |
---|
78 | \param filename: The path and name of the file to load the bindings from |
---|
79 | */ |
---|
80 | void EventHandler::loadKeyBindings (const char* fileName) |
---|
81 | { |
---|
82 | FILE* stream; |
---|
83 | |
---|
84 | PRINTF(4)("Loading key bindings from %s\n", fileName); |
---|
85 | |
---|
86 | // remove old bindings if present |
---|
87 | if( this->keyAliases != NULL) |
---|
88 | { |
---|
89 | free (this->keyAliases); |
---|
90 | this->keyAliases = NULL; |
---|
91 | } |
---|
92 | |
---|
93 | // create parser |
---|
94 | IniParser parser(fileName); |
---|
95 | if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1) |
---|
96 | { |
---|
97 | PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName); |
---|
98 | return; |
---|
99 | } |
---|
100 | // allocate empty lookup table |
---|
101 | this->keyAliases = (KeyBindings*) calloc (1, sizeof (KeyBindings)); |
---|
102 | |
---|
103 | char namebuf[256]; |
---|
104 | char valuebuf[256]; |
---|
105 | memset (namebuf, 0, 256); |
---|
106 | memset (valuebuf, 0, 256); |
---|
107 | int* index; |
---|
108 | |
---|
109 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
110 | { |
---|
111 | //index = nameToIndex (valuebuf); |
---|
112 | int c; |
---|
113 | if( (c = keynameToSDLK (valuebuf)) != -1) |
---|
114 | { |
---|
115 | index[1] = c; index[0] = 0; |
---|
116 | } |
---|
117 | if( (c = buttonnameToSDLB (valuebuf)) != -1) |
---|
118 | { |
---|
119 | index[1] = c; index[0] = 1; |
---|
120 | } |
---|
121 | |
---|
122 | switch( index[0]) |
---|
123 | { |
---|
124 | case 0: |
---|
125 | PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf); |
---|
126 | strcpy (this->keyAliases->keys[index[1]], namebuf); |
---|
127 | break; |
---|
128 | case 1: |
---|
129 | PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf); |
---|
130 | strcpy (this->keyAliases->buttons[index[1]], namebuf); |
---|
131 | break; |
---|
132 | default: |
---|
133 | break; |
---|
134 | } |
---|
135 | memset (namebuf, 0, 256); |
---|
136 | memset (valuebuf, 0, 256); |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | // PARSE MISC SECTION |
---|
141 | if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1) |
---|
142 | { |
---|
143 | PRINTF(1)("Could not find key bindings in %s\n", fileName); |
---|
144 | return; |
---|
145 | } |
---|
146 | |
---|
147 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
148 | { |
---|
149 | //index = nameToIndex (valuebuf); |
---|
150 | int c; |
---|
151 | if( (c = keynameToSDLK (valuebuf)) != -1) |
---|
152 | { |
---|
153 | index[1] = c; index[0] = 0; |
---|
154 | } |
---|
155 | if( (c = buttonnameToSDLB (valuebuf)) != -1) |
---|
156 | { |
---|
157 | index[1] = c; index[0] = 1; |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | switch( index[0]) |
---|
162 | { |
---|
163 | case 0: |
---|
164 | PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf); |
---|
165 | strcpy (keyAliases->keys[index[1]], namebuf); |
---|
166 | break; |
---|
167 | case 1: |
---|
168 | PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf); |
---|
169 | strcpy (keyAliases->buttons[index[1]], namebuf); |
---|
170 | break; |
---|
171 | default: |
---|
172 | break; |
---|
173 | } |
---|
174 | memset (namebuf, 0, 256); |
---|
175 | memset (valuebuf, 0, 256); |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | void EventHandler::setState(elState state) |
---|
181 | { |
---|
182 | this->state = state; |
---|
183 | } |
---|
184 | |
---|
185 | void EventHandler::subscribeListener(EventListener* el, elState state, int eventType) |
---|
186 | { |
---|
187 | if( likely(this->listeners[state][eventType] == NULL)) |
---|
188 | this->listeners[state][eventType] = el; |
---|
189 | else |
---|
190 | PRINTF(0)("Someone tried to subscribe to event %i @ state %i but this event has already been subscribed\n", eventType, state); |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | void EventHandler::unsubscribeListener(int eventType, elState state) |
---|
195 | { |
---|
196 | this->listeners[state][eventType] = NULL; |
---|
197 | } |
---|
198 | |
---|
199 | void EventHandler::flush() |
---|
200 | { |
---|
201 | for(int i = 0; i < ES_NUMBER; ++i) |
---|
202 | { |
---|
203 | for(int j = 0; j < SDLK_LAST; ++j) |
---|
204 | { |
---|
205 | this->listeners[i][j] = NULL; |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | void EventHandler::process() |
---|
213 | { |
---|
214 | SDL_Event event; |
---|
215 | //Command cmd; |
---|
216 | Event ev; |
---|
217 | EventListener* listener; |
---|
218 | while( SDL_PollEvent (&event)) |
---|
219 | { |
---|
220 | //memset (ev.cmd, 0, CMD_LENGHT); |
---|
221 | switch( event.type) |
---|
222 | { |
---|
223 | case SDL_KEYDOWN: |
---|
224 | ev.bPressed = true; |
---|
225 | listener = this->listeners[this->state][event.key.keysym.sym]; |
---|
226 | //if( listener != NULL) |
---|
227 | //listener->process(); |
---|
228 | break; |
---|
229 | case SDL_KEYUP: |
---|
230 | /* |
---|
231 | strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]); |
---|
232 | cmd.bUp = true; |
---|
233 | if( strlen (cmd.cmd) > 0) relay(&cmd); |
---|
234 | */ |
---|
235 | break; |
---|
236 | case SDL_MOUSEMOTION: |
---|
237 | /* |
---|
238 | strcpy( cmd.cmd, "cursor"); |
---|
239 | cmd.x = event.motion.x; |
---|
240 | cmd.y = event.motion.y; |
---|
241 | cmd.xrel = event.motion.xrel; |
---|
242 | cmd.yrel = event.motion.yrel; |
---|
243 | if( strlen (cmd.cmd) > 0) relay(&cmd); |
---|
244 | */ |
---|
245 | break; |
---|
246 | case SDL_MOUSEBUTTONUP: |
---|
247 | /* |
---|
248 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
249 | cmd.bUp = true; |
---|
250 | if( strlen (cmd.cmd) > 0) relay(&cmd); |
---|
251 | */ |
---|
252 | break; |
---|
253 | case SDL_MOUSEBUTTONDOWN: |
---|
254 | /* |
---|
255 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
256 | cmd.bUp = false; |
---|
257 | if( strlen (cmd.cmd) > 0) relay(&cmd); |
---|
258 | */ |
---|
259 | break; |
---|
260 | case SDL_JOYAXISMOTION: |
---|
261 | case SDL_JOYBALLMOTION: |
---|
262 | case SDL_JOYHATMOTION: |
---|
263 | case SDL_JOYBUTTONDOWN: |
---|
264 | case SDL_JOYBUTTONUP: |
---|
265 | break; |
---|
266 | default: |
---|
267 | break; |
---|
268 | } |
---|
269 | } |
---|
270 | } |
---|