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