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