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