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 | #include "shell_command.h" |
---|
20 | |
---|
21 | #include "text_engine.h" |
---|
22 | #include "list.h" |
---|
23 | #include "graphics_engine.h" |
---|
24 | #include "event_handler.h" |
---|
25 | #include "debug.h" |
---|
26 | #include "class_list.h" |
---|
27 | |
---|
28 | #include "key_names.h" |
---|
29 | #include <stdarg.h> |
---|
30 | #include <stdio.h> |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | SHELL_COMMAND(clear, Shell, clear)->describe("Clears the shell from unwanted lines (empties all buffers)"); |
---|
35 | |
---|
36 | /** |
---|
37 | * standard constructor |
---|
38 | */ |
---|
39 | Shell::Shell () |
---|
40 | { |
---|
41 | this->setClassID(CL_SHELL, "Shell"); |
---|
42 | this->setName("Shell"); |
---|
43 | |
---|
44 | this->keepBufferArray[0] = '\0'; |
---|
45 | this->keepBuffer = false; |
---|
46 | |
---|
47 | this->bActive = false; |
---|
48 | this->buffer = new tList<char>; |
---|
49 | this->bufferIterator = this->buffer->getIterator(); |
---|
50 | |
---|
51 | this->inputHistory = new tList<char>; |
---|
52 | //this->commandList = new tList<ShellCommand>; |
---|
53 | |
---|
54 | this->textSize = 15; |
---|
55 | this->lineSpacing = 5; |
---|
56 | |
---|
57 | //this->bufferSize = 0; |
---|
58 | this->bufferText = NULL; |
---|
59 | this->setBufferSize(10); |
---|
60 | this->bufferDisplaySize = 10; |
---|
61 | this->setAbsCoor2D(3, -400); |
---|
62 | this->delayed = 0; |
---|
63 | this->setRepeatDelay(.3, .05); |
---|
64 | this->pressedKey = SDLK_FIRST; |
---|
65 | |
---|
66 | this->inputLineText = NULL; |
---|
67 | this->inputLine = new char[1]; |
---|
68 | this->inputLine[0] = '\0'; |
---|
69 | |
---|
70 | this->rebuildText(); |
---|
71 | this->completionList = NULL; |
---|
72 | |
---|
73 | // EVENT-Handler subscription of '`' to all States, and all other keyboard commands to ES_SEHLL |
---|
74 | EventHandler* evh = EventHandler::getInstance(); |
---|
75 | evh->subscribe(this, ES_ALL, SDLK_BACKQUOTE); |
---|
76 | for (int i = 1; i < SDLK_LAST; i++) |
---|
77 | evh->subscribe(this, ES_SHELL, i); |
---|
78 | } |
---|
79 | |
---|
80 | Shell* Shell::singletonRef = NULL; |
---|
81 | |
---|
82 | /** |
---|
83 | * standard deconstructor |
---|
84 | */ |
---|
85 | Shell::~Shell () |
---|
86 | { |
---|
87 | // delete the displayable Buffers |
---|
88 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
89 | delete this->bufferText[i]; |
---|
90 | delete[] this->bufferText; |
---|
91 | |
---|
92 | // delete the inputLine |
---|
93 | delete this->inputLineText; |
---|
94 | delete this->inputLine; |
---|
95 | |
---|
96 | // delete all the Chars in the Buffers |
---|
97 | char* charElem = this->bufferIterator->firstElement(); |
---|
98 | while (charElem != NULL) |
---|
99 | { |
---|
100 | delete charElem; |
---|
101 | charElem = this->bufferIterator->nextElement(); |
---|
102 | } |
---|
103 | delete this->bufferIterator; |
---|
104 | |
---|
105 | // if (this->completionList != NULL) |
---|
106 | //delete this->completionList; |
---|
107 | |
---|
108 | Shell::singletonRef = NULL; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * activates the shell |
---|
113 | * |
---|
114 | * This also feeds the Last few lines from the main buffers into the displayBuffer |
---|
115 | */ |
---|
116 | void Shell::activate() |
---|
117 | { |
---|
118 | if (this->bActive == true) |
---|
119 | PRINTF(3)("The shell is already active\n"); |
---|
120 | this->bActive = true; |
---|
121 | |
---|
122 | EventHandler::getInstance()->setState(ES_SHELL); |
---|
123 | this->setRelCoorSoft2D(0, 0, 1, 5); |
---|
124 | |
---|
125 | this->bufferIterator->lastElement(); |
---|
126 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
127 | this->bufferText[i]->setText(this->bufferIterator->prevElement(), true); |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * deactiveates the Shell. |
---|
132 | */ |
---|
133 | void Shell::deactivate() |
---|
134 | { |
---|
135 | if (this->bActive == false) |
---|
136 | PRINTF(3)("The shell is already inactive\n"); |
---|
137 | this->bActive = false; |
---|
138 | |
---|
139 | EventHandler::getInstance()->setState(ES_GAME); |
---|
140 | this->setRelCoorSoft2D(0, -400, 1, 5); |
---|
141 | |
---|
142 | this->bufferIterator->lastElement(); |
---|
143 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
144 | this->bufferText[i]->setText(this->bufferIterator->prevElement(), false); |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | /** |
---|
149 | * sets the size of the text and spacing |
---|
150 | * @param textSize the size of the Text in Pixels |
---|
151 | * @param lineSpacing the size of the Spacing between two lines in pixels |
---|
152 | * |
---|
153 | * this also rebuilds the entire Text, inputLine and displayBuffer, |
---|
154 | * to be accurate again. |
---|
155 | */ |
---|
156 | void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing) |
---|
157 | { |
---|
158 | this->textSize = textSize; |
---|
159 | this->lineSpacing = lineSpacing; |
---|
160 | |
---|
161 | this->rebuildText(); |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * rebuilds the Text's |
---|
166 | * |
---|
167 | * use this function, if you changed the Font/Size or something else. |
---|
168 | */ |
---|
169 | void Shell::rebuildText() |
---|
170 | { |
---|
171 | if (this->inputLineText == NULL) |
---|
172 | delete this->inputLineText; |
---|
173 | this->inputLineText = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC); |
---|
174 | this->inputLineText->setColor(1, 0, 0); |
---|
175 | this->inputLineText->setAlignment(TEXT_ALIGN_LEFT); |
---|
176 | this->inputLineText->setText(NULL); |
---|
177 | this->inputLineText->setParent2D(this); |
---|
178 | this->inputLineText->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize); |
---|
179 | |
---|
180 | this->setBufferDisplaySize(this->bufferDisplaySize); |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * sets The count of Lines to display in the buffer. |
---|
185 | * @param bufferDisplaySize the count of lines to display in the Shell-Buffer. |
---|
186 | */ |
---|
187 | void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) |
---|
188 | { |
---|
189 | if (this->bufferText != NULL) |
---|
190 | { |
---|
191 | for (unsigned int i = 0; i < this->bufferDisplaySize; i++) |
---|
192 | delete this->bufferText[i]; |
---|
193 | delete[] this->bufferText; |
---|
194 | } |
---|
195 | |
---|
196 | this->bufferText = new Text*[bufferDisplaySize]; |
---|
197 | for (unsigned int i = 0; i < bufferDisplaySize; i++) |
---|
198 | { |
---|
199 | this->bufferText[i] = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC); |
---|
200 | this->bufferText[i]->setColor(1, 0, 0); |
---|
201 | this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT); |
---|
202 | this->bufferText[i]->setRelCoor2D(calculateLinePosition(i)); |
---|
203 | this->bufferText[i]->setText(NULL); |
---|
204 | this->bufferText[i]->setParent2D(this); |
---|
205 | } |
---|
206 | this->bufferDisplaySize = bufferDisplaySize; |
---|
207 | |
---|
208 | this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1); |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * deletes all the Buffers |
---|
213 | */ |
---|
214 | void Shell::flushBuffers() |
---|
215 | { |
---|
216 | // remove all chars from the BufferTexts. |
---|
217 | if (this->bufferText) |
---|
218 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
219 | { |
---|
220 | this->bufferText[i]->setText(NULL, true); |
---|
221 | } |
---|
222 | |
---|
223 | // delete all the Chars in the Buffers |
---|
224 | tIterator<char>* charIterator = this->buffer->getIterator(); |
---|
225 | char* charElem = charIterator->firstElement(); |
---|
226 | while (charElem != NULL) |
---|
227 | { |
---|
228 | delete charElem; |
---|
229 | |
---|
230 | charElem = charIterator->nextElement(); |
---|
231 | } |
---|
232 | delete charIterator; |
---|
233 | delete this->buffer; |
---|
234 | this->buffer = new tList<char>; |
---|
235 | } |
---|
236 | |
---|
237 | /** |
---|
238 | * adds a new Line to the List of Buffers |
---|
239 | * @param line the Line as in the first argument in printf |
---|
240 | */ |
---|
241 | bool Shell::addBufferLineStatic(const char* line, ...) |
---|
242 | { |
---|
243 | va_list arguments; |
---|
244 | va_start(arguments, line); |
---|
245 | |
---|
246 | #if DEBUG < 3 |
---|
247 | if (Shell::singletonRef == NULL) |
---|
248 | #endif |
---|
249 | |
---|
250 | vprintf(line, arguments); |
---|
251 | #if DEBUG < 3 |
---|
252 | else |
---|
253 | #else |
---|
254 | if (Shell::singletonRef != NULL) |
---|
255 | #endif |
---|
256 | Shell::singletonRef->addBufferLine(line, arguments); |
---|
257 | return true; |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * add a Line to the List of Buffers |
---|
262 | * @param line |
---|
263 | * @param arguments |
---|
264 | * |
---|
265 | * This function Adds one line to the buffer. |
---|
266 | * and displays the line as the First Line of the display-buffer |
---|
267 | */ |
---|
268 | void Shell::addBufferLine(const char* line, va_list arguments) |
---|
269 | { |
---|
270 | vsprintf(this->bufferArray, line, arguments); |
---|
271 | |
---|
272 | char* inputEnd; |
---|
273 | char* newLineBegin; |
---|
274 | char* newLineEnd; |
---|
275 | |
---|
276 | // check if we have something left in the buffers |
---|
277 | if (unlikely(this->keepBuffer)) |
---|
278 | { |
---|
279 | strcat(this->keepBufferArray, this->bufferArray); |
---|
280 | inputEnd = this->keepBufferArray + strlen(this->keepBufferArray); |
---|
281 | newLineBegin = this->keepBufferArray; |
---|
282 | this->keepBuffer = false; |
---|
283 | } |
---|
284 | else |
---|
285 | { |
---|
286 | inputEnd = this->bufferArray + strlen(this->bufferArray); |
---|
287 | newLineBegin = this->bufferArray; |
---|
288 | } |
---|
289 | |
---|
290 | // adding all the new Lines |
---|
291 | while (newLineBegin < inputEnd) |
---|
292 | { |
---|
293 | newLineEnd = strchr(newLineBegin, '\n'); |
---|
294 | if (newLineEnd != NULL && *newLineEnd == '\n') |
---|
295 | *newLineEnd = '\0'; |
---|
296 | else |
---|
297 | { |
---|
298 | // newLineEnd = newLineBegin + strlen(newLineBegin); |
---|
299 | strcpy(this->keepBufferArray, newLineBegin); |
---|
300 | this->keepBuffer = true; |
---|
301 | break; |
---|
302 | } |
---|
303 | |
---|
304 | char* addLine = new char[strlen(newLineBegin)+1]; |
---|
305 | strcpy(addLine, newLineBegin); |
---|
306 | |
---|
307 | this->buffer->add(addLine); |
---|
308 | |
---|
309 | if (this->buffer->getSize() > this->bufferSize) |
---|
310 | { |
---|
311 | delete this->buffer->firstElement(); |
---|
312 | this->buffer->remove(this->buffer->firstElement()); |
---|
313 | } |
---|
314 | |
---|
315 | if (this->bActive) |
---|
316 | { |
---|
317 | this->printToDisplayBuffer(addLine); |
---|
318 | } |
---|
319 | newLineBegin = newLineEnd+1; |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | * prints out some text to the input-buffers |
---|
325 | * @param text the text to output. |
---|
326 | */ |
---|
327 | void Shell::printToDisplayBuffer(const char* text) |
---|
328 | { |
---|
329 | if(likely(bufferText != NULL)) |
---|
330 | { |
---|
331 | Text* lastText = this->bufferText[this->bufferDisplaySize-1]; |
---|
332 | |
---|
333 | Text* swapText; |
---|
334 | Text* moveText = this->bufferText[0]; |
---|
335 | this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10); |
---|
336 | for (unsigned int i = 1; i < this->bufferDisplaySize; i++) |
---|
337 | { |
---|
338 | if ( i < this->bufferDisplaySize-1) |
---|
339 | this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5); |
---|
340 | swapText = this->bufferText[i]; |
---|
341 | this ->bufferText[i] = moveText; |
---|
342 | moveText = swapText; |
---|
343 | } |
---|
344 | lastText->setRelCoor2D(this->calculateLinePosition(0)); |
---|
345 | this->bufferText[0] = lastText; |
---|
346 | |
---|
347 | this->bufferText[0]->setText(text, true); |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | /** |
---|
352 | * moves the buffer around lineCount lines upwards (negative values move down) |
---|
353 | * @param lineCount the Count of lines to move upwards |
---|
354 | * |
---|
355 | * @todo |
---|
356 | */ |
---|
357 | void Shell::moveBuffer(unsigned int lineCount) |
---|
358 | { |
---|
359 | } |
---|
360 | |
---|
361 | /** |
---|
362 | * @param lineNumber the n-th line from the bottom |
---|
363 | * @returns the Buffer at Line lineNumber |
---|
364 | */ |
---|
365 | const char* Shell::getBufferLine(unsigned int lineNumber) |
---|
366 | { |
---|
367 | tIterator<char>* charIterator = this->buffer->getIterator(); |
---|
368 | char* charElem = charIterator->firstElement(); |
---|
369 | |
---|
370 | int i = 1; |
---|
371 | while (charElem != NULL) |
---|
372 | { |
---|
373 | if (i++ < lineNumber) |
---|
374 | { |
---|
375 | delete charIterator; |
---|
376 | return charElem; |
---|
377 | } |
---|
378 | |
---|
379 | charElem = charIterator->nextElement(); |
---|
380 | } |
---|
381 | delete charIterator; |
---|
382 | } |
---|
383 | |
---|
384 | /** |
---|
385 | * deletes the InputLine |
---|
386 | */ |
---|
387 | void Shell::flushInputLine() |
---|
388 | { |
---|
389 | if (likely(this->inputLine != NULL)) |
---|
390 | { |
---|
391 | delete[] this->inputLine; |
---|
392 | } |
---|
393 | this->inputLine = new char[1]; |
---|
394 | *this->inputLine = '\0'; |
---|
395 | this->inputLineText->setText(this->inputLine, true); |
---|
396 | } |
---|
397 | |
---|
398 | /** |
---|
399 | * adds one character to the inputLine |
---|
400 | * @param character the character to add to the inputLine |
---|
401 | */ |
---|
402 | void Shell::addCharacter(char character) |
---|
403 | { |
---|
404 | char* addCharLine = new char[strlen(inputLine)+2]; |
---|
405 | |
---|
406 | sprintf(addCharLine, "%s%c", this->inputLine, character); |
---|
407 | delete this->inputLine; |
---|
408 | this->inputLine = addCharLine; |
---|
409 | this->inputLineText->setText(inputLine, true); |
---|
410 | } |
---|
411 | |
---|
412 | /** |
---|
413 | * adds multiple Characters to thr inputLine |
---|
414 | * @param characters a \\0 terminated char-array to add to the InputLine |
---|
415 | */ |
---|
416 | void Shell::addCharacters(const char* characters) |
---|
417 | { |
---|
418 | char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1]; |
---|
419 | |
---|
420 | sprintf(addCharLine, "%s%s", this->inputLine, characters); |
---|
421 | delete this->inputLine; |
---|
422 | this->inputLine = addCharLine; |
---|
423 | this->inputLineText->setText(inputLine, true); |
---|
424 | } |
---|
425 | |
---|
426 | /** |
---|
427 | * removes characterCount characters from the InputLine |
---|
428 | * @param characterCount the count of Characters to remove from the input Line |
---|
429 | */ |
---|
430 | void Shell::removeCharacters(unsigned int characterCount) |
---|
431 | { |
---|
432 | if (strlen(this->inputLine) == 0) |
---|
433 | return; |
---|
434 | |
---|
435 | if (characterCount > strlen(this->inputLine)) |
---|
436 | characterCount = strlen(this->inputLine); |
---|
437 | |
---|
438 | char* removeCharLine = new char[strlen(inputLine)-characterCount+1]; |
---|
439 | |
---|
440 | strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount); |
---|
441 | removeCharLine[strlen(inputLine)-characterCount] = '\0'; |
---|
442 | delete this->inputLine; |
---|
443 | this->inputLine = removeCharLine; |
---|
444 | this->inputLineText->setText(inputLine, true); |
---|
445 | } |
---|
446 | |
---|
447 | /** |
---|
448 | * executes the command stored in the inputLine |
---|
449 | * @return true if the command was commited successfully, false otherwise |
---|
450 | */ |
---|
451 | bool Shell::executeCommand() |
---|
452 | { |
---|
453 | this->addBufferLineStatic("Execute Command: %s\n", this->inputLine); |
---|
454 | |
---|
455 | char* newCommand = new char[strlen(this->inputLine)+1]; |
---|
456 | strcpy(newCommand, this->inputLine); |
---|
457 | this->inputHistory->add(newCommand); |
---|
458 | |
---|
459 | ShellCommandBase::execute(this->inputLine); |
---|
460 | |
---|
461 | this->flushInputLine(); |
---|
462 | |
---|
463 | return false; |
---|
464 | } |
---|
465 | |
---|
466 | /** |
---|
467 | * clears the Shell (empties all buffers) |
---|
468 | */ |
---|
469 | void Shell::clear() |
---|
470 | { |
---|
471 | this->flushBuffers(); |
---|
472 | this->addBufferLine("orxonox - shell\n ==================== \n", NULL); |
---|
473 | } |
---|
474 | |
---|
475 | /** |
---|
476 | * sets the Repeate-delay and rate |
---|
477 | * @param repeatDelay the Delay it takes, to repeate a key |
---|
478 | * @param repeatRate the rate to repeate a pressed key |
---|
479 | */ |
---|
480 | void Shell::setRepeatDelay(float repeatDelay, float repeatRate) |
---|
481 | { |
---|
482 | this->repeatDelay = repeatDelay; |
---|
483 | this->repeatRate = repeatRate; |
---|
484 | |
---|
485 | } |
---|
486 | |
---|
487 | /** |
---|
488 | * listens for some event |
---|
489 | * @param event the Event happened |
---|
490 | */ |
---|
491 | void Shell::process(const Event &event) |
---|
492 | { |
---|
493 | if (event.bPressed) |
---|
494 | { |
---|
495 | PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type)); |
---|
496 | if (event.type == SDLK_BACKQUOTE) |
---|
497 | { |
---|
498 | if (EventHandler::getInstance()->getState() == ES_GAME) |
---|
499 | this->activate(); |
---|
500 | else |
---|
501 | this->deactivate(); |
---|
502 | } |
---|
503 | else if (event.type == SDLK_F1) |
---|
504 | this->help(); |
---|
505 | else if (event.type == SDLK_F2) |
---|
506 | this->debug(); |
---|
507 | else if (event.type == SDLK_TAB) |
---|
508 | this->autoComplete(); |
---|
509 | else if (event.type == SDLK_BACKSPACE) |
---|
510 | { |
---|
511 | this->delayed = this->repeatDelay; |
---|
512 | this->pressedKey = SDLK_BACKSPACE; |
---|
513 | this->removeCharacters(1); |
---|
514 | } |
---|
515 | else if (event.type == SDLK_RETURN) |
---|
516 | this->executeCommand(); |
---|
517 | /* |
---|
518 | else if (event.type == SDLK_UP) |
---|
519 | { |
---|
520 | // this->flushInputLine(); |
---|
521 | tIterator<char>* iterator = this->commandList->getIterator(); |
---|
522 | char* command = iterator->lastElement(); |
---|
523 | while (command) |
---|
524 | { |
---|
525 | if (!strcmp (command, inputLine)) |
---|
526 | { |
---|
527 | inputLine = iterator->prevElement(); |
---|
528 | return; |
---|
529 | } |
---|
530 | command = iterator->prevElement(); |
---|
531 | } |
---|
532 | inputLine = iterator->lastElement(); |
---|
533 | } |
---|
534 | */ |
---|
535 | else if (likely(event.type < 127)) |
---|
536 | { |
---|
537 | Uint8 *keystate = SDL_GetKeyState(NULL); |
---|
538 | this->delayed = this->repeatDelay; |
---|
539 | if (unlikely( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] )) |
---|
540 | { |
---|
541 | this->pressedKey = event.type-32; |
---|
542 | this->addCharacter(event.type-32); |
---|
543 | } |
---|
544 | else |
---|
545 | { |
---|
546 | this->pressedKey = event.type; |
---|
547 | this->addCharacter(event.type); |
---|
548 | } |
---|
549 | } |
---|
550 | } |
---|
551 | else // if(!event.bPressed) |
---|
552 | { |
---|
553 | if (this->pressedKey == event.type || (this->pressedKey == event.type - 32)) |
---|
554 | { |
---|
555 | this->pressedKey = SDLK_FIRST; |
---|
556 | this->delayed = 0.0; |
---|
557 | } |
---|
558 | } |
---|
559 | } |
---|
560 | |
---|
561 | /** |
---|
562 | * ticks the Shell for dt Seconds |
---|
563 | * @param dt the elapsed time since the last tick(); |
---|
564 | */ |
---|
565 | void Shell::tick(float dt) |
---|
566 | { |
---|
567 | if (this->delayed > 0.0) |
---|
568 | this->delayed -= dt; |
---|
569 | else if (this->pressedKey != SDLK_FIRST ) |
---|
570 | { |
---|
571 | this->delayed = this->repeatRate; |
---|
572 | if (this->pressedKey == SDLK_BACKSPACE) |
---|
573 | this->removeCharacters(1); |
---|
574 | else if (pressedKey < 127) |
---|
575 | this->addCharacter(this->pressedKey); |
---|
576 | } |
---|
577 | } |
---|
578 | |
---|
579 | /** |
---|
580 | * displays the Shell |
---|
581 | */ |
---|
582 | void Shell::draw() const |
---|
583 | { |
---|
584 | glPushMatrix(); |
---|
585 | // transform for alignment. |
---|
586 | // setting the Blending effects |
---|
587 | |
---|
588 | glColor4f(0.0f, 0.0f, 0.8f, .4); |
---|
589 | glEnable(GL_BLEND); |
---|
590 | glDisable(GL_TEXTURE_2D); |
---|
591 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
592 | |
---|
593 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
594 | glBegin(GL_TRIANGLE_STRIP); |
---|
595 | |
---|
596 | glTexCoord2f(0, 0); |
---|
597 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
598 | |
---|
599 | glTexCoord2f(1, 0); |
---|
600 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
601 | |
---|
602 | glTexCoord2f(0, 1); |
---|
603 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
---|
604 | |
---|
605 | glTexCoord2f(1, 1); |
---|
606 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
---|
607 | |
---|
608 | glEnd(); |
---|
609 | } |
---|
610 | |
---|
611 | |
---|
612 | /** |
---|
613 | * autocompletes the Shell's inputLine |
---|
614 | * @returns true, if a result was found, false otherwise |
---|
615 | * |
---|
616 | * @todo implement it!! |
---|
617 | */ |
---|
618 | bool Shell::autoComplete() |
---|
619 | { |
---|
620 | //PRINTF(3)("AutoCompletion not implemented yet\n"); |
---|
621 | |
---|
622 | char* completionLine = new char[strlen(inputLine)+1]; |
---|
623 | strcpy(completionLine, this->inputLine); |
---|
624 | |
---|
625 | char* commandBegin = strrchr(completionLine, ' '); |
---|
626 | if (commandBegin == NULL) |
---|
627 | commandBegin = completionLine; |
---|
628 | else |
---|
629 | { |
---|
630 | if(commandBegin >= completionLine + strlen(completionLine)) |
---|
631 | commandBegin = completionLine + strlen(completionLine); |
---|
632 | else |
---|
633 | commandBegin++; |
---|
634 | } |
---|
635 | |
---|
636 | char* objectStart; |
---|
637 | if (objectStart = strstr(commandBegin, "::")) |
---|
638 | { |
---|
639 | char* classIdentity = new char[objectStart - commandBegin +1]; |
---|
640 | strncpy(classIdentity, commandBegin, objectStart - commandBegin); |
---|
641 | classIdentity[objectStart - commandBegin] = '\0'; |
---|
642 | this->objectComplete(objectStart+2, ClassList::StringToID(classIdentity)); |
---|
643 | delete[] classIdentity; |
---|
644 | } |
---|
645 | else |
---|
646 | this->classComplete(commandBegin); |
---|
647 | |
---|
648 | delete[] completionLine; |
---|
649 | } |
---|
650 | |
---|
651 | /** |
---|
652 | * autocompletes a className |
---|
653 | * @param classBegin the Beginning of a String to autoComplete |
---|
654 | * @return true on success, false otherwise |
---|
655 | */ |
---|
656 | bool Shell::classComplete(const char* classBegin) |
---|
657 | { |
---|
658 | if (unlikely(classBegin == NULL)) |
---|
659 | return false; |
---|
660 | const tList<const char>* clList = ClassList::getClassList(); |
---|
661 | if (clList != NULL) |
---|
662 | { |
---|
663 | const tList<const char>* classList = this->createCompleteList(clList, classBegin); |
---|
664 | if (classList != NULL) |
---|
665 | this->generalComplete(classList, classBegin, "%s::", "::"); |
---|
666 | else |
---|
667 | return false; |
---|
668 | } |
---|
669 | else |
---|
670 | return false; |
---|
671 | return true; |
---|
672 | } |
---|
673 | |
---|
674 | /** |
---|
675 | * autocompletes an ObjectName |
---|
676 | * @param objectBegin the beginning string of a Object |
---|
677 | * @param classID the ID of the Class to search for. |
---|
678 | * @return true on success, false otherwise |
---|
679 | */ |
---|
680 | bool Shell::objectComplete(const char* objectBegin, long classID) |
---|
681 | { |
---|
682 | printf("%s\n", objectBegin); |
---|
683 | |
---|
684 | if (unlikely(objectBegin == NULL)) |
---|
685 | return false; |
---|
686 | tList<BaseObject>* boList = ClassList::getList(classID); |
---|
687 | if (boList != NULL) |
---|
688 | { |
---|
689 | printf("\n", boList->firstElement()->getName()); |
---|
690 | const tList<const char>* objectList = this->createCompleteList(boList, objectBegin); |
---|
691 | if (objectList != NULL) |
---|
692 | this->generalComplete(objectList, objectBegin, "%s"); |
---|
693 | else |
---|
694 | return false; |
---|
695 | } |
---|
696 | else |
---|
697 | return false; |
---|
698 | return true; |
---|
699 | } |
---|
700 | |
---|
701 | /** |
---|
702 | * completes a Function |
---|
703 | * @param functionBegin the beginning of the function String |
---|
704 | */ |
---|
705 | bool Shell::functionComplete(const char* functionBegin) |
---|
706 | { |
---|
707 | } |
---|
708 | |
---|
709 | /** |
---|
710 | * completes the inputline on grounds of an inputList |
---|
711 | * @param stringList the List to parse through |
---|
712 | * @param begin the String to search in the inputList, and to extend with it. |
---|
713 | * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::" |
---|
714 | * @param addBack what should be added at the end of the completion |
---|
715 | * @param addFront what should be added to the front of one finished completion |
---|
716 | * @return true if ok, false otherwise |
---|
717 | */ |
---|
718 | bool Shell::generalComplete(const tList<const char>* stringList, const char* begin, const char* displayAs, const char* addBack, const char* addFront) |
---|
719 | { |
---|
720 | if (stringList->getSize() == 0) |
---|
721 | return false; |
---|
722 | |
---|
723 | const char* addString = stringList->firstElement(); |
---|
724 | unsigned int addLength = 0; |
---|
725 | unsigned int inputLenght = strlen(begin); |
---|
726 | |
---|
727 | if (addString != NULL) |
---|
728 | addLength = strlen(addString); |
---|
729 | tIterator<const char>* charIterator = stringList->getIterator(); |
---|
730 | const char* charElem = charIterator->firstElement(); |
---|
731 | while (charElem != NULL) |
---|
732 | { |
---|
733 | PRINTF(0)(displayAs, charElem); |
---|
734 | for (unsigned int i = inputLenght; i < addLength; i++) |
---|
735 | if (addString[i] != charElem[i]) |
---|
736 | { |
---|
737 | addLength = i; |
---|
738 | break; |
---|
739 | } |
---|
740 | charElem = charIterator->nextElement(); |
---|
741 | } |
---|
742 | delete charIterator; |
---|
743 | |
---|
744 | if (addLength >= inputLenght) |
---|
745 | { |
---|
746 | char* adder = new char[addLength+1]; |
---|
747 | strncpy(adder, addString, addLength); |
---|
748 | adder[addLength] = '\0'; |
---|
749 | this->removeCharacters(inputLenght); |
---|
750 | this->addCharacters(adder); |
---|
751 | if (addBack != NULL && stringList->getSize() == 1) |
---|
752 | this->addCharacters("::"); |
---|
753 | delete[] adder; |
---|
754 | } |
---|
755 | return true; |
---|
756 | } |
---|
757 | |
---|
758 | /** |
---|
759 | * searches for classes, which beginn with classNameBegin |
---|
760 | * @param inputList the List to parse through |
---|
761 | * @param classNameBegin the beginning string |
---|
762 | * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards, |
---|
763 | * !! The strings MUST NOT be deleted !! |
---|
764 | */ |
---|
765 | const tList<const char>* Shell::createCompleteList(const tList<const char>* inputList, const char* classNameBegin) |
---|
766 | { |
---|
767 | if (inputList == NULL || classNameBegin == NULL) |
---|
768 | return NULL; |
---|
769 | unsigned int searchLength = strlen(classNameBegin); |
---|
770 | if (this->completionList != NULL) |
---|
771 | delete this->completionList; |
---|
772 | this->completionList = new tList<const char>; |
---|
773 | |
---|
774 | // tList<const char>* classList = ClassList::getClassList(); |
---|
775 | |
---|
776 | tIterator<const char>* iterator = inputList->getIterator(); |
---|
777 | const char* enumString = iterator->firstElement(); |
---|
778 | while (enumString != NULL) |
---|
779 | { |
---|
780 | if (strlen(enumString)>searchLength+1 && |
---|
781 | !strncasecmp(enumString, classNameBegin, searchLength)) |
---|
782 | { |
---|
783 | this->completionList->add(enumString); |
---|
784 | } |
---|
785 | enumString = iterator->nextElement(); |
---|
786 | } |
---|
787 | delete iterator; |
---|
788 | |
---|
789 | return this->completionList; |
---|
790 | } |
---|
791 | |
---|
792 | /** |
---|
793 | * searches for classes, which beginn with classNameBegin |
---|
794 | * @param inputList the List to parse through |
---|
795 | * @param classNameBegin the beginning string |
---|
796 | * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards, |
---|
797 | * !! The strings MUST NOT be deleted !! |
---|
798 | */ |
---|
799 | const tList<const char>* Shell::createCompleteList(const tList<BaseObject>* inputList, const char* classNameBegin) |
---|
800 | { |
---|
801 | if (inputList == NULL || classNameBegin == NULL) |
---|
802 | return NULL; |
---|
803 | unsigned int searchLength = strlen(classNameBegin); |
---|
804 | if (this->completionList != NULL) |
---|
805 | delete this->completionList; |
---|
806 | this->completionList = new tList<const char>; |
---|
807 | |
---|
808 | tIterator<BaseObject>* iterator = inputList->getIterator(); |
---|
809 | BaseObject* enumBO = iterator->firstElement(); |
---|
810 | while (enumBO != NULL) |
---|
811 | { |
---|
812 | if (enumBO->getName() != NULL && |
---|
813 | strlen(enumBO->getName())>searchLength+1 && |
---|
814 | !strncasecmp(enumBO->getName(), classNameBegin, searchLength)) |
---|
815 | { |
---|
816 | this->completionList->add(enumBO->getName()); |
---|
817 | } |
---|
818 | enumBO = iterator->nextElement(); |
---|
819 | } |
---|
820 | delete iterator; |
---|
821 | |
---|
822 | return this->completionList; |
---|
823 | } |
---|
824 | |
---|
825 | /** |
---|
826 | * prints out some nice help about the Shell |
---|
827 | */ |
---|
828 | void Shell::help() const |
---|
829 | { |
---|
830 | PRINT(0)("Help for the most important Shell-commands\n"); |
---|
831 | PRINT(0)("F1 - HELP; F2 - DEBUG; ` - open/close shell\n"); |
---|
832 | PRINT(0)("input order:\n"); |
---|
833 | PRINT(0)("ClassName::objectName function [parameter1, [parameter2 ...]] or\n"); |
---|
834 | PRINT(0)("Command [parameter]\n"); |
---|
835 | } |
---|
836 | |
---|
837 | |
---|
838 | /////////////////////// |
---|
839 | // HELPER FUNCTIONS // |
---|
840 | /////////////////////// |
---|
841 | |
---|
842 | /** |
---|
843 | * calculates the position of a Buffer-Display Line |
---|
844 | * @param lineNumber the lineNumber from the bottom to calculate the position from |
---|
845 | * @returns the Position of the Line. |
---|
846 | */ |
---|
847 | Vector Shell::calculateLinePosition(unsigned int lineNumber) |
---|
848 | { |
---|
849 | return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0); |
---|
850 | } |
---|
851 | |
---|
852 | |
---|
853 | |
---|
854 | /** |
---|
855 | * displays some nice output from the Shell |
---|
856 | */ |
---|
857 | void Shell::debug() const |
---|
858 | { |
---|
859 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
860 | |
---|
861 | if (this->pressedKey != SDLK_FIRST) |
---|
862 | printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay); |
---|
863 | |
---|
864 | |
---|
865 | char* tmpChar = this->bufferIterator->firstElement(); |
---|
866 | while(tmpChar != NULL) |
---|
867 | { |
---|
868 | printf(tmpChar); |
---|
869 | tmpChar = this->bufferIterator->nextElement(); |
---|
870 | } |
---|
871 | } |
---|
872 | |
---|
873 | |
---|
874 | |
---|
875 | // void Shell::testI (int i) |
---|
876 | // { |
---|
877 | // PRINTF(3)("This is the Test for one Int '%d'\n", i); |
---|
878 | // } |
---|
879 | // |
---|
880 | // void Shell::testS (const char* s) |
---|
881 | // { |
---|
882 | // PRINTF(3)("This is the Test for one String '%s'\n", s); |
---|
883 | // } |
---|
884 | // |
---|
885 | // void Shell::testB (bool b) |
---|
886 | // { |
---|
887 | // PRINTF(3)("This is the Test for one Bool: "); |
---|
888 | // if (b) |
---|
889 | // PRINTF(3)("true\n"); |
---|
890 | // else |
---|
891 | // PRINTF(3)("false\n"); |
---|
892 | // } |
---|
893 | // |
---|
894 | // void Shell::testF (float f) |
---|
895 | // { |
---|
896 | // PRINTF(3)("This is the Test for one Float '%f'\n", f); |
---|
897 | // } |
---|
898 | // |
---|
899 | // void Shell::testSF (const char* s, float f) |
---|
900 | // { |
---|
901 | // PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f); |
---|
902 | // } |
---|