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, 15 years ago

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

File size: 10.2 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
23#include "debug.h"
24#include "compiler.h"
25#include "key_names.h"
26
27
28namespace OrxShell
29{
30  SHELL_COMMAND(help, ShellInput, help)
31  ->describe("retrieve some help about the input mode")
32  ->setAlias("help");
33
34  ObjectListDefinition(ShellInput);
35
36  /**
37   * @brief constructor
38   * this also generates a ShellCompletion automatically.
39  */
40  ShellInput::ShellInput ()
41      : Text ("")
42  {
43    this->registerObject(this, ShellInput::_objectList);
44
45    this->pressedKey = SDLK_FIRST;
46
47    this->historyIT = ShellInput::history.begin();
48    this->setHistoryLength(50);
49    this->historyScrolling = false;
50    this->delayed = 0;
51    this->setRepeatDelay(.3, .05);
52
53    // subscribe all keyboard commands to ES_SHELL
54    for (int i = 1; i < SDLK_LAST; i++)
55    {
56      //if (!this->isEventSubscribed(ES_SHELL, i))
57      this->subscribeEvent(ES_SHELL, i);
58    }
59    // unsubscribe unused TODO improve.
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);
64  }
65
66  std::list<std::string> ShellInput::history;
67
68  /**
69   * @brief standard deconstructor
70   */
71  ShellInput::~ShellInput ()
72  {}
73
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)
80  {
81    this->repeatDelay = repeatDelay;
82    this->repeatRate = repeatRate;
83  }
84
85  /**
86   * @brief deletes the InputLine
87   */
88  void ShellInput::flush()
89  {
90    this->inputLineBegin.clear();
91    this->inputLineEnd.clear();
92    this->clear();
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->inputLineBegin = text;
102    this->inputLineEnd.clear();
103    this->setText(text);
104  }
105
106
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    }
118
119    this->inputLineBegin += character;
120    this->setText(this->inputLineBegin + this->inputLineEnd);
121  }
122
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    }
134
135    this->inputLineBegin += characters;
136    this->setText(this->inputLineBegin + this->inputLineEnd);
137  }
138
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)
144  {
145    if (this->historyScrolling)
146    {
147      this->history.pop_back();
148      this->historyScrolling = false;
149    }
150    if (this->inputLineBegin.size() < characterCount)
151      characterCount = this->inputLineBegin.size();
152
153    this->inputLineBegin.erase(this->inputLineBegin.size() - characterCount, this->inputLineBegin.size());
154    this->setText(this->inputLineBegin + this->inputLineEnd);
155  }
156
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  {
163    if (this->getInput().empty())
164      return false;
165    DebugBuffer::addBufferLineStatic("Execute Command: %s\n", this->getInput().c_str());
166
167    ShellCommand::execute(this->getInput());
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->getInput())
178      this->history.push_back(this->getInput());
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->getInput());
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   * @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);
253
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
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());
268
269    if (className.empty())
270    {
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");
277    }
278    else if (!className.empty() && functionName.empty())
279    {
280      ShellCommandClass::help(className);
281      //PRINTF(1)("%s::%s\n", className, functionName);
282    }
283  }
284
285  /**
286   * @brief ticks the ShellInput
287   * @param dt the time passed since the last update
288   */
289  void ShellInput::tick(float dt)
290  {
291    if (this->delayed > 0.0)
292      this->delayed -= dt;
293    else if (this->pressedKey != SDLK_FIRST )
294    {
295      this->delayed = this->repeatRate;
296      switch (this->pressedKey )
297      {
298        case SDLK_BACKSPACE:
299        this->removeCharacters(1);
300        break;
301        case SDLK_UP:
302        this->historyMoveUp();
303        break;
304        case SDLK_DOWN:
305        this->historyMoveDown();
306        break;
307        default:
308        {
309          if (likely(pressedKey < 127))
310            this->addCharacter(this->pressedKey);
311        }
312      }
313    }
314  }
315
316  /**
317   * @brief listens for some event
318   * @param event the Event happened
319   */
320  void ShellInput::process(const Event &event)
321  {
322    if (event.bPressed)
323    {
324      PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type).c_str());
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;
335        this->pressedEvent = event.type;
336      }
337      else if (event.type == SDLK_DOWN)
338      {
339        this->historyMoveDown();
340        this->pressedKey = event.type;
341        this->pressedEvent = event.type;
342      }
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      }
355      else if (event.type == SDLK_TAB)
356      {
357        this->completion.autoComplete(this->inputLineBegin);
358        this->setText(this->getInput());
359      }
360      else if (event.type == SDLK_BACKSPACE)
361      {
362        this->delayed = this->repeatDelay;
363        this->pressedKey = SDLK_BACKSPACE;
364        this->pressedEvent = SDLK_BACKSPACE;
365        this->removeCharacters(1);
366      }
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      }
375      else if (event.type == SDLK_RETURN)
376      {
377        this->executeCommand();
378        this->pressedKey = event.type;
379        this->pressedEvent = event.type;
380      }
381      // any other keyboard key
382      else if (likely(event.type < 127))
383      {
384        this->addCharacter(event.x);
385        this->pressedKey = event.x;
386        this->pressedEvent = event.type;
387      }
388      this->delayed = this->repeatDelay;
389    }
390    else // if(!event.bPressed)
391    {
392      if (this->pressedEvent == event.type)
393      {
394        this->pressedEvent = 0;
395        this->pressedKey = 0;
396        this->delayed = 0.0;
397      }
398    }
399  }
400
401}
Note: See TracBrowser for help on using the repository browser.