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