Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: commented the event handler and event class

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