Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/textEngine/src/command_node.cc @ 3777

Last change on this file since 3777 was 3681, checked in by bensch, 19 years ago

orxonox/branches/textEngine: merged trunk here.
merged with command:
svn merge ../trunk textEngine -r 3467:HEAD
no conflicts

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