| 1 | /* |
|---|
| 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. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
|---|
| 17 | |
|---|
| 18 | #include "shell.h" |
|---|
| 19 | |
|---|
| 20 | #include "text_engine.h" |
|---|
| 21 | #include "list.h" |
|---|
| 22 | |
|---|
| 23 | #include <stdarg.h> |
|---|
| 24 | #include <stdio.h> |
|---|
| 25 | |
|---|
| 26 | using namespace std; |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * standard constructor |
|---|
| 31 | */ |
|---|
| 32 | Shell::Shell () |
|---|
| 33 | { |
|---|
| 34 | this->setClassID(CL_SHELL, "Shell"); |
|---|
| 35 | this->setName("Shell"); |
|---|
| 36 | |
|---|
| 37 | this->buffer = new tList<char>; |
|---|
| 38 | |
|---|
| 39 | this->textSize = 10; |
|---|
| 40 | |
|---|
| 41 | //this->bufferSize = 0; |
|---|
| 42 | this->bufferText = NULL; |
|---|
| 43 | this->setBufferSize(100); |
|---|
| 44 | this->setBufferDisplaySize(10); |
|---|
| 45 | |
|---|
| 46 | this->inputLineText = TextEngine::getInstance()->createText("fonts/earth.ttf", 10, TEXT_DYNAMIC, 255, 0, 0); |
|---|
| 47 | this->inputLineText->setText(NULL); |
|---|
| 48 | |
|---|
| 49 | //this->addBufferLineStatic("asjflksjdvklasmv %s", "doom"); |
|---|
| 50 | //TextEngine::getInstance()->debug(); |
|---|
| 51 | //exit(-1); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | Shell* Shell::singletonRef = NULL; |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * standard deconstructor |
|---|
| 58 | */ |
|---|
| 59 | Shell::~Shell () |
|---|
| 60 | { |
|---|
| 61 | // delete what has to be deleted here |
|---|
| 62 | for (int i = 0; i < this->bufferDisplaySize; i++) |
|---|
| 63 | delete this->bufferText[i]; |
|---|
| 64 | delete this->bufferText; |
|---|
| 65 | delete this->inputLineText; |
|---|
| 66 | |
|---|
| 67 | Shell::singletonRef = NULL; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 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 | */ |
|---|
| 74 | void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) |
|---|
| 75 | { |
|---|
| 76 | if (this->bufferText != NULL) |
|---|
| 77 | { |
|---|
| 78 | for (unsigned int i = 0; i < this->bufferDisplaySize; i++) |
|---|
| 79 | delete this->bufferText[i]; |
|---|
| 80 | delete this->bufferText; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | this->bufferText = new Text*[bufferDisplaySize]; |
|---|
| 84 | for (unsigned int i = 0; i < bufferDisplaySize; i++) |
|---|
| 85 | { |
|---|
| 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); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | this->bufferDisplaySize = bufferDisplaySize; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * deletes all the Buffers |
|---|
| 98 | */ |
|---|
| 99 | void Shell::flushBuffers() |
|---|
| 100 | { |
|---|
| 101 | // remove all chars from the BufferTexts. |
|---|
| 102 | if (this->bufferText) |
|---|
| 103 | for (int i; i < this->bufferDisplaySize; i++) |
|---|
| 104 | { |
|---|
| 105 | this->bufferText[i]->setText(NULL); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 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; |
|---|
| 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 |
|---|
| 126 | * |
|---|
| 127 | * @todo optimize |
|---|
| 128 | */ |
|---|
| 129 | bool Shell::addBufferLineStatic(const char* line, ...) |
|---|
| 130 | { |
|---|
| 131 | va_list arguments; |
|---|
| 132 | va_start(arguments, line); |
|---|
| 133 | |
|---|
| 134 | if (Shell::singletonRef == NULL) |
|---|
| 135 | vprintf(line, arguments); |
|---|
| 136 | else |
|---|
| 137 | Shell::singletonRef->addBufferLine(line, arguments); |
|---|
| 138 | return true; |
|---|
| 139 | } |
|---|
| 140 | int curr = 0; |
|---|
| 141 | |
|---|
| 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 | */ |
|---|
| 150 | void Shell::addBufferLine(const char* line, va_list arguments) |
|---|
| 151 | { |
|---|
| 152 | vsprintf(this->bufferArray, line, arguments); |
|---|
| 153 | |
|---|
| 154 | char* newLine = new char[strlen(this->bufferArray)+1]; |
|---|
| 155 | strcpy(newLine, this->bufferArray); |
|---|
| 156 | |
|---|
| 157 | this->buffer->add(newLine); |
|---|
| 158 | |
|---|
| 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); |
|---|
| 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 |
|---|
| 182 | * |
|---|
| 183 | * @todo |
|---|
| 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 | { |
|---|
| 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; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | /** |
|---|
| 214 | * deletes the InputLine |
|---|
| 215 | */ |
|---|
| 216 | void Shell::flushInputLine() |
|---|
| 217 | { |
|---|
| 218 | if (likely(this->inputLine != NULL)) |
|---|
| 219 | { |
|---|
| 220 | delete [] this->inputLine; |
|---|
| 221 | } |
|---|
| 222 | this->inputLine = new char[1]; |
|---|
| 223 | *this->inputLine = '\0'; |
|---|
| 224 | |
|---|
| 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 | { |
|---|
| 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; |
|---|
| 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 | { |
|---|
| 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; |
|---|
| 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 | { |
|---|
| 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; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 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) |
|---|
| 276 | |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | /** |
|---|
| 280 | * ticks the Shell for dt Seconds |
|---|
| 281 | * @param dt the elapsed time since the last tick(); |
|---|
| 282 | */ |
|---|
| 283 | //void Shell::tick(float dt) |
|---|
| 284 | //{ |
|---|
| 285 | //} |
|---|
| 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 | } |
|---|