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 | #include "shell_buffer.h" |
---|
21 | #include "shell_input.h" |
---|
22 | |
---|
23 | |
---|
24 | #include "text_engine.h" |
---|
25 | #include "list.h" |
---|
26 | #include "graphics_engine.h" |
---|
27 | #include "event_handler.h" |
---|
28 | #include "debug.h" |
---|
29 | #include "class_list.h" |
---|
30 | |
---|
31 | #include "key_names.h" |
---|
32 | #include <stdarg.h> |
---|
33 | #include <stdio.h> |
---|
34 | |
---|
35 | using namespace std; |
---|
36 | |
---|
37 | SHELL_COMMAND(clear, Shell, clear)->describe("Clears the shell from unwanted lines (empties all buffers)"); |
---|
38 | SHELL_COMMAND(deactivate, Shell, deactivate)->describe("Deactivates the Shell. (moves it into background)"); |
---|
39 | |
---|
40 | /** |
---|
41 | * standard constructor |
---|
42 | */ |
---|
43 | Shell::Shell () |
---|
44 | { |
---|
45 | this->setClassID(CL_SHELL, "Shell"); |
---|
46 | this->setName("Shell"); |
---|
47 | |
---|
48 | this->setAbsCoor2D(3, -400); |
---|
49 | this->textSize = 15; |
---|
50 | this->lineSpacing = 5; |
---|
51 | this->bActive = false; |
---|
52 | |
---|
53 | |
---|
54 | // BUFFER |
---|
55 | this->bufferText = NULL; |
---|
56 | this->setBufferDisplaySize(10); |
---|
57 | |
---|
58 | // INPUT LINE |
---|
59 | this->shellInput = new ShellInput; |
---|
60 | this->delayed = 0; |
---|
61 | this->shellInput->setRepeatDelay(.3, .05); |
---|
62 | //this->commandList = new tList<ShellCommand>; |
---|
63 | |
---|
64 | this->rebuildText(); |
---|
65 | this->completionList = NULL; |
---|
66 | |
---|
67 | // EVENT-Handler subscription of '`' to all States, and all other keyboard commands to ES_SEHLL |
---|
68 | EventHandler* evh = EventHandler::getInstance(); |
---|
69 | evh->subscribe(this, ES_ALL, SDLK_BACKQUOTE); |
---|
70 | for (int i = 1; i < SDLK_LAST; i++) |
---|
71 | evh->subscribe(this, ES_SHELL, i); |
---|
72 | } |
---|
73 | |
---|
74 | Shell* Shell::singletonRef = NULL; |
---|
75 | |
---|
76 | /** |
---|
77 | * standard deconstructor |
---|
78 | */ |
---|
79 | Shell::~Shell () |
---|
80 | { |
---|
81 | // delete the displayable Buffers |
---|
82 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
83 | delete this->bufferText[i]; |
---|
84 | delete[] this->bufferText; |
---|
85 | |
---|
86 | // delete the inputLine |
---|
87 | |
---|
88 | Shell::singletonRef = NULL; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * activates the shell |
---|
93 | * |
---|
94 | * This also feeds the Last few lines from the main buffers into the displayBuffer |
---|
95 | */ |
---|
96 | void Shell::activate() |
---|
97 | { |
---|
98 | if (this->bActive == true) |
---|
99 | PRINTF(3)("The shell is already active\n"); |
---|
100 | this->bActive = true; |
---|
101 | |
---|
102 | EventHandler::getInstance()->setState(ES_SHELL); |
---|
103 | this->setRelCoorSoft2D(0, 0, 1, 5); |
---|
104 | |
---|
105 | ShellBuffer::getInstance()->getBufferIterator()->lastElement(); |
---|
106 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
107 | this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), true); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * deactiveates the Shell. |
---|
112 | */ |
---|
113 | void Shell::deactivate() |
---|
114 | { |
---|
115 | if (this->bActive == false) |
---|
116 | PRINTF(3)("The shell is already inactive\n"); |
---|
117 | this->bActive = false; |
---|
118 | |
---|
119 | EventHandler::getInstance()->setState(ES_GAME); |
---|
120 | this->setRelCoorSoft2D(0, -400, 1, 5); |
---|
121 | |
---|
122 | ShellBuffer::getInstance()->getBufferIterator()->lastElement(); |
---|
123 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
124 | this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), false); |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | /** |
---|
129 | * sets the size of the text and spacing |
---|
130 | * @param textSize the size of the Text in Pixels |
---|
131 | * @param lineSpacing the size of the Spacing between two lines in pixels |
---|
132 | * |
---|
133 | * this also rebuilds the entire Text, inputLine and displayBuffer, |
---|
134 | * to be accurate again. |
---|
135 | */ |
---|
136 | void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing) |
---|
137 | { |
---|
138 | this->textSize = textSize; |
---|
139 | this->lineSpacing = lineSpacing; |
---|
140 | |
---|
141 | this->rebuildText(); |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * rebuilds the Text's |
---|
146 | * |
---|
147 | * use this function, if you changed the Font/Size or something else. |
---|
148 | */ |
---|
149 | void Shell::rebuildText() |
---|
150 | { |
---|
151 | this->shellInput->setFont("fonts/Aniron_Bold.ttf", this->textSize); |
---|
152 | this->shellInput->setColor(1, 0, 0); |
---|
153 | this->shellInput->setAlignment(TEXT_ALIGN_LEFT); |
---|
154 | this->shellInput->setText(NULL); |
---|
155 | this->shellInput->setParent2D(this); |
---|
156 | this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize); |
---|
157 | |
---|
158 | this->setBufferDisplaySize(this->bufferDisplaySize); |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * sets The count of Lines to display in the buffer. |
---|
163 | * @param bufferDisplaySize the count of lines to display in the Shell-Buffer. |
---|
164 | */ |
---|
165 | void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) |
---|
166 | { |
---|
167 | if (this->bufferText != NULL) |
---|
168 | { |
---|
169 | for (unsigned int i = 0; i < this->bufferDisplaySize; i++) |
---|
170 | delete this->bufferText[i]; |
---|
171 | delete[] this->bufferText; |
---|
172 | } |
---|
173 | |
---|
174 | this->bufferText = new Text*[bufferDisplaySize]; |
---|
175 | for (unsigned int i = 0; i < bufferDisplaySize; i++) |
---|
176 | { |
---|
177 | this->bufferText[i] = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC); |
---|
178 | this->bufferText[i]->setColor(1, 0, 0); |
---|
179 | this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT); |
---|
180 | this->bufferText[i]->setRelCoor2D(calculateLinePosition(i)); |
---|
181 | this->bufferText[i]->setText(NULL); |
---|
182 | this->bufferText[i]->setParent2D(this); |
---|
183 | } |
---|
184 | this->bufferDisplaySize = bufferDisplaySize; |
---|
185 | |
---|
186 | this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1); |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * deletes all the Buffers |
---|
191 | */ |
---|
192 | void Shell::flush() |
---|
193 | { |
---|
194 | // remove all chars from the BufferTexts. |
---|
195 | if (this->bufferText) |
---|
196 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
197 | { |
---|
198 | this->bufferText[i]->setText(NULL, true); |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | // BUFFER FLUSHING |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * prints out some text to the input-buffers |
---|
207 | * @param text the text to output. |
---|
208 | */ |
---|
209 | void Shell::printToDisplayBuffer(const char* text) |
---|
210 | { |
---|
211 | if(likely(bufferText != NULL)) |
---|
212 | { |
---|
213 | Text* lastText = this->bufferText[this->bufferDisplaySize-1]; |
---|
214 | |
---|
215 | Text* swapText; |
---|
216 | Text* moveText = this->bufferText[0]; |
---|
217 | this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10); |
---|
218 | for (unsigned int i = 1; i < this->bufferDisplaySize; i++) |
---|
219 | { |
---|
220 | if ( i < this->bufferDisplaySize-1) |
---|
221 | this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5); |
---|
222 | swapText = this->bufferText[i]; |
---|
223 | this ->bufferText[i] = moveText; |
---|
224 | moveText = swapText; |
---|
225 | } |
---|
226 | lastText->setRelCoor2D(this->calculateLinePosition(0)); |
---|
227 | this->bufferText[0] = lastText; |
---|
228 | |
---|
229 | this->bufferText[0]->setText(text, true); |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * clears the Shell (empties all buffers) |
---|
235 | */ |
---|
236 | void Shell::clear() |
---|
237 | { |
---|
238 | this->flush(); |
---|
239 | ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL); |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | /** |
---|
245 | * listens for some event |
---|
246 | * @param event the Event happened |
---|
247 | */ |
---|
248 | void Shell::process(const Event &event) |
---|
249 | { |
---|
250 | if (event.bPressed) |
---|
251 | { |
---|
252 | PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type)); |
---|
253 | if (event.type == SDLK_BACKQUOTE) |
---|
254 | { |
---|
255 | if (EventHandler::getInstance()->getState() == ES_GAME) |
---|
256 | this->activate(); |
---|
257 | else |
---|
258 | this->deactivate(); |
---|
259 | } |
---|
260 | else if (event.type == SDLK_F1) |
---|
261 | this->help(); |
---|
262 | else if (event.type == SDLK_F2) |
---|
263 | this->debug(); |
---|
264 | else if (event.type == SDLK_TAB) |
---|
265 | ;//this->autoComplete(); |
---|
266 | else if (event.type == SDLK_BACKSPACE) |
---|
267 | { |
---|
268 | this->delayed = this->repeatDelay; |
---|
269 | this->pressedKey = SDLK_BACKSPACE; |
---|
270 | this->shellInput->removeCharacters(1); |
---|
271 | } |
---|
272 | else if (event.type == SDLK_RETURN) |
---|
273 | this->shellInput->executeCommand(); |
---|
274 | /* |
---|
275 | else if (event.type == SDLK_UP) |
---|
276 | { |
---|
277 | // this->flushInputLine(); |
---|
278 | tIterator<char>* iterator = this->commandList->getIterator(); |
---|
279 | char* command = iterator->lastElement(); |
---|
280 | while (command) |
---|
281 | { |
---|
282 | if (!strcmp (command, inputLine)) |
---|
283 | { |
---|
284 | inputLine = iterator->prevElement(); |
---|
285 | return; |
---|
286 | } |
---|
287 | command = iterator->prevElement(); |
---|
288 | } |
---|
289 | inputLine = iterator->lastElement(); |
---|
290 | } |
---|
291 | */ |
---|
292 | else if (likely(event.type < 127)) |
---|
293 | { |
---|
294 | Uint8 *keystate = SDL_GetKeyState(NULL); |
---|
295 | this->delayed = this->repeatDelay; |
---|
296 | if (unlikely( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] )) |
---|
297 | { |
---|
298 | this->pressedKey = event.type-32; |
---|
299 | this->shellInput->addCharacter(event.type-32); |
---|
300 | } |
---|
301 | else |
---|
302 | { |
---|
303 | this->pressedKey = event.type; |
---|
304 | this->shellInput->addCharacter(event.type); |
---|
305 | } |
---|
306 | } |
---|
307 | } |
---|
308 | else // if(!event.bPressed) |
---|
309 | { |
---|
310 | if (this->pressedKey == event.type || (this->pressedKey == event.type - 32)) |
---|
311 | { |
---|
312 | this->pressedKey = SDLK_FIRST; |
---|
313 | this->delayed = 0.0; |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | * ticks the Shell for dt Seconds |
---|
320 | * @param dt the elapsed time since the last tick(); |
---|
321 | */ |
---|
322 | void Shell::tick(float dt) |
---|
323 | { |
---|
324 | if (this->delayed > 0.0) |
---|
325 | this->delayed -= dt; |
---|
326 | else if (this->pressedKey != SDLK_FIRST ) |
---|
327 | { |
---|
328 | this->delayed = this->repeatRate; |
---|
329 | if (this->pressedKey == SDLK_BACKSPACE) |
---|
330 | this->shellInput->removeCharacters(1); |
---|
331 | else if (pressedKey < 127) |
---|
332 | this->shellInput->addCharacter(this->pressedKey); |
---|
333 | } |
---|
334 | } |
---|
335 | /** |
---|
336 | * displays the Shell |
---|
337 | */ |
---|
338 | void Shell::draw() const |
---|
339 | { |
---|
340 | glPushMatrix(); |
---|
341 | // transform for alignment. |
---|
342 | // setting the Blending effects |
---|
343 | |
---|
344 | glColor4f(0.0f, 0.0f, 0.8f, .4); |
---|
345 | glEnable(GL_BLEND); |
---|
346 | glDisable(GL_TEXTURE_2D); |
---|
347 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
348 | |
---|
349 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
350 | glBegin(GL_TRIANGLE_STRIP); |
---|
351 | |
---|
352 | glTexCoord2f(0, 0); |
---|
353 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
354 | |
---|
355 | glTexCoord2f(1, 0); |
---|
356 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
357 | |
---|
358 | glTexCoord2f(0, 1); |
---|
359 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
---|
360 | |
---|
361 | glTexCoord2f(1, 1); |
---|
362 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
---|
363 | |
---|
364 | glEnd(); |
---|
365 | } |
---|
366 | |
---|
367 | /** |
---|
368 | * prints out some nice help about the Shell |
---|
369 | */ |
---|
370 | void Shell::help() const |
---|
371 | { |
---|
372 | PRINT(0)("Help for the most important Shell-commands\n"); |
---|
373 | PRINT(0)("F1 - HELP; F2 - DEBUG; ` - open/close shell\n"); |
---|
374 | PRINT(0)("input order:\n"); |
---|
375 | PRINT(0)("ClassName::objectName function [parameter1, [parameter2 ...]] or\n"); |
---|
376 | PRINT(0)("Command [parameter]\n"); |
---|
377 | } |
---|
378 | |
---|
379 | |
---|
380 | /////////////////////// |
---|
381 | // HELPER FUNCTIONS // |
---|
382 | /////////////////////// |
---|
383 | |
---|
384 | /** |
---|
385 | * calculates the position of a Buffer-Display Line |
---|
386 | * @param lineNumber the lineNumber from the bottom to calculate the position from |
---|
387 | * @returns the Position of the Line. |
---|
388 | */ |
---|
389 | Vector Shell::calculateLinePosition(unsigned int lineNumber) |
---|
390 | { |
---|
391 | return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0); |
---|
392 | } |
---|
393 | |
---|
394 | |
---|
395 | |
---|
396 | /** |
---|
397 | * displays some nice output from the Shell |
---|
398 | */ |
---|
399 | void Shell::debug() const |
---|
400 | { |
---|
401 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
402 | |
---|
403 | if (this->pressedKey != SDLK_FIRST) |
---|
404 | printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay); |
---|
405 | |
---|
406 | |
---|
407 | ShellBuffer::getInstance()->debug(); |
---|
408 | } |
---|
409 | |
---|
410 | |
---|
411 | |
---|
412 | // void Shell::testI (int i) |
---|
413 | // { |
---|
414 | // PRINTF(3)("This is the Test for one Int '%d'\n", i); |
---|
415 | // } |
---|
416 | // |
---|
417 | // void Shell::testS (const char* s) |
---|
418 | // { |
---|
419 | // PRINTF(3)("This is the Test for one String '%s'\n", s); |
---|
420 | // } |
---|
421 | // |
---|
422 | // void Shell::testB (bool b) |
---|
423 | // { |
---|
424 | // PRINTF(3)("This is the Test for one Bool: "); |
---|
425 | // if (b) |
---|
426 | // PRINTF(3)("true\n"); |
---|
427 | // else |
---|
428 | // PRINTF(3)("false\n"); |
---|
429 | // } |
---|
430 | // |
---|
431 | // void Shell::testF (float f) |
---|
432 | // { |
---|
433 | // PRINTF(3)("This is the Test for one Float '%f'\n", f); |
---|
434 | // } |
---|
435 | // |
---|
436 | // void Shell::testSF (const char* s, float f) |
---|
437 | // { |
---|
438 | // PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f); |
---|
439 | // } |
---|