Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: partial Completion in the ShellInput again (show but no Completion)

File size: 6.0 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;
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
68/**
69 * sets the Repeate-delay and rate
70 * @param repeatDelay the Delay it takes, to repeate a key
71 * @param repeatRate the rate to repeate a pressed key
72 */
73void ShellInput::setRepeatDelay(float repeatDelay, float repeatRate)
74{
75  this->repeatDelay = repeatDelay;
76  this->repeatRate = repeatRate;
77
78}
79
80/**
81 * deletes the InputLine
82 */
83void ShellInput::flush()
84{
85  if (likely(this->inputLine != NULL))
86  {
87    delete[] this->inputLine;
88  }
89  this->inputLine = new char[1];
90  *this->inputLine = '\0';
91  this->setText(this->inputLine, true);
92}
93
94/**
95 * adds one character to the inputLine
96 * @param character the character to add to the inputLine
97 */
98void ShellInput::addCharacter(char character)
99{
100  char* addCharLine = new char[strlen(inputLine)+2];
101
102  sprintf(addCharLine, "%s%c", this->inputLine, character);
103  delete this->inputLine;
104  this->inputLine = addCharLine;
105  this->setText(inputLine, true);
106}
107
108/**
109 * adds multiple Characters to thr inputLine
110 * @param characters a \\0 terminated char-array to add to the InputLine
111 */
112void ShellInput::addCharacters(const char* characters)
113{
114  char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1];
115
116  sprintf(addCharLine, "%s%s", this->inputLine, characters);
117  delete this->inputLine;
118  this->inputLine = addCharLine;
119  this->setText(inputLine, true);
120}
121
122/**
123 * removes characterCount characters from the InputLine
124 * @param characterCount the count of Characters to remove from the input Line
125 */
126void ShellInput::removeCharacters(unsigned int characterCount)
127{
128  if (strlen(this->inputLine) == 0)
129    return;
130
131  if (characterCount > strlen(this->inputLine))
132    characterCount = strlen(this->inputLine);
133
134  char* removeCharLine = new char[strlen(inputLine)-characterCount+1];
135
136  strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount);
137  removeCharLine[strlen(inputLine)-characterCount] = '\0';
138  delete this->inputLine;
139  this->inputLine = removeCharLine;
140  this->setText(inputLine, true);
141}
142
143/**
144 * executes the command stored in the inputLine
145 * @return true if the command was commited successfully, false otherwise
146 */
147bool ShellInput::executeCommand()
148{
149  ShellBuffer::addBufferLineStatic("Execute Command: %s\n", this->inputLine);
150
151  char* newCommand = new char[strlen(this->inputLine)+1];
152  strcpy(newCommand, this->inputLine);
153  this->inputHistory->add(newCommand);
154
155  ShellCommandBase::execute(this->inputLine);
156
157  this->flush();
158
159  return false;
160}
161
162
163/**
164 * prints out some nice help about the Shell
165 */
166void ShellInput::help() const
167{
168  PRINT(0)("Help for the most important Shell-commands\n");
169  PRINT(0)("F1 - HELP; F2 - DEBUG; ` - open/close shell\n");
170  PRINT(0)("input order:\n");
171  PRINT(0)("ClassName::objectName function [parameter1, [parameter2 ...]]  or\n");
172  PRINT(0)("Command [parameter]\n");
173}
174
175void ShellInput::tick(float dt)
176{
177  if (this->delayed > 0.0)
178    this->delayed -= dt;
179  else if (this->pressedKey != SDLK_FIRST )
180  {
181    this->delayed = this->repeatRate;
182    if (this->pressedKey == SDLK_BACKSPACE)
183      this->removeCharacters(1);
184    else if (pressedKey < 127)
185      this->addCharacter(this->pressedKey);
186  }
187}
188
189/**
190 * listens for some event
191 * @param event the Event happened
192 */
193void ShellInput::process(const Event &event)
194{
195  if (event.bPressed)
196  {
197    PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type));
198    if (event.type == SDLK_F1)
199      this->help();
200    else if (event.type == SDLK_F2)
201      this->debug();
202    else if (event.type == SDLK_TAB)
203      this->completion->autoComplete(this);
204    else if (event.type == SDLK_BACKSPACE)
205    {
206      this->delayed = this->repeatDelay;
207      this->pressedKey = SDLK_BACKSPACE;
208      this->removeCharacters(1);
209    }
210    else if (event.type == SDLK_RETURN)
211      this->executeCommand();
212    /*
213    else if (event.type == SDLK_UP)
214    {
215//      this->flushInputLine();
216    tIterator<char>* iterator = this->commandList->getIterator();
217    char* command = iterator->lastElement();
218    while (command)
219    {
220    if (!strcmp (command, inputLine))
221    {
222    inputLine = iterator->prevElement();
223    return;
224  }
225    command = iterator->prevElement();
226  }
227    inputLine = iterator->lastElement();
228  }
229    */
230    else if (likely(event.type < 127))
231    {
232      Uint8 *keystate = SDL_GetKeyState(NULL);
233      this->delayed = this->repeatDelay;
234      if (unlikely( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] ))
235      {
236        this->pressedKey = event.type-32;
237        this->addCharacter(event.type-32);
238      }
239      else
240      {
241        this->pressedKey = event.type;
242        this->addCharacter(event.type);
243      }
244    }
245  }
246  else // if(!event.bPressed)
247  {
248    if (this->pressedKey == event.type || (this->pressedKey == event.type - 32))
249    {
250      this->pressedKey = SDLK_FIRST;
251      this->delayed = 0.0;
252    }
253  }
254}
Note: See TracBrowser for help on using the repository browser.