[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: |
---|
[5068] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[5068] | 18 | #include "shell.h" |
---|
[5129] | 19 | #include "shell_command.h" |
---|
[5175] | 20 | #include "shell_buffer.h" |
---|
[1853] | 21 | |
---|
[5175] | 22 | |
---|
[5072] | 23 | #include "text_engine.h" |
---|
| 24 | #include "list.h" |
---|
[5093] | 25 | #include "graphics_engine.h" |
---|
| 26 | #include "event_handler.h" |
---|
[5129] | 27 | #include "debug.h" |
---|
[5113] | 28 | #include "class_list.h" |
---|
| 29 | |
---|
| 30 | #include "key_names.h" |
---|
[5075] | 31 | #include <stdarg.h> |
---|
| 32 | #include <stdio.h> |
---|
| 33 | |
---|
[1856] | 34 | using namespace std; |
---|
[1853] | 35 | |
---|
[5164] | 36 | SHELL_COMMAND(clear, Shell, clear)->describe("Clears the shell from unwanted lines (empties all buffers)"); |
---|
[5172] | 37 | SHELL_COMMAND(deactivate, Shell, deactivate)->describe("Deactivates the Shell. (moves it into background)"); |
---|
[1856] | 38 | |
---|
[3245] | 39 | /** |
---|
[4838] | 40 | * standard constructor |
---|
[5068] | 41 | */ |
---|
| 42 | Shell::Shell () |
---|
[3365] | 43 | { |
---|
[5072] | 44 | this->setClassID(CL_SHELL, "Shell"); |
---|
| 45 | this->setName("Shell"); |
---|
| 46 | |
---|
[5175] | 47 | this->setAbsCoor2D(3, -400); |
---|
| 48 | this->textSize = 15; |
---|
| 49 | this->lineSpacing = 5; |
---|
[5113] | 50 | this->bActive = false; |
---|
[5072] | 51 | |
---|
[5129] | 52 | |
---|
[5175] | 53 | // BUFFER |
---|
| 54 | this->bufferText = NULL; |
---|
| 55 | this->setBufferDisplaySize(10); |
---|
[5080] | 56 | |
---|
[5175] | 57 | // INPUT LINE |
---|
[5095] | 58 | this->delayed = 0; |
---|
[5097] | 59 | this->setRepeatDelay(.3, .05); |
---|
[5095] | 60 | this->pressedKey = SDLK_FIRST; |
---|
[5074] | 61 | |
---|
[5113] | 62 | this->inputLineText = NULL; |
---|
[5093] | 63 | this->inputLine = new char[1]; |
---|
| 64 | this->inputLine[0] = '\0'; |
---|
[5175] | 65 | this->inputHistory = new tList<char>; |
---|
| 66 | //this->commandList = new tList<ShellCommand>; |
---|
[5093] | 67 | |
---|
[5113] | 68 | this->rebuildText(); |
---|
| 69 | this->completionList = NULL; |
---|
[5093] | 70 | |
---|
[5113] | 71 | // EVENT-Handler subscription of '`' to all States, and all other keyboard commands to ES_SEHLL |
---|
[5093] | 72 | EventHandler* evh = EventHandler::getInstance(); |
---|
[5096] | 73 | evh->subscribe(this, ES_ALL, SDLK_BACKQUOTE); |
---|
[5119] | 74 | for (int i = 1; i < SDLK_LAST; i++) |
---|
[5095] | 75 | evh->subscribe(this, ES_SHELL, i); |
---|
[5068] | 76 | } |
---|
[4320] | 77 | |
---|
[5068] | 78 | Shell* Shell::singletonRef = NULL; |
---|
[1853] | 79 | |
---|
[3245] | 80 | /** |
---|
[4838] | 81 | * standard deconstructor |
---|
[5068] | 82 | */ |
---|
| 83 | Shell::~Shell () |
---|
[3543] | 84 | { |
---|
[5099] | 85 | // delete the displayable Buffers |
---|
[5080] | 86 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
| 87 | delete this->bufferText[i]; |
---|
[5113] | 88 | delete[] this->bufferText; |
---|
[5093] | 89 | |
---|
[5099] | 90 | // delete the inputLine |
---|
[5080] | 91 | delete this->inputLineText; |
---|
[5093] | 92 | delete this->inputLine; |
---|
[5079] | 93 | |
---|
[5068] | 94 | Shell::singletonRef = NULL; |
---|
[3543] | 95 | } |
---|
[5068] | 96 | |
---|
[5119] | 97 | /** |
---|
| 98 | * activates the shell |
---|
| 99 | * |
---|
| 100 | * This also feeds the Last few lines from the main buffers into the displayBuffer |
---|
| 101 | */ |
---|
[5113] | 102 | void Shell::activate() |
---|
| 103 | { |
---|
| 104 | if (this->bActive == true) |
---|
| 105 | PRINTF(3)("The shell is already active\n"); |
---|
| 106 | this->bActive = true; |
---|
| 107 | |
---|
| 108 | EventHandler::getInstance()->setState(ES_SHELL); |
---|
| 109 | this->setRelCoorSoft2D(0, 0, 1, 5); |
---|
[5118] | 110 | |
---|
[5175] | 111 | ShellBuffer::getInstance()->getBufferIterator()->lastElement(); |
---|
[5118] | 112 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
[5175] | 113 | this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), true); |
---|
[5113] | 114 | } |
---|
| 115 | |
---|
[5119] | 116 | /** |
---|
| 117 | * deactiveates the Shell. |
---|
| 118 | */ |
---|
[5113] | 119 | void Shell::deactivate() |
---|
| 120 | { |
---|
| 121 | if (this->bActive == false) |
---|
| 122 | PRINTF(3)("The shell is already inactive\n"); |
---|
| 123 | this->bActive = false; |
---|
| 124 | |
---|
| 125 | EventHandler::getInstance()->setState(ES_GAME); |
---|
| 126 | this->setRelCoorSoft2D(0, -400, 1, 5); |
---|
[5118] | 127 | |
---|
[5175] | 128 | ShellBuffer::getInstance()->getBufferIterator()->lastElement(); |
---|
[5157] | 129 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
[5175] | 130 | this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), false); |
---|
[5113] | 131 | } |
---|
| 132 | |
---|
[5158] | 133 | |
---|
[5119] | 134 | /** |
---|
| 135 | * sets the size of the text and spacing |
---|
| 136 | * @param textSize the size of the Text in Pixels |
---|
| 137 | * @param lineSpacing the size of the Spacing between two lines in pixels |
---|
| 138 | * |
---|
| 139 | * this also rebuilds the entire Text, inputLine and displayBuffer, |
---|
| 140 | * to be accurate again. |
---|
| 141 | */ |
---|
[5113] | 142 | void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing) |
---|
| 143 | { |
---|
| 144 | this->textSize = textSize; |
---|
| 145 | this->lineSpacing = lineSpacing; |
---|
| 146 | |
---|
| 147 | this->rebuildText(); |
---|
| 148 | } |
---|
| 149 | |
---|
[5127] | 150 | /** |
---|
| 151 | * rebuilds the Text's |
---|
| 152 | * |
---|
| 153 | * use this function, if you changed the Font/Size or something else. |
---|
| 154 | */ |
---|
[5113] | 155 | void Shell::rebuildText() |
---|
| 156 | { |
---|
| 157 | if (this->inputLineText == NULL) |
---|
| 158 | delete this->inputLineText; |
---|
[5122] | 159 | this->inputLineText = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC); |
---|
[5121] | 160 | this->inputLineText->setColor(1, 0, 0); |
---|
[5113] | 161 | this->inputLineText->setAlignment(TEXT_ALIGN_LEFT); |
---|
| 162 | this->inputLineText->setText(NULL); |
---|
| 163 | this->inputLineText->setParent2D(this); |
---|
[5124] | 164 | this->inputLineText->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize); |
---|
[5113] | 165 | |
---|
| 166 | this->setBufferDisplaySize(this->bufferDisplaySize); |
---|
| 167 | } |
---|
| 168 | |
---|
[5074] | 169 | /** |
---|
| 170 | * sets The count of Lines to display in the buffer. |
---|
| 171 | * @param bufferDisplaySize the count of lines to display in the Shell-Buffer. |
---|
| 172 | */ |
---|
[5072] | 173 | void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) |
---|
| 174 | { |
---|
[5080] | 175 | if (this->bufferText != NULL) |
---|
[5072] | 176 | { |
---|
[5080] | 177 | for (unsigned int i = 0; i < this->bufferDisplaySize; i++) |
---|
| 178 | delete this->bufferText[i]; |
---|
[5113] | 179 | delete[] this->bufferText; |
---|
[5072] | 180 | } |
---|
[5080] | 181 | |
---|
| 182 | this->bufferText = new Text*[bufferDisplaySize]; |
---|
| 183 | for (unsigned int i = 0; i < bufferDisplaySize; i++) |
---|
[5072] | 184 | { |
---|
[5122] | 185 | this->bufferText[i] = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC); |
---|
[5121] | 186 | this->bufferText[i]->setColor(1, 0, 0); |
---|
[5080] | 187 | this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT); |
---|
[5120] | 188 | this->bufferText[i]->setRelCoor2D(calculateLinePosition(i)); |
---|
[5080] | 189 | this->bufferText[i]->setText(NULL); |
---|
[5089] | 190 | this->bufferText[i]->setParent2D(this); |
---|
[5072] | 191 | } |
---|
[5113] | 192 | this->bufferDisplaySize = bufferDisplaySize; |
---|
[5111] | 193 | |
---|
[5113] | 194 | this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1); |
---|
[5072] | 195 | } |
---|
[5068] | 196 | |
---|
| 197 | /** |
---|
| 198 | * deletes all the Buffers |
---|
| 199 | */ |
---|
[5175] | 200 | void Shell::flush() |
---|
[5068] | 201 | { |
---|
[5072] | 202 | // remove all chars from the BufferTexts. |
---|
[5080] | 203 | if (this->bufferText) |
---|
[5125] | 204 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
[5080] | 205 | { |
---|
[5125] | 206 | this->bufferText[i]->setText(NULL, true); |
---|
[5080] | 207 | } |
---|
[5068] | 208 | |
---|
[5072] | 209 | |
---|
[5175] | 210 | // BUFFER FLUSHING |
---|
[5068] | 211 | } |
---|
| 212 | |
---|
| 213 | /** |
---|
[5118] | 214 | * prints out some text to the input-buffers |
---|
| 215 | * @param text the text to output. |
---|
| 216 | */ |
---|
| 217 | void Shell::printToDisplayBuffer(const char* text) |
---|
| 218 | { |
---|
| 219 | if(likely(bufferText != NULL)) |
---|
| 220 | { |
---|
| 221 | Text* lastText = this->bufferText[this->bufferDisplaySize-1]; |
---|
[5113] | 222 | |
---|
[5118] | 223 | Text* swapText; |
---|
| 224 | Text* moveText = this->bufferText[0]; |
---|
[5120] | 225 | this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10); |
---|
[5118] | 226 | for (unsigned int i = 1; i < this->bufferDisplaySize; i++) |
---|
| 227 | { |
---|
| 228 | if ( i < this->bufferDisplaySize-1) |
---|
[5120] | 229 | this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5); |
---|
[5118] | 230 | swapText = this->bufferText[i]; |
---|
| 231 | this ->bufferText[i] = moveText; |
---|
| 232 | moveText = swapText; |
---|
| 233 | } |
---|
[5120] | 234 | lastText->setRelCoor2D(this->calculateLinePosition(0)); |
---|
[5118] | 235 | this->bufferText[0] = lastText; |
---|
| 236 | |
---|
[5122] | 237 | this->bufferText[0]->setText(text, true); |
---|
[5118] | 238 | } |
---|
[5068] | 239 | } |
---|
| 240 | |
---|
| 241 | /** |
---|
| 242 | * deletes the InputLine |
---|
| 243 | */ |
---|
| 244 | void Shell::flushInputLine() |
---|
| 245 | { |
---|
[5072] | 246 | if (likely(this->inputLine != NULL)) |
---|
| 247 | { |
---|
[5125] | 248 | delete[] this->inputLine; |
---|
[5072] | 249 | } |
---|
| 250 | this->inputLine = new char[1]; |
---|
| 251 | *this->inputLine = '\0'; |
---|
[5125] | 252 | this->inputLineText->setText(this->inputLine, true); |
---|
[5068] | 253 | } |
---|
| 254 | |
---|
| 255 | /** |
---|
| 256 | * adds one character to the inputLine |
---|
| 257 | * @param character the character to add to the inputLine |
---|
| 258 | */ |
---|
| 259 | void Shell::addCharacter(char character) |
---|
| 260 | { |
---|
[5072] | 261 | char* addCharLine = new char[strlen(inputLine)+2]; |
---|
| 262 | |
---|
| 263 | sprintf(addCharLine, "%s%c", this->inputLine, character); |
---|
| 264 | delete this->inputLine; |
---|
| 265 | this->inputLine = addCharLine; |
---|
[5123] | 266 | this->inputLineText->setText(inputLine, true); |
---|
[5068] | 267 | } |
---|
| 268 | |
---|
| 269 | /** |
---|
| 270 | * adds multiple Characters to thr inputLine |
---|
[5166] | 271 | * @param characters a \\0 terminated char-array to add to the InputLine |
---|
[5068] | 272 | */ |
---|
| 273 | void Shell::addCharacters(const char* characters) |
---|
| 274 | { |
---|
[5072] | 275 | char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1]; |
---|
| 276 | |
---|
| 277 | sprintf(addCharLine, "%s%s", this->inputLine, characters); |
---|
| 278 | delete this->inputLine; |
---|
| 279 | this->inputLine = addCharLine; |
---|
[5157] | 280 | this->inputLineText->setText(inputLine, true); |
---|
[5068] | 281 | } |
---|
| 282 | |
---|
| 283 | /** |
---|
| 284 | * removes characterCount characters from the InputLine |
---|
| 285 | * @param characterCount the count of Characters to remove from the input Line |
---|
| 286 | */ |
---|
| 287 | void Shell::removeCharacters(unsigned int characterCount) |
---|
| 288 | { |
---|
[5093] | 289 | if (strlen(this->inputLine) == 0) |
---|
| 290 | return; |
---|
| 291 | |
---|
[5072] | 292 | if (characterCount > strlen(this->inputLine)) |
---|
| 293 | characterCount = strlen(this->inputLine); |
---|
| 294 | |
---|
| 295 | char* removeCharLine = new char[strlen(inputLine)-characterCount+1]; |
---|
| 296 | |
---|
| 297 | strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount); |
---|
[5093] | 298 | removeCharLine[strlen(inputLine)-characterCount] = '\0'; |
---|
[5072] | 299 | delete this->inputLine; |
---|
| 300 | this->inputLine = removeCharLine; |
---|
[5157] | 301 | this->inputLineText->setText(inputLine, true); |
---|
[5068] | 302 | } |
---|
| 303 | |
---|
[5096] | 304 | /** |
---|
| 305 | * executes the command stored in the inputLine |
---|
| 306 | * @return true if the command was commited successfully, false otherwise |
---|
| 307 | */ |
---|
| 308 | bool Shell::executeCommand() |
---|
| 309 | { |
---|
[5175] | 310 | ShellBuffer::addBufferLineStatic("Execute Command: %s\n", this->inputLine); |
---|
[5125] | 311 | |
---|
[5129] | 312 | char* newCommand = new char[strlen(this->inputLine)+1]; |
---|
| 313 | strcpy(newCommand, this->inputLine); |
---|
| 314 | this->inputHistory->add(newCommand); |
---|
| 315 | |
---|
[5135] | 316 | ShellCommandBase::execute(this->inputLine); |
---|
[5125] | 317 | |
---|
| 318 | this->flushInputLine(); |
---|
| 319 | |
---|
[5096] | 320 | return false; |
---|
| 321 | } |
---|
| 322 | |
---|
[5166] | 323 | /** |
---|
| 324 | * clears the Shell (empties all buffers) |
---|
| 325 | */ |
---|
[5130] | 326 | void Shell::clear() |
---|
| 327 | { |
---|
[5175] | 328 | this->flush(); |
---|
| 329 | ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL); |
---|
[5130] | 330 | } |
---|
| 331 | |
---|
[5097] | 332 | /** |
---|
| 333 | * sets the Repeate-delay and rate |
---|
| 334 | * @param repeatDelay the Delay it takes, to repeate a key |
---|
| 335 | * @param repeatRate the rate to repeate a pressed key |
---|
| 336 | */ |
---|
| 337 | void Shell::setRepeatDelay(float repeatDelay, float repeatRate) |
---|
| 338 | { |
---|
| 339 | this->repeatDelay = repeatDelay; |
---|
| 340 | this->repeatRate = repeatRate; |
---|
| 341 | |
---|
| 342 | } |
---|
| 343 | |
---|
[5069] | 344 | /** |
---|
| 345 | * listens for some event |
---|
| 346 | * @param event the Event happened |
---|
| 347 | */ |
---|
| 348 | void Shell::process(const Event &event) |
---|
| 349 | { |
---|
[5093] | 350 | if (event.bPressed) |
---|
| 351 | { |
---|
[5141] | 352 | PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type)); |
---|
[5093] | 353 | if (event.type == SDLK_BACKQUOTE) |
---|
| 354 | { |
---|
| 355 | if (EventHandler::getInstance()->getState() == ES_GAME) |
---|
[5113] | 356 | this->activate(); |
---|
[5093] | 357 | else |
---|
[5113] | 358 | this->deactivate(); |
---|
[5093] | 359 | } |
---|
[5119] | 360 | else if (event.type == SDLK_F1) |
---|
| 361 | this->help(); |
---|
| 362 | else if (event.type == SDLK_F2) |
---|
| 363 | this->debug(); |
---|
[5093] | 364 | else if (event.type == SDLK_TAB) |
---|
| 365 | this->autoComplete(); |
---|
| 366 | else if (event.type == SDLK_BACKSPACE) |
---|
[5095] | 367 | { |
---|
| 368 | this->delayed = this->repeatDelay; |
---|
| 369 | this->pressedKey = SDLK_BACKSPACE; |
---|
[5093] | 370 | this->removeCharacters(1); |
---|
[5095] | 371 | } |
---|
[5096] | 372 | else if (event.type == SDLK_RETURN) |
---|
| 373 | this->executeCommand(); |
---|
[5129] | 374 | /* |
---|
| 375 | else if (event.type == SDLK_UP) |
---|
| 376 | { |
---|
| 377 | // this->flushInputLine(); |
---|
| 378 | tIterator<char>* iterator = this->commandList->getIterator(); |
---|
| 379 | char* command = iterator->lastElement(); |
---|
| 380 | while (command) |
---|
| 381 | { |
---|
| 382 | if (!strcmp (command, inputLine)) |
---|
| 383 | { |
---|
| 384 | inputLine = iterator->prevElement(); |
---|
| 385 | return; |
---|
| 386 | } |
---|
| 387 | command = iterator->prevElement(); |
---|
| 388 | } |
---|
| 389 | inputLine = iterator->lastElement(); |
---|
| 390 | } |
---|
| 391 | */ |
---|
[5097] | 392 | else if (likely(event.type < 127)) |
---|
[5095] | 393 | { |
---|
[5128] | 394 | Uint8 *keystate = SDL_GetKeyState(NULL); |
---|
[5095] | 395 | this->delayed = this->repeatDelay; |
---|
[5128] | 396 | if (unlikely( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] )) |
---|
| 397 | { |
---|
| 398 | this->pressedKey = event.type-32; |
---|
| 399 | this->addCharacter(event.type-32); |
---|
| 400 | } |
---|
| 401 | else |
---|
| 402 | { |
---|
| 403 | this->pressedKey = event.type; |
---|
| 404 | this->addCharacter(event.type); |
---|
| 405 | } |
---|
[5095] | 406 | } |
---|
[5093] | 407 | } |
---|
[5095] | 408 | else // if(!event.bPressed) |
---|
| 409 | { |
---|
[5128] | 410 | if (this->pressedKey == event.type || (this->pressedKey == event.type - 32)) |
---|
[5113] | 411 | { |
---|
[5095] | 412 | this->pressedKey = SDLK_FIRST; |
---|
[5113] | 413 | this->delayed = 0.0; |
---|
| 414 | } |
---|
[5095] | 415 | } |
---|
[5069] | 416 | } |
---|
| 417 | |
---|
[5068] | 418 | /** |
---|
| 419 | * ticks the Shell for dt Seconds |
---|
| 420 | * @param dt the elapsed time since the last tick(); |
---|
| 421 | */ |
---|
[5095] | 422 | void Shell::tick(float dt) |
---|
| 423 | { |
---|
| 424 | if (this->delayed > 0.0) |
---|
| 425 | this->delayed -= dt; |
---|
| 426 | else if (this->pressedKey != SDLK_FIRST ) |
---|
| 427 | { |
---|
[5097] | 428 | this->delayed = this->repeatRate; |
---|
[5095] | 429 | if (this->pressedKey == SDLK_BACKSPACE) |
---|
| 430 | this->removeCharacters(1); |
---|
| 431 | else if (pressedKey < 127) |
---|
| 432 | this->addCharacter(this->pressedKey); |
---|
| 433 | } |
---|
| 434 | } |
---|
[5068] | 435 | /** |
---|
| 436 | * displays the Shell |
---|
| 437 | */ |
---|
| 438 | void Shell::draw() const |
---|
| 439 | { |
---|
[5099] | 440 | glPushMatrix(); |
---|
| 441 | // transform for alignment. |
---|
| 442 | // setting the Blending effects |
---|
| 443 | |
---|
| 444 | glColor4f(0.0f, 0.0f, 0.8f, .4); |
---|
| 445 | glEnable(GL_BLEND); |
---|
| 446 | glDisable(GL_TEXTURE_2D); |
---|
| 447 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
| 448 | |
---|
[5158] | 449 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
| 450 | glBegin(GL_TRIANGLE_STRIP); |
---|
[5099] | 451 | |
---|
[5158] | 452 | glTexCoord2f(0, 0); |
---|
[5099] | 453 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
| 454 | |
---|
[5158] | 455 | glTexCoord2f(1, 0); |
---|
[5113] | 456 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
[5099] | 457 | |
---|
[5158] | 458 | glTexCoord2f(0, 1); |
---|
| 459 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
---|
| 460 | |
---|
| 461 | glTexCoord2f(1, 1); |
---|
[5113] | 462 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
---|
[5099] | 463 | |
---|
| 464 | glEnd(); |
---|
[5068] | 465 | } |
---|
| 466 | |
---|
| 467 | |
---|
| 468 | /** |
---|
| 469 | * autocompletes the Shell's inputLine |
---|
| 470 | * @returns true, if a result was found, false otherwise |
---|
[5100] | 471 | * |
---|
| 472 | * @todo implement it!! |
---|
[5068] | 473 | */ |
---|
| 474 | bool Shell::autoComplete() |
---|
| 475 | { |
---|
[5100] | 476 | //PRINTF(3)("AutoCompletion not implemented yet\n"); |
---|
| 477 | |
---|
| 478 | char* completionLine = new char[strlen(inputLine)+1]; |
---|
| 479 | strcpy(completionLine, this->inputLine); |
---|
| 480 | |
---|
[5113] | 481 | char* commandBegin = strrchr(completionLine, ' '); |
---|
[5100] | 482 | if (commandBegin == NULL) |
---|
| 483 | commandBegin = completionLine; |
---|
| 484 | else |
---|
| 485 | { |
---|
| 486 | if(commandBegin >= completionLine + strlen(completionLine)) |
---|
| 487 | commandBegin = completionLine + strlen(completionLine); |
---|
| 488 | else |
---|
| 489 | commandBegin++; |
---|
| 490 | } |
---|
| 491 | |
---|
[5113] | 492 | char* objectStart; |
---|
| 493 | if (objectStart = strstr(commandBegin, "::")) |
---|
| 494 | { |
---|
| 495 | char* classIdentity = new char[objectStart - commandBegin +1]; |
---|
| 496 | strncpy(classIdentity, commandBegin, objectStart - commandBegin); |
---|
| 497 | classIdentity[objectStart - commandBegin] = '\0'; |
---|
| 498 | this->objectComplete(objectStart+2, ClassList::StringToID(classIdentity)); |
---|
| 499 | delete[] classIdentity; |
---|
| 500 | } |
---|
| 501 | else |
---|
| 502 | this->classComplete(commandBegin); |
---|
[5102] | 503 | |
---|
[5113] | 504 | delete[] completionLine; |
---|
| 505 | } |
---|
[5102] | 506 | |
---|
[5113] | 507 | /** |
---|
| 508 | * autocompletes a className |
---|
| 509 | * @param classBegin the Beginning of a String to autoComplete |
---|
| 510 | * @return true on success, false otherwise |
---|
| 511 | */ |
---|
| 512 | bool Shell::classComplete(const char* classBegin) |
---|
| 513 | { |
---|
| 514 | if (unlikely(classBegin == NULL)) |
---|
| 515 | return false; |
---|
| 516 | const tList<const char>* clList = ClassList::getClassList(); |
---|
| 517 | if (clList != NULL) |
---|
| 518 | { |
---|
| 519 | const tList<const char>* classList = this->createCompleteList(clList, classBegin); |
---|
| 520 | if (classList != NULL) |
---|
| 521 | this->generalComplete(classList, classBegin, "%s::", "::"); |
---|
| 522 | else |
---|
| 523 | return false; |
---|
| 524 | } |
---|
| 525 | else |
---|
| 526 | return false; |
---|
| 527 | return true; |
---|
[5105] | 528 | } |
---|
| 529 | |
---|
| 530 | /** |
---|
[5113] | 531 | * autocompletes an ObjectName |
---|
| 532 | * @param objectBegin the beginning string of a Object |
---|
| 533 | * @param classID the ID of the Class to search for. |
---|
| 534 | * @return true on success, false otherwise |
---|
| 535 | */ |
---|
| 536 | bool Shell::objectComplete(const char* objectBegin, long classID) |
---|
| 537 | { |
---|
| 538 | printf("%s\n", objectBegin); |
---|
| 539 | |
---|
| 540 | if (unlikely(objectBegin == NULL)) |
---|
| 541 | return false; |
---|
| 542 | tList<BaseObject>* boList = ClassList::getList(classID); |
---|
| 543 | if (boList != NULL) |
---|
| 544 | { |
---|
| 545 | printf("\n", boList->firstElement()->getName()); |
---|
| 546 | const tList<const char>* objectList = this->createCompleteList(boList, objectBegin); |
---|
| 547 | if (objectList != NULL) |
---|
| 548 | this->generalComplete(objectList, objectBegin, "%s"); |
---|
| 549 | else |
---|
| 550 | return false; |
---|
| 551 | } |
---|
| 552 | else |
---|
| 553 | return false; |
---|
| 554 | return true; |
---|
| 555 | } |
---|
| 556 | |
---|
[5166] | 557 | /** |
---|
| 558 | * completes a Function |
---|
| 559 | * @param functionBegin the beginning of the function String |
---|
| 560 | */ |
---|
[5113] | 561 | bool Shell::functionComplete(const char* functionBegin) |
---|
| 562 | { |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | /** |
---|
| 566 | * completes the inputline on grounds of an inputList |
---|
| 567 | * @param stringList the List to parse through |
---|
| 568 | * @param begin the String to search in the inputList, and to extend with it. |
---|
| 569 | * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::" |
---|
| 570 | * @param addBack what should be added at the end of the completion |
---|
| 571 | * @param addFront what should be added to the front of one finished completion |
---|
| 572 | * @return true if ok, false otherwise |
---|
| 573 | */ |
---|
| 574 | bool Shell::generalComplete(const tList<const char>* stringList, const char* begin, const char* displayAs, const char* addBack, const char* addFront) |
---|
| 575 | { |
---|
| 576 | if (stringList->getSize() == 0) |
---|
| 577 | return false; |
---|
| 578 | |
---|
| 579 | const char* addString = stringList->firstElement(); |
---|
| 580 | unsigned int addLength = 0; |
---|
| 581 | unsigned int inputLenght = strlen(begin); |
---|
| 582 | |
---|
| 583 | if (addString != NULL) |
---|
| 584 | addLength = strlen(addString); |
---|
| 585 | tIterator<const char>* charIterator = stringList->getIterator(); |
---|
[5115] | 586 | const char* charElem = charIterator->firstElement(); |
---|
[5113] | 587 | while (charElem != NULL) |
---|
| 588 | { |
---|
| 589 | PRINTF(0)(displayAs, charElem); |
---|
| 590 | for (unsigned int i = inputLenght; i < addLength; i++) |
---|
| 591 | if (addString[i] != charElem[i]) |
---|
| 592 | { |
---|
| 593 | addLength = i; |
---|
| 594 | break; |
---|
| 595 | } |
---|
| 596 | charElem = charIterator->nextElement(); |
---|
| 597 | } |
---|
| 598 | delete charIterator; |
---|
| 599 | |
---|
| 600 | if (addLength >= inputLenght) |
---|
| 601 | { |
---|
| 602 | char* adder = new char[addLength+1]; |
---|
| 603 | strncpy(adder, addString, addLength); |
---|
| 604 | adder[addLength] = '\0'; |
---|
| 605 | this->removeCharacters(inputLenght); |
---|
| 606 | this->addCharacters(adder); |
---|
| 607 | if (addBack != NULL && stringList->getSize() == 1) |
---|
| 608 | this->addCharacters("::"); |
---|
| 609 | delete[] adder; |
---|
| 610 | } |
---|
| 611 | return true; |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | /** |
---|
| 615 | * searches for classes, which beginn with classNameBegin |
---|
| 616 | * @param inputList the List to parse through |
---|
| 617 | * @param classNameBegin the beginning string |
---|
| 618 | * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards, |
---|
| 619 | * !! The strings MUST NOT be deleted !! |
---|
| 620 | */ |
---|
| 621 | const tList<const char>* Shell::createCompleteList(const tList<const char>* inputList, const char* classNameBegin) |
---|
| 622 | { |
---|
| 623 | if (inputList == NULL || classNameBegin == NULL) |
---|
| 624 | return NULL; |
---|
| 625 | unsigned int searchLength = strlen(classNameBegin); |
---|
| 626 | if (this->completionList != NULL) |
---|
| 627 | delete this->completionList; |
---|
| 628 | this->completionList = new tList<const char>; |
---|
| 629 | |
---|
| 630 | // tList<const char>* classList = ClassList::getClassList(); |
---|
| 631 | |
---|
| 632 | tIterator<const char>* iterator = inputList->getIterator(); |
---|
[5115] | 633 | const char* enumString = iterator->firstElement(); |
---|
[5113] | 634 | while (enumString != NULL) |
---|
| 635 | { |
---|
| 636 | if (strlen(enumString)>searchLength+1 && |
---|
| 637 | !strncasecmp(enumString, classNameBegin, searchLength)) |
---|
| 638 | { |
---|
| 639 | this->completionList->add(enumString); |
---|
| 640 | } |
---|
| 641 | enumString = iterator->nextElement(); |
---|
| 642 | } |
---|
| 643 | delete iterator; |
---|
| 644 | |
---|
| 645 | return this->completionList; |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | /** |
---|
| 649 | * searches for classes, which beginn with classNameBegin |
---|
| 650 | * @param inputList the List to parse through |
---|
| 651 | * @param classNameBegin the beginning string |
---|
| 652 | * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards, |
---|
| 653 | * !! The strings MUST NOT be deleted !! |
---|
| 654 | */ |
---|
| 655 | const tList<const char>* Shell::createCompleteList(const tList<BaseObject>* inputList, const char* classNameBegin) |
---|
| 656 | { |
---|
| 657 | if (inputList == NULL || classNameBegin == NULL) |
---|
| 658 | return NULL; |
---|
| 659 | unsigned int searchLength = strlen(classNameBegin); |
---|
| 660 | if (this->completionList != NULL) |
---|
| 661 | delete this->completionList; |
---|
| 662 | this->completionList = new tList<const char>; |
---|
| 663 | |
---|
| 664 | tIterator<BaseObject>* iterator = inputList->getIterator(); |
---|
[5115] | 665 | BaseObject* enumBO = iterator->firstElement(); |
---|
[5113] | 666 | while (enumBO != NULL) |
---|
| 667 | { |
---|
| 668 | if (enumBO->getName() != NULL && |
---|
| 669 | strlen(enumBO->getName())>searchLength+1 && |
---|
| 670 | !strncasecmp(enumBO->getName(), classNameBegin, searchLength)) |
---|
| 671 | { |
---|
| 672 | this->completionList->add(enumBO->getName()); |
---|
| 673 | } |
---|
| 674 | enumBO = iterator->nextElement(); |
---|
| 675 | } |
---|
| 676 | delete iterator; |
---|
| 677 | |
---|
| 678 | return this->completionList; |
---|
| 679 | } |
---|
| 680 | |
---|
[5166] | 681 | /** |
---|
| 682 | * prints out some nice help about the Shell |
---|
| 683 | */ |
---|
[5119] | 684 | void Shell::help() const |
---|
| 685 | { |
---|
| 686 | PRINT(0)("Help for the most important Shell-commands\n"); |
---|
| 687 | PRINT(0)("F1 - HELP; F2 - DEBUG; ` - open/close shell\n"); |
---|
| 688 | PRINT(0)("input order:\n"); |
---|
| 689 | PRINT(0)("ClassName::objectName function [parameter1, [parameter2 ...]] or\n"); |
---|
| 690 | PRINT(0)("Command [parameter]\n"); |
---|
| 691 | } |
---|
| 692 | |
---|
[5120] | 693 | |
---|
| 694 | /////////////////////// |
---|
| 695 | // HELPER FUNCTIONS // |
---|
| 696 | /////////////////////// |
---|
[5166] | 697 | |
---|
| 698 | /** |
---|
| 699 | * calculates the position of a Buffer-Display Line |
---|
| 700 | * @param lineNumber the lineNumber from the bottom to calculate the position from |
---|
| 701 | * @returns the Position of the Line. |
---|
| 702 | */ |
---|
[5120] | 703 | Vector Shell::calculateLinePosition(unsigned int lineNumber) |
---|
| 704 | { |
---|
[5124] | 705 | return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0); |
---|
[5120] | 706 | } |
---|
| 707 | |
---|
| 708 | |
---|
| 709 | |
---|
[5113] | 710 | /** |
---|
[5068] | 711 | * displays some nice output from the Shell |
---|
| 712 | */ |
---|
| 713 | void Shell::debug() const |
---|
| 714 | { |
---|
[5119] | 715 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
| 716 | |
---|
[5096] | 717 | if (this->pressedKey != SDLK_FIRST) |
---|
| 718 | printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay); |
---|
[5119] | 719 | |
---|
| 720 | |
---|
[5177] | 721 | ShellBuffer::getInstance()->debug(); |
---|
[5068] | 722 | } |
---|
[5166] | 723 | |
---|
| 724 | |
---|
| 725 | |
---|
| 726 | // void Shell::testI (int i) |
---|
| 727 | // { |
---|
| 728 | // PRINTF(3)("This is the Test for one Int '%d'\n", i); |
---|
| 729 | // } |
---|
| 730 | // |
---|
| 731 | // void Shell::testS (const char* s) |
---|
| 732 | // { |
---|
| 733 | // PRINTF(3)("This is the Test for one String '%s'\n", s); |
---|
| 734 | // } |
---|
| 735 | // |
---|
| 736 | // void Shell::testB (bool b) |
---|
| 737 | // { |
---|
| 738 | // PRINTF(3)("This is the Test for one Bool: "); |
---|
| 739 | // if (b) |
---|
| 740 | // PRINTF(3)("true\n"); |
---|
| 741 | // else |
---|
| 742 | // PRINTF(3)("false\n"); |
---|
| 743 | // } |
---|
| 744 | // |
---|
| 745 | // void Shell::testF (float f) |
---|
| 746 | // { |
---|
| 747 | // PRINTF(3)("This is the Test for one Float '%f'\n", f); |
---|
| 748 | // } |
---|
| 749 | // |
---|
| 750 | // void Shell::testSF (const char* s, float f) |
---|
| 751 | // { |
---|
| 752 | // PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f); |
---|
| 753 | // } |
---|