Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Windows runs again without any segfaults
it seems, that windows links object files differently… and extremely strange

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