/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ #include "shell.h" #include "shell_command.h" #include "shell_buffer.h" #include "shell_input.h" #include "text_engine.h" #include "list.h" #include "graphics_engine.h" #include "event_handler.h" #include "debug.h" #include "class_list.h" #include "key_names.h" #include #include using namespace std; SHELL_COMMAND(clear, Shell, clear)->describe("Clears the shell from unwanted lines (empties all buffers)"); SHELL_COMMAND(deactivate, Shell, deactivate)->describe("Deactivates the Shell. (moves it into background)"); /** * standard constructor */ Shell::Shell () { this->setClassID(CL_SHELL, "Shell"); this->setName("Shell"); this->setAbsCoor2D(3, -400); this->textSize = 15; this->lineSpacing = 5; this->bActive = false; // BUFFER this->bufferText = NULL; this->setBufferDisplaySize(10); // INPUT LINE this->shellInput = new ShellInput; this->delayed = 0; this->shellInput->setRepeatDelay(.3, .05); //this->commandList = new tList; this->rebuildText(); this->completionList = NULL; // EVENT-Handler subscription of '`' to all States, and all other keyboard commands to ES_SEHLL EventHandler* evh = EventHandler::getInstance(); evh->subscribe(this, ES_ALL, SDLK_BACKQUOTE); for (int i = 1; i < SDLK_LAST; i++) evh->subscribe(this, ES_SHELL, i); } Shell* Shell::singletonRef = NULL; /** * standard deconstructor */ Shell::~Shell () { // delete the displayable Buffers for (int i = 0; i < this->bufferDisplaySize; i++) delete this->bufferText[i]; delete[] this->bufferText; // delete the inputLine Shell::singletonRef = NULL; } /** * activates the shell * * This also feeds the Last few lines from the main buffers into the displayBuffer */ void Shell::activate() { if (this->bActive == true) PRINTF(3)("The shell is already active\n"); this->bActive = true; EventHandler::getInstance()->setState(ES_SHELL); this->setRelCoorSoft2D(0, 0, 1, 5); ShellBuffer::getInstance()->getBufferIterator()->lastElement(); for (int i = 0; i < this->bufferDisplaySize; i++) this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), true); } /** * deactiveates the Shell. */ void Shell::deactivate() { if (this->bActive == false) PRINTF(3)("The shell is already inactive\n"); this->bActive = false; EventHandler::getInstance()->setState(ES_GAME); this->setRelCoorSoft2D(0, -400, 1, 5); ShellBuffer::getInstance()->getBufferIterator()->lastElement(); for (int i = 0; i < this->bufferDisplaySize; i++) this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), false); } /** * sets the size of the text and spacing * @param textSize the size of the Text in Pixels * @param lineSpacing the size of the Spacing between two lines in pixels * * this also rebuilds the entire Text, inputLine and displayBuffer, * to be accurate again. */ void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing) { this->textSize = textSize; this->lineSpacing = lineSpacing; this->rebuildText(); } /** * rebuilds the Text's * * use this function, if you changed the Font/Size or something else. */ void Shell::rebuildText() { this->shellInput->setFont("fonts/Aniron_Bold.ttf", this->textSize); this->shellInput->setColor(1, 0, 0); this->shellInput->setAlignment(TEXT_ALIGN_LEFT); this->shellInput->setText(NULL); this->shellInput->setParent2D(this); this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize); this->setBufferDisplaySize(this->bufferDisplaySize); } /** * sets The count of Lines to display in the buffer. * @param bufferDisplaySize the count of lines to display in the Shell-Buffer. */ void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) { if (this->bufferText != NULL) { for (unsigned int i = 0; i < this->bufferDisplaySize; i++) delete this->bufferText[i]; delete[] this->bufferText; } this->bufferText = new Text*[bufferDisplaySize]; for (unsigned int i = 0; i < bufferDisplaySize; i++) { this->bufferText[i] = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC); this->bufferText[i]->setColor(1, 0, 0); this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT); this->bufferText[i]->setRelCoor2D(calculateLinePosition(i)); this->bufferText[i]->setText(NULL); this->bufferText[i]->setParent2D(this); } this->bufferDisplaySize = bufferDisplaySize; this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1); } /** * deletes all the Buffers */ void Shell::flush() { // remove all chars from the BufferTexts. if (this->bufferText) for (int i = 0; i < this->bufferDisplaySize; i++) { this->bufferText[i]->setText(NULL, true); } // BUFFER FLUSHING } /** * prints out some text to the input-buffers * @param text the text to output. */ void Shell::printToDisplayBuffer(const char* text) { if(likely(bufferText != NULL)) { Text* lastText = this->bufferText[this->bufferDisplaySize-1]; Text* swapText; Text* moveText = this->bufferText[0]; this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10); for (unsigned int i = 1; i < this->bufferDisplaySize; i++) { if ( i < this->bufferDisplaySize-1) this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5); swapText = this->bufferText[i]; this ->bufferText[i] = moveText; moveText = swapText; } lastText->setRelCoor2D(this->calculateLinePosition(0)); this->bufferText[0] = lastText; this->bufferText[0]->setText(text, true); } } /** * clears the Shell (empties all buffers) */ void Shell::clear() { this->flush(); ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL); } /** * listens for some event * @param event the Event happened */ void Shell::process(const Event &event) { if (event.bPressed) { PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type)); if (event.type == SDLK_BACKQUOTE) { if (EventHandler::getInstance()->getState() == ES_GAME) this->activate(); else this->deactivate(); } else if (event.type == SDLK_F1) this->help(); else if (event.type == SDLK_F2) this->debug(); else if (event.type == SDLK_TAB) ;//this->autoComplete(); else if (event.type == SDLK_BACKSPACE) { this->delayed = this->repeatDelay; this->pressedKey = SDLK_BACKSPACE; this->shellInput->removeCharacters(1); } else if (event.type == SDLK_RETURN) this->shellInput->executeCommand(); /* else if (event.type == SDLK_UP) { // this->flushInputLine(); tIterator* iterator = this->commandList->getIterator(); char* command = iterator->lastElement(); while (command) { if (!strcmp (command, inputLine)) { inputLine = iterator->prevElement(); return; } command = iterator->prevElement(); } inputLine = iterator->lastElement(); } */ else if (likely(event.type < 127)) { Uint8 *keystate = SDL_GetKeyState(NULL); this->delayed = this->repeatDelay; if (unlikely( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] )) { this->pressedKey = event.type-32; this->shellInput->addCharacter(event.type-32); } else { this->pressedKey = event.type; this->shellInput->addCharacter(event.type); } } } else // if(!event.bPressed) { if (this->pressedKey == event.type || (this->pressedKey == event.type - 32)) { this->pressedKey = SDLK_FIRST; this->delayed = 0.0; } } } /** * ticks the Shell for dt Seconds * @param dt the elapsed time since the last tick(); */ void Shell::tick(float dt) { if (this->delayed > 0.0) this->delayed -= dt; else if (this->pressedKey != SDLK_FIRST ) { this->delayed = this->repeatRate; if (this->pressedKey == SDLK_BACKSPACE) this->shellInput->removeCharacters(1); else if (pressedKey < 127) this->shellInput->addCharacter(this->pressedKey); } } /** * displays the Shell */ void Shell::draw() const { glPushMatrix(); // transform for alignment. // setting the Blending effects glColor4f(0.0f, 0.0f, 0.8f, .4); glEnable(GL_BLEND); glDisable(GL_TEXTURE_2D); glBlendFunc(GL_SRC_ALPHA, GL_ONE); glBindTexture(GL_TEXTURE_2D, 0); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(0, 0); glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y ); glTexCoord2f(1, 0); glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y ); glTexCoord2f(0, 1); glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); glTexCoord2f(1, 1); glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); glEnd(); } /** * prints out some nice help about the Shell */ void Shell::help() const { PRINT(0)("Help for the most important Shell-commands\n"); PRINT(0)("F1 - HELP; F2 - DEBUG; ` - open/close shell\n"); PRINT(0)("input order:\n"); PRINT(0)("ClassName::objectName function [parameter1, [parameter2 ...]] or\n"); PRINT(0)("Command [parameter]\n"); } /////////////////////// // HELPER FUNCTIONS // /////////////////////// /** * calculates the position of a Buffer-Display Line * @param lineNumber the lineNumber from the bottom to calculate the position from * @returns the Position of the Line. */ Vector Shell::calculateLinePosition(unsigned int lineNumber) { return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0); } /** * displays some nice output from the Shell */ void Shell::debug() const { PRINT(3)("Debugging output to console (not this shell)\n"); if (this->pressedKey != SDLK_FIRST) printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay); ShellBuffer::getInstance()->debug(); } // void Shell::testI (int i) // { // PRINTF(3)("This is the Test for one Int '%d'\n", i); // } // // void Shell::testS (const char* s) // { // PRINTF(3)("This is the Test for one String '%s'\n", s); // } // // void Shell::testB (bool b) // { // PRINTF(3)("This is the Test for one Bool: "); // if (b) // PRINTF(3)("true\n"); // else // PRINTF(3)("false\n"); // } // // void Shell::testF (float f) // { // PRINTF(3)("This is the Test for one Float '%f'\n", f); // } // // void Shell::testSF (const char* s, float f) // { // PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f); // }