Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5375 was 5371, checked in by bensch, 19 years ago

orxonox/trunk: todo-list

File size: 8.3 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{
[5285]36  this->setClassID(CL_EVENT_HANDLER, "EventHandler");
37  this->setName("EventHandler");
38
[5236]39  SDL_InitSubSystem(SDL_INIT_JOYSTICK);
40  SDL_InitSubSystem(SDL_INIT_EVENTTHREAD);
[5371]41  SDL_SetEventFilter(EventHandler::eventFilter);
[5236]42
[4350]43
[4355]44  /* now initialize them all to zero */
[5291]45  this->flush(ES_ALL);
46
[4407]47  this->state = ES_GAME;
[5285]48  this->keyMapper = NULL;
[4329]49}
50
[4407]51
[4329]52/**
[4836]53 *  the singleton reference to this class
[4329]54*/
[4346]55EventHandler* EventHandler::singletonRef = NULL;
[4329]56
[4407]57
[4329]58/**
[4836]59 *  standard deconstructor
[4329]60
61*/
[4780]62EventHandler::~EventHandler ()
[4329]63{
[4817]64  for(int i = 0; i < ES_NUMBER; ++i)
65  {
[4866]66    for(int j = 0; j < EV_NUMBER; ++j)
[4817]67    {
68      if( this->listeners[i][j] != NULL)
69      {
[5285]70        PRINTF(2)("forgot to unsubscribe an EventListener!\n");// %s!\n", this->listeners[i][j]->getName());
[4817]71      }
72    }
73  }
[4866]74  delete this->keyMapper;
[5236]75
76  SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
77
[4866]78  EventHandler::singletonRef = NULL;
[4352]79}
[4329]80
[4352]81
[4450]82/**
[4836]83 *  initializes the event handler
[5285]84 *
85 * this has to be called before the use of the event handler
[4450]86*/
[4866]87void EventHandler::init(IniParser* iniParser)
[4407]88{
89  this->keyMapper = new KeyMapper();
[4866]90  this->keyMapper->loadKeyBindings(iniParser);
[4407]91}
92
[4450]93/**
[4836]94 *  subscribe to an event
95 * @param el: the event listener that wants to subscribe itself, the listener that will be called when the evetn occures
96 * @param state: for which the listener wants to receive events
97 * @param eventType: the event type that wants to be listened for.
[4450]98
99   This is one of the most important function of the EventHandler. If you would like to subscribe for more
100   than one state, you have to subscribe for each state again. If you want to subscribe for all states, use
101   state = ES_ALL, which will subscribe your listener for all states together.
[5291]102 */
[4405]103void EventHandler::subscribe(EventListener* el, elState state, int eventType)
[4354]104{
[4450]105  PRINTF(4)("Subscribing event type: %i\n", eventType);
[4407]106  if( state == ES_ALL )
107    {
[5291]108      for(unsigned int i = 0; i < ES_NUMBER; i++)
109        if( likely(this->listeners[i][eventType] == NULL))
[4780]110          this->listeners[i][eventType] = el;
111        else
[5300]112          PRINTF(2)("%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]113    }
[4780]114  else
[4407]115    if( likely(this->listeners[state][eventType] == NULL))
116      {
[4780]117        this->listeners[state][eventType] = el;
[4407]118      }
119    else
[5300]120      PRINTF(2)("% 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]121}
122
123
[4450]124/**
[4836]125 *  unsubscribe from the EventHandler
126 * @param state: the stat in which it has been subscribed
127 * @param eventType: the event, that shall be unsubscribed
[4450]128
[4780]129   if you want to unsubscribe an event listener from all subscribed events, just use the
[4450]130   unsubscribe(EventListener* el, elState state) function
131*/
[4419]132void EventHandler::unsubscribe(elState state, int eventType)
[4355]133{
[4450]134  PRINTF(4)("Unsubscribing event type nr: %i\n", eventType);
[5291]135  if (state == ES_ALL)
136    for (unsigned int i = 0; i < ES_NUMBER; i++)
137      this->listeners[i][eventType] = NULL;
138  else
139    this->listeners[state][eventType] = NULL;
[4355]140}
[4354]141
[4450]142
143/**
[5285]144 * unsubscribe all events from a specific listener
[4836]145 * @param el: the listener that wants to unsubscribe itself
146 * @param state: the state in which the events shall be unsubscribed
[4780]147
[4450]148*/
149void EventHandler::unsubscribe(EventListener* el, elState state)
[4355]150{
[5291]151  if( el == NULL || state >= ES_NUMBER)
[4816]152    return;
[4364]153  if( state == ES_ALL)
[4355]154    {
[5291]155      for(unsigned int i = 0; i < ES_NUMBER; i++)
[4780]156        {
[5291]157          for(unsigned int j = 0; j < EV_NUMBER; j++)
[4780]158            {
159              if( this->listeners[i][j] == el )
160                this->listeners[i][j] = NULL;
161            }
162        }
[4364]163    }
164  else
165    {
[5291]166      for(int j = 0; j < EV_NUMBER; j++)
[4780]167        {
168          if( this->listeners[state][j] == el )
169            this->listeners[state][j] = NULL;
170        }
[4355]171    }
172}
173
[4450]174
175/**
[5285]176 * flush all registered events
[4836]177 * @param state: a specific state
[4450]178*/
179void EventHandler::flush(elState state)
[4420]180{
181  if( state == ES_ALL)
182    {
183      for(int i = 0; i < ES_NUMBER; ++i)
[4780]184        {
185          for(int j = 0; j < EV_NUMBER; ++j)
186            {
187              this->listeners[i][j] = NULL;
188            }
189        }
[4420]190    }
191  else
192    {
193      for(int j = 0; j < EV_NUMBER; ++j)
[4780]194        {
195          this->listeners[state][j] = NULL;
196        }
[4420]197    }
198}
[4355]199
200
[4450]201/**
[4836]202 *  core function of event handler: receives all events from SDL
[4420]203
[4450]204   The event from the SDL framework are collected here and distributed to all listeners.
205*/
[4355]206void EventHandler::process()
207{
208  SDL_Event event;
[4361]209  Event ev;
[4407]210  EventListener* listener = NULL;
[4355]211  while( SDL_PollEvent (&event))
212    {
213      switch( event.type)
[4780]214        {
215        case SDL_KEYDOWN:
216          ev.bPressed = true;
217          ev.type = event.key.keysym.sym;
218          break;
219        case SDL_KEYUP:
220          ev.bPressed = false;
221          ev.type = event.key.keysym.sym;
222          break;
223        case SDL_MOUSEMOTION:
224          ev.bPressed = false;
225          ev.type = EV_MOUSE_MOTION;
226          ev.x = event.motion.x;
227          ev.y = event.motion.y;
228          ev.xRel = event.motion.xrel;
229          ev.yRel = event.motion.yrel;
230          break;
231        case SDL_MOUSEBUTTONUP:
232          ev.bPressed = false;
233          ev.type = event.button.button + SDLK_LAST;
234          break;
235        case SDL_MOUSEBUTTONDOWN:
236          ev.bPressed = true;
237          ev.type = event.button.button + SDLK_LAST;
238          break;
239        case SDL_JOYAXISMOTION:
240          ev.bPressed = false;
241          ev.type = EV_JOY_AXIS_MOTION;
242          break;
243        case SDL_JOYBALLMOTION:
244          ev.bPressed = false;
245          ev.type = EV_JOY_BALL_MOTION;
246          break;
247        case SDL_JOYHATMOTION:
248          ev.bPressed = false;
249          ev.type = EV_JOY_HAT_MOTION;
250          break;
251        case SDL_JOYBUTTONDOWN:
252          ev.bPressed = true;
253          ev.type = EV_JOY_BUTTON;
254          break;
255        case SDL_JOYBUTTONUP:
256          ev.bPressed = true;
257          ev.type = EV_JOY_BUTTON;
258          break;
[4782]259        case SDL_VIDEORESIZE:
260          ev.resize = event.resize;
[4783]261          ev.type = EV_VIDEO_RESIZE;
[4782]262          break;
[4780]263        default:
264          ev.type = EV_UNKNOWN;
265          break;
266        }
[4362]267
[4780]268      /* small debug routine: shows all events dispatched by the event handler */
[4450]269      PRINT(4)("\n==========================| EventHandler::process () |===\n");
[4780]270      PRINT(4)("=  Got Event nr %i, for state %i", ev.type, this->state);
271
[4407]272      listener = this->listeners[this->state][ev.type];
[4365]273      if( listener != NULL)
[4780]274        {
275          PRINT(4)("=  Event dispatcher msg: This event has been consumed\n");
276          PRINT(4)("=======================================================\n");
277          listener->process(ev);
278        }
[4365]279      else
[4780]280        {
281          PRINT(4)("=  Event dispatcher msg: This event has NOT been consumed\n");
282          PRINT(4)("=======================================================\n");
283        }
[4355]284    }
285}
[4388]286
[5371]287
[5237]288int EventHandler::eventFilter(const SDL_Event *event)
289{
290  if (event->type == SDL_KEYDOWN &&  event->key.keysym.sym == SDLK_TAB && SDL_GetKeyState(NULL)[SDLK_LALT])
291  {
[5371]292    printf("Not sending event to the WindowManager\n");
293    return 0;
[5237]294  }
295  return 1;
296}
297
298/**
299 * outputs some nice information about the EventHandler
300 */
[4872]301void EventHandler::debug() const
302{
303  PRINT(0)("===============================\n");
304  PRINT(0)(" EventHandle Debug Information \n");
305  PRINT(0)("===============================\n");
306  for(int i = 0; i < ES_NUMBER; ++i)
307    for(int j = 0; j < EV_NUMBER; ++j)
308      if( this->listeners[i][j] != NULL)
[4873]309        PRINT(0)("Event %d of State %d subscribed to %s (%p)\n", j, i, this->listeners[i][j]->getName(), this->listeners[i][j]);
[4872]310  PRINT(0)("============================EH=\n");
311}
Note: See TracBrowser for help on using the repository browser.