Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/adm/src/lib/shell/shell_input.cc @ 10677

Last change on this file since 10677 was 10618, checked in by bknecht, 18 years ago

merged cleanup into trunk (only improvements)

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