Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more aliases

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