Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell_input.cc @ 5197

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

orxonox/trunk: doxygen-tags

File size: 6.4 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: ...
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "shell_input.h"
19
20
21
22#include "shell_command.h"
23#include "shell_completion.h"
24#include "event_handler.h"
25
26#include "debug.h"
27#include "list.h"
28#include "compiler.h"
29#include "stdlibincl.h"
30#include "key_names.h"
31
32
33using namespace std;
34
35
36/**
37 * standard constructor
38 * @todo this constructor is not jet implemented - do it
39*/
40ShellInput::ShellInput ()
41{
42  this->pressedKey = SDLK_FIRST;
43
44  this->inputLine = new char[1];
45  this->inputLine[0] = '\0';
46  this->inputHistory = new tList<char>;
47  this->delayed = 0;
48  this->setRepeatDelay(.3, .05);
49
50  // subscribe all keyboard commands to ES_SEHLL
51  EventHandler* evh = EventHandler::getInstance();
52  for (int i = 1; i < SDLK_LAST; i++)
53    evh->subscribe(this, ES_SHELL, i);
54
55  this->completion = new ShellCompletion(this);
56}
57
58/**
59 * standard deconstructor
60*/
61ShellInput::~ShellInput ()
62{
63  // delete what has to be deleted here
64  delete[] this->inputLine;
65  delete this->completion;
66
67  tIterator<char>* itH = this->inputHistory->getIterator();
68  char* histEl = itH->firstElement();
69  while (histEl != NULL)
70  {
71    delete[] histEl;
72    histEl = itH->nextElement();
73  }
74  delete itH;
75  delete this->inputHistory;
76}
77
78/**
79 * sets the Repeate-delay and rate
80 * @param repeatDelay the Delay it takes, to repeate a key
81 * @param repeatRate the rate to repeate a pressed key
82 */
83void ShellInput::setRepeatDelay(float repeatDelay, float repeatRate)
84{
85  this->repeatDelay = repeatDelay;
86  this->repeatRate = repeatRate;
87}
88
89/**
90 * deletes the InputLine
91 */
92void ShellInput::flush()
93{
94  if (likely(this->inputLine != NULL))
95  {
96    delete[] this->inputLine;
97  }
98  this->inputLine = new char[1];
99  *this->inputLine = '\0';
100  this->setText(this->inputLine, true);
101}
102
103/**
104 * adds one character to the inputLine
105 * @param character the character to add to the inputLine
106 */
107void ShellInput::addCharacter(char character)
108{
109  char* addCharLine = new char[strlen(this->inputLine)+2];
110
111  sprintf(addCharLine, "%s%c", this->inputLine, character);
112  delete[] this->inputLine;
113  this->inputLine = addCharLine;
114  this->setText(this->inputLine, true);
115}
116
117/**
118 * adds multiple Characters to thr inputLine
119 * @param characters a \\0 terminated char-array to add to the InputLine
120 */
121void ShellInput::addCharacters(const char* characters)
122{
123  char* addCharLine = new char[strlen(this->inputLine)+strlen(characters)+1];
124
125  sprintf(addCharLine, "%s%s", this->inputLine, characters);
126  delete[] this->inputLine;
127  this->inputLine = addCharLine;
128  this->setText(this->inputLine, true);
129}
130
131/**
132 * removes characterCount characters from the InputLine
133 * @param characterCount the count of Characters to remove from the input Line
134 */
135void ShellInput::removeCharacters(unsigned int characterCount)
136{
137  if (strlen(this->inputLine) == 0)
138    return;
139
140  if (characterCount > strlen(this->inputLine))
141    characterCount = strlen(this->inputLine);
142
143  char* removeCharLine = new char[strlen(this->inputLine)-characterCount+1];
144
145  strncpy(removeCharLine, this->inputLine, strlen(this->inputLine)-characterCount);
146  removeCharLine[strlen(this->inputLine)-characterCount] = '\0';
147  delete[] this->inputLine;
148  this->inputLine = removeCharLine;
149  this->setText(this->inputLine, true);
150}
151
152/**
153 * executes the command stored in the inputLine
154 * @return true if the command was commited successfully, false otherwise
155 */
156bool ShellInput::executeCommand()
157{
158  ShellBuffer::addBufferLineStatic("Execute Command: %s\n", this->inputLine);
159
160  char* newCommand = new char[strlen(this->inputLine)+1];
161  strcpy(newCommand, this->inputLine);
162  this->inputHistory->add(newCommand);
163
164  ShellCommandBase::execute(this->inputLine);
165
166  this->flush();
167
168  return false;
169}
170
171
172/**
173 * prints out some nice help about the Shell
174 */
175void ShellInput::help() const
176{
177  PRINT(0)("Help for the most important Shell-commands\n");
178  PRINT(0)("F1 - HELP; F2 - DEBUG; ` - open/close shell\n");
179  PRINT(0)("input order:\n");
180  PRINT(0)("ClassName::objectName function [parameter1, [parameter2 ...]]  or\n");
181  PRINT(0)("Command [parameter]\n");
182}
183
184/**
185 * ticks the ShellInput
186 * @param dt the time passed since the last update
187 */
188void ShellInput::tick(float dt)
189{
190  if (this->delayed > 0.0)
191    this->delayed -= dt;
192  else if (this->pressedKey != SDLK_FIRST )
193  {
194    this->delayed = this->repeatRate;
195    if (this->pressedKey == SDLK_BACKSPACE)
196      this->removeCharacters(1);
197    else if (pressedKey < 127)
198      this->addCharacter(this->pressedKey);
199  }
200}
201
202/**
203 * listens for some event
204 * @param event the Event happened
205 */
206void ShellInput::process(const Event &event)
207{
208  if (event.bPressed)
209  {
210    PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type));
211    if (event.type == SDLK_F1)
212      this->help();
213    else if (event.type == SDLK_F2)
214      this->debug();
215    else if (event.type == SDLK_TAB)
216      this->completion->autoComplete();
217    else if (event.type == SDLK_BACKSPACE)
218    {
219      this->delayed = this->repeatDelay;
220      this->pressedKey = SDLK_BACKSPACE;
221      this->removeCharacters(1);
222    }
223    else if (event.type == SDLK_RETURN)
224      this->executeCommand();
225    /*
226    else if (event.type == SDLK_UP)
227    {
228//      this->flushInputLine();
229    tIterator<char>* iterator = this->commandList->getIterator();
230    char* command = iterator->lastElement();
231    while (command)
232    {
233    if (!strcmp (command, inputLine))
234    {
235    inputLine = iterator->prevElement();
236    return;
237  }
238    command = iterator->prevElement();
239  }
240    inputLine = iterator->lastElement();
241  }
242    */
243    else if (likely(event.type < 127))
244    {
245      Uint8 *keystate = SDL_GetKeyState(NULL);
246      this->delayed = this->repeatDelay;
247      if (unlikely( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] ))
248      {
249        this->pressedKey = event.type-32;
250        this->addCharacter(event.type-32);
251      }
252      else
253      {
254        this->pressedKey = event.type;
255        this->addCharacter(event.type);
256      }
257    }
258  }
259  else // if(!event.bPressed)
260  {
261    if (this->pressedKey == event.type || (this->pressedKey == event.type - 32))
262    {
263      this->pressedKey = SDLK_FIRST;
264      this->delayed = 0.0;
265    }
266  }
267}
Note: See TracBrowser for help on using the repository browser.