[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" |
---|
[5179] | 21 | #include "shell_input.h" |
---|
[1853] | 22 | |
---|
[5175] | 23 | |
---|
[5072] | 24 | #include "text_engine.h" |
---|
| 25 | #include "list.h" |
---|
[5093] | 26 | #include "graphics_engine.h" |
---|
| 27 | #include "event_handler.h" |
---|
[5129] | 28 | #include "debug.h" |
---|
[5113] | 29 | #include "class_list.h" |
---|
| 30 | |
---|
| 31 | #include "key_names.h" |
---|
[5075] | 32 | #include <stdarg.h> |
---|
| 33 | #include <stdio.h> |
---|
| 34 | |
---|
[1856] | 35 | using namespace std; |
---|
[1853] | 36 | |
---|
[5164] | 37 | SHELL_COMMAND(clear, Shell, clear)->describe("Clears the shell from unwanted lines (empties all buffers)"); |
---|
[5172] | 38 | SHELL_COMMAND(deactivate, Shell, deactivate)->describe("Deactivates the Shell. (moves it into background)"); |
---|
[1856] | 39 | |
---|
[3245] | 40 | /** |
---|
[4838] | 41 | * standard constructor |
---|
[5068] | 42 | */ |
---|
| 43 | Shell::Shell () |
---|
[3365] | 44 | { |
---|
[5072] | 45 | this->setClassID(CL_SHELL, "Shell"); |
---|
| 46 | this->setName("Shell"); |
---|
| 47 | |
---|
[5183] | 48 | // Element2D and generals |
---|
[5175] | 49 | this->setAbsCoor2D(3, -400); |
---|
| 50 | this->textSize = 15; |
---|
| 51 | this->lineSpacing = 5; |
---|
[5113] | 52 | this->bActive = false; |
---|
[5072] | 53 | |
---|
[5175] | 54 | // BUFFER |
---|
| 55 | this->bufferText = NULL; |
---|
| 56 | this->setBufferDisplaySize(10); |
---|
[5080] | 57 | |
---|
[5175] | 58 | // INPUT LINE |
---|
[5179] | 59 | this->shellInput = new ShellInput; |
---|
[5175] | 60 | //this->commandList = new tList<ShellCommand>; |
---|
[5093] | 61 | |
---|
[5113] | 62 | this->rebuildText(); |
---|
[5093] | 63 | |
---|
[5180] | 64 | // EVENT-Handler subscription of '`' to all States. |
---|
| 65 | EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE); |
---|
| 66 | |
---|
[5068] | 67 | } |
---|
[4320] | 68 | |
---|
[5068] | 69 | Shell* Shell::singletonRef = NULL; |
---|
[1853] | 70 | |
---|
[3245] | 71 | /** |
---|
[4838] | 72 | * standard deconstructor |
---|
[5068] | 73 | */ |
---|
| 74 | Shell::~Shell () |
---|
[3543] | 75 | { |
---|
[5099] | 76 | // delete the displayable Buffers |
---|
[5080] | 77 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
| 78 | delete this->bufferText[i]; |
---|
[5113] | 79 | delete[] this->bufferText; |
---|
[5093] | 80 | |
---|
[5099] | 81 | // delete the inputLine |
---|
[5180] | 82 | delete this->shellInput; |
---|
[5079] | 83 | |
---|
[5068] | 84 | Shell::singletonRef = NULL; |
---|
[3543] | 85 | } |
---|
[5068] | 86 | |
---|
[5119] | 87 | /** |
---|
| 88 | * activates the shell |
---|
| 89 | * |
---|
| 90 | * This also feeds the Last few lines from the main buffers into the displayBuffer |
---|
| 91 | */ |
---|
[5113] | 92 | void Shell::activate() |
---|
| 93 | { |
---|
| 94 | if (this->bActive == true) |
---|
| 95 | PRINTF(3)("The shell is already active\n"); |
---|
| 96 | this->bActive = true; |
---|
| 97 | |
---|
| 98 | EventHandler::getInstance()->setState(ES_SHELL); |
---|
| 99 | this->setRelCoorSoft2D(0, 0, 1, 5); |
---|
[5118] | 100 | |
---|
[5175] | 101 | ShellBuffer::getInstance()->getBufferIterator()->lastElement(); |
---|
[5118] | 102 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
[5175] | 103 | this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), true); |
---|
[5113] | 104 | } |
---|
| 105 | |
---|
[5119] | 106 | /** |
---|
| 107 | * deactiveates the Shell. |
---|
| 108 | */ |
---|
[5113] | 109 | void Shell::deactivate() |
---|
| 110 | { |
---|
| 111 | if (this->bActive == false) |
---|
| 112 | PRINTF(3)("The shell is already inactive\n"); |
---|
| 113 | this->bActive = false; |
---|
| 114 | |
---|
| 115 | EventHandler::getInstance()->setState(ES_GAME); |
---|
| 116 | this->setRelCoorSoft2D(0, -400, 1, 5); |
---|
[5118] | 117 | |
---|
[5175] | 118 | ShellBuffer::getInstance()->getBufferIterator()->lastElement(); |
---|
[5157] | 119 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
[5175] | 120 | this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), false); |
---|
[5113] | 121 | } |
---|
| 122 | |
---|
[5158] | 123 | |
---|
[5119] | 124 | /** |
---|
| 125 | * sets the size of the text and spacing |
---|
| 126 | * @param textSize the size of the Text in Pixels |
---|
| 127 | * @param lineSpacing the size of the Spacing between two lines in pixels |
---|
| 128 | * |
---|
| 129 | * this also rebuilds the entire Text, inputLine and displayBuffer, |
---|
| 130 | * to be accurate again. |
---|
| 131 | */ |
---|
[5113] | 132 | void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing) |
---|
| 133 | { |
---|
| 134 | this->textSize = textSize; |
---|
| 135 | this->lineSpacing = lineSpacing; |
---|
| 136 | |
---|
| 137 | this->rebuildText(); |
---|
| 138 | } |
---|
| 139 | |
---|
[5127] | 140 | /** |
---|
| 141 | * rebuilds the Text's |
---|
| 142 | * |
---|
| 143 | * use this function, if you changed the Font/Size or something else. |
---|
| 144 | */ |
---|
[5113] | 145 | void Shell::rebuildText() |
---|
| 146 | { |
---|
[5179] | 147 | this->shellInput->setFont("fonts/Aniron_Bold.ttf", this->textSize); |
---|
| 148 | this->shellInput->setColor(1, 0, 0); |
---|
| 149 | this->shellInput->setAlignment(TEXT_ALIGN_LEFT); |
---|
| 150 | this->shellInput->setText(NULL); |
---|
| 151 | this->shellInput->setParent2D(this); |
---|
| 152 | this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize); |
---|
[5113] | 153 | |
---|
| 154 | this->setBufferDisplaySize(this->bufferDisplaySize); |
---|
| 155 | } |
---|
| 156 | |
---|
[5074] | 157 | /** |
---|
| 158 | * sets The count of Lines to display in the buffer. |
---|
| 159 | * @param bufferDisplaySize the count of lines to display in the Shell-Buffer. |
---|
| 160 | */ |
---|
[5072] | 161 | void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) |
---|
| 162 | { |
---|
[5080] | 163 | if (this->bufferText != NULL) |
---|
[5072] | 164 | { |
---|
[5080] | 165 | for (unsigned int i = 0; i < this->bufferDisplaySize; i++) |
---|
| 166 | delete this->bufferText[i]; |
---|
[5113] | 167 | delete[] this->bufferText; |
---|
[5072] | 168 | } |
---|
[5080] | 169 | |
---|
| 170 | this->bufferText = new Text*[bufferDisplaySize]; |
---|
| 171 | for (unsigned int i = 0; i < bufferDisplaySize; i++) |
---|
[5072] | 172 | { |
---|
[5122] | 173 | this->bufferText[i] = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC); |
---|
[5121] | 174 | this->bufferText[i]->setColor(1, 0, 0); |
---|
[5080] | 175 | this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT); |
---|
[5120] | 176 | this->bufferText[i]->setRelCoor2D(calculateLinePosition(i)); |
---|
[5080] | 177 | this->bufferText[i]->setText(NULL); |
---|
[5089] | 178 | this->bufferText[i]->setParent2D(this); |
---|
[5072] | 179 | } |
---|
[5113] | 180 | this->bufferDisplaySize = bufferDisplaySize; |
---|
[5111] | 181 | |
---|
[5113] | 182 | this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1); |
---|
[5072] | 183 | } |
---|
[5068] | 184 | |
---|
| 185 | /** |
---|
| 186 | * deletes all the Buffers |
---|
| 187 | */ |
---|
[5175] | 188 | void Shell::flush() |
---|
[5068] | 189 | { |
---|
[5072] | 190 | // remove all chars from the BufferTexts. |
---|
[5080] | 191 | if (this->bufferText) |
---|
[5125] | 192 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
[5080] | 193 | { |
---|
[5125] | 194 | this->bufferText[i]->setText(NULL, true); |
---|
[5080] | 195 | } |
---|
[5068] | 196 | |
---|
[5072] | 197 | |
---|
[5175] | 198 | // BUFFER FLUSHING |
---|
[5068] | 199 | } |
---|
| 200 | |
---|
| 201 | /** |
---|
[5118] | 202 | * prints out some text to the input-buffers |
---|
| 203 | * @param text the text to output. |
---|
| 204 | */ |
---|
| 205 | void Shell::printToDisplayBuffer(const char* text) |
---|
| 206 | { |
---|
| 207 | if(likely(bufferText != NULL)) |
---|
| 208 | { |
---|
| 209 | Text* lastText = this->bufferText[this->bufferDisplaySize-1]; |
---|
[5113] | 210 | |
---|
[5118] | 211 | Text* swapText; |
---|
| 212 | Text* moveText = this->bufferText[0]; |
---|
[5120] | 213 | this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10); |
---|
[5118] | 214 | for (unsigned int i = 1; i < this->bufferDisplaySize; i++) |
---|
| 215 | { |
---|
| 216 | if ( i < this->bufferDisplaySize-1) |
---|
[5120] | 217 | this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5); |
---|
[5118] | 218 | swapText = this->bufferText[i]; |
---|
| 219 | this ->bufferText[i] = moveText; |
---|
| 220 | moveText = swapText; |
---|
| 221 | } |
---|
[5120] | 222 | lastText->setRelCoor2D(this->calculateLinePosition(0)); |
---|
[5118] | 223 | this->bufferText[0] = lastText; |
---|
| 224 | |
---|
[5122] | 225 | this->bufferText[0]->setText(text, true); |
---|
[5118] | 226 | } |
---|
[5068] | 227 | } |
---|
| 228 | |
---|
| 229 | /** |
---|
[5166] | 230 | * clears the Shell (empties all buffers) |
---|
| 231 | */ |
---|
[5130] | 232 | void Shell::clear() |
---|
| 233 | { |
---|
[5175] | 234 | this->flush(); |
---|
| 235 | ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL); |
---|
[5130] | 236 | } |
---|
| 237 | |
---|
[5097] | 238 | |
---|
| 239 | |
---|
[5069] | 240 | /** |
---|
| 241 | * listens for some event |
---|
| 242 | * @param event the Event happened |
---|
| 243 | */ |
---|
| 244 | void Shell::process(const Event &event) |
---|
| 245 | { |
---|
[5093] | 246 | if (event.bPressed) |
---|
| 247 | { |
---|
| 248 | if (event.type == SDLK_BACKQUOTE) |
---|
| 249 | { |
---|
| 250 | if (EventHandler::getInstance()->getState() == ES_GAME) |
---|
[5113] | 251 | this->activate(); |
---|
[5093] | 252 | else |
---|
[5113] | 253 | this->deactivate(); |
---|
[5093] | 254 | } |
---|
| 255 | } |
---|
[5069] | 256 | } |
---|
| 257 | |
---|
[5068] | 258 | /** |
---|
| 259 | * displays the Shell |
---|
| 260 | */ |
---|
| 261 | void Shell::draw() const |
---|
| 262 | { |
---|
[5099] | 263 | glPushMatrix(); |
---|
| 264 | // transform for alignment. |
---|
| 265 | // setting the Blending effects |
---|
| 266 | |
---|
| 267 | glColor4f(0.0f, 0.0f, 0.8f, .4); |
---|
| 268 | glEnable(GL_BLEND); |
---|
| 269 | glDisable(GL_TEXTURE_2D); |
---|
| 270 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
| 271 | |
---|
[5158] | 272 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
| 273 | glBegin(GL_TRIANGLE_STRIP); |
---|
[5099] | 274 | |
---|
[5158] | 275 | glTexCoord2f(0, 0); |
---|
[5099] | 276 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
| 277 | |
---|
[5158] | 278 | glTexCoord2f(1, 0); |
---|
[5113] | 279 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
[5099] | 280 | |
---|
[5158] | 281 | glTexCoord2f(0, 1); |
---|
| 282 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
---|
| 283 | |
---|
| 284 | glTexCoord2f(1, 1); |
---|
[5113] | 285 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
---|
[5099] | 286 | |
---|
| 287 | glEnd(); |
---|
[5068] | 288 | } |
---|
| 289 | |
---|
[5120] | 290 | /////////////////////// |
---|
| 291 | // HELPER FUNCTIONS // |
---|
| 292 | /////////////////////// |
---|
[5166] | 293 | |
---|
| 294 | /** |
---|
| 295 | * calculates the position of a Buffer-Display Line |
---|
| 296 | * @param lineNumber the lineNumber from the bottom to calculate the position from |
---|
| 297 | * @returns the Position of the Line. |
---|
| 298 | */ |
---|
[5120] | 299 | Vector Shell::calculateLinePosition(unsigned int lineNumber) |
---|
| 300 | { |
---|
[5124] | 301 | return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0); |
---|
[5120] | 302 | } |
---|
| 303 | |
---|
| 304 | |
---|
| 305 | |
---|
[5113] | 306 | /** |
---|
[5068] | 307 | * displays some nice output from the Shell |
---|
| 308 | */ |
---|
| 309 | void Shell::debug() const |
---|
| 310 | { |
---|
[5119] | 311 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
| 312 | |
---|
[5180] | 313 | // if (this->pressedKey != SDLK_FIRST) |
---|
| 314 | // printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay); |
---|
[5119] | 315 | |
---|
| 316 | |
---|
[5177] | 317 | ShellBuffer::getInstance()->debug(); |
---|
[5068] | 318 | } |
---|
[5166] | 319 | |
---|
| 320 | |
---|
| 321 | |
---|
| 322 | // void Shell::testI (int i) |
---|
| 323 | // { |
---|
| 324 | // PRINTF(3)("This is the Test for one Int '%d'\n", i); |
---|
| 325 | // } |
---|
| 326 | // |
---|
| 327 | // void Shell::testS (const char* s) |
---|
| 328 | // { |
---|
| 329 | // PRINTF(3)("This is the Test for one String '%s'\n", s); |
---|
| 330 | // } |
---|
| 331 | // |
---|
| 332 | // void Shell::testB (bool b) |
---|
| 333 | // { |
---|
| 334 | // PRINTF(3)("This is the Test for one Bool: "); |
---|
| 335 | // if (b) |
---|
| 336 | // PRINTF(3)("true\n"); |
---|
| 337 | // else |
---|
| 338 | // PRINTF(3)("false\n"); |
---|
| 339 | // } |
---|
| 340 | // |
---|
| 341 | // void Shell::testF (float f) |
---|
| 342 | // { |
---|
| 343 | // PRINTF(3)("This is the Test for one Float '%f'\n", f); |
---|
| 344 | // } |
---|
| 345 | // |
---|
| 346 | // void Shell::testSF (const char* s, float f) |
---|
| 347 | // { |
---|
| 348 | // PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f); |
---|
| 349 | // } |
---|