Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6917 was 6813, checked in by bensch, 19 years ago

orxonox/trunk: particles, player etc.

File size: 9.8 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
[5388]28#include "t_stack.h"
29
[4329]30using namespace std;
31
32
33/**
[4836]34 *  standard constructor
[4329]35*/
[4780]36EventHandler::EventHandler ()
[4329]37{
[5285]38  this->setClassID(CL_EVENT_HANDLER, "EventHandler");
39  this->setName("EventHandler");
40
[5236]41  SDL_InitSubSystem(SDL_INIT_JOYSTICK);
42  SDL_InitSubSystem(SDL_INIT_EVENTTHREAD);
[5371]43  SDL_SetEventFilter(EventHandler::eventFilter);
[5236]44
[4350]45
[4355]46  /* now initialize them all to zero */
[5291]47  this->flush(ES_ALL);
[5786]48  this->withUNICODE(false);
[5978]49  this->grabEvents(false);
[5291]50
[4407]51  this->state = ES_GAME;
[5285]52  this->keyMapper = NULL;
[5388]53  this->stateStack = NULL;
[6802]54  this->eventsGrabbed = false;
[4329]55}
56
[4407]57
[4329]58/**
[4836]59 *  the singleton reference to this class
[4329]60*/
[4346]61EventHandler* EventHandler::singletonRef = NULL;
[4329]62
[4407]63
[4329]64/**
[4836]65 *  standard deconstructor
[4329]66
67*/
[4780]68EventHandler::~EventHandler ()
[4329]69{
[4817]70  for(int i = 0; i < ES_NUMBER; ++i)
71  {
[4866]72    for(int j = 0; j < EV_NUMBER; ++j)
[4817]73    {
74      if( this->listeners[i][j] != NULL)
75      {
[5285]76        PRINTF(2)("forgot to unsubscribe an EventListener!\n");// %s!\n", this->listeners[i][j]->getName());
[4817]77      }
78    }
79  }
[5388]80  delete this->stateStack;
[4866]81  delete this->keyMapper;
[5236]82
83  SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
84
[4866]85  EventHandler::singletonRef = NULL;
[4352]86}
[4329]87
[4352]88
[4450]89/**
[4836]90 *  initializes the event handler
[5285]91 *
92 * this has to be called before the use of the event handler
[4450]93*/
[4866]94void EventHandler::init(IniParser* iniParser)
[4407]95{
[5388]96  if (this->keyMapper == NULL)
97  {
98    this->keyMapper = new KeyMapper();
99    this->keyMapper->loadKeyBindings(iniParser);
100  }
101  if (this->stateStack == NULL)
102    this->stateStack = new tStack<short>;
[4407]103}
104
[4450]105/**
[5388]106 * pushes the current State in the State-stack, and selects state
107 * @param state the new State to set
108 */
109void EventHandler::pushState(elState state)
110{
111  if (likely(state != ES_NULL && state != ES_ALL && this->stateStack != NULL))
112  {
113    this->stateStack->push(this->state);
114    this->setState(state);
115  }
116  else
117  {
118    PRINTF(2)("unable to push State\n");
119  }
120}
121
122/**
123 * this removes the topmost stack-entry and select the underlying one
124 * @returns the next stack-entry
125 */
126elState EventHandler::popState()
127{
128  if (unlikely(this->stateStack == NULL))
129    return ES_NULL;
130  elState state = (elState)this->stateStack->pop();
131  if (state == ES_NULL)
132  {
133    PRINTF(2)("No more states availiable. (unable to pop state)\n");
134    return ES_NULL;
135  }
136  else
137  {
138    this->setState(state);
139    return state;
140  }
141}
142
143
144/**
[4836]145 *  subscribe to an event
146 * @param el: the event listener that wants to subscribe itself, the listener that will be called when the evetn occures
147 * @param state: for which the listener wants to receive events
148 * @param eventType: the event type that wants to be listened for.
[4450]149
150   This is one of the most important function of the EventHandler. If you would like to subscribe for more
151   than one state, you have to subscribe for each state again. If you want to subscribe for all states, use
152   state = ES_ALL, which will subscribe your listener for all states together.
[5291]153 */
[4405]154void EventHandler::subscribe(EventListener* el, elState state, int eventType)
[4354]155{
[4450]156  PRINTF(4)("Subscribing event type: %i\n", eventType);
[4407]157  if( state == ES_ALL )
[6771]158  {
159    for(unsigned int i = 0; i < ES_NUMBER; i++)
160      if( likely(this->listeners[i][eventType] == NULL))
161        this->listeners[i][eventType] = el;
162      else
163        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);
164  }
165  else
166    if( likely(this->listeners[state][eventType] == NULL))
[4407]167    {
[6771]168      this->listeners[state][eventType] = el;
[4407]169    }
170    else
[5300]171      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]172}
173
174
[4450]175/**
[4836]176 *  unsubscribe from the EventHandler
177 * @param state: the stat in which it has been subscribed
178 * @param eventType: the event, that shall be unsubscribed
[4450]179
[4780]180   if you want to unsubscribe an event listener from all subscribed events, just use the
[4450]181   unsubscribe(EventListener* el, elState state) function
182*/
[4419]183void EventHandler::unsubscribe(elState state, int eventType)
[4355]184{
[4450]185  PRINTF(4)("Unsubscribing event type nr: %i\n", eventType);
[5291]186  if (state == ES_ALL)
187    for (unsigned int i = 0; i < ES_NUMBER; i++)
188      this->listeners[i][eventType] = NULL;
189  else
190    this->listeners[state][eventType] = NULL;
[4355]191}
[4354]192
[4450]193
194/**
[5285]195 * unsubscribe all events from a specific listener
[4836]196 * @param el: the listener that wants to unsubscribe itself
197 * @param state: the state in which the events shall be unsubscribed
[4780]198
[4450]199*/
200void EventHandler::unsubscribe(EventListener* el, elState state)
[4355]201{
[5291]202  if( el == NULL || state >= ES_NUMBER)
[4816]203    return;
[4364]204  if( state == ES_ALL)
[6771]205  {
206    for(unsigned int i = 0; i < ES_NUMBER; i++)
[4355]207    {
[6771]208      for(unsigned int j = 0; j < EV_NUMBER; j++)
209      {
210        if( this->listeners[i][j] == el )
211          this->listeners[i][j] = NULL;
212      }
[4364]213    }
[6771]214  }
[4364]215  else
[6771]216  {
217    for(int j = 0; j < EV_NUMBER; j++)
[4364]218    {
[6771]219      if( this->listeners[state][j] == el )
220        this->listeners[state][j] = NULL;
[4355]221    }
[6771]222  }
[4355]223}
224
[4450]225
226/**
[5285]227 * flush all registered events
[4836]228 * @param state: a specific state
[4450]229*/
230void EventHandler::flush(elState state)
[4420]231{
232  if( state == ES_ALL)
[6771]233  {
234    for(int i = 0; i < ES_NUMBER; ++i)
[4420]235    {
[6771]236      for(int j = 0; j < EV_NUMBER; ++j)
237      {
238        this->listeners[i][j] = NULL;
239      }
[4420]240    }
[6771]241  }
[4420]242  else
[6771]243  {
244    for(int j = 0; j < EV_NUMBER; ++j)
[4420]245    {
[6771]246      this->listeners[state][j] = NULL;
[4420]247    }
[6771]248  }
[4420]249}
[4355]250
251
[5786]252void EventHandler::withUNICODE(bool enableUNICODE)
253{
254  SDL_EnableUNICODE(enableUNICODE);
255  this->bUNICODE = enableUNICODE;
256}
257
[5978]258void EventHandler::grabEvents(bool grabEvents)
259{
[6054]260  this->eventsGrabbed = grabEvents;
261  if(!grabEvents)
262    SDL_WM_GrabInput(SDL_GRAB_OFF);
263  else
[6813]264    SDL_WM_GrabInput(SDL_GRAB_ON);
[5978]265}
[5786]266
[4450]267/**
[4836]268 *  core function of event handler: receives all events from SDL
[4420]269
[4450]270   The event from the SDL framework are collected here and distributed to all listeners.
271*/
[4355]272void EventHandler::process()
273{
274  SDL_Event event;
[4361]275  Event ev;
[4407]276  EventListener* listener = NULL;
[4355]277  while( SDL_PollEvent (&event))
[6771]278  {
279    switch( event.type)
[4355]280    {
[6771]281      case SDL_KEYDOWN:
282        ev.bPressed = true;
283        ev.type = event.key.keysym.sym;
284        if (unlikely(this->bUNICODE))
285          ev.x = event.key.keysym.unicode;
286        break;
287      case SDL_KEYUP:
288        ev.bPressed = false;
289        ev.type = event.key.keysym.sym;
290        if (unlikely(this->bUNICODE))
291          ev.x = event.key.keysym.unicode;
292        break;
293      case SDL_MOUSEMOTION:
294        ev.bPressed = false;
295        ev.type = EV_MOUSE_MOTION;
296        ev.x = event.motion.x;
297        ev.y = event.motion.y;
298        ev.xRel = event.motion.xrel;
299        ev.yRel = event.motion.yrel;
300        break;
301      case SDL_MOUSEBUTTONUP:
302        ev.bPressed = false;
303        ev.type = event.button.button + SDLK_LAST;
304        break;
305      case SDL_MOUSEBUTTONDOWN:
306        ev.bPressed = true;
307        ev.type = event.button.button + SDLK_LAST;
308        break;
309      case SDL_JOYAXISMOTION:
310        ev.bPressed = false;
311        ev.type = EV_JOY_AXIS_MOTION;
312        break;
313      case SDL_JOYBALLMOTION:
314        ev.bPressed = false;
315        ev.type = EV_JOY_BALL_MOTION;
316        break;
317      case SDL_JOYHATMOTION:
318        ev.bPressed = false;
319        ev.type = EV_JOY_HAT_MOTION;
320        break;
321      case SDL_JOYBUTTONDOWN:
322        ev.bPressed = true;
323        ev.type = EV_JOY_BUTTON;
324        break;
325      case SDL_JOYBUTTONUP:
326        ev.bPressed = true;
327        ev.type = EV_JOY_BUTTON;
328        break;
329      case SDL_VIDEORESIZE:
330        ev.resize = event.resize;
331        ev.type = EV_VIDEO_RESIZE;
332        break;
333      case SDL_QUIT:
334        ev.type = EV_MAIN_QUIT;
335        break;
336      default:
337        ev.type = EV_UNKNOWN;
338        break;
339    }
[4362]340
[6771]341    /* small debug routine: shows all events dispatched by the event handler */
342    PRINT(4)("\n==========================| EventHandler::process () |===\n");
343    PRINT(4)("=  Got Event nr %i, for state %i", ev.type, this->state);
[4780]344
[6771]345    listener = this->listeners[this->state][ev.type];
346    if( listener != NULL)
347    {
348      PRINT(4)("=  Event dispatcher msg: This event has been consumed\n");
349      PRINT(4)("=======================================================\n");
350      listener->process(ev);
[4355]351    }
[6771]352    else
353    {
354      PRINT(4)("=  Event dispatcher msg: This event has NOT been consumed\n");
355      PRINT(4)("=======================================================\n");
356    }
357  }
[4355]358}
[4388]359
[5371]360
[5237]361int EventHandler::eventFilter(const SDL_Event *event)
362{
[6802]363  if (likely(EventHandler::getInstance()->eventsGrabbed))
[5237]364  {
[6802]365    if (event->type == SDL_KEYDOWN &&  event->key.keysym.sym == SDLK_TAB && SDL_GetKeyState(NULL)[SDLK_LALT])
366    {
367      PRINTF(3)("Not sending event to the WindowManager\n");
368      EventHandler::getInstance()->grabEvents(false);
369      return 0;
370    }
371  }
372  else
373  {
374    if (event->type == SDL_MOUSEBUTTONDOWN)
375    {
376      EventHandler::getInstance()->grabEvents( true);
377      return 0;
378    }
379  }
[6054]380
[5237]381  return 1;
382}
383
384/**
385 * outputs some nice information about the EventHandler
386 */
[4872]387void EventHandler::debug() const
388{
389  PRINT(0)("===============================\n");
390  PRINT(0)(" EventHandle Debug Information \n");
391  PRINT(0)("===============================\n");
392  for(int i = 0; i < ES_NUMBER; ++i)
393    for(int j = 0; j < EV_NUMBER; ++j)
394      if( this->listeners[i][j] != NULL)
[4873]395        PRINT(0)("Event %d of State %d subscribed to %s (%p)\n", j, i, this->listeners[i][j]->getName(), this->listeners[i][j]);
[4872]396  PRINT(0)("============================EH=\n");
397}
Note: See TracBrowser for help on using the repository browser.