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