[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" |
---|
[1853] | 19 | |
---|
[5072] | 20 | #include "text_engine.h" |
---|
| 21 | #include "list.h" |
---|
| 22 | |
---|
[5075] | 23 | #include <stdarg.h> |
---|
| 24 | #include <stdio.h> |
---|
| 25 | |
---|
[1856] | 26 | using namespace std; |
---|
[1853] | 27 | |
---|
[1856] | 28 | |
---|
[3245] | 29 | /** |
---|
[4838] | 30 | * standard constructor |
---|
[5068] | 31 | */ |
---|
| 32 | Shell::Shell () |
---|
[3365] | 33 | { |
---|
[5072] | 34 | this->setClassID(CL_SHELL, "Shell"); |
---|
| 35 | this->setName("Shell"); |
---|
| 36 | |
---|
| 37 | this->buffer = new tList<char>; |
---|
| 38 | |
---|
[5080] | 39 | this->textSize = 10; |
---|
| 40 | |
---|
[5074] | 41 | //this->bufferSize = 0; |
---|
[5080] | 42 | this->bufferText = NULL; |
---|
[5072] | 43 | this->setBufferSize(100); |
---|
[5082] | 44 | this->setBufferDisplaySize(8); |
---|
[5074] | 45 | |
---|
[5080] | 46 | this->inputLineText = TextEngine::getInstance()->createText("fonts/earth.ttf", 10, TEXT_DYNAMIC, 255, 0, 0); |
---|
| 47 | this->inputLineText->setText(NULL); |
---|
[5074] | 48 | |
---|
[5075] | 49 | //this->addBufferLineStatic("asjflksjdvklasmv %s", "doom"); |
---|
| 50 | //TextEngine::getInstance()->debug(); |
---|
[5074] | 51 | //exit(-1); |
---|
[5068] | 52 | } |
---|
[4320] | 53 | |
---|
[5068] | 54 | Shell* Shell::singletonRef = NULL; |
---|
[1853] | 55 | |
---|
[3245] | 56 | /** |
---|
[4838] | 57 | * standard deconstructor |
---|
[5068] | 58 | */ |
---|
| 59 | Shell::~Shell () |
---|
[3543] | 60 | { |
---|
| 61 | // delete what has to be deleted here |
---|
[5080] | 62 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
| 63 | delete this->bufferText[i]; |
---|
[5079] | 64 | delete this->bufferText; |
---|
[5080] | 65 | delete this->inputLineText; |
---|
[5079] | 66 | |
---|
[5068] | 67 | Shell::singletonRef = NULL; |
---|
[3543] | 68 | } |
---|
[5068] | 69 | |
---|
[5074] | 70 | /** |
---|
| 71 | * sets The count of Lines to display in the buffer. |
---|
| 72 | * @param bufferDisplaySize the count of lines to display in the Shell-Buffer. |
---|
| 73 | */ |
---|
[5072] | 74 | void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) |
---|
| 75 | { |
---|
[5080] | 76 | if (this->bufferText != NULL) |
---|
[5072] | 77 | { |
---|
[5080] | 78 | for (unsigned int i = 0; i < this->bufferDisplaySize; i++) |
---|
| 79 | delete this->bufferText[i]; |
---|
| 80 | delete this->bufferText; |
---|
[5072] | 81 | } |
---|
[5080] | 82 | |
---|
| 83 | this->bufferText = new Text*[bufferDisplaySize]; |
---|
| 84 | for (unsigned int i = 0; i < bufferDisplaySize; i++) |
---|
[5072] | 85 | { |
---|
[5080] | 86 | this->bufferText[i] = TextEngine::getInstance()->createText("fonts/earth.ttf", this->textSize, TEXT_DYNAMIC, 255, 0, 0); |
---|
| 87 | this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT); |
---|
| 88 | this->bufferText[i]->setPosition2D(5, 400 + 12*i); |
---|
| 89 | this->bufferText[i]->setText(NULL); |
---|
[5072] | 90 | } |
---|
[5080] | 91 | |
---|
| 92 | |
---|
| 93 | this->bufferDisplaySize = bufferDisplaySize; |
---|
[5072] | 94 | } |
---|
[5068] | 95 | |
---|
| 96 | /** |
---|
| 97 | * deletes all the Buffers |
---|
| 98 | */ |
---|
| 99 | void Shell::flushBuffers() |
---|
| 100 | { |
---|
[5072] | 101 | // remove all chars from the BufferTexts. |
---|
[5080] | 102 | if (this->bufferText) |
---|
| 103 | for (int i; i < this->bufferDisplaySize; i++) |
---|
| 104 | { |
---|
| 105 | this->bufferText[i]->setText(NULL); |
---|
| 106 | } |
---|
[5068] | 107 | |
---|
[5072] | 108 | |
---|
| 109 | // delete all the Chars in the Buffers |
---|
| 110 | tIterator<char>* charIterator = this->buffer->getIterator(); |
---|
| 111 | char* charElem = charIterator->nextElement(); |
---|
| 112 | |
---|
| 113 | while (charElem != NULL) |
---|
| 114 | { |
---|
| 115 | delete charElem; |
---|
| 116 | |
---|
| 117 | charElem = charIterator->nextElement(); |
---|
| 118 | } |
---|
| 119 | delete charIterator; |
---|
[5068] | 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | * adds a new Line to the List of Buffers |
---|
| 124 | * @param line the Line as in the first argument in printf |
---|
| 125 | * @param args the arguments as a va_list |
---|
[5072] | 126 | * |
---|
| 127 | * @todo optimize |
---|
[5068] | 128 | */ |
---|
[5075] | 129 | bool Shell::addBufferLineStatic(const char* line, ...) |
---|
[5068] | 130 | { |
---|
[5075] | 131 | va_list arguments; |
---|
| 132 | va_start(arguments, line); |
---|
[5072] | 133 | |
---|
[5075] | 134 | if (Shell::singletonRef == NULL) |
---|
| 135 | vprintf(line, arguments); |
---|
| 136 | else |
---|
| 137 | Shell::singletonRef->addBufferLine(line, arguments); |
---|
| 138 | return true; |
---|
| 139 | } |
---|
[5080] | 140 | int curr = 0; |
---|
[5072] | 141 | |
---|
[5080] | 142 | /** |
---|
| 143 | * add a Line to the List of Buffers |
---|
| 144 | * @param line |
---|
| 145 | * @param arguments |
---|
| 146 | * |
---|
| 147 | * This function Adds one line to the buffer. |
---|
| 148 | * and displays the line as the First Line of the display-buffer |
---|
| 149 | */ |
---|
[5075] | 150 | void Shell::addBufferLine(const char* line, va_list arguments) |
---|
| 151 | { |
---|
[5078] | 152 | vsprintf(this->bufferArray, line, arguments); |
---|
[5072] | 153 | |
---|
[5078] | 154 | char* newLine = new char[strlen(this->bufferArray)+1]; |
---|
| 155 | strcpy(newLine, this->bufferArray); |
---|
[5073] | 156 | |
---|
[5080] | 157 | this->buffer->add(newLine); |
---|
[5075] | 158 | |
---|
[5080] | 159 | if (this->buffer->getSize() > this->bufferSize) |
---|
| 160 | { |
---|
| 161 | delete this->buffer->firstElement(); |
---|
| 162 | this->buffer->remove(this->buffer->firstElement()); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | if (likely(bufferText != NULL)) |
---|
| 166 | { |
---|
| 167 | Text* moveText = this->bufferText[this->bufferDisplaySize-1]; |
---|
| 168 | for (int i = this->bufferDisplaySize-1; i > 0; i--) |
---|
| 169 | { |
---|
| 170 | this->bufferText[i] = this->bufferText[i-1]; |
---|
| 171 | } |
---|
| 172 | this->bufferText[0] = moveText; |
---|
| 173 | } |
---|
| 174 | this->bufferText[0]->setText(newLine); |
---|
| 175 | // this->bufferText-> |
---|
| 176 | // this->inputLineText->setText(newLine); |
---|
[5068] | 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * moves the buffer around lineCount lines upwards (negative values move down) |
---|
| 181 | * @param lineCount the Count of lines to move upwards |
---|
[5072] | 182 | * |
---|
| 183 | * @todo |
---|
[5068] | 184 | */ |
---|
| 185 | void Shell::moveBuffer(int lineCount) |
---|
| 186 | { |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /** |
---|
| 190 | * @param lineNumber the n-th line from the bottom |
---|
| 191 | * @returns the Buffer at Line lineNumber |
---|
| 192 | */ |
---|
| 193 | const char* Shell::getBufferLine(unsigned int lineNumber) |
---|
| 194 | { |
---|
[5072] | 195 | tIterator<char>* charIterator = this->buffer->getIterator(); |
---|
| 196 | char* charElem = charIterator->nextElement(); |
---|
| 197 | |
---|
| 198 | int i = 1; |
---|
| 199 | while (charElem != NULL) |
---|
| 200 | { |
---|
| 201 | if (i++ < lineNumber) |
---|
| 202 | { |
---|
| 203 | delete charIterator; |
---|
| 204 | return charElem; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | charElem = charIterator->nextElement(); |
---|
| 208 | } |
---|
| 209 | delete charIterator; |
---|
[5068] | 210 | } |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | /** |
---|
| 214 | * deletes the InputLine |
---|
| 215 | */ |
---|
| 216 | void Shell::flushInputLine() |
---|
| 217 | { |
---|
[5072] | 218 | if (likely(this->inputLine != NULL)) |
---|
| 219 | { |
---|
| 220 | delete [] this->inputLine; |
---|
| 221 | } |
---|
| 222 | this->inputLine = new char[1]; |
---|
| 223 | *this->inputLine = '\0'; |
---|
| 224 | |
---|
[5068] | 225 | } |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | * adds one character to the inputLine |
---|
| 229 | * @param character the character to add to the inputLine |
---|
| 230 | */ |
---|
| 231 | void Shell::addCharacter(char character) |
---|
| 232 | { |
---|
[5072] | 233 | char* addCharLine = new char[strlen(inputLine)+2]; |
---|
| 234 | |
---|
| 235 | sprintf(addCharLine, "%s%c", this->inputLine, character); |
---|
| 236 | delete this->inputLine; |
---|
| 237 | this->inputLine = addCharLine; |
---|
[5068] | 238 | } |
---|
| 239 | |
---|
| 240 | /** |
---|
| 241 | * adds multiple Characters to thr inputLine |
---|
| 242 | * @param characters a '\0' terminated char-array to add to the InputLine |
---|
| 243 | */ |
---|
| 244 | void Shell::addCharacters(const char* characters) |
---|
| 245 | { |
---|
[5072] | 246 | char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1]; |
---|
| 247 | |
---|
| 248 | sprintf(addCharLine, "%s%s", this->inputLine, characters); |
---|
| 249 | delete this->inputLine; |
---|
| 250 | this->inputLine = addCharLine; |
---|
[5068] | 251 | } |
---|
| 252 | |
---|
| 253 | /** |
---|
| 254 | * removes characterCount characters from the InputLine |
---|
| 255 | * @param characterCount the count of Characters to remove from the input Line |
---|
| 256 | */ |
---|
| 257 | void Shell::removeCharacters(unsigned int characterCount) |
---|
| 258 | { |
---|
[5072] | 259 | if (characterCount > strlen(this->inputLine)) |
---|
| 260 | characterCount = strlen(this->inputLine); |
---|
| 261 | |
---|
| 262 | char* removeCharLine = new char[strlen(inputLine)-characterCount+1]; |
---|
| 263 | |
---|
| 264 | strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount); |
---|
| 265 | delete this->inputLine; |
---|
| 266 | this->inputLine = removeCharLine; |
---|
[5068] | 267 | } |
---|
| 268 | |
---|
[5069] | 269 | /** |
---|
| 270 | * listens for some event |
---|
| 271 | * @param event the Event happened |
---|
| 272 | */ |
---|
| 273 | void Shell::process(const Event &event) |
---|
| 274 | { |
---|
| 275 | // if (event.type) |
---|
[5068] | 276 | |
---|
[5069] | 277 | } |
---|
| 278 | |
---|
[5068] | 279 | /** |
---|
| 280 | * ticks the Shell for dt Seconds |
---|
| 281 | * @param dt the elapsed time since the last tick(); |
---|
| 282 | */ |
---|
[5074] | 283 | //void Shell::tick(float dt) |
---|
| 284 | //{ |
---|
| 285 | //} |
---|
[5068] | 286 | |
---|
| 287 | /** |
---|
| 288 | * displays the Shell |
---|
| 289 | */ |
---|
| 290 | void Shell::draw() const |
---|
| 291 | { |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | |
---|
| 295 | /** |
---|
| 296 | * autocompletes the Shell's inputLine |
---|
| 297 | * @returns true, if a result was found, false otherwise |
---|
| 298 | */ |
---|
| 299 | bool Shell::autoComplete() |
---|
| 300 | { |
---|
| 301 | |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | /** |
---|
| 305 | * displays some nice output from the Shell |
---|
| 306 | */ |
---|
| 307 | void Shell::debug() const |
---|
| 308 | { |
---|
| 309 | |
---|
| 310 | } |
---|