Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5711 was 5553, checked in by bensch, 19 years ago

orxonox/trunk: Quit Events are listened on.

  1. Quit-Button from the Windows Handler works
  2. ctrl-c also quits the game.

(smoothly)

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