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