Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: SubString::split algorithm revisited: now it Splits Strings where Delimitters are, but it ereases the Neighbours of those delimiters, if you want it
also there is now a way to have empty Entries.

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