Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/event/event_handler.cc @ 4651

Last change on this file since 4651 was 4519, checked in by bensch, 19 years ago

orxonox/trunk: changed all getInstances into inline functions to save some (minor) time

File size: 6.5 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
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT
17
18#include "event_handler.h"
19
20#include "event_listener.h"
21#include "event.h"
22#include "key_mapper.h"
23
24#include "compiler.h"
25#include "debug.h"
26
27using namespace std;
28
29
30/**
31   \brief standard constructor
32*/
33EventHandler::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  this->state = ES_GAME;
50}
51
52
53/**
54   \brief the singleton reference to this class
55*/
56EventHandler* EventHandler::singletonRef = NULL;
57
58
59/**
60   \brief standard deconstructor
61
62*/
63EventHandler::~EventHandler () 
64{
65  EventHandler::singletonRef = NULL;
66  delete this->keyMapper;
67}
68
69
70/**
71   \brief initializes the event handler
72
73   this has to be called before the use of the event handler
74*/
75void EventHandler::init()
76{
77  this->keyMapper = new KeyMapper();
78  this->keyMapper->loadKeyBindings();
79}
80
81
82/**
83   \brief set the state of the event handler
84   \param state: to which the event handler shall change
85*/
86void EventHandler::setState(elState state)
87{
88  this->state = state;
89}
90
91
92/**
93   \brief subscribe to an event
94   \param el: the event listener that wants to subscribe itself, the listener that will be called when the evetn occures
95   \param state: for which the listener wants to receive events
96   \param eventType: the event type that wants to be listened for.
97
98   This is one of the most important function of the EventHandler. If you would like to subscribe for more
99   than one state, you have to subscribe for each state again. If you want to subscribe for all states, use
100   state = ES_ALL, which will subscribe your listener for all states together.
101*/
102void EventHandler::subscribe(EventListener* el, elState state, int eventType)
103{
104  PRINTF(4)("Subscribing event type: %i\n", eventType);
105  if( state == ES_ALL )
106    {
107      for(int i = 0; i < ES_NUMBER; ++i)
108        if( likely(this->listeners[state][eventType] == NULL))
109          this->listeners[i][eventType] = el;
110        else
111          PRINTF(1)("Someone tried to subscribe to event %i @ state %i but this event has already been subscribed\n", eventType, state);
112    }
113  else 
114    if( likely(this->listeners[state][eventType] == NULL))
115      {
116        this->listeners[state][eventType] = el;
117      }
118    else
119      PRINTF(1)("Someone tried to subscribe to event %i @ state %i but this event has already been subscribed\n", eventType, state);
120}
121
122
123/**
124   \brief unsubscribe from the EventHandler
125   \param state: the stat in which it has been subscribed
126   \param eventType: the event, that shall be unsubscribed
127
128   if you want to unsubscribe an event listener from all subscribed events, just use the
129   unsubscribe(EventListener* el, elState state) function
130*/
131void EventHandler::unsubscribe(elState state, int eventType)
132{
133  PRINTF(4)("Unsubscribing event type nr: %i\n", eventType);
134  this->listeners[state][eventType] = NULL;
135}
136
137
138/**
139   \brief unsubscribe all events from a specific listener
140   \param el: the listener that wants to unsubscribe itself
141   \param state: the state in which the events shall be unsubscribed
142   
143*/
144void EventHandler::unsubscribe(EventListener* el, elState state)
145{
146  if( state == ES_ALL)
147    {
148      for(int i = 0; i < ES_NUMBER; ++i)
149        {
150          for(int j = 0; j < EV_NUMBER; ++j)
151            {
152              if( this->listeners[i][j] == el )
153                this->listeners[i][j] = NULL;
154            }
155        }
156    }
157  else
158    {
159      for(int j = 0; j < EV_NUMBER; ++j)
160        {
161          if( this->listeners[state][j] == el )
162            this->listeners[state][j] = NULL;
163        }
164    }
165}
166
167
168/**
169   \brief flush all registered events
170   \param state: a specific state
171*/
172void EventHandler::flush(elState state)
173{
174  if( state == ES_ALL)
175    {
176      for(int i = 0; i < ES_NUMBER; ++i)
177        {
178          for(int j = 0; j < EV_NUMBER; ++j)
179            {
180              this->listeners[i][j] = NULL;
181            }
182        }
183    }
184  else
185    {
186      for(int j = 0; j < EV_NUMBER; ++j)
187        {
188          this->listeners[state][j] = NULL;
189        }
190    }
191}
192
193
194/**
195   \brief core function of event handler: receives all events from SDL
196
197   The event from the SDL framework are collected here and distributed to all listeners.
198*/
199void EventHandler::process()
200{
201  SDL_Event event;
202  Event ev;
203  EventListener* listener = NULL;
204  while( SDL_PollEvent (&event))
205    {
206      switch( event.type)
207        {
208        case SDL_KEYDOWN:
209          ev.bPressed = true;
210          ev.type = event.key.keysym.sym;
211          break;
212        case SDL_KEYUP:
213          ev.bPressed = false;
214          ev.type = event.key.keysym.sym;
215          break;
216        case SDL_MOUSEMOTION:
217          ev.bPressed = false;
218          ev.type = EV_MOUSE_MOTION;
219          ev.x = event.motion.x;
220          ev.y = event.motion.y;
221          ev.xRel = event.motion.xrel;
222          ev.yRel = event.motion.yrel;
223          break;
224        case SDL_MOUSEBUTTONUP:
225          ev.bPressed = false;
226          ev.type = event.button.button + SDLK_LAST;
227          break;
228        case SDL_MOUSEBUTTONDOWN:
229          ev.bPressed = true;
230          ev.type = event.button.button + SDLK_LAST;
231          break;
232        case SDL_JOYAXISMOTION:
233          ev.bPressed = false;
234          ev.type = EV_JOY_AXIS_MOTION;
235          break;
236        case SDL_JOYBALLMOTION:
237          ev.bPressed = false;
238          ev.type = EV_JOY_BALL_MOTION;
239          break;
240        case SDL_JOYHATMOTION:
241          ev.bPressed = false;
242          ev.type = EV_JOY_HAT_MOTION;
243          break;
244        case SDL_JOYBUTTONDOWN:
245          ev.bPressed = true;
246          ev.type = EV_JOY_BUTTON;
247          break;
248        case SDL_JOYBUTTONUP:
249          ev.bPressed = true;
250          ev.type = EV_JOY_BUTTON;
251          break;
252        default:
253          ev.type = EV_UNKNOWN;
254          break;
255        }
256
257      /* small debug routine: shows alle events dispatched by the event handler */
258      PRINT(4)("\n==========================| EventHandler::process () |===\n");
259      PRINT(4)("=  Got Event nr %i, for state %i", ev.type, this->state); 
260     
261      listener = this->listeners[this->state][ev.type];
262      if( listener != NULL)
263        {
264          PRINT(4)("=  Event dispatcher msg: This event has been consumed\n");
265          PRINT(4)("=======================================================\n");     
266          listener->process(ev);
267        }
268      else
269        {
270          PRINT(4)("=  Event dispatcher msg: This event has NOT been consumed\n");
271          PRINT(4)("=======================================================\n");     
272        }
273
274
275    }
276}
277
Note: See TracBrowser for help on using the repository browser.