Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7984 was 7919, checked in by bensch, 18 years ago

orxonox/trunk: merged the gui branche back
merged with command:
https://svn.orxonox.net/orxonox/branches/gui
no conflicts

File size: 13.1 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
[7756]28#include <algorithm>
[4329]29
30/**
[7919]31 * @brief standard constructor
32 */
[4780]33EventHandler::EventHandler ()
[4329]34{
[5285]35  this->setClassID(CL_EVENT_HANDLER, "EventHandler");
36  this->setName("EventHandler");
37
[5236]38  SDL_InitSubSystem(SDL_INIT_JOYSTICK);
39  SDL_InitSubSystem(SDL_INIT_EVENTTHREAD);
[5371]40  SDL_SetEventFilter(EventHandler::eventFilter);
[5236]41
[4350]42
[4355]43  /* now initialize them all to zero */
[7919]44  for (unsigned int i = 0; i < ES_NUMBER; i++)
45    this->bUNICODE[i] = false;
[5978]46  this->grabEvents(false);
[5291]47
[4407]48  this->state = ES_GAME;
[6802]49  this->eventsGrabbed = false;
[4329]50}
51
[4407]52
[4329]53/**
[7919]54 * @brief the singleton reference to this class
[4329]55*/
[4346]56EventHandler* EventHandler::singletonRef = NULL;
[4329]57
[4407]58
[4329]59/**
[7919]60 * @brief standard deconstructor
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    {
[7756]68      if(!this->listeners[i][j].empty())
[4817]69      {
[5285]70        PRINTF(2)("forgot to unsubscribe an EventListener!\n");// %s!\n", this->listeners[i][j]->getName());
[4817]71      }
72    }
73  }
[5236]74  SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
75
[4866]76  EventHandler::singletonRef = NULL;
[4352]77}
[4329]78
[4352]79
[4450]80/**
[7919]81 * @brief initializes the event handler
[5285]82 *
83 * this has to be called before the use of the event handler
[4450]84*/
[7256]85void EventHandler::init()
[4407]86{
[7756]87  this->keyMapper.loadKeyBindings();
[4407]88}
89
[4450]90/**
[7919]91 * @param state: to which the event handler shall change
92 */
93void EventHandler::setState(elState state)
94{
95  if (state == this->state)
96    return;
97
98  /// When Changing the State, all the keys will be released.
99  /// This is done by sending each eventListener, that still
100  /// has an Event subscribed, a Release Event.
101  int keyCount;
102  Uint8 * pressedKeys = SDL_GetKeyState(&keyCount);
103  for (unsigned int i = 0; i < SDLK_LAST; i++)
104  {
105    if (pressedKeys[i])
106    {
107      Event ev;
108      ev.bPressed = false;
109      ev.type = i;
110      if (unlikely(this->bUNICODE[this->state]))
111        ev.x = i;
112      this->dispachEvent(this->state, ev );
113    }
114  }
115
116  // switching to the new State.
117  elState oldState = this->state;
118  this->state = state;
119
120  // in the End the Corresponding handler will be notified.
121  Event stateSwitchEvent;
122  stateSwitchEvent.type = EV_LEAVE_STATE;
123  this->dispachEvent(oldState, stateSwitchEvent);
124
125  SDL_EnableUNICODE(this->bUNICODE[state]);
126};
127
128
129/**
130 * @brief pushes the current State in the State-stack, and selects state
[5388]131 * @param state the new State to set
132 */
133void EventHandler::pushState(elState state)
134{
[7164]135  if (likely(state != ES_NULL && state != ES_ALL ))
[5388]136  {
[7164]137    this->stateStack.push(this->state);
[5388]138    this->setState(state);
139  }
140  else
141  {
142    PRINTF(2)("unable to push State\n");
143  }
144}
145
146/**
[7919]147 * @brief this removes the topmost stack-entry and select the underlying one
[5388]148 * @returns the next stack-entry
149 */
150elState EventHandler::popState()
151{
[7166]152  if (stateStack.empty())
153    return ES_NULL;
[7164]154  elState state =  (elState)stateStack.top();
155  this->stateStack.pop();
[5388]156  if (state == ES_NULL)
157  {
158    PRINTF(2)("No more states availiable. (unable to pop state)\n");
159    return ES_NULL;
160  }
161  else
162  {
163    this->setState(state);
164    return state;
165  }
166}
167
168
169/**
[7756]170 * @brief subscribe to an event
[4836]171 * @param el: the event listener that wants to subscribe itself, the listener that will be called when the evetn occures
172 * @param state: for which the listener wants to receive events
173 * @param eventType: the event type that wants to be listened for.
[4450]174
175   This is one of the most important function of the EventHandler. If you would like to subscribe for more
176   than one state, you have to subscribe for each state again. If you want to subscribe for all states, use
177   state = ES_ALL, which will subscribe your listener for all states together.
[5291]178 */
[4405]179void EventHandler::subscribe(EventListener* el, elState state, int eventType)
[4354]180{
[4450]181  PRINTF(4)("Subscribing event type: %i\n", eventType);
[4407]182  if( state == ES_ALL )
[6771]183  {
184    for(unsigned int i = 0; i < ES_NUMBER; i++)
[7919]185      if (!this->findListener( NULL, (elState)i, eventType, el))
186        this->listeners[i][eventType].push_back(el);
187      else
[7756]188      {
[7919]189        PRINTF(2)("%s::%s was already subscribed to state %d event %d\n", el->getClassName(), el->getName(), i, eventType);
[7756]190      }
[6771]191  }
192  else
[7756]193  {
[7919]194    if (!this->findListener( NULL, state, eventType, el))
195      this->listeners[state][eventType].push_back(el);
196    else
[4407]197    {
[7919]198      PRINTF(2)("%s::%s was already subscribed to state %d event %d\n", el->getClassName(), el->getName(), state, eventType);
[4407]199    }
[7756]200  }
[4354]201}
202
203
[4450]204/**
[7756]205 * @brief unsubscribe from the EventHandler
[4836]206 * @param state: the stat in which it has been subscribed
207 * @param eventType: the event, that shall be unsubscribed
[7919]208 *
209 * if you want to unsubscribe an event listener from all subscribed events, just use the
210 * unsubscribe(EventListener* el, elState state) function
211 */
[7868]212void EventHandler::unsubscribe(EventListener* el, elState state, int eventType)
[4355]213{
[4450]214  PRINTF(4)("Unsubscribing event type nr: %i\n", eventType);
[5291]215  if (state == ES_ALL)
216    for (unsigned int i = 0; i < ES_NUMBER; i++)
[7868]217    {
[7919]218      std::vector<EventListener*>::iterator listener;
219      if (this->findListener(&listener, (elState)i, eventType, el))
[7868]220        this->listeners[i][eventType].erase(listener);
221    }
[5291]222  else
[7868]223  {
[7919]224    std::vector<EventListener*>::iterator listener;
225    if (this->findListener(&listener, state, eventType, el))
[7868]226      this->listeners[state][eventType].erase(listener);
227  }
[4355]228}
[4354]229
[4450]230
231/**
[7868]232 * @brief unsubscribe all events from a specific listener
[4836]233 * @param el: the listener that wants to unsubscribe itself
234 * @param state: the state in which the events shall be unsubscribed
[7868]235 */
[4450]236void EventHandler::unsubscribe(EventListener* el, elState state)
[4355]237{
[7919]238  assert( el != NULL && state < ES_NUMBER);
[4364]239  if( state == ES_ALL)
[6771]240  {
241    for(unsigned int i = 0; i < ES_NUMBER; i++)
[4355]242    {
[6771]243      for(unsigned int j = 0; j < EV_NUMBER; j++)
244      {
[7756]245        std::vector<EventListener*>::iterator deller = std::find (this->listeners[i][j].begin(), this->listeners[i][j].end(), el);
246        if( deller != this->listeners[i][j].end())
247          this->listeners[i][j].erase(deller);
[6771]248      }
[4364]249    }
[6771]250  }
[4364]251  else
[6771]252  {
253    for(int j = 0; j < EV_NUMBER; j++)
[4364]254    {
[7756]255      std::vector<EventListener*>::iterator deller =  std::find (this->listeners[state][j].begin(), this->listeners[state][j].end(), el);
256      if( deller != this->listeners[state][j].end())
257        this->listeners[state][j].erase(deller);
[4355]258    }
[6771]259  }
[4355]260}
261
[7919]262/**
263 * @brief returns true if at state and eventType there is something subscribed.
264 * @param state the state to check in.
265 * @param eventType the eventtype to check.
266 * @returns true if a event is subscibed.
267 */
[7756]268bool EventHandler::isSubscribed(elState state, int eventType)
269{
270  return(listeners[state][eventType].empty()) ? false : true;
271};
[4450]272
[7756]273
274
[4450]275/**
[7919]276 * @brief flush all registered events
[4836]277 * @param state: a specific state
[4450]278*/
279void EventHandler::flush(elState state)
[4420]280{
281  if( state == ES_ALL)
[6771]282  {
283    for(int i = 0; i < ES_NUMBER; ++i)
[4420]284    {
[6771]285      for(int j = 0; j < EV_NUMBER; ++j)
286      {
[7756]287        this->listeners[i][j].clear();
[6771]288      }
[4420]289    }
[6771]290  }
[4420]291  else
[6771]292  {
293    for(int j = 0; j < EV_NUMBER; ++j)
[4420]294    {
[7756]295      this->listeners[state][j].clear();
[4420]296    }
[6771]297  }
[4420]298}
[4355]299
300
[7919]301bool EventHandler::findListener(std::vector<EventListener*>::iterator* it, elState state, int eventType, EventListener* listener)
[5786]302{
[7919]303  std::vector<EventListener*>::iterator findIterator =
304    std::find(this->listeners[state][eventType].begin(), this->listeners[state][eventType].end(), listener);
305  if (it != NULL)
306    *it = findIterator;
307  return ( findIterator != this->listeners[state][eventType].end());
308
[5786]309}
310
[7919]311
312
313/**
314 * @brief if the unicode characters should be recorded.
315 * @param state the State in whitch to set the new Value.
316 * @param enableUNICODE: enabled, or disabled.
317 */
318void EventHandler::withUNICODE(elState state, bool enableUNICODE)
319{
320  this->bUNICODE[state] = enableUNICODE;
321  if (this->state == state)
322    SDL_EnableUNICODE(enableUNICODE);
323}
324
325/**
326 * @brief grabs InputEvents.
327 * @param grabEvents if the Events should be grabbed(true) or released(false)
328 */
[5978]329void EventHandler::grabEvents(bool grabEvents)
330{
[6054]331  this->eventsGrabbed = grabEvents;
332  if(!grabEvents)
[6990]333  {
334    SDL_ShowCursor(SDL_ENABLE);
[6054]335    SDL_WM_GrabInput(SDL_GRAB_OFF);
[6990]336  }
[6054]337  else
[6990]338  {
[6813]339    SDL_WM_GrabInput(SDL_GRAB_ON);
[6990]340    SDL_ShowCursor(SDL_DISABLE);
341  }
[5978]342}
[5786]343
[7919]344
345
[4450]346/**
[7919]347 * @brief core function of event handler: receives all events from SDL
348 *
349 * The event from the SDL framework are collected here and distributed to all listeners.
350 */
351void EventHandler::process() const
[4355]352{
353  SDL_Event event;
[4361]354  Event ev;
[4407]355  EventListener* listener = NULL;
[4355]356  while( SDL_PollEvent (&event))
[6771]357  {
358    switch( event.type)
[4355]359    {
[6771]360      case SDL_KEYDOWN:
361        ev.bPressed = true;
362        ev.type = event.key.keysym.sym;
[7919]363        if (unlikely(this->bUNICODE[this->state]))
[6771]364          ev.x = event.key.keysym.unicode;
365        break;
366      case SDL_KEYUP:
367        ev.bPressed = false;
368        ev.type = event.key.keysym.sym;
[7919]369        if (unlikely(this->bUNICODE[this->state]))
[6771]370          ev.x = event.key.keysym.unicode;
371        break;
372      case SDL_MOUSEMOTION:
373        ev.bPressed = false;
374        ev.type = EV_MOUSE_MOTION;
375        ev.x = event.motion.x;
376        ev.y = event.motion.y;
377        ev.xRel = event.motion.xrel;
378        ev.yRel = event.motion.yrel;
379        break;
380      case SDL_MOUSEBUTTONUP:
381        ev.bPressed = false;
382        ev.type = event.button.button + SDLK_LAST;
383        break;
384      case SDL_MOUSEBUTTONDOWN:
385        ev.bPressed = true;
386        ev.type = event.button.button + SDLK_LAST;
387        break;
388      case SDL_JOYAXISMOTION:
389        ev.bPressed = false;
390        ev.type = EV_JOY_AXIS_MOTION;
391        break;
392      case SDL_JOYBALLMOTION:
393        ev.bPressed = false;
394        ev.type = EV_JOY_BALL_MOTION;
395        break;
396      case SDL_JOYHATMOTION:
397        ev.bPressed = false;
398        ev.type = EV_JOY_HAT_MOTION;
399        break;
400      case SDL_JOYBUTTONDOWN:
401        ev.bPressed = true;
402        ev.type = EV_JOY_BUTTON;
403        break;
404      case SDL_JOYBUTTONUP:
405        ev.bPressed = true;
406        ev.type = EV_JOY_BUTTON;
407        break;
[7919]408      case SDL_ACTIVEEVENT:
409        ev.type = EV_WINDOW_FOCUS;
410        ev.bPressed = (event.active.gain != 0);
411        break;
[6771]412      case SDL_VIDEORESIZE:
413        ev.resize = event.resize;
414        ev.type = EV_VIDEO_RESIZE;
415        break;
416      case SDL_QUIT:
417        ev.type = EV_MAIN_QUIT;
418        break;
419      default:
420        ev.type = EV_UNKNOWN;
421        break;
422    }
[7919]423    this->dispachEvent(this->state, ev);
424  }
425}
[4362]426
[4780]427
[7919]428/**
429 * @brief dispaches an Event.
430 * @param event the Event to dispach.
431 */
432void EventHandler::dispachEvent(elState state, const Event& event) const
433{
434  /* small debug routine: shows all events dispatched by the event handler */
435  PRINT(4)("\n==========================| EventHandler::process () |===\n");
436  PRINT(4)("=  Got Event nr %i, for state %i\n", event.type, state);
[7756]437
[7919]438  /// setting a temporary state in case of an EventListener's process changes the state.
439  for (unsigned int i = 0; i < this->listeners[state][event.type].size(); i++)
440  {
441    PRINT(4)("=  Event dispatcher msg: This event has been consumed\n");
442    PRINT(4)("=  Got Event nr %i, for state %i (%d registered) to %s::%s(%p)\n", event.type, i, state, this->listeners[state][event.type][i]->getClassName(), this->listeners[state][event.type][i]->getName(), this->listeners[state][event.type][i]);
443    PRINT(4)("=======================================================\n");
444    this->listeners[state][event.type][i]->process(event);
[6771]445  }
[7919]446  /*    else
447  {
448        PRINT(4)("=  Event dispatcher msg: This event has NOT been consumed\n");
449        PRINT(4)("=======================================================\n");
450  }*/
[4355]451}
[4388]452
[5371]453
[7919]454
455/**
456 * @brief An eventFilter.
457 * @param event the Event to be filtered.
458 * @returns 0 on filtered Event. 1 Otherwise.
459 */
[5237]460int EventHandler::eventFilter(const SDL_Event *event)
461{
[6802]462  if (likely(EventHandler::getInstance()->eventsGrabbed))
[5237]463  {
[6802]464    if (event->type == SDL_KEYDOWN &&  event->key.keysym.sym == SDLK_TAB && SDL_GetKeyState(NULL)[SDLK_LALT])
465    {
466      PRINTF(3)("Not sending event to the WindowManager\n");
467      EventHandler::getInstance()->grabEvents(false);
468      return 0;
469    }
470  }
471  else
472  {
473    if (event->type == SDL_MOUSEBUTTONDOWN)
474    {
475      EventHandler::getInstance()->grabEvents( true);
476      return 0;
477    }
478  }
[6054]479
[5237]480  return 1;
481}
482
483/**
[7919]484 * @brief outputs some nice information about the EventHandler
[5237]485 */
[4872]486void EventHandler::debug() const
487{
488  PRINT(0)("===============================\n");
489  PRINT(0)(" EventHandle Debug Information \n");
490  PRINT(0)("===============================\n");
491  for(int i = 0; i < ES_NUMBER; ++i)
[7919]492  {
[4872]493    for(int j = 0; j < EV_NUMBER; ++j)
[7756]494      for (unsigned int evl = 0; evl < this->listeners[i][j].size(); evl++)
495        PRINT(0)("Event %d of State %d subscribed to %s (%p)\n", j, i, this->listeners[i][j][evl]->getName(), this->listeners[i][j][evl]);
[7919]496  }
[4872]497  PRINT(0)("============================EH=\n");
498}
Note: See TracBrowser for help on using the repository browser.