Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: started implementing the process function

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