Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7912 was 7868, checked in by bensch, 19 years ago

trunk: merge Check in the Event-changes:
r7867 | bensch | 2006-05-26 13:19:46 +0200 (Fri, 26 May 2006) | 1 line

Events better subscribed


r7866 | bensch | 2006-05-26 13:11:10 +0200 (Fri, 26 May 2006) | 1 line

Events are subscribed at the EventListener, and not the EventHandler

File size: 8.5 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
27namespace OrxShell
28{
29  SHELL_COMMAND(help, ShellInput, help)
30  ->describe("retrieve some help about the input mode")
31  ->setAlias("help");
32
33
34  /**
35   * @brief constructor
36   * this also generates a ShellCompletion automatically.
37  */
38  ShellInput::ShellInput ()
39      : Text ("")
40  {
41    this->pressedKey = SDLK_FIRST;
42    this->setClassID(CL_SHELL_INPUT, "ShellInput");
43
44    this->inputLine = "";
45    this->historyIT = ShellInput::history.begin();
46    this->setHistoryLength(50);
47    this->historyScrolling = false;
48    this->delayed = 0;
49    this->setRepeatDelay(.3, .05);
50
51    // subscribe all keyboard commands to ES_SHELL
52    for (int i = 1; i < SDLK_LAST; i++)
53    {
54      if (!this->isEventSubscribed(ES_SHELL, i))
55        this->subscribeEvent(ES_SHELL, i);
56    }
57    // unsubscribe unused TODO improve.
58    this->unsubscribeEvent(ES_SHELL, SDLK_BACKQUOTE);
59    this->unsubscribeEvent(ES_SHELL, SDLK_F12);
60    this->unsubscribeEvent(ES_SHELL, SDLK_PAGEUP);
61    this->unsubscribeEvent(ES_SHELL, SDLK_PAGEDOWN);
62
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->inputLine.clear();
90    this->setText(this->inputLine);
91  }
92
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)
98  {
99    this->inputLine = text;
100    this->setText(this->inputLine);
101  }
102
103
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    }
115
116    this->inputLine += character;
117    this->setText(this->inputLine);
118  }
119
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    }
131
132    this->inputLine += characters;
133    this->setText(this->inputLine);
134  }
135
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)
141  {
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);
152  }
153
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());
161
162    if (this->inputLine.empty())
163      return false;
164
165    ShellCommand::execute(this->inputLine);
166
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
175    if (history.empty() || history.back() != this->inputLine)
176      this->history.push_back(this->inputLine);
177    if (this->history.size() > this->historyLength)
178    {
179      this->history.pop_front();
180    }
181
182    this->flush();
183
184    return true;
185  }
186
187
188  /**
189   * @brief moves one entry up in the history.
190   */
191  void ShellInput::historyMoveUp()
192  {
193    if (!this->historyScrolling)
194    {
195      this->history.push_back(this->inputLine);
196      this->historyScrolling = true;
197      this->historyIT = --this->history.end();
198    }
199
200    if(this->historyIT != this->history.begin())
201    {
202      std::string prevElem = *(--this->historyIT);
203      /*if (prevElem == NULL) /// TODO STD
204        return;
205      else */
206      {
207        this->flush();
208        this->setInputText(prevElem);
209      }
210    }
211  }
212
213  /**
214   * @brief moves one entry down in the history
215   */
216  void ShellInput::historyMoveDown()
217  {
218    if (!this->historyScrolling)
219      return;
220    if (!this->history.empty() && this->historyIT != --this->history.end())
221    {
222      std::string nextElem = *(++this->historyIT);
223      /*    if (nextElem == NULL) /// TODO FIX STD
224        return;
225      else */
226      {
227        this->flush();
228        this->setInputText(nextElem);
229      }
230    }
231  }
232
233
234  /**
235   * @brief prints out some nice help about the Shell
236   */
237  void ShellInput::help(const std::string& className, const std::string& functionName)
238  {
239    printf("%s::%s\n", className.c_str(), functionName.c_str());
240
241    if (className.empty())
242    {
243      PRINT(0)("Help for the most important Shell-commands\n");
244      PRINT(0)("F1 - HELP; F2 - DEBUG; '`' - open/close shell\n");
245      PRINT(0)("input order:\n");
246      PRINT(0)("ClassName [objectName] function [parameter1, [parameter2 ...]]  or\n");
247      PRINT(0)("Alias [parameter]\n");
248      PRINT(0)("- Also try 'help className'");
249    }
250    else if (!className.empty() && functionName.empty())
251    {
252      ShellCommandClass::help(className);
253      //PRINTF(1)("%s::%s\n", className, functionName);
254    }
255  }
256
257  /**
258   * @brief ticks the ShellInput
259   * @param dt the time passed since the last update
260   */
261  void ShellInput::tick(float dt)
262  {
263    if (this->delayed > 0.0)
264      this->delayed -= dt;
265    else if (this->pressedKey != SDLK_FIRST )
266    {
267      this->delayed = this->repeatRate;
268      switch (this->pressedKey )
269      {
270        case SDLK_BACKSPACE:
271          this->removeCharacters(1);
272          break;
273        case SDLK_UP:
274          this->historyMoveUp();
275          break;
276        case SDLK_DOWN:
277          this->historyMoveDown();
278          break;
279        default:
280          {
281            if (likely(pressedKey < 127))
282              this->addCharacter(this->pressedKey);
283          }
284      }
285    }
286  }
287
288  /**
289   * @brief listens for some event
290   * @param event the Event happened
291   */
292  void ShellInput::process(const Event &event)
293  {
294    if (event.bPressed)
295    {
296      PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type).c_str());
297      if (event.type == SDLK_F1)
298        this->help();
299      else if (event.type == SDLK_F2)
300      {
301        ;//this->debug();
302      }
303      else if (event.type == SDLK_UP)
304      {
305        this->historyMoveUp();
306        this->pressedKey = event.type;
307      }
308      else if (event.type == SDLK_DOWN)
309      {
310        this->historyMoveDown();
311        this->pressedKey = event.type;
312      }
313      else if (event.type == SDLK_TAB)
314      {
315        this->completion.autoComplete(this->inputLine);
316        this->setText(this->inputLine);
317      }
318      else if (event.type == SDLK_BACKSPACE)
319      {
320        this->delayed = this->repeatDelay;
321        this->pressedKey = SDLK_BACKSPACE;
322        this->removeCharacters(1);
323      }
324      else if (event.type == SDLK_RETURN)
325      {
326        this->executeCommand();
327        this->pressedKey = event.type;
328      }
329      // any other keyboard key
330      else if (likely(event.type < 127))
331      {
332        this->addCharacter(event.x);
333        this->pressedKey = event.x;
334        this->pressedEvent = event.type;
335      }
336      this->delayed = this->repeatDelay;
337    }
338    else // if(!event.bPressed)
339    {
340      if (this->pressedEvent == event.type)
341      {
342        this->pressedEvent = 0;
343        this->pressedKey = 0;
344        this->delayed = 0.0;
345      }
346    }
347  }
348
349}
Note: See TracBrowser for help on using the repository browser.