Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/command_node.cc @ 4415

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

orxonox/trunk: integrating the event handler into orxonox mainclass

File size: 7.4 KB
RevLine 
[2066]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: Christian Meyer
[2816]13   co-programmer: Patrick Boenzli
[2066]14*/
15
[3591]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COMMAND_NODE
[2066]17
18#include "command_node.h"
19#include "keynames.h"
20#include "ini_parser.h"
[2100]21#include "world_entity.h"
[2636]22#include "game_loader.h"
[3216]23#include "world.h"
[3608]24#include "list.h"
25#include "orxonox.h"
[4326]26#include "p_node.h"
[2066]27
28#include <stdio.h>
[2166]29#include <string.h>
[2105]30#include <stdlib.h>
[2066]31
32using namespace std;
33
[2141]34/**
[2636]35   \brief constructs a CommandNode to handle remote input
36   \param ID: unique denumerator to identify the node in the network
[2141]37*/
[2066]38CommandNode::CommandNode (int ID)
39{
[4326]40  this->bound = new tList<PNode>();
[3213]41  this->aliases = NULL;
42  this->netID = ID;
43  this->bLocalInput = false;
44  this->bEnabled = true;
[3216]45  this->world = NULL;
[2066]46}
47
[2141]48/**
[2636]49   \brief constructs a CommandNode to handle local input
50   \param filename: The path and name of the file to load the key bindings from
[2141]51*/
[2066]52CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE)
53{
[3213]54  this->aliases = NULL;
55  this->bLocalInput = true;
56  this->netID = 0;
[4326]57  this->bound = new tList<PNode>();
[3213]58  this->bEnabled = true;
[3216]59  this->world = NULL;
[4084]60  this->loadBindings(filename);
[2066]61}
62
[2141]63/**
[2636]64   \brief removes the CommandNode from memory
[2141]65*/
[2066]66CommandNode::~CommandNode ()
67{
[2636]68  if( aliases != NULL) free (aliases);
[3213]69  if( bound != NULL) delete bound; /* \todo should this delete bound? dangerous FIX */
[2066]70}
71
[2816]72
[3194]73/**
74  \brief this resets the command node
75
76   deleting all data contained in the command node to fill it up again
77
78  \todo coppling to different game-entities
79  \todo reset/destroy has to be redesigned
80*/
81
[2816]82void CommandNode::reset()
83{
[3194]84  this->bound->destroy();
[3213]85  //this->bound = NULL; /* \todo this produces a NULLpointer error.. FIX */
86  this->bEnabled = false;
[3216]87  this->world = NULL;
[2816]88}
89
[3213]90void CommandNode::enable(bool bEnabled)
91{
92  this->bEnabled = bEnabled;
93}
94
[3216]95
[2141]96/**
[3216]97  \brief adds Node to a GameWorld
98
99   this is usefull, if you want to catch events in a world class. usualy
100   this is done automaticaly via GameLoader. Reset it via
101   CommandNode::reset()
102
103*/
104void CommandNode::addToWorld(World* world)
105{
106  this->world = world;
107}
108
109
110/**
[2636]111   \brief loads new key bindings from a file
112   \param filename: The path and name of the file to load the bindings from
[2141]113*/
[4084]114void CommandNode::loadBindings (char* filename = DEFAULT_KEYBIND_FILE)
[2066]115{
[2636]116  FILE* stream;
117 
[3591]118  PRINTF(4)("Loading key bindings from %s\n", filename);
[2636]119 
120  // remove old bindings if present
121  if( aliases != NULL)
122    {
123      free (aliases);
124      aliases = NULL;
125    }
126 
127  // create parser
128  IniParser parser (filename);
[4091]129  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
[2636]130    {
[4091]131      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", filename);
[2636]132      return;
133    }
134  // allocate empty lookup table
135  aliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
136 
137  char namebuf[256];
138  char valuebuf[256];
139  memset (namebuf, 0, 256);
140  memset (valuebuf, 0, 256);
141  int* index;
142 
[3236]143  while( parser.nextVar (namebuf, valuebuf) != -1)
[2636]144    {
[4085]145      index = nameToIndex (valuebuf);
[2636]146      switch( index[0])
[2066]147        {
[2636]148        case 0:
[4085]149          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
150          strcpy (aliases->keys[index[1]], namebuf);
[2636]151          break;
152        case 1:
[4085]153          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
154          strcpy (aliases->buttons[index[1]], namebuf);
[2636]155          break;
156        default:
157          break;
[2066]158        }
[2636]159      memset (namebuf, 0, 256);
160      memset (valuebuf, 0, 256);
161    }
[4089]162
163
164  // PARSE MISC SECTION
[4091]165  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
[4089]166    {
167      PRINTF(1)("Could not find key bindings in %s\n", filename);
168      return;
169    }
170
171  while( parser.nextVar (namebuf, valuebuf) != -1)
172    {
173      index = nameToIndex (valuebuf);
174      switch( index[0])
175        {
176        case 0:
177          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
178          strcpy (aliases->keys[index[1]], namebuf);
179          break;
180        case 1:
181          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
182          strcpy (aliases->buttons[index[1]], namebuf);
183          break;
184        default:
185          break;
186        }
187      memset (namebuf, 0, 256);
188      memset (valuebuf, 0, 256);
189    }
190
[2066]191}
192
[2141]193/**
[2636]194   \brief binds a WorldEntity to the CommandNode
195   \param entity: Pointer to the entity to bind
[2141]196*/
[4326]197void CommandNode::bind (PNode* entity)
[2066]198{
[3225]199  bound->add (entity);
[2066]200}
201
[2141]202/**
[2636]203   \brief removes an entity from the list of the CommandNode
204   \param entity: Pointer to the entity to relese
[2141]205*/
[4326]206void CommandNode::unbind (PNode* entity)
[2066]207{
[3225]208  bound->remove (entity);
[2066]209}
210
[3225]211int* CommandNode::nameToIndex (char* name)
[2066]212{
[2636]213  coord[0] = -1;
214  coord[1] = -1;
215  int c;
[3225]216  if( (c = keynameToSDLK (name)) != -1)
[2636]217    {
218      coord[1] = c;
219      coord[0] = 0;
220    }
[3225]221  if( (c = buttonnameToSDLB (name)) != -1)
[2636]222    {
223      coord[1] = c;
224      coord[0] = 1;
225    }
226  return coord;
[2066]227}
228
[2141]229/**
[2636]230   \brief tells the CommandNode to run through all pending events and relay them accordingly
[2141]231*/
[2066]232void CommandNode::process ()
233{
[3213]234  if( this->bEnabled) 
235    {
[3225]236      if( bLocalInput) processLocal ();
237      else processNetwork ();
[3213]238    }
[2066]239}
240
[3236]241void CommandNode::processLocal ()
[2066]242{
[2636]243  SDL_Event event;
244  Command cmd;
245  while( SDL_PollEvent (&event))
246    {
[3591]247      PRINTF(3)("CommandNode::processLocal() =========================got Event\n");
[2636]248      memset (cmd.cmd, 0, CMD_LENGHT); 
249      switch( event.type)
[2066]250        {
[2636]251        case SDL_KEYDOWN:
252          strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]);
253          cmd.bUp = false;
[3225]254          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]255          break;
256        case SDL_KEYUP:
257          strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]);
258          cmd.bUp = true;
[3225]259          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]260          break;
261        case SDL_MOUSEMOTION:
262          strcpy( cmd.cmd, "cursor");
263          cmd.x = event.motion.x;
264          cmd.y = event.motion.y;
265          cmd.xrel = event.motion.xrel;
266          cmd.yrel = event.motion.yrel;
[4326]267          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]268          break;
269        case SDL_MOUSEBUTTONUP:
270          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
271          cmd.bUp = true;
[3225]272          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]273          break;
274        case SDL_MOUSEBUTTONDOWN:
275          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
276          cmd.bUp = false;
[3225]277          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]278          break;
279        case SDL_JOYAXISMOTION:
280        case SDL_JOYBALLMOTION:
281        case SDL_JOYHATMOTION:
282        case SDL_JOYBUTTONDOWN:
283        case SDL_JOYBUTTONUP:
284          break;
285        default:
286          Orxonox *orx = Orxonox::getInstance();
[4408]287          orx->graphicsHandler(&event);
[2636]288          break;
[2066]289        }
[2636]290    }
[2066]291}
292
[2816]293
[3225]294void CommandNode::processNetwork ()
[2066]295{
296
297}
298
[2816]299
[2066]300void CommandNode::relay (Command* cmd)
301{
[2636]302  Orxonox *orx = Orxonox::getInstance();
[3225]303  if( orx->systemCommand (cmd)) return;
[3216]304
[2636]305  GameLoader* gl = GameLoader::getInstance();
[3216]306  if( gl->worldCommand(cmd)) return;
307
[3225]308  if( bLocalInput) sendOverNetwork (cmd);
[2636]309 
[3216]310  if( this->world->command(cmd)) return;
311
[4326]312  tIterator<PNode>* iterator = bound->getIterator();
313  PNode* entity = iterator->nextElement();
[3216]314  while( entity != NULL)
[2636]315    {
[4326]316      ((WorldEntity*)entity)->command (cmd); /*no absorbtion of command! strange*/
[3654]317      entity = iterator->nextElement();
[2636]318    }
[3654]319  delete iterator;
[2066]320}
[2096]321
[2816]322
[2141]323/**
[2636]324   \brief sets the network identifier of the CommandNode
325   \param ID: the new ID to use
[2141]326*/
[3225]327void CommandNode::setNetID (int ID)
[2096]328{
[2636]329  netID = ID;
[2096]330}
331
[3225]332void CommandNode::sendOverNetwork (Command* cmd)
[2096]333{
334}
Note: See TracBrowser for help on using the repository browser.