Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: simple ClassCompletion… had to sleep about it… now i've got to learn

File size: 6.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: ...
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
184void ShellInput::tick(float dt)
185{
186  if (this->delayed > 0.0)
187    this->delayed -= dt;
188  else if (this->pressedKey != SDLK_FIRST )
189  {
190    this->delayed = this->repeatRate;
191    if (this->pressedKey == SDLK_BACKSPACE)
192      this->removeCharacters(1);
193    else if (pressedKey < 127)
194      this->addCharacter(this->pressedKey);
195  }
196}
197
198/**
199 * listens for some event
200 * @param event the Event happened
201 */
202void ShellInput::process(const Event &event)
203{
204  if (event.bPressed)
205  {
206    PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type));
207    if (event.type == SDLK_F1)
208      this->help();
209    else if (event.type == SDLK_F2)
210      this->debug();
211    else if (event.type == SDLK_TAB)
212      this->completion->autoComplete();
213    else if (event.type == SDLK_BACKSPACE)
214    {
215      this->delayed = this->repeatDelay;
216      this->pressedKey = SDLK_BACKSPACE;
217      this->removeCharacters(1);
218    }
219    else if (event.type == SDLK_RETURN)
220      this->executeCommand();
221    /*
222    else if (event.type == SDLK_UP)
223    {
224//      this->flushInputLine();
225    tIterator<char>* iterator = this->commandList->getIterator();
226    char* command = iterator->lastElement();
227    while (command)
228    {
229    if (!strcmp (command, inputLine))
230    {
231    inputLine = iterator->prevElement();
232    return;
233  }
234    command = iterator->prevElement();
235  }
236    inputLine = iterator->lastElement();
237  }
238    */
239    else if (likely(event.type < 127))
240    {
241      Uint8 *keystate = SDL_GetKeyState(NULL);
242      this->delayed = this->repeatDelay;
243      if (unlikely( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] ))
244      {
245        this->pressedKey = event.type-32;
246        this->addCharacter(event.type-32);
247      }
248      else
249      {
250        this->pressedKey = event.type;
251        this->addCharacter(event.type);
252      }
253    }
254  }
255  else // if(!event.bPressed)
256  {
257    if (this->pressedKey == event.type || (this->pressedKey == event.type - 32))
258    {
259      this->pressedKey = SDLK_FIRST;
260      this->delayed = 0.0;
261    }
262  }
263}
Note: See TracBrowser for help on using the repository browser.