Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 4352 in orxonox.OLD for orxonox/trunk


Ignore:
Timestamp:
May 28, 2005, 12:56:01 AM (19 years ago)
Author:
patrick
Message:

orxonox/trunk: reimplemented the key binding function, kept most of the stuff since it seems very helpful

Location:
orxonox/trunk/src/util/event
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/util/event/event.h

    r4350 r4352  
    88#define _EVENT_H
    99
    10 #define CMD_LENGHT 16
    1110
    1211#include "base_object.h"
     12#include "event_def.h"
    1313
    1414
  • orxonox/trunk/src/util/event/event_handler.cc

    r4350 r4352  
    1919#include "event_listener.h"
    2020
     21#include "ini_parser.h"
     22#include "keynames.h"
     23
    2124#include "SDL_keysym.h"
    2225
     
    2932EventHandler::EventHandler ()
    3033{
    31    this->setClassID(CL_EVENT_HANDLER, "EventHandler");
     34  this->setClassID(CL_EVENT_HANDLER, "EventHandler");
    3235
    33    this->listeners = new EventListener*[SDLK_LAST];
     36  this->listeners = new EventListener**[ES_NUMBER];
     37  for(int i = 0; i < ES_NUMBER; ++i)
     38    this->listeners[i] = new EventListener*[SDLK_LAST];
    3439}
    3540
     
    5661{
    5762  EventHandler::singletonRef = NULL;
     63}
    5864
     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    }
    59167}
     168
  • orxonox/trunk/src/util/event/event_handler.h

    r4350 r4352  
    99
    1010#include "base_object.h"
     11#include "event_def.h"
    1112
    1213class EventListener;
    1314template<class T> class tList;
     15
     16
     17#define N_STD_KEYS SDLK_LAST
     18#define N_BUTTONS 6
     19#define DEFAULT_KEYBIND_FILE "~/.orxonox/orxonox.conf"
    1420
    1521typedef enum elState
     
    2127    ES_NUMBER,
    2228  };
     29
     30//! Key aliasing structure
     31/**
     32   This structure contains the key aliasing information, e.g. the command strings that
     33   have been bound to the keys.
     34*/
     35typedef struct
     36{
     37  char keys[N_STD_KEYS][CMD_LENGHT];
     38  char buttons[N_BUTTONS][CMD_LENGHT];
     39} KeyBindings;
    2340
    2441
     
    3956
    4057  void tick(float t);
     58  void process();
    4159
    4260 private:
    4361  EventHandler(void);
     62
    4463  static EventHandler* singletonRef;
    4564
    46   EventListener** listeners;                         //!< a list of registered listeners
     65  KeyBindings* keyAliases;
     66  EventListener*** listeners;                         //!< a list of registered listeners
    4767 
    4868};
  • orxonox/trunk/src/util/event/event_listener.h

    r4346 r4352  
    99
    1010#include "base_object.h"
     11#include "event_def.h"
    1112
    1213
Note: See TracChangeset for help on using the changeset viewer.