Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4366 was 4366, checked in by patrick, 19 years ago

orxonox/trunk: player defined keys mapping started

File size: 6.6 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   This code is inspired by Christian Meyers CommandNode code (in Orxonox v0.2.3)
16*/
17
18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT
19
20#include "event_handler.h"
21#include "event_listener.h"
22#include "event.h"
23
24#include "ini_parser.h"
25#include "keynames.h"
26
27
28using namespace std;
29
30
31/**
32   \brief standard constructor
33*/
34EventHandler::EventHandler () 
35{
36  this->setClassID(CL_EVENT_HANDLER, "EventHandler"); 
37
38  this->listeners = new EventListener**[ES_NUMBER];
39  for(int i = 0; i < ES_NUMBER; ++i)
40    this->listeners[i] = new EventListener*[EV_NUMBER];
41
42  /* now initialize them all to zero */
43  for(int i = 0; i < ES_NUMBER; ++i)
44    {
45      for(int j = 0; j < SDLK_LAST; ++j)
46        {
47          this->listeners[i][j] = NULL;
48        }
49    }
50}
51
52/**
53   \brief the singleton reference to this class
54*/
55EventHandler* EventHandler::singletonRef = NULL;
56
57/**
58   \returns a Pointer to this Class
59*/
60EventHandler* EventHandler::getInstance(void)
61{
62  if (!EventHandler::singletonRef)
63    EventHandler::singletonRef = new EventHandler();
64  return EventHandler::singletonRef;
65}
66
67/**
68   \brief standard deconstructor
69
70*/
71EventHandler::~EventHandler () 
72{
73  EventHandler::singletonRef = NULL;
74}
75
76
77/**
78   \brief loads new key bindings from a file
79   \param filename: The path and name of the file to load the bindings from
80*/
81void EventHandler::loadKeyBindings (const char* fileName)
82{
83  FILE* stream;
84 
85  PRINTF(4)("Loading key bindings from %s\n", fileName);
86 
87  // remove old bindings if present
88  if( this->keyAliases != NULL)
89    {
90      free (this->keyAliases);
91      this->keyAliases = NULL;
92    }
93 
94  // create parser
95  IniParser parser(fileName);
96  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
97    {
98      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName);
99      return;
100    }
101  // allocate empty lookup table
102  this->keyAliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
103 
104  char namebuf[256];
105  char valuebuf[256];
106  memset (namebuf, 0, 256);
107  memset (valuebuf, 0, 256);
108  int* index;
109 
110  while( parser.nextVar (namebuf, valuebuf) != -1)
111    {
112      //index = nameToIndex (valuebuf);
113      int c;
114      if( (c = keynameToSDLK (valuebuf)) != -1)
115        {
116          index[1] = c; index[0] = 0;
117        }
118      if( (c = buttonnameToSDLB (valuebuf)) != -1)
119        {
120          index[1] = c; index[0] = 1;
121        }
122
123      switch( index[0])
124        {
125        case 0:
126          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
127          strcpy (this->keyAliases->keys[index[1]], namebuf);
128          break;
129        case 1:
130          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
131          strcpy (this->keyAliases->buttons[index[1]], namebuf);
132          break;
133        default:
134          break;
135        }
136      memset (namebuf, 0, 256);
137      memset (valuebuf, 0, 256);
138    }
139
140
141  // PARSE MISC SECTION
142  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
143    {
144      PRINTF(1)("Could not find key bindings in %s\n", fileName);
145      return;
146    }
147
148  while( parser.nextVar (namebuf, valuebuf) != -1)
149    {
150      //index = nameToIndex (valuebuf);     
151      int c;
152      if( (c = keynameToSDLK (valuebuf)) != -1)
153        {
154          index[1] = c; index[0] = 0;
155        }
156      if( (c = buttonnameToSDLB (valuebuf)) != -1)
157        {
158          index[1] = c; index[0] = 1;
159        }
160
161
162      switch( index[0])
163        {
164        case 0:
165          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
166          strcpy (keyAliases->keys[index[1]], namebuf);
167          break;
168        case 1:
169          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
170          strcpy (keyAliases->buttons[index[1]], namebuf);
171          break;
172        default:
173          break;
174        }
175      memset (namebuf, 0, 256);
176      memset (valuebuf, 0, 256);
177    }
178}
179
180
181void EventHandler::setState(elState state)
182{
183  this->state = state;
184}
185
186void EventHandler::subscribeListener(EventListener* el, elState state, int eventType)
187{
188  if( likely(this->listeners[state][eventType] == NULL))
189    this->listeners[state][eventType] = el;
190  else
191    PRINTF(0)("Someone tried to subscribe to event %i @ state %i but this event has already been subscribed\n", eventType, state);
192}
193
194
195void EventHandler::unsubscribeListener(int eventType, elState state)
196{
197  this->listeners[state][eventType] = NULL;
198}
199
200void EventHandler::flush(elState state)
201{
202  if( state == ES_ALL)
203    {
204      for(int i = 0; i < ES_NUMBER; ++i)
205        {
206          for(int j = 0; j < SDLK_LAST; ++j)
207            {
208              this->listeners[i][j] = NULL;
209            }
210        }
211    }
212  else
213    {
214      for(int j = 0; j < SDLK_LAST; ++j)
215        {
216          this->listeners[state][j] = NULL;
217        }
218    }
219}
220
221
222
223void EventHandler::process()
224{
225  SDL_Event event;
226  Event ev;
227  EventListener* listener;
228  while( SDL_PollEvent (&event))
229    {
230      switch( event.type)
231        {
232        case SDL_KEYDOWN:
233          ev.bPressed = true;
234          ev.type = event.key.keysym.sym;
235          break;
236        case SDL_KEYUP:
237          ev.bPressed = false;
238          ev.type = event.key.keysym.sym;
239          break;
240        case SDL_MOUSEMOTION:
241          ev.bPressed = false;
242          ev.type = EV_MOUSE_MOTION;
243          ev.x = event.motion.x;
244          ev.y = event.motion.y;
245          ev.xRel = event.motion.xrel;
246          ev.yRel = event.motion.yrel;
247          break;
248        case SDL_MOUSEBUTTONUP:
249          ev.bPressed = false;
250          ev.type = EV_MOUSE_BUTTON;
251          break;
252        case SDL_MOUSEBUTTONDOWN:
253          ev.bPressed = true;
254          ev.type = EV_MOUSE_BUTTON;
255          break;
256        case SDL_JOYAXISMOTION:
257          ev.bPressed = false;
258          ev.type = EV_JOY_AXIS_MOTION;
259          break;
260        case SDL_JOYBALLMOTION:
261          ev.bPressed = false;
262          ev.type = EV_JOY_BALL_MOTION;
263          break;
264        case SDL_JOYHATMOTION:
265          ev.bPressed = false;
266          ev.type = EV_JOY_HAT_MOTION;
267          break;
268        case SDL_JOYBUTTONDOWN:
269          ev.bPressed = true;
270          ev.type = EV_JOY_BUTTON;
271          break;
272        case SDL_JOYBUTTONUP:
273          ev.bPressed = true;
274          ev.type = EV_JOY_BUTTON;
275          break;
276        default:
277          break;
278        }
279
280      /* small debug routine: shows alle events dispatched by the event handler */
281      PRINT(0)("\n==========================| EventHandler::process () |===\n");
282      PRINT(0)("=  Got Event nr%i\n, for state %i", event.type, this->state); 
283     
284      listener = this->listeners[this->state][event.key.keysym.sym];
285      if( listener != NULL)
286        {
287          listener->process(ev);
288          PRINTF(0)("=  Event dispatcher msg: This event has been consumed\n");
289        }
290      else
291        {
292          PRINTF(0)("=  Event dispatcher msg: This event has NOT been consumed\n");
293        }
294
295      PRINT(0)("=======================================================\n");     
296    }
297}
Note: See TracBrowser for help on using the repository browser.