Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Fixed a Bug in the Shell Exection, that compromised a Vector

File size: 8.5 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: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
17
18#include "shell_input.h"
19
20#include "shell_command.h"
21#include "shell_command_class.h"
22#include "event_handler.h"
23
24#include "debug.h"
25#include "compiler.h"
26#include "key_names.h"
27
28namespace OrxShell
29{
30  SHELL_COMMAND(help, ShellInput, help)
31  ->describe("retrieve some help about the input mode")
32  ->setAlias("help");
33
34
35  /**
36   * @brief constructor
37   * this also generates a ShellCompletion automatically.
38  */
39  ShellInput::ShellInput ()
40      : Text ("")
41  {
42    this->pressedKey = SDLK_FIRST;
43    this->setClassID(CL_SHELL_INPUT, "ShellInput");
44
45    this->inputLine = "";
46    this->historyIT = ShellInput::history.begin();
47    this->setHistoryLength(50);
48    this->historyScrolling = false;
49    this->delayed = 0;
50    this->setRepeatDelay(.3, .05);
51
52    // subscribe all keyboard commands to ES_SEHLL
53    EventHandler* evh = EventHandler::getInstance();
54    for (int i = 1; i < SDLK_LAST; i++)
55    {
56      if (!evh->isSubscribed(ES_SHELL, i))
57        evh->subscribe(this, ES_SHELL, i);
58    }
59    // unsubscribe unused TODO improve.
60    evh->unsubscribe(ES_SHELL, SDLK_BACKQUOTE);
61    evh->unsubscribe(ES_SHELL, SDLK_F12);
62    evh->unsubscribe(ES_SHELL, SDLK_PAGEUP);
63    evh->unsubscribe(ES_SHELL, SDLK_PAGEDOWN);
64
65  }
66
67  std::list<std::string> ShellInput::history;
68
69  /**
70   * @brief standard deconstructor
71   */
72  ShellInput::~ShellInput ()
73  {}
74
75  /**
76   * @brief sets the Repeate-delay and rate
77   * @param repeatDelay the Delay it takes, to repeate a key
78   * @param repeatRate the rate to repeate a pressed key
79   */
80  void ShellInput::setRepeatDelay(float repeatDelay, float repeatRate)
81  {
82    this->repeatDelay = repeatDelay;
83    this->repeatRate = repeatRate;
84  }
85
86  /**
87   * @brief deletes the InputLine
88   */
89  void ShellInput::flush()
90  {
91    this->inputLine.clear();
92    this->setText(this->inputLine);
93  }
94
95  /**
96   * @brief sets the entire text of the InputLine to text
97   * @param text the new Text to set as InputLine
98   */
99  void ShellInput::setInputText(const std::string& text)
100  {
101    this->inputLine = text;
102    this->setText(this->inputLine);
103  }
104
105
106  /**
107   * @brief adds one character to the inputLine
108   * @param character the character to add to the inputLine
109   */
110  void ShellInput::addCharacter(char character)
111  {
112    if (this->historyScrolling)
113    {
114      this->history.pop_back();
115      this->historyScrolling = false;
116    }
117
118    this->inputLine += character;
119    this->setText(this->inputLine);
120  }
121
122  /**
123   * @brief adds multiple Characters to thr inputLine
124   * @param characters a \\0 terminated char-array to add to the InputLine
125   */
126  void ShellInput::addCharacters(const std::string& characters)
127  {
128    if (this->historyScrolling)
129    {
130      this->history.pop_back();
131      this->historyScrolling = false;
132    }
133
134    this->inputLine += characters;
135    this->setText(this->inputLine);
136  }
137
138  /**
139   * @brief removes characterCount characters from the InputLine
140   * @param characterCount the count of Characters to remove from the input Line
141   */
142  void ShellInput::removeCharacters(unsigned int characterCount)
143  {
144    if (this->historyScrolling)
145    {
146      this->history.pop_back();
147      this->historyScrolling = false;
148    }
149    if (this->inputLine.size() < characterCount)
150      characterCount = this->inputLine.size();
151
152    this->inputLine.erase(this->inputLine.size() - characterCount, this->inputLine.size());
153    this->setText(this->inputLine);
154  }
155
156  /**
157   * @brief executes the command stored in the inputLine
158   * @return true if the command was commited successfully, false otherwise
159   */
160  bool ShellInput::executeCommand()
161  {
162    ShellBuffer::addBufferLineStatic("Execute Command: %s\n", this->inputLine.c_str());
163
164    if (this->inputLine.empty())
165      return false;
166
167    ShellCommand::execute(this->inputLine);
168
169    // removing the eventually added Entry (from scrolling) to the List
170    if (this->historyScrolling)
171    {
172      this->history.pop_back();
173      this->historyScrolling = false;
174    }
175
176    // adding the new Command to the History
177    if (history.empty() || history.back() != this->inputLine)
178      this->history.push_back(this->inputLine);
179    if (this->history.size() > this->historyLength)
180    {
181      this->history.pop_front();
182    }
183
184    this->flush();
185
186    return true;
187  }
188
189
190  /**
191   * @brief moves one entry up in the history.
192   */
193  void ShellInput::historyMoveUp()
194  {
195    if (!this->historyScrolling)
196    {
197      this->history.push_back(this->inputLine);
198      this->historyScrolling = true;
199      this->historyIT = --this->history.end();
200    }
201
202    if(this->historyIT != this->history.begin())
203    {
204      std::string prevElem = *(--this->historyIT);
205      /*if (prevElem == NULL) /// TODO STD
206        return;
207      else */
208      {
209        this->flush();
210        this->setInputText(prevElem);
211      }
212    }
213  }
214
215  /**
216   * @brief moves one entry down in the history
217   */
218  void ShellInput::historyMoveDown()
219  {
220    if (!this->historyScrolling)
221      return;
222    if (!this->history.empty() && this->historyIT != --this->history.end())
223    {
224      std::string nextElem = *(++this->historyIT);
225      /*    if (nextElem == NULL) /// TODO FIX STD
226        return;
227      else */
228      {
229        this->flush();
230        this->setInputText(nextElem);
231      }
232    }
233  }
234
235
236  /**
237   * @brief prints out some nice help about the Shell
238   */
239  void ShellInput::help(const std::string& className, const std::string& functionName)
240  {
241    printf("%s::%s\n", className.c_str(), functionName.c_str());
242
243    if (className.empty())
244    {
245      PRINT(0)("Help for the most important Shell-commands\n");
246      PRINT(0)("F1 - HELP; F2 - DEBUG; '`' - open/close shell\n");
247      PRINT(0)("input order:\n");
248      PRINT(0)("ClassName [objectName] function [parameter1, [parameter2 ...]]  or\n");
249      PRINT(0)("Alias [parameter]\n");
250      PRINT(0)("- Also try 'help className'");
251    }
252    else if (!className.empty() && functionName.empty())
253    {
254      ShellCommandClass::help(className);
255      //PRINTF(1)("%s::%s\n", className, functionName);
256    }
257  }
258
259  /**
260   * @brief ticks the ShellInput
261   * @param dt the time passed since the last update
262   */
263  void ShellInput::tick(float dt)
264  {
265    if (this->delayed > 0.0)
266      this->delayed -= dt;
267    else if (this->pressedKey != SDLK_FIRST )
268    {
269      this->delayed = this->repeatRate;
270      switch (this->pressedKey )
271      {
272        case SDLK_BACKSPACE:
273          this->removeCharacters(1);
274          break;
275        case SDLK_UP:
276          this->historyMoveUp();
277          break;
278        case SDLK_DOWN:
279          this->historyMoveDown();
280          break;
281        default:
282          {
283            if (likely(pressedKey < 127))
284              this->addCharacter(this->pressedKey);
285          }
286      }
287    }
288  }
289
290  /**
291   * @brief listens for some event
292   * @param event the Event happened
293   */
294  void ShellInput::process(const Event &event)
295  {
296    if (event.bPressed)
297    {
298      PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type).c_str());
299      if (event.type == SDLK_F1)
300        this->help();
301      else if (event.type == SDLK_F2)
302      {
303        ;//this->debug();
304      }
305      else if (event.type == SDLK_UP)
306      {
307        this->historyMoveUp();
308        this->pressedKey = event.type;
309      }
310      else if (event.type == SDLK_DOWN)
311      {
312        this->historyMoveDown();
313        this->pressedKey = event.type;
314      }
315      else if (event.type == SDLK_TAB)
316      {
317        this->completion.autoComplete(this->inputLine);
318        this->setText(this->inputLine);
319      }
320      else if (event.type == SDLK_BACKSPACE)
321      {
322        this->delayed = this->repeatDelay;
323        this->pressedKey = SDLK_BACKSPACE;
324        this->removeCharacters(1);
325      }
326      else if (event.type == SDLK_RETURN)
327      {
328        this->executeCommand();
329        this->pressedKey = event.type;
330      }
331      // any other keyboard key
332      else if (likely(event.type < 127))
333      {
334        this->addCharacter(event.x);
335        this->pressedKey = event.x;
336      }
337      this->delayed = this->repeatDelay;
338    }
339    else // if(!event.bPressed)
340    {
341      if (this->pressedKey == event.x || this->pressedKey == event.type)
342      {
343        this->pressedKey = 0;
344        this->delayed = 0.0;
345      }
346    }
347  }
348
349}
Note: See TracBrowser for help on using the repository browser.