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 "array.h" |
---|
23 | #include "graphics_engine.h" |
---|
24 | #include "event_handler.h" |
---|
25 | |
---|
26 | #include "load_param.h" |
---|
27 | #include "debug.h" |
---|
28 | #include <stdarg.h> |
---|
29 | #include <stdio.h> |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | |
---|
34 | /** |
---|
35 | * standard constructor |
---|
36 | */ |
---|
37 | Shell::Shell () |
---|
38 | { |
---|
39 | this->setClassID(CL_SHELL, "Shell"); |
---|
40 | this->setName("Shell"); |
---|
41 | |
---|
42 | this->buffer = new tList<char>; |
---|
43 | |
---|
44 | this->textSize = 10; |
---|
45 | |
---|
46 | //this->bufferSize = 0; |
---|
47 | this->bufferText = NULL; |
---|
48 | this->setBufferSize(100); |
---|
49 | this->setBufferDisplaySize(10); |
---|
50 | this->setAbsCoor2D(3, GraphicsEngine::getInstance()->getResolutionY()); |
---|
51 | this->delayed = 0; |
---|
52 | this->setRepeatDelay(.3, .05); |
---|
53 | this->pressedKey = SDLK_FIRST; |
---|
54 | |
---|
55 | this->inputLineText = TextEngine::getInstance()->createText("fonts/earth.ttf", 10, TEXT_DYNAMIC, 255, 0, 0); |
---|
56 | this->inputLineText->setAlignment(TEXT_ALIGN_LEFT); |
---|
57 | this->inputLineText->setText(NULL); |
---|
58 | this->inputLine = new char[1]; |
---|
59 | this->inputLine[0] = '\0'; |
---|
60 | this->inputLineText->setParent2D(this); |
---|
61 | |
---|
62 | |
---|
63 | EventHandler* evh = EventHandler::getInstance(); |
---|
64 | evh->subscribe(this, ES_ALL, SDLK_BACKQUOTE); |
---|
65 | for (int i = 1; i < SDLK_F15; i++) |
---|
66 | evh->subscribe(this, ES_SHELL, i); |
---|
67 | } |
---|
68 | |
---|
69 | Shell* Shell::singletonRef = NULL; |
---|
70 | |
---|
71 | /** |
---|
72 | * standard deconstructor |
---|
73 | */ |
---|
74 | Shell::~Shell () |
---|
75 | { |
---|
76 | // delete the displayable Buffers |
---|
77 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
78 | delete this->bufferText[i]; |
---|
79 | delete this->bufferText; |
---|
80 | |
---|
81 | // delete the inputLine |
---|
82 | delete this->inputLineText; |
---|
83 | delete this->inputLine; |
---|
84 | |
---|
85 | // delete all the Chars in the Buffers |
---|
86 | tIterator<char>* charIterator = this->buffer->getIterator(); |
---|
87 | char* charElem = charIterator->nextElement(); |
---|
88 | while (charElem != NULL) |
---|
89 | { |
---|
90 | delete charElem; |
---|
91 | charElem = charIterator->nextElement(); |
---|
92 | } |
---|
93 | delete charIterator; |
---|
94 | |
---|
95 | Shell::singletonRef = NULL; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * sets The count of Lines to display in the buffer. |
---|
100 | * @param bufferDisplaySize the count of lines to display in the Shell-Buffer. |
---|
101 | */ |
---|
102 | void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) |
---|
103 | { |
---|
104 | if (this->bufferText != NULL) |
---|
105 | { |
---|
106 | for (unsigned int i = 0; i < this->bufferDisplaySize; i++) |
---|
107 | delete this->bufferText[i]; |
---|
108 | delete this->bufferText; |
---|
109 | } |
---|
110 | |
---|
111 | this->bufferText = new Text*[bufferDisplaySize]; |
---|
112 | for (unsigned int i = 0; i < bufferDisplaySize; i++) |
---|
113 | { |
---|
114 | this->bufferText[i] = TextEngine::getInstance()->createText("fonts/earth.ttf", this->textSize, TEXT_DYNAMIC, 255, 0, 0); |
---|
115 | this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT); |
---|
116 | this->bufferText[i]->setRelCoor2D(5, 12+12*i); |
---|
117 | this->bufferText[i]->setText(NULL); |
---|
118 | this->bufferText[i]->setParent2D(this); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | this->bufferDisplaySize = bufferDisplaySize; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * deletes all the Buffers |
---|
127 | */ |
---|
128 | void Shell::flushBuffers() |
---|
129 | { |
---|
130 | // remove all chars from the BufferTexts. |
---|
131 | if (this->bufferText) |
---|
132 | for (int i; i < this->bufferDisplaySize; i++) |
---|
133 | { |
---|
134 | this->bufferText[i]->setText(NULL); |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | // delete all the Chars in the Buffers |
---|
139 | tIterator<char>* charIterator = this->buffer->getIterator(); |
---|
140 | char* charElem = charIterator->nextElement(); |
---|
141 | |
---|
142 | while (charElem != NULL) |
---|
143 | { |
---|
144 | delete charElem; |
---|
145 | |
---|
146 | charElem = charIterator->nextElement(); |
---|
147 | } |
---|
148 | delete charIterator; |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * adds a new Line to the List of Buffers |
---|
153 | * @param line the Line as in the first argument in printf |
---|
154 | * @param args the arguments as a va_list |
---|
155 | * |
---|
156 | * @todo optimize |
---|
157 | */ |
---|
158 | bool Shell::addBufferLineStatic(const char* line, ...) |
---|
159 | { |
---|
160 | va_list arguments; |
---|
161 | va_start(arguments, line); |
---|
162 | |
---|
163 | #if DEBUG < 3 |
---|
164 | if (Shell::singletonRef == NULL) |
---|
165 | #endif |
---|
166 | |
---|
167 | vprintf(line, arguments); |
---|
168 | #if DEBUG < 3 |
---|
169 | else |
---|
170 | #else |
---|
171 | if (Shell::singletonRef != NULL) |
---|
172 | #endif |
---|
173 | Shell::singletonRef->addBufferLine(line, arguments); |
---|
174 | return true; |
---|
175 | } |
---|
176 | int curr = 0; |
---|
177 | |
---|
178 | /** |
---|
179 | * add a Line to the List of Buffers |
---|
180 | * @param line |
---|
181 | * @param arguments |
---|
182 | * |
---|
183 | * This function Adds one line to the buffer. |
---|
184 | * and displays the line as the First Line of the display-buffer |
---|
185 | */ |
---|
186 | void Shell::addBufferLine(const char* line, va_list arguments) |
---|
187 | { |
---|
188 | vsprintf(this->bufferArray, line, arguments); |
---|
189 | |
---|
190 | char* newLine = new char[strlen(this->bufferArray)+1]; |
---|
191 | strcpy(newLine, this->bufferArray); |
---|
192 | |
---|
193 | this->buffer->add(newLine); |
---|
194 | |
---|
195 | if (this->buffer->getSize() > this->bufferSize) |
---|
196 | { |
---|
197 | delete this->buffer->firstElement(); |
---|
198 | this->buffer->remove(this->buffer->firstElement()); |
---|
199 | } |
---|
200 | |
---|
201 | if (likely(bufferText != NULL)) |
---|
202 | { |
---|
203 | Text* moveText = this->bufferText[this->bufferDisplaySize-1]; |
---|
204 | for (int i = this->bufferDisplaySize-1; i > 0; i--) |
---|
205 | { |
---|
206 | this->bufferText[i] = this->bufferText[i-1]; |
---|
207 | } |
---|
208 | this->bufferText[0] = moveText; |
---|
209 | } |
---|
210 | this->bufferText[0]->setText(newLine); |
---|
211 | // this->bufferText-> |
---|
212 | // this->inputLineText->setText(newLine); |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * moves the buffer around lineCount lines upwards (negative values move down) |
---|
217 | * @param lineCount the Count of lines to move upwards |
---|
218 | * |
---|
219 | * @todo |
---|
220 | */ |
---|
221 | void Shell::moveBuffer(int lineCount) |
---|
222 | { |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * @param lineNumber the n-th line from the bottom |
---|
227 | * @returns the Buffer at Line lineNumber |
---|
228 | */ |
---|
229 | const char* Shell::getBufferLine(unsigned int lineNumber) |
---|
230 | { |
---|
231 | tIterator<char>* charIterator = this->buffer->getIterator(); |
---|
232 | char* charElem = charIterator->nextElement(); |
---|
233 | |
---|
234 | int i = 1; |
---|
235 | while (charElem != NULL) |
---|
236 | { |
---|
237 | if (i++ < lineNumber) |
---|
238 | { |
---|
239 | delete charIterator; |
---|
240 | return charElem; |
---|
241 | } |
---|
242 | |
---|
243 | charElem = charIterator->nextElement(); |
---|
244 | } |
---|
245 | delete charIterator; |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | /** |
---|
250 | * deletes the InputLine |
---|
251 | */ |
---|
252 | void Shell::flushInputLine() |
---|
253 | { |
---|
254 | if (likely(this->inputLine != NULL)) |
---|
255 | { |
---|
256 | delete [] this->inputLine; |
---|
257 | } |
---|
258 | this->inputLine = new char[1]; |
---|
259 | *this->inputLine = '\0'; |
---|
260 | |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * adds one character to the inputLine |
---|
265 | * @param character the character to add to the inputLine |
---|
266 | */ |
---|
267 | void Shell::addCharacter(char character) |
---|
268 | { |
---|
269 | char* addCharLine = new char[strlen(inputLine)+2]; |
---|
270 | |
---|
271 | sprintf(addCharLine, "%s%c", this->inputLine, character); |
---|
272 | delete this->inputLine; |
---|
273 | this->inputLine = addCharLine; |
---|
274 | this->inputLineText->setText(inputLine); |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * adds multiple Characters to thr inputLine |
---|
279 | * @param characters a '\0' terminated char-array to add to the InputLine |
---|
280 | */ |
---|
281 | void Shell::addCharacters(const char* characters) |
---|
282 | { |
---|
283 | char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1]; |
---|
284 | |
---|
285 | sprintf(addCharLine, "%s%s", this->inputLine, characters); |
---|
286 | delete this->inputLine; |
---|
287 | this->inputLine = addCharLine; |
---|
288 | this->inputLineText->setText(inputLine); |
---|
289 | } |
---|
290 | |
---|
291 | /** |
---|
292 | * removes characterCount characters from the InputLine |
---|
293 | * @param characterCount the count of Characters to remove from the input Line |
---|
294 | */ |
---|
295 | void Shell::removeCharacters(unsigned int characterCount) |
---|
296 | { |
---|
297 | if (strlen(this->inputLine) == 0) |
---|
298 | return; |
---|
299 | |
---|
300 | if (characterCount > strlen(this->inputLine)) |
---|
301 | characterCount = strlen(this->inputLine); |
---|
302 | |
---|
303 | char* removeCharLine = new char[strlen(inputLine)-characterCount+1]; |
---|
304 | |
---|
305 | strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount); |
---|
306 | removeCharLine[strlen(inputLine)-characterCount] = '\0'; |
---|
307 | delete this->inputLine; |
---|
308 | this->inputLine = removeCharLine; |
---|
309 | this->inputLineText->setText(inputLine); |
---|
310 | } |
---|
311 | |
---|
312 | /** |
---|
313 | * executes the command stored in the inputLine |
---|
314 | * @return true if the command was commited successfully, false otherwise |
---|
315 | */ |
---|
316 | bool Shell::executeCommand() |
---|
317 | { |
---|
318 | |
---|
319 | this->addBufferLineStatic("Execute Command: %s\n", this->inputLine); |
---|
320 | delete this->inputLine; |
---|
321 | this->inputLine = new char[1]; |
---|
322 | this->inputLine[0]='\0'; |
---|
323 | this->inputLineText->setText(this->inputLine); |
---|
324 | return false; |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * sets the Repeate-delay and rate |
---|
329 | * @param repeatDelay the Delay it takes, to repeate a key |
---|
330 | * @param repeatRate the rate to repeate a pressed key |
---|
331 | */ |
---|
332 | void Shell::setRepeatDelay(float repeatDelay, float repeatRate) |
---|
333 | { |
---|
334 | this->repeatDelay = repeatDelay; |
---|
335 | this->repeatRate = repeatRate; |
---|
336 | |
---|
337 | } |
---|
338 | |
---|
339 | |
---|
340 | #include "key_names.h" |
---|
341 | /** |
---|
342 | * listens for some event |
---|
343 | * @param event the Event happened |
---|
344 | */ |
---|
345 | void Shell::process(const Event &event) |
---|
346 | { |
---|
347 | if (event.bPressed) |
---|
348 | { |
---|
349 | PRINTF(4)("Shell received command %s\n", SDLKToKeyname(event.type)); |
---|
350 | if (event.type == SDLK_BACKQUOTE) |
---|
351 | { |
---|
352 | if (EventHandler::getInstance()->getState() == ES_GAME) |
---|
353 | { |
---|
354 | EventHandler::getInstance()->setState(ES_SHELL); |
---|
355 | this->setRelCoorSoft2D(0, GraphicsEngine::getInstance()->getResolutionY()-150, 1, 5); |
---|
356 | } |
---|
357 | |
---|
358 | else |
---|
359 | { |
---|
360 | EventHandler::getInstance()->setState(ES_GAME); |
---|
361 | this->setRelCoorSoft2D(0, GraphicsEngine::getInstance()->getResolutionY()+10, 1, 5); |
---|
362 | } |
---|
363 | } |
---|
364 | else if (event.type == SDLK_TAB) |
---|
365 | this->autoComplete(); |
---|
366 | else if (event.type == SDLK_BACKSPACE) |
---|
367 | { |
---|
368 | this->delayed = this->repeatDelay; |
---|
369 | this->pressedKey = SDLK_BACKSPACE; |
---|
370 | this->removeCharacters(1); |
---|
371 | } |
---|
372 | else if (event.type == SDLK_RETURN) |
---|
373 | this->executeCommand(); |
---|
374 | else if (likely(event.type < 127)) |
---|
375 | { |
---|
376 | this->delayed = this->repeatDelay; |
---|
377 | this->pressedKey = event.type; |
---|
378 | this->addCharacter(event.type); |
---|
379 | } |
---|
380 | } |
---|
381 | else // if(!event.bPressed) |
---|
382 | { |
---|
383 | if (this->pressedKey == event.type) |
---|
384 | this->pressedKey = SDLK_FIRST; |
---|
385 | this->delayed = 0.0; |
---|
386 | } |
---|
387 | } |
---|
388 | |
---|
389 | /** |
---|
390 | * ticks the Shell for dt Seconds |
---|
391 | * @param dt the elapsed time since the last tick(); |
---|
392 | */ |
---|
393 | void Shell::tick(float dt) |
---|
394 | { |
---|
395 | if (this->delayed > 0.0) |
---|
396 | this->delayed -= dt; |
---|
397 | else if (this->pressedKey != SDLK_FIRST ) |
---|
398 | { |
---|
399 | this->delayed = this->repeatRate; |
---|
400 | if (this->pressedKey == SDLK_BACKSPACE) |
---|
401 | this->removeCharacters(1); |
---|
402 | else if (pressedKey < 127) |
---|
403 | this->addCharacter(this->pressedKey); |
---|
404 | } |
---|
405 | } |
---|
406 | |
---|
407 | /** |
---|
408 | * displays the Shell |
---|
409 | */ |
---|
410 | void Shell::draw() const |
---|
411 | { |
---|
412 | glPushMatrix(); |
---|
413 | // transform for alignment. |
---|
414 | // setting the Blending effects |
---|
415 | |
---|
416 | glColor4f(0.0f, 0.0f, 0.8f, .4); |
---|
417 | glEnable(GL_BLEND); |
---|
418 | glDisable(GL_TEXTURE_2D); |
---|
419 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
420 | |
---|
421 | // glBindTexture(GL_TEXTURE_2D, this->texture); |
---|
422 | glBegin(GL_QUADS); |
---|
423 | |
---|
424 | // glTexCoord2f(this->texCoord.minU, this->texCoord.minV); |
---|
425 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
426 | |
---|
427 | // glTexCoord2f(this->texCoord.maxU, this->texCoord.minV); |
---|
428 | glVertex2f(this->getAbsCoor2D().x + 800, this->getAbsCoor2D().y ); |
---|
429 | |
---|
430 | // glTexCoord2f(this->texCoord.maxU, this->texCoord.maxV); |
---|
431 | glVertex2f(this->getAbsCoor2D().x + 800, this->getAbsCoor2D().y + 150); |
---|
432 | |
---|
433 | // glTexCoord2f(this->texCoord.minU, this->texCoord.maxV); |
---|
434 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + 150); |
---|
435 | |
---|
436 | glEnd(); |
---|
437 | } |
---|
438 | |
---|
439 | |
---|
440 | /** |
---|
441 | * autocompletes the Shell's inputLine |
---|
442 | * @returns true, if a result was found, false otherwise |
---|
443 | * |
---|
444 | * @todo implement it!! |
---|
445 | */ |
---|
446 | bool Shell::autoComplete() |
---|
447 | { |
---|
448 | //PRINTF(3)("AutoCompletion not implemented yet\n"); |
---|
449 | |
---|
450 | char* completionLine = new char[strlen(inputLine)+1]; |
---|
451 | strcpy(completionLine, this->inputLine); |
---|
452 | |
---|
453 | char* commandBegin = strrchr(completionLine, ' '); |
---|
454 | if (commandBegin == NULL) |
---|
455 | commandBegin = completionLine; |
---|
456 | else |
---|
457 | { |
---|
458 | if(commandBegin >= completionLine + strlen(completionLine)) |
---|
459 | commandBegin = completionLine + strlen(completionLine); |
---|
460 | else |
---|
461 | commandBegin++; |
---|
462 | } |
---|
463 | |
---|
464 | printf("%s\n",commandBegin); |
---|
465 | Array<char*>* classArray = LoadClassDescription::searchClassWithShort(commandBegin); |
---|
466 | if (classArray->getCount() == 0) |
---|
467 | { |
---|
468 | delete[] completionLine; |
---|
469 | delete classArray; |
---|
470 | //PRINTF(0)("no completion found for %s\n", commandBegin); |
---|
471 | return false; |
---|
472 | } |
---|
473 | |
---|
474 | for (unsigned int i = 0; i < classArray->getCount(); i++) |
---|
475 | { |
---|
476 | PRINTF(0)("%s\n", classArray->getEntry(i)); |
---|
477 | } |
---|
478 | if (classArray->getCount() == 1) |
---|
479 | { |
---|
480 | this->removeCharacters(strlen(commandBegin)); |
---|
481 | this->addCharacters(classArray->getEntry(0)); |
---|
482 | this->addCharacter(' '); |
---|
483 | } |
---|
484 | |
---|
485 | delete[] completionLine; |
---|
486 | delete classArray; |
---|
487 | } |
---|
488 | |
---|
489 | /** |
---|
490 | * displays some nice output from the Shell |
---|
491 | */ |
---|
492 | void Shell::debug() const |
---|
493 | { |
---|
494 | if (this->pressedKey != SDLK_FIRST) |
---|
495 | printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay); |
---|
496 | |
---|
497 | } |
---|