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