Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/shell/shell_input.cc @ 9865

Last change on this file since 9865 was 9865, checked in by bensch, 18 years ago

orxonox/new_class_id: moving through the Text with a cursor works.

File size: 10.2 KB
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[5254]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[7374]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
[1853]17
[5178]18#include "shell_input.h"
[1853]19
[5181]20#include "shell_command.h"
[5639]21#include "shell_command_class.h"
[5179]22
23#include "debug.h"
24#include "compiler.h"
[5180]25#include "key_names.h"
[5179]26
[9692]27
[7374]28namespace OrxShell
[3365]29{
[7374]30  SHELL_COMMAND(help, ShellInput, help)
31  ->describe("retrieve some help about the input mode")
32  ->setAlias("help");
[4320]33
[9715]34  ObjectListDefinition(ShellInput);
[5180]35
[7374]36  /**
37   * @brief constructor
38   * this also generates a ShellCompletion automatically.
39  */
40  ShellInput::ShellInput ()
[7458]41      : Text ("")
[5309]42  {
[9692]43    this->registerObject(this, ShellInput::_objectList);
44
[7374]45    this->pressedKey = SDLK_FIRST;
[7341]46
[7729]47    this->historyIT = ShellInput::history.begin();
[7374]48    this->setHistoryLength(50);
49    this->historyScrolling = false;
50    this->delayed = 0;
51    this->setRepeatDelay(.3, .05);
[1853]52
[7868]53    // subscribe all keyboard commands to ES_SHELL
[7374]54    for (int i = 1; i < SDLK_LAST; i++)
55    {
[8339]56      //if (!this->isEventSubscribed(ES_SHELL, i))
[9865]57      this->subscribeEvent(ES_SHELL, i);
[7374]58    }
59    // unsubscribe unused TODO improve.
[7868]60    this->unsubscribeEvent(ES_SHELL, SDLK_BACKQUOTE);
61    this->unsubscribeEvent(ES_SHELL, SDLK_F12);
62    this->unsubscribeEvent(ES_SHELL, SDLK_PAGEUP);
63    this->unsubscribeEvent(ES_SHELL, SDLK_PAGEDOWN);
[7374]64  }
[5179]65
[7729]66  std::list<std::string> ShellInput::history;
67
[7374]68  /**
69   * @brief standard deconstructor
70   */
71  ShellInput::~ShellInput ()
[7729]72  {}
[5179]73
[7374]74  /**
75   * @brief sets the Repeate-delay and rate
76   * @param repeatDelay the Delay it takes, to repeate a key
77   * @param repeatRate the rate to repeate a pressed key
78   */
79  void ShellInput::setRepeatDelay(float repeatDelay, float repeatRate)
[5244]80  {
[7374]81    this->repeatDelay = repeatDelay;
82    this->repeatRate = repeatRate;
[5244]83  }
84
[7374]85  /**
86   * @brief deletes the InputLine
87   */
88  void ShellInput::flush()
[5244]89  {
[9865]90    this->inputLineBegin.clear();
91    this->inputLineEnd.clear();
92    this->clear();
[5244]93  }
94
[7374]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)
[5244]100  {
[9865]101    this->inputLineBegin = text;
102    this->inputLineEnd.clear();
103    this->setText(text);
[5244]104  }
105
[5179]106
[7374]107  /**
108   * @brief adds one character to the inputLine
109   * @param character the character to add to the inputLine
110   */
111  void ShellInput::addCharacter(char character)
112  {
113    if (this->historyScrolling)
114    {
115      this->history.pop_back();
116      this->historyScrolling = false;
117    }
[5179]118
[9865]119    this->inputLineBegin += character;
120    this->setText(this->inputLineBegin + this->inputLineEnd);
[7374]121  }
[5369]122
[7374]123  /**
124   * @brief adds multiple Characters to thr inputLine
125   * @param characters a \\0 terminated char-array to add to the InputLine
126   */
127  void ShellInput::addCharacters(const std::string& characters)
128  {
129    if (this->historyScrolling)
130    {
131      this->history.pop_back();
132      this->historyScrolling = false;
133    }
[5179]134
[9865]135    this->inputLineBegin += characters;
136    this->setText(this->inputLineBegin + this->inputLineEnd);
[5243]137  }
138
[7374]139  /**
140   * @brief removes characterCount characters from the InputLine
141   * @param characterCount the count of Characters to remove from the input Line
142   */
143  void ShellInput::removeCharacters(unsigned int characterCount)
[5243]144  {
[7374]145    if (this->historyScrolling)
146    {
147      this->history.pop_back();
148      this->historyScrolling = false;
149    }
[9865]150    if (this->inputLineBegin.size() < characterCount)
151      characterCount = this->inputLineBegin.size();
[7374]152
[9865]153    this->inputLineBegin.erase(this->inputLineBegin.size() - characterCount, this->inputLineBegin.size());
154    this->setText(this->inputLineBegin + this->inputLineEnd);
[5243]155  }
156
[7374]157  /**
158   * @brief executes the command stored in the inputLine
159   * @return true if the command was commited successfully, false otherwise
160   */
161  bool ShellInput::executeCommand()
162  {
[9865]163    if (this->getInput().empty())
[7374]164      return false;
[9865]165    DebugBuffer::addBufferLineStatic("Execute Command: %s\n", this->getInput().c_str());
[5179]166
[9865]167    ShellCommand::execute(this->getInput());
[5180]168
[7374]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
[9865]177    if (history.empty() || history.back() != this->getInput())
178      this->history.push_back(this->getInput());
[7374]179    if (this->history.size() > this->historyLength)
180    {
181      this->history.pop_front();
182    }
183
184    this->flush();
185
186    return true;
[5243]187  }
188
[7374]189
190  /**
191   * @brief moves one entry up in the history.
192   */
193  void ShellInput::historyMoveUp()
[5243]194  {
[7374]195    if (!this->historyScrolling)
[5785]196    {
[9865]197      this->history.push_back(this->getInput());
[7374]198      this->historyScrolling = true;
199      this->historyIT = --this->history.end();
[5785]200    }
[7374]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    }
[5243]213  }
214
[7374]215  /**
216   * @brief moves one entry down in the history
217   */
218  void ShellInput::historyMoveDown()
[5243]219  {
[7374]220    if (!this->historyScrolling)
[5785]221      return;
[7374]222    if (!this->history.empty() && this->historyIT != --this->history.end())
[5785]223    {
[7374]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      }
[5785]232    }
[5243]233  }
234
[9865]235  /**
236   * @brief moves the cursor chars Characters to the right.
237   * @param chars how much to move the cursor.
238   */
239  void ShellInput::moveCursor(int chars)
240  {
241    if (chars > 0)
242    {
243      printf("move cursor %d to the right\n", chars);
244      if (chars >= (int) this->inputLineEnd.size())
245        chars = inputLineEnd.size();
246      this->inputLineBegin += this->inputLineEnd.substr(0, chars);
247      this->inputLineEnd.erase(0, chars);
248    }
249    else if (chars < 0)
250    {
251      chars = -chars;
252      printf("move cursor %d to the left\n", chars);
[5243]253
[9865]254      if (chars >= (int) this->inputLineBegin.size())
255        chars = inputLineBegin.size();
256      this->inputLineEnd = this->inputLineBegin.substr(this->inputLineBegin.size() - chars) + this->inputLineEnd;
257      this->inputLineBegin.erase(this->inputLineBegin.size() - chars);
258    }
259  }
260
261
[7374]262  /**
263   * @brief prints out some nice help about the Shell
264   */
265  void ShellInput::help(const std::string& className, const std::string& functionName)
266  {
267    printf("%s::%s\n", className.c_str(), functionName.c_str());
[5207]268
[7374]269    if (className.empty())
270    {
[9865]271      PRINT(0)("== Help for the most important Shell-commands\n");
272      PRINT(0)("  F1 - HELP; F2 - DEBUG; '`' - open/close shell\n");
273      PRINT(0)("  input order:\n");
274      PRINT(0)("   ClassName [objectName] function [parameter1, [parameter2 ...]]  or\n");
275      PRINT(0)("   Alias [parameter]\n");
276      PRINT(0)("- Also try 'help className' or pushing 'TAB'\n");
[7374]277    }
278    else if (!className.empty() && functionName.empty())
279    {
280      ShellCommandClass::help(className);
281      //PRINTF(1)("%s::%s\n", className, functionName);
282    }
[5204]283  }
[5180]284
[7374]285  /**
286   * @brief ticks the ShellInput
287   * @param dt the time passed since the last update
288   */
289  void ShellInput::tick(float dt)
[5180]290  {
[7374]291    if (this->delayed > 0.0)
292      this->delayed -= dt;
293    else if (this->pressedKey != SDLK_FIRST )
[5786]294    {
[7374]295      this->delayed = this->repeatRate;
296      switch (this->pressedKey )
[5786]297      {
[7374]298        case SDLK_BACKSPACE:
[9865]299        this->removeCharacters(1);
300        break;
[7374]301        case SDLK_UP:
[9865]302        this->historyMoveUp();
303        break;
[7374]304        case SDLK_DOWN:
[9865]305        this->historyMoveDown();
306        break;
[7374]307        default:
[9865]308        {
309          if (likely(pressedKey < 127))
310            this->addCharacter(this->pressedKey);
311        }
[5786]312      }
313    }
[5180]314  }
315
[7374]316  /**
317   * @brief listens for some event
318   * @param event the Event happened
319   */
320  void ShellInput::process(const Event &event)
[5180]321  {
[7374]322    if (event.bPressed)
[5786]323    {
[7676]324      PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type).c_str());
[7374]325      if (event.type == SDLK_F1)
326        this->help();
327      else if (event.type == SDLK_F2)
328      {
329        ;//this->debug();
330      }
331      else if (event.type == SDLK_UP)
332      {
333        this->historyMoveUp();
334        this->pressedKey = event.type;
[7919]335        this->pressedEvent = event.type;
[7374]336      }
337      else if (event.type == SDLK_DOWN)
338      {
339        this->historyMoveDown();
340        this->pressedKey = event.type;
[7919]341        this->pressedEvent = event.type;
[7374]342      }
[9865]343      else if (event.type == SDLK_LEFT)
344      {
345        this->moveCursor(-1);
346        this->pressedKey = event.type;
347        this->pressedEvent = event.type;
348      }
349      else if (event.type == SDLK_RIGHT)
350      {
351        this->moveCursor(+1);
352        this->pressedKey = event.type;
353        this->pressedEvent = event.type;
354      }
[7374]355      else if (event.type == SDLK_TAB)
356      {
[9865]357        this->completion.autoComplete(this->inputLineBegin);
358        this->setText(this->getInput());
[7374]359      }
360      else if (event.type == SDLK_BACKSPACE)
361      {
362        this->delayed = this->repeatDelay;
363        this->pressedKey = SDLK_BACKSPACE;
[7919]364        this->pressedEvent = SDLK_BACKSPACE;
[7374]365        this->removeCharacters(1);
366      }
[9865]367      else if (event.type == SDLK_DELETE)
368      {
369        if (!this->inputLineEnd.empty())
370        {
371          this->inputLineEnd.erase(0, 1);
372          this->setText(this->getInput());
373        }
374      }
[7374]375      else if (event.type == SDLK_RETURN)
376      {
377        this->executeCommand();
378        this->pressedKey = event.type;
[7919]379        this->pressedEvent = event.type;
[7374]380      }
381      // any other keyboard key
382      else if (likely(event.type < 127))
383      {
384        this->addCharacter(event.x);
385        this->pressedKey = event.x;
[7858]386        this->pressedEvent = event.type;
[7374]387      }
[5180]388      this->delayed = this->repeatDelay;
389    }
[7374]390    else // if(!event.bPressed)
[5786]391    {
[7858]392      if (this->pressedEvent == event.type)
[7374]393      {
[7858]394        this->pressedEvent = 0;
[7374]395        this->pressedKey = 0;
396        this->delayed = 0.0;
397      }
[5786]398    }
[5180]399  }
[7374]400
[5180]401}
Note: See TracBrowser for help on using the repository browser.