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