Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5849 was 5786, checked in by bensch, 19 years ago

orxonox/trunk: key-repeat in the Shell is smoother now

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