Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model_old/src/lib/shell/shell_input.cc @ 8471

Last change on this file since 8471 was 7343, checked in by bensch, 19 years ago

oroxnox/trunk: ShellInput and ShellCompletion more c++-like

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