Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Shell Input is totally out of shell.cc/h

File size: 5.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: ...
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "shell_input.h"
19
20#include "event_handler.h"
21
22#include "shell_command.h"
23#include "debug.h"
24#include "list.h"
25#include "compiler.h"
26#include "stdlibincl.h"
27#include "key_names.h"
28
29
30using namespace std;
31
32
33/**
34 * standard constructor
35 * @todo this constructor is not jet implemented - do it
36*/
37ShellInput::ShellInput ()
38{
39  this->pressedKey = SDLK_FIRST;
40
41  this->inputLine = new char[1];
42  this->inputLine[0] = '\0';
43  this->inputHistory = new tList<char>;
44  this->delayed = 0;
45  this->setRepeatDelay(.3, .05);
46
47  // subscribe all keyboard commands to ES_SEHLL
48  EventHandler* evh = EventHandler::getInstance();
49  for (int i = 1; i < SDLK_LAST; i++)
50    evh->subscribe(this, ES_SHELL, i);
51
52}
53
54/**
55 * standard deconstructor
56*/
57ShellInput::~ShellInput ()
58{
59  // delete what has to be deleted here
60}
61
62/**
63 * sets the Repeate-delay and rate
64 * @param repeatDelay the Delay it takes, to repeate a key
65 * @param repeatRate the rate to repeate a pressed key
66 */
67void ShellInput::setRepeatDelay(float repeatDelay, float repeatRate)
68{
69  this->repeatDelay = repeatDelay;
70  this->repeatRate = repeatRate;
71
72}
73
74/**
75 * deletes the InputLine
76 */
77void ShellInput::flush()
78{
79  if (likely(this->inputLine != NULL))
80  {
81    delete[] this->inputLine;
82  }
83  this->inputLine = new char[1];
84  *this->inputLine = '\0';
85  this->setText(this->inputLine, true);
86}
87
88/**
89 * adds one character to the inputLine
90 * @param character the character to add to the inputLine
91 */
92void ShellInput::addCharacter(char character)
93{
94  char* addCharLine = new char[strlen(inputLine)+2];
95
96  sprintf(addCharLine, "%s%c", this->inputLine, character);
97  delete this->inputLine;
98  this->inputLine = addCharLine;
99  this->setText(inputLine, true);
100}
101
102/**
103 * adds multiple Characters to thr inputLine
104 * @param characters a \\0 terminated char-array to add to the InputLine
105 */
106void ShellInput::addCharacters(const char* characters)
107{
108  char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1];
109
110  sprintf(addCharLine, "%s%s", this->inputLine, characters);
111  delete this->inputLine;
112  this->inputLine = addCharLine;
113  this->setText(inputLine, true);
114}
115
116/**
117 * removes characterCount characters from the InputLine
118 * @param characterCount the count of Characters to remove from the input Line
119 */
120void ShellInput::removeCharacters(unsigned int characterCount)
121{
122  if (strlen(this->inputLine) == 0)
123    return;
124
125  if (characterCount > strlen(this->inputLine))
126    characterCount = strlen(this->inputLine);
127
128  char* removeCharLine = new char[strlen(inputLine)-characterCount+1];
129
130  strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount);
131  removeCharLine[strlen(inputLine)-characterCount] = '\0';
132  delete this->inputLine;
133  this->inputLine = removeCharLine;
134  this->setText(inputLine, true);
135}
136
137/**
138 * executes the command stored in the inputLine
139 * @return true if the command was commited successfully, false otherwise
140 */
141bool ShellInput::executeCommand()
142{
143  ShellBuffer::addBufferLineStatic("Execute Command: %s\n", this->inputLine);
144
145  char* newCommand = new char[strlen(this->inputLine)+1];
146  strcpy(newCommand, this->inputLine);
147  this->inputHistory->add(newCommand);
148
149  ShellCommandBase::execute(this->inputLine);
150
151  this->flush();
152
153  return false;
154}
155
156
157/**
158 * prints out some nice help about the Shell
159 */
160void ShellInput::help() const
161{
162  PRINT(0)("Help for the most important Shell-commands\n");
163  PRINT(0)("F1 - HELP; F2 - DEBUG; ` - open/close shell\n");
164  PRINT(0)("input order:\n");
165  PRINT(0)("ClassName::objectName function [parameter1, [parameter2 ...]]  or\n");
166  PRINT(0)("Command [parameter]\n");
167}
168
169void ShellInput::tick(float dt)
170{
171  if (this->delayed > 0.0)
172    this->delayed -= dt;
173  else if (this->pressedKey != SDLK_FIRST )
174  {
175    this->delayed = this->repeatRate;
176    if (this->pressedKey == SDLK_BACKSPACE)
177      this->removeCharacters(1);
178    else if (pressedKey < 127)
179      this->addCharacter(this->pressedKey);
180  }
181}
182
183/**
184 * listens for some event
185 * @param event the Event happened
186 */
187void ShellInput::process(const Event &event)
188{
189  if (event.bPressed)
190  {
191    PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type));
192    if (event.type == SDLK_F1)
193      this->help();
194    else if (event.type == SDLK_F2)
195      this->debug();
196    else if (event.type == SDLK_TAB)
197      ;//this->autoComplete();
198    else if (event.type == SDLK_BACKSPACE)
199    {
200      this->delayed = this->repeatDelay;
201      this->pressedKey = SDLK_BACKSPACE;
202      this->removeCharacters(1);
203    }
204    else if (event.type == SDLK_RETURN)
205      this->executeCommand();
206    /*
207    else if (event.type == SDLK_UP)
208    {
209//      this->flushInputLine();
210    tIterator<char>* iterator = this->commandList->getIterator();
211    char* command = iterator->lastElement();
212    while (command)
213    {
214    if (!strcmp (command, inputLine))
215    {
216    inputLine = iterator->prevElement();
217    return;
218  }
219    command = iterator->prevElement();
220  }
221    inputLine = iterator->lastElement();
222  }
223    */
224    else if (likely(event.type < 127))
225    {
226      Uint8 *keystate = SDL_GetKeyState(NULL);
227      this->delayed = this->repeatDelay;
228      if (unlikely( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] ))
229      {
230        this->pressedKey = event.type-32;
231        this->addCharacter(event.type-32);
232      }
233      else
234      {
235        this->pressedKey = event.type;
236        this->addCharacter(event.type);
237      }
238    }
239  }
240  else // if(!event.bPressed)
241  {
242    if (this->pressedKey == event.type || (this->pressedKey == event.type - 32))
243    {
244      this->pressedKey = SDLK_FIRST;
245      this->delayed = 0.0;
246    }
247  }
248}
Note: See TracBrowser for help on using the repository browser.