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