Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: implemented event subscribe function

File size: 4.3 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
41/**
42   \brief the singleton reference to this class
43*/
44EventHandler* EventHandler::singletonRef = NULL;
45
46/**
47   \returns a Pointer to this Class
48*/
49EventHandler* EventHandler::getInstance(void)
50{
51  if (!EventHandler::singletonRef)
52    EventHandler::singletonRef = new EventHandler();
53  return EventHandler::singletonRef;
54}
55
56/**
57   \brief standard deconstructor
58
59*/
60EventHandler::~EventHandler () 
61{
62  EventHandler::singletonRef = NULL;
63}
64
65
66/**
67   \brief loads new key bindings from a file
68   \param filename: The path and name of the file to load the bindings from
69*/
70void EventHandler::loadKeyBindings (const char* fileName)
71{
72  FILE* stream;
73 
74  PRINTF(4)("Loading key bindings from %s\n", fileName);
75 
76  // remove old bindings if present
77  if( this->keyAliases != NULL)
78    {
79      free (this->keyAliases);
80      this->keyAliases = NULL;
81    }
82 
83  // create parser
84  IniParser parser(fileName);
85  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
86    {
87      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName);
88      return;
89    }
90  // allocate empty lookup table
91  this->keyAliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
92 
93  char namebuf[256];
94  char valuebuf[256];
95  memset (namebuf, 0, 256);
96  memset (valuebuf, 0, 256);
97  int* index;
98 
99  while( parser.nextVar (namebuf, valuebuf) != -1)
100    {
101      //index = nameToIndex (valuebuf);
102      int c;
103      if( (c = keynameToSDLK (valuebuf)) != -1)
104        {
105          index[1] = c; index[0] = 0;
106        }
107      if( (c = buttonnameToSDLB (valuebuf)) != -1)
108        {
109          index[1] = c; index[0] = 1;
110        }
111
112      switch( index[0])
113        {
114        case 0:
115          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
116          strcpy (this->keyAliases->keys[index[1]], namebuf);
117          break;
118        case 1:
119          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
120          strcpy (this->keyAliases->buttons[index[1]], namebuf);
121          break;
122        default:
123          break;
124        }
125      memset (namebuf, 0, 256);
126      memset (valuebuf, 0, 256);
127    }
128
129
130  // PARSE MISC SECTION
131  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
132    {
133      PRINTF(1)("Could not find key bindings in %s\n", fileName);
134      return;
135    }
136
137  while( parser.nextVar (namebuf, valuebuf) != -1)
138    {
139      //index = nameToIndex (valuebuf);     
140      int c;
141      if( (c = keynameToSDLK (valuebuf)) != -1)
142        {
143          index[1] = c; index[0] = 0;
144        }
145      if( (c = buttonnameToSDLB (valuebuf)) != -1)
146        {
147          index[1] = c; index[0] = 1;
148        }
149
150
151      switch( index[0])
152        {
153        case 0:
154          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
155          strcpy (keyAliases->keys[index[1]], namebuf);
156          break;
157        case 1:
158          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
159          strcpy (keyAliases->buttons[index[1]], namebuf);
160          break;
161        default:
162          break;
163        }
164      memset (namebuf, 0, 256);
165      memset (valuebuf, 0, 256);
166    }
167}
168
169
170void EventHandler::setState(elState state)
171{
172  this->state = state;
173}
174
175void EventHandler::subscribeListener(EventListener* el, elState state, int eventType)
176{
177  if( likely(this->listeners[state][eventType] == NULL))
178    this->listeners[state][eventType] = el;
179  else
180    PRINTF(0)("Someone tried to subscribe to event %i @ state %i but this event has already been subscribed\n", eventType, state);
181}
182
183
184void EventHandler::unsubscribeListener(EventListener* el, elState state)
185{}
186
187void EventHandler::flush()
188{}
Note: See TracBrowser for help on using the repository browser.