Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/event/event_handler.cc @ 4365

Last change on this file since 4365 was 4365, checked in by patrick, 19 years ago

orxonox/trunk: event listener/ handler now work together, events are being transmitted to the receivers

File size: 6.6 KB
Line 
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 "ini_parser.h"
25#include "keynames.h"
26
27
28using namespace std;
29
30
31/**
32   \brief standard constructor
33*/
34EventHandler::EventHandler () 
35{
36  this->setClassID(CL_EVENT_HANDLER, "EventHandler"); 
37
38  this->listeners = new EventListener**[ES_NUMBER];
39  for(int i = 0; i < ES_NUMBER; ++i)
40    this->listeners[i] = new EventListener*[EV_NUMBER];
41
42  /* now initialize them all to zero */
43  for(int i = 0; i < ES_NUMBER; ++i)
44    {
45      for(int j = 0; j < SDLK_LAST; ++j)
46        {
47          this->listeners[i][j] = NULL;
48        }
49    }
50}
51
52/**
53   \brief the singleton reference to this class
54*/
55EventHandler* EventHandler::singletonRef = NULL;
56
57/**
58   \returns a Pointer to this Class
59*/
60EventHandler* EventHandler::getInstance(void)
61{
62  if (!EventHandler::singletonRef)
63    EventHandler::singletonRef = new EventHandler();
64  return EventHandler::singletonRef;
65}
66
67/**
68   \brief standard deconstructor
69
70*/
71EventHandler::~EventHandler () 
72{
73  EventHandler::singletonRef = NULL;
74}
75
76
77/**
78   \brief loads new key bindings from a file
79   \param filename: The path and name of the file to load the bindings from
80*/
81void EventHandler::loadKeyBindings (const char* fileName)
82{
83  FILE* stream;
84 
85  PRINTF(4)("Loading key bindings from %s\n", fileName);
86 
87  // remove old bindings if present
88  if( this->keyAliases != NULL)
89    {
90      free (this->keyAliases);
91      this->keyAliases = NULL;
92    }
93 
94  // create parser
95  IniParser parser(fileName);
96  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
97    {
98      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName);
99      return;
100    }
101  // allocate empty lookup table
102  this->keyAliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
103 
104  char namebuf[256];
105  char valuebuf[256];
106  memset (namebuf, 0, 256);
107  memset (valuebuf, 0, 256);
108  int* index;
109 
110  while( parser.nextVar (namebuf, valuebuf) != -1)
111    {
112      //index = nameToIndex (valuebuf);
113      int c;
114      if( (c = keynameToSDLK (valuebuf)) != -1)
115        {
116          index[1] = c; index[0] = 0;
117        }
118      if( (c = buttonnameToSDLB (valuebuf)) != -1)
119        {
120          index[1] = c; index[0] = 1;
121        }
122
123      switch( index[0])
124        {
125        case 0:
126          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
127          strcpy (this->keyAliases->keys[index[1]], namebuf);
128          break;
129        case 1:
130          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
131          strcpy (this->keyAliases->buttons[index[1]], namebuf);
132          break;
133        default:
134          break;
135        }
136      memset (namebuf, 0, 256);
137      memset (valuebuf, 0, 256);
138    }
139
140
141  // PARSE MISC SECTION
142  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
143    {
144      PRINTF(1)("Could not find key bindings in %s\n", fileName);
145      return;
146    }
147
148  while( parser.nextVar (namebuf, valuebuf) != -1)
149    {
150      //index = nameToIndex (valuebuf);     
151      int c;
152      if( (c = keynameToSDLK (valuebuf)) != -1)
153        {
154          index[1] = c; index[0] = 0;
155        }
156      if( (c = buttonnameToSDLB (valuebuf)) != -1)
157        {
158          index[1] = c; index[0] = 1;
159        }
160
161
162      switch( index[0])
163        {
164        case 0:
165          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
166          strcpy (keyAliases->keys[index[1]], namebuf);
167          break;
168        case 1:
169          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
170          strcpy (keyAliases->buttons[index[1]], namebuf);
171          break;
172        default:
173          break;
174        }
175      memset (namebuf, 0, 256);
176      memset (valuebuf, 0, 256);
177    }
178}
179
180
181void EventHandler::setState(elState state)
182{
183  this->state = state;
184}
185
186void EventHandler::subscribeListener(EventListener* el, elState state, int eventType)
187{
188  if( likely(this->listeners[state][eventType] == NULL))
189    this->listeners[state][eventType] = el;
190  else
191    PRINTF(0)("Someone tried to subscribe to event %i @ state %i but this event has already been subscribed\n", eventType, state);
192}
193
194
195void EventHandler::unsubscribeListener(int eventType, elState state)
196{
197  this->listeners[state][eventType] = NULL;
198}
199
200void EventHandler::flush(elState state)
201{
202  if( state == ES_ALL)
203    {
204      for(int i = 0; i < ES_NUMBER; ++i)
205        {
206          for(int j = 0; j < SDLK_LAST; ++j)
207            {
208              this->listeners[i][j] = NULL;
209            }
210        }
211    }
212  else
213    {
214      for(int j = 0; j < SDLK_LAST; ++j)
215        {
216          this->listeners[state][j] = NULL;
217        }
218    }
219}
220
221
222
223void EventHandler::process()
224{
225  SDL_Event event;
226  Event ev;
227  EventListener* listener;
228  while( SDL_PollEvent (&event))
229    {
230      //memset (ev.cmd, 0, CMD_LENGHT);
231      switch( event.type)
232        {
233        case SDL_KEYDOWN:
234          ev.bPressed = true;
235          ev.type = event.key.keysym.sym;
236          break;
237        case SDL_KEYUP:
238          ev.bPressed = false;
239          ev.type = event.key.keysym.sym;
240          break;
241        case SDL_MOUSEMOTION:
242          ev.bPressed = false;
243          ev.type = EV_MOUSE_MOTION;
244          ev.x = event.motion.x;
245          ev.y = event.motion.y;
246          ev.xRel = event.motion.xrel;
247          ev.yRel = event.motion.yrel;
248          break;
249        case SDL_MOUSEBUTTONUP:
250          ev.bPressed = false;
251          ev.type = EV_MOUSE_BUTTON;
252          break;
253        case SDL_MOUSEBUTTONDOWN:
254          ev.bPressed = true;
255          ev.type = EV_MOUSE_BUTTON;
256          break;
257        case SDL_JOYAXISMOTION:
258          ev.bPressed = false;
259          ev.type = EV_JOY_AXIS_MOTION;
260          break;
261        case SDL_JOYBALLMOTION:
262          ev.bPressed = false;
263          ev.type = EV_JOY_BALL_MOTION;
264          break;
265        case SDL_JOYHATMOTION:
266          ev.bPressed = false;
267          ev.type = EV_JOY_HAT_MOTION;
268          break;
269        case SDL_JOYBUTTONDOWN:
270          ev.bPressed = true;
271          ev.type = EV_JOY_BUTTON;
272          break;
273        case SDL_JOYBUTTONUP:
274          ev.bPressed = true;
275          ev.type = EV_JOY_BUTTON;
276          break;
277        default:
278          break;
279        }
280
281      /* small debug routine: shows alle events dispatched by the event handler */
282      PRINT(0)("\n==========================| EventHandler::Process () |===\n");
283      PRINT(0)("=  Got Event nr%i\n, for state %i", event.type, this->state); 
284     
285      listener = this->listeners[this->state][event.key.keysym.sym];
286      if( listener != NULL)
287        {
288          listener->process(ev);
289          PRINTF(0)("=  Event dispatcher msg: This event has been consumed\n");
290        }
291      else
292        {
293          PRINTF(0)("=  Event dispatcher msg: This event has NOT been consumed\n");
294        }
295
296      PRINT(0)("=======================================================\n");     
297    }
298}
Note: See TracBrowser for help on using the repository browser.