Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/event/event_handler.cc @ 4425

Last change on this file since 4425 was 4420, checked in by patrick, 20 years ago

orxonox/trunk: faster and easier unsubscribe function implemented

File size: 5.4 KB
Line 
1/*
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:
12   main-programmer: Patrick Boenzli
13   co-programmer:
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT
17
18#include "event_handler.h"
19
20#include "event_listener.h"
21#include "event.h"
22#include "key_mapper.h"
23
24#include "compiler.h"
25#include "debug.h"
26
27using namespace std;
28
29
30/**
31   \brief standard constructor
32*/
33EventHandler::EventHandler () 
34{
35  this->setClassID(CL_EVENT_HANDLER, "EventHandler"); 
36
37  this->listeners = new EventListener**[ES_NUMBER];
38  for(int i = 0; i < ES_NUMBER; ++i)
39    this->listeners[i] = new EventListener*[EV_NUMBER];
40
41  /* now initialize them all to zero */
42  for(int i = 0; i < ES_NUMBER; ++i)
43    {
44      for(int j = 0; j < SDLK_LAST; ++j)
45        {
46          this->listeners[i][j] = NULL;
47        }
48    }
49  this->state = ES_GAME;
50}
51
52
53/**
54   \brief the singleton reference to this class
55*/
56EventHandler* EventHandler::singletonRef = NULL;
57
58
59/**
60   \returns a Pointer to this Class
61*/
62EventHandler* EventHandler::getInstance(void)
63{
64  if (!EventHandler::singletonRef)
65    EventHandler::singletonRef = new EventHandler();
66  return EventHandler::singletonRef;
67}
68
69
70/**
71   \brief standard deconstructor
72
73*/
74EventHandler::~EventHandler () 
75{
76  EventHandler::singletonRef = NULL;
77  delete this->keyMapper;
78}
79
80
81
82void EventHandler::init()
83{
84  this->keyMapper = new KeyMapper();
85  this->keyMapper->loadKeyBindings();
86}
87
88
89
90void EventHandler::setState(elState state)
91{
92  this->state = state;
93}
94
95
96void EventHandler::subscribe(EventListener* el, elState state, int eventType)
97{
98  PRINTF(0)("Subscribing event type: %i\n", eventType);
99  if( state == ES_ALL )
100    {
101      for(int i = 0; i < ES_NUMBER; ++i)
102        if( likely(this->listeners[state][eventType] == NULL))
103          this->listeners[i][eventType] = el;
104        else
105          PRINTF(0)("Someone tried to subscribe to event %i @ state %i but this event has already been subscribed\n", eventType, state);
106    }
107  else 
108    if( likely(this->listeners[state][eventType] == NULL))
109      {
110        this->listeners[state][eventType] = el;
111      }
112    else
113      PRINTF(0)("Someone tried to subscribe to event %i @ state %i but this event has already been subscribed\n", eventType, state);
114}
115
116
117void EventHandler::unsubscribe(elState state, int eventType)
118{
119  PRINTF(0)("Unsubscribing event type nr: %i\n", eventType);
120  this->listeners[state][eventType] = NULL;
121}
122
123void EventHandler::flush(elState state)
124{
125  if( state == ES_ALL)
126    {
127      for(int i = 0; i < ES_NUMBER; ++i)
128        {
129          for(int j = 0; j < EV_NUMBER; ++j)
130            {
131              this->listeners[i][j] = NULL;
132            }
133        }
134    }
135  else
136    {
137      for(int j = 0; j < EV_NUMBER; ++j)
138        {
139          this->listeners[state][j] = NULL;
140        }
141    }
142}
143
144void EventHandler::unsubscribe(EventListener* el, elState state)
145{
146  if( state == ES_ALL)
147    {
148      for(int i = 0; i < ES_NUMBER; ++i)
149        {
150          for(int j = 0; j < EV_NUMBER; ++j)
151            {
152              if( this->listeners[i][j] == el )
153                this->listeners[i][j] = NULL;
154            }
155        }
156    }
157  else
158    {
159      for(int j = 0; j < EV_NUMBER; ++j)
160        {
161          if( this->listeners[state][j] == el )
162            this->listeners[state][j] = NULL;
163        }
164    }
165}
166
167
168
169void EventHandler::process()
170{
171  SDL_Event event;
172  Event ev;
173  EventListener* listener = NULL;
174  while( SDL_PollEvent (&event))
175    {
176      switch( event.type)
177        {
178        case SDL_KEYDOWN:
179          ev.bPressed = true;
180          ev.type = event.key.keysym.sym;
181          break;
182        case SDL_KEYUP:
183          ev.bPressed = false;
184          ev.type = event.key.keysym.sym;
185          break;
186        case SDL_MOUSEMOTION:
187          ev.bPressed = false;
188          ev.type = EV_MOUSE_MOTION;
189          ev.x = event.motion.x;
190          ev.y = event.motion.y;
191          ev.xRel = event.motion.xrel;
192          ev.yRel = event.motion.yrel;
193          break;
194        case SDL_MOUSEBUTTONUP:
195          ev.bPressed = false;
196          ev.type = event.button.button + SDLK_LAST;
197          break;
198        case SDL_MOUSEBUTTONDOWN:
199          ev.bPressed = true;
200          ev.type = event.button.button + SDLK_LAST;
201          break;
202        case SDL_JOYAXISMOTION:
203          ev.bPressed = false;
204          ev.type = EV_JOY_AXIS_MOTION;
205          break;
206        case SDL_JOYBALLMOTION:
207          ev.bPressed = false;
208          ev.type = EV_JOY_BALL_MOTION;
209          break;
210        case SDL_JOYHATMOTION:
211          ev.bPressed = false;
212          ev.type = EV_JOY_HAT_MOTION;
213          break;
214        case SDL_JOYBUTTONDOWN:
215          ev.bPressed = true;
216          ev.type = EV_JOY_BUTTON;
217          break;
218        case SDL_JOYBUTTONUP:
219          ev.bPressed = true;
220          ev.type = EV_JOY_BUTTON;
221          break;
222        default:
223          ev.type = EV_UNKNOWN;
224          break;
225        }
226
227      /* small debug routine: shows alle events dispatched by the event handler */
228      PRINT(0)("\n==========================| EventHandler::process () |===\n");
229      PRINT(0)("=  Got Event nr %i, for state %i", ev.type, this->state); 
230     
231      listener = this->listeners[this->state][ev.type];
232      if( listener != NULL)
233        {
234          PRINTF(0)("=  Event dispatcher msg: This event has been consumed\n");
235          listener->process(ev);
236        }
237      else
238        {
239          PRINTF(0)("=  Event dispatcher msg: This event has NOT been consumed\n");
240        }
241
242      PRINT(0)("=======================================================\n");     
243    }
244}
245
246
247void EventHandler::test()
248{
249  PRINT(0)("\n==========================| EventHandler::test () |===\n"); 
250  PRINT(0)("Eventhandler init successfuly\n");
251  KeyMapper km;
252  PRINT(0)("KeyMapper loaded\n");
253  km.loadKeyBindings();
254  km.debug();
255
256  PRINT(0)("=======================================================\n");     
257}
Note: See TracBrowser for help on using the repository browser.