Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5207 was 5093, checked in by bensch, 19 years ago

orxonox/trunk: inputLine is working.

File size: 8.4 KB
RevLine 
[4780]1/*
[4329]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:
[4346]12   main-programmer: Patrick Boenzli
[4780]13   co-programmer:
[4329]14*/
15
[4346]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT
[4329]17
[4346]18#include "event_handler.h"
[4388]19
[4350]20#include "event_listener.h"
[4361]21#include "event.h"
[4388]22#include "key_mapper.h"
[4329]23
[4381]24#include "compiler.h"
25#include "debug.h"
[4873]26#include "class_list.h"
[4352]27
[4329]28using namespace std;
29
30
31/**
[4836]32 *  standard constructor
[4329]33*/
[4780]34EventHandler::EventHandler ()
[4329]35{
[4780]36  this->setClassID(CL_EVENT_HANDLER, "EventHandler");
[4350]37
[4352]38  this->listeners = new EventListener**[ES_NUMBER];
39  for(int i = 0; i < ES_NUMBER; ++i)
[4365]40    this->listeners[i] = new EventListener*[EV_NUMBER];
[4355]41
42  /* now initialize them all to zero */
43  for(int i = 0; i < ES_NUMBER; ++i)
44    {
[4873]45      for(int j = 0; j < EV_NUMBER; ++j)
[4780]46        {
47          this->listeners[i][j] = NULL;
48        }
[4355]49    }
[4407]50  this->state = ES_GAME;
[4329]51}
52
[4407]53
[4329]54/**
[4836]55 *  the singleton reference to this class
[4329]56*/
[4346]57EventHandler* EventHandler::singletonRef = NULL;
[4329]58
[4407]59
[4329]60/**
[4836]61 *  standard deconstructor
[4329]62
63*/
[4780]64EventHandler::~EventHandler ()
[4329]65{
[4817]66  for(int i = 0; i < ES_NUMBER; ++i)
67  {
[4866]68    for(int j = 0; j < EV_NUMBER; ++j)
[4817]69    {
70      if( this->listeners[i][j] != NULL)
71      {
[4838]72        PRINTF(2)("forgot to unsubscribe an EventListener %s!\n");//, this->listeners[i][j]->getName());
[4817]73      }
74    }
75  }
[4866]76  delete this->keyMapper;
77  EventHandler::singletonRef = NULL;
[4352]78}
[4329]79
[4352]80
[4450]81/**
[4836]82 *  initializes the event handler
[4352]83
[4450]84   this has to be called before the use of the event handler
85*/
[4866]86void EventHandler::init(IniParser* iniParser)
[4407]87{
88  this->keyMapper = new KeyMapper();
[4866]89  this->keyMapper->loadKeyBindings(iniParser);
[4407]90}
91
[4450]92/**
[4836]93 *  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.
[4450]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.
[4780]101 *
[4836]102 * @todo this can also be done with the & operator, and checking for states, just set the esState to 1,2,4,8, and then 15 is equal to ES_ALL
[4450]103*/
[4405]104void EventHandler::subscribe(EventListener* el, elState state, int eventType)
[4354]105{
[4450]106  PRINTF(4)("Subscribing event type: %i\n", eventType);
[4407]107  if( state == ES_ALL )
108    {
109      for(int i = 0; i < ES_NUMBER; ++i)
[4780]110        if( likely(this->listeners[state][eventType] == NULL))
111          this->listeners[i][eventType] = el;
112        else
113          PRINTF(1)("%s of class %s tried to subscribe to event %i @ state %i but this event has already been subscribed\n", el->getName(), el->getClassName(), eventType, state);
[4407]114    }
[4780]115  else
[4407]116    if( likely(this->listeners[state][eventType] == NULL))
117      {
[4780]118        this->listeners[state][eventType] = el;
[4407]119      }
120    else
[4780]121      PRINTF(1)("% of class %s tried to subscribe to event %i @ state %i but this event has already been subscribed\n", el->getName(), el->getClassName(), eventType, state);
[4354]122}
123
124
[4450]125/**
[4836]126 *  unsubscribe from the EventHandler
127 * @param state: the stat in which it has been subscribed
128 * @param eventType: the event, that shall be unsubscribed
[4450]129
[4780]130   if you want to unsubscribe an event listener from all subscribed events, just use the
[4450]131   unsubscribe(EventListener* el, elState state) function
132*/
[4419]133void EventHandler::unsubscribe(elState state, int eventType)
[4355]134{
[4450]135  PRINTF(4)("Unsubscribing event type nr: %i\n", eventType);
[4355]136  this->listeners[state][eventType] = NULL;
137}
[4354]138
[4450]139
140/**
[4836]141 *  unsubscribe all events from a specific listener
142 * @param el: the listener that wants to unsubscribe itself
143 * @param state: the state in which the events shall be unsubscribed
[4780]144
[4450]145*/
146void EventHandler::unsubscribe(EventListener* el, elState state)
[4355]147{
[4816]148  if( el == NULL)
149    return;
[4364]150  if( state == ES_ALL)
[4355]151    {
[4364]152      for(int i = 0; i < ES_NUMBER; ++i)
[4780]153        {
154          for(int j = 0; j < EV_NUMBER; ++j)
155            {
156              if( this->listeners[i][j] == el )
157                this->listeners[i][j] = NULL;
158            }
159        }
[4364]160    }
161  else
162    {
[4420]163      for(int j = 0; j < EV_NUMBER; ++j)
[4780]164        {
165          if( this->listeners[state][j] == el )
166            this->listeners[state][j] = NULL;
167        }
[4355]168    }
169}
170
[4450]171
172/**
[4836]173 *  flush all registered events
174 * @param state: a specific state
[4450]175*/
176void EventHandler::flush(elState state)
[4420]177{
178  if( state == ES_ALL)
179    {
180      for(int i = 0; i < ES_NUMBER; ++i)
[4780]181        {
182          for(int j = 0; j < EV_NUMBER; ++j)
183            {
184              this->listeners[i][j] = NULL;
185            }
186        }
[4420]187    }
188  else
189    {
190      for(int j = 0; j < EV_NUMBER; ++j)
[4780]191        {
192          this->listeners[state][j] = NULL;
193        }
[4420]194    }
195}
[4355]196
197
[4450]198/**
[4836]199 *  core function of event handler: receives all events from SDL
[4420]200
[4450]201   The event from the SDL framework are collected here and distributed to all listeners.
202*/
[4355]203void EventHandler::process()
204{
205  SDL_Event event;
[4361]206  Event ev;
[4407]207  EventListener* listener = NULL;
[4355]208  while( SDL_PollEvent (&event))
209    {
210      switch( event.type)
[4780]211        {
212        case SDL_KEYDOWN:
213          ev.bPressed = true;
214          ev.type = event.key.keysym.sym;
215          break;
216        case SDL_KEYUP:
217          ev.bPressed = false;
218          ev.type = event.key.keysym.sym;
219          break;
220        case SDL_MOUSEMOTION:
221          ev.bPressed = false;
222          ev.type = EV_MOUSE_MOTION;
223          ev.x = event.motion.x;
224          ev.y = event.motion.y;
225          ev.xRel = event.motion.xrel;
226          ev.yRel = event.motion.yrel;
227          break;
228        case SDL_MOUSEBUTTONUP:
229          ev.bPressed = false;
230          ev.type = event.button.button + SDLK_LAST;
231          break;
232        case SDL_MOUSEBUTTONDOWN:
233          ev.bPressed = true;
234          ev.type = event.button.button + SDLK_LAST;
235          break;
236        case SDL_JOYAXISMOTION:
237          ev.bPressed = false;
238          ev.type = EV_JOY_AXIS_MOTION;
239          break;
240        case SDL_JOYBALLMOTION:
241          ev.bPressed = false;
242          ev.type = EV_JOY_BALL_MOTION;
243          break;
244        case SDL_JOYHATMOTION:
245          ev.bPressed = false;
246          ev.type = EV_JOY_HAT_MOTION;
247          break;
248        case SDL_JOYBUTTONDOWN:
249          ev.bPressed = true;
250          ev.type = EV_JOY_BUTTON;
251          break;
252        case SDL_JOYBUTTONUP:
253          ev.bPressed = true;
254          ev.type = EV_JOY_BUTTON;
255          break;
[4782]256        case SDL_VIDEORESIZE:
257          ev.resize = event.resize;
[4783]258          ev.type = EV_VIDEO_RESIZE;
[4782]259          break;
[4780]260        default:
261          ev.type = EV_UNKNOWN;
262          break;
263        }
[4362]264
[4780]265      /* small debug routine: shows all events dispatched by the event handler */
[4450]266      PRINT(4)("\n==========================| EventHandler::process () |===\n");
[4780]267      PRINT(4)("=  Got Event nr %i, for state %i", ev.type, this->state);
268
[4873]269      //! @todo fix this debug code away */
270      ////////////////////////////////////////////////////////////////
[4407]271      listener = this->listeners[this->state][ev.type];
[4873]272      if (!ClassList::exists(listener, CL_EVENT_LISTENER) && listener != NULL)
273      {
274        ClassList::debug(3, CL_EVENT_LISTENER);
275        this->debug();
276        printf("ERROR THIS EVENT DOES NOT EXIST\n");
277        return;
278      }
279      ////////////////////////////////////////////////////////////////
[4365]280      if( listener != NULL)
[4780]281        {
282          PRINT(4)("=  Event dispatcher msg: This event has been consumed\n");
283          PRINT(4)("=======================================================\n");
284          listener->process(ev);
285        }
[4365]286      else
[4780]287        {
288          PRINT(4)("=  Event dispatcher msg: This event has NOT been consumed\n");
289          PRINT(4)("=======================================================\n");
290        }
[4355]291    }
292}
[4388]293
[4872]294
295void EventHandler::debug() const
296{
297  PRINT(0)("===============================\n");
298  PRINT(0)(" EventHandle Debug Information \n");
299  PRINT(0)("===============================\n");
300  for(int i = 0; i < ES_NUMBER; ++i)
301  {
302    for(int j = 0; j < EV_NUMBER; ++j)
303    {
304      if( this->listeners[i][j] != NULL)
305      {
[4873]306        PRINT(0)("Event %d of State %d subscribed to %s (%p)\n", j, i, this->listeners[i][j]->getName(), this->listeners[i][j]);
[4872]307      }
308    }
309  }
310  PRINT(0)("============================EH=\n");
311}
Note: See TracBrowser for help on using the repository browser.