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