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) |
---|
38 | ->describe("Clears the shell from unwanted lines (empties all buffers)") |
---|
39 | ->setAlias("clear"); |
---|
40 | SHELL_COMMAND(deactivate, Shell, deactivate) |
---|
41 | ->describe("Deactivates the Shell. (moves it into background)") |
---|
42 | ->setAlias("hide"); |
---|
43 | SHELL_COMMAND(textsize, Shell, setTextSize) |
---|
44 | ->describe("Sets the size of the Text (size, linespacing)"); |
---|
45 | |
---|
46 | /** |
---|
47 | * standard constructor |
---|
48 | */ |
---|
49 | Shell::Shell () |
---|
50 | { |
---|
51 | this->setClassID(CL_SHELL, "Shell"); |
---|
52 | this->setName("Shell"); |
---|
53 | |
---|
54 | // register the shell at the ShellBuffer |
---|
55 | ShellBuffer::getInstance()->registerShell(this); |
---|
56 | |
---|
57 | |
---|
58 | // Element2D and generals |
---|
59 | this->setAbsCoor2D(3, -400); |
---|
60 | this->textSize = 15; |
---|
61 | this->lineSpacing = 0; |
---|
62 | this->bActive = false; |
---|
63 | |
---|
64 | // BUFFER |
---|
65 | this->bufferText = NULL; |
---|
66 | this->bufferDisplaySize = 10; |
---|
67 | |
---|
68 | // INPUT LINE |
---|
69 | this->shellInput = new ShellInput; |
---|
70 | //this->commandList = new tList<ShellCommand>; |
---|
71 | |
---|
72 | this->rebuildText(); |
---|
73 | |
---|
74 | // EVENT-Handler subscription of '`' to all States. |
---|
75 | EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * standard deconstructor |
---|
80 | */ |
---|
81 | Shell::~Shell () |
---|
82 | { |
---|
83 | // delete the displayable Buffers |
---|
84 | for (unsigned int i = 0; i < this->bufferDisplaySize; i++) |
---|
85 | delete this->bufferText[i]; |
---|
86 | delete[] this->bufferText; |
---|
87 | |
---|
88 | // delete the inputLine |
---|
89 | delete this->shellInput; |
---|
90 | ShellBuffer::getInstance()->unregisterShell(this); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * activates the shell |
---|
95 | * |
---|
96 | * This also feeds the Last few lines from the main buffers into the displayBuffer |
---|
97 | */ |
---|
98 | void Shell::activate() |
---|
99 | { |
---|
100 | if (this->bActive == true) |
---|
101 | PRINTF(3)("The shell is already active\n"); |
---|
102 | this->bActive = true; |
---|
103 | |
---|
104 | EventHandler::getInstance()->setState(ES_SHELL); |
---|
105 | this->setRelCoorSoft2D(0, 0, 1, 5); |
---|
106 | |
---|
107 | ShellBuffer::getInstance()->getBufferIterator()->lastElement(); |
---|
108 | for (unsigned int i = 0; i < this->bufferDisplaySize; i++) |
---|
109 | this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), true); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * deactiveates the Shell. |
---|
114 | */ |
---|
115 | void Shell::deactivate() |
---|
116 | { |
---|
117 | if (this->bActive == false) |
---|
118 | PRINTF(3)("The shell is already inactive\n"); |
---|
119 | this->bActive = false; |
---|
120 | |
---|
121 | EventHandler::getInstance()->setState(ES_GAME); |
---|
122 | this->setRelCoorSoft2D(0, -400, 1, 5); |
---|
123 | |
---|
124 | ShellBuffer::getInstance()->getBufferIterator()->lastElement(); |
---|
125 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
126 | this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), false); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /** |
---|
131 | * sets the size of the text and spacing |
---|
132 | * @param textSize the size of the Text in Pixels |
---|
133 | * @param lineSpacing the size of the Spacing between two lines in pixels |
---|
134 | * |
---|
135 | * this also rebuilds the entire Text, inputLine and displayBuffer, |
---|
136 | * to be accurate again. |
---|
137 | */ |
---|
138 | void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing) |
---|
139 | { |
---|
140 | this->textSize = textSize; |
---|
141 | this->lineSpacing = lineSpacing; |
---|
142 | this->shellInput->setFont("fonts/dpquake_.ttf", this->textSize); |
---|
143 | |
---|
144 | // this->rebuildText(); |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * rebuilds the Text's |
---|
149 | * |
---|
150 | * use this function, if you changed the Font/Size or something else. |
---|
151 | */ |
---|
152 | void Shell::rebuildText() |
---|
153 | { |
---|
154 | this->shellInput->setFont("fonts/dpquake_.ttf", this->textSize); |
---|
155 | this->shellInput->setColor(1, 0, 0); |
---|
156 | this->shellInput->setAlignment(TEXT_ALIGN_LEFT); |
---|
157 | this->shellInput->setParent2D(this); |
---|
158 | this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize); |
---|
159 | |
---|
160 | this->setBufferDisplaySize(this->bufferDisplaySize); |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * sets The count of Lines to display in the buffer. |
---|
165 | * @param bufferDisplaySize the count of lines to display in the Shell-Buffer. |
---|
166 | */ |
---|
167 | void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) |
---|
168 | { |
---|
169 | if (this->bufferText != NULL) |
---|
170 | { |
---|
171 | for (unsigned int i = 0; i < this->bufferDisplaySize; i++) |
---|
172 | delete this->bufferText[i]; |
---|
173 | delete[] this->bufferText; |
---|
174 | } |
---|
175 | |
---|
176 | this->bufferText = new Text*[bufferDisplaySize]; |
---|
177 | for (unsigned int i = 0; i < bufferDisplaySize; i++) |
---|
178 | { |
---|
179 | this->bufferText[i] = TextEngine::getInstance()->createText("fonts/dpquake_.ttf", this->textSize, TEXT_RENDER_DYNAMIC); |
---|
180 | this->bufferText[i]->setColor(1, 0, 0); |
---|
181 | this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT); |
---|
182 | this->bufferText[i]->setRelCoor2D(calculateLinePosition(i)); |
---|
183 | this->bufferText[i]->setText(NULL); |
---|
184 | this->bufferText[i]->setParent2D(this); |
---|
185 | } |
---|
186 | this->bufferDisplaySize = bufferDisplaySize; |
---|
187 | |
---|
188 | this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1); |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * deletes all the Buffers |
---|
193 | */ |
---|
194 | void Shell::flush() |
---|
195 | { |
---|
196 | // remove all chars from the BufferTexts. |
---|
197 | if (this->bufferText) |
---|
198 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
199 | { |
---|
200 | this->bufferText[i]->setText(NULL, true); |
---|
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 | * listens for some event |
---|
244 | * @param event the Event happened |
---|
245 | */ |
---|
246 | void Shell::process(const Event &event) |
---|
247 | { |
---|
248 | if (event.bPressed) |
---|
249 | { |
---|
250 | if (event.type == SDLK_BACKQUOTE) |
---|
251 | { |
---|
252 | if (EventHandler::getInstance()->getState() == ES_GAME) |
---|
253 | this->activate(); |
---|
254 | else |
---|
255 | this->deactivate(); |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * displays the Shell |
---|
262 | */ |
---|
263 | void Shell::draw() const |
---|
264 | { |
---|
265 | glPushMatrix(); |
---|
266 | // transform for alignment. |
---|
267 | // setting the Blending effects |
---|
268 | |
---|
269 | glColor4f(0.0f, 0.0f, 0.8f, .4); |
---|
270 | glEnable(GL_BLEND); |
---|
271 | glDisable(GL_TEXTURE_2D); |
---|
272 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
273 | |
---|
274 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
275 | glBegin(GL_TRIANGLE_STRIP); |
---|
276 | |
---|
277 | glTexCoord2f(0, 0); |
---|
278 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
279 | |
---|
280 | glTexCoord2f(1, 0); |
---|
281 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
282 | |
---|
283 | glTexCoord2f(0, 1); |
---|
284 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
---|
285 | |
---|
286 | glTexCoord2f(1, 1); |
---|
287 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
---|
288 | |
---|
289 | glEnd(); |
---|
290 | } |
---|
291 | |
---|
292 | /////////////////////// |
---|
293 | // HELPER FUNCTIONS // |
---|
294 | /////////////////////// |
---|
295 | |
---|
296 | /** |
---|
297 | * calculates the position of a Buffer-Display Line |
---|
298 | * @param lineNumber the lineNumber from the bottom to calculate the position from |
---|
299 | * @returns the Position of the Line. |
---|
300 | */ |
---|
301 | Vector Shell::calculateLinePosition(unsigned int lineNumber) |
---|
302 | { |
---|
303 | return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0); |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | |
---|
308 | /** |
---|
309 | * displays some nice output from the Shell |
---|
310 | */ |
---|
311 | void Shell::debug() const |
---|
312 | { |
---|
313 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
314 | |
---|
315 | // if (this->pressedKey != SDLK_FIRST) |
---|
316 | // printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay); |
---|
317 | |
---|
318 | |
---|
319 | ShellBuffer::getInstance()->debug(); |
---|
320 | } |
---|
321 | |
---|
322 | // void Shell::testI (int i) |
---|
323 | // { |
---|
324 | // PRINTF(3)("This is the Test for one Int '%d'\n", i); |
---|
325 | // } |
---|
326 | // |
---|
327 | // void Shell::testS (const char* s) |
---|
328 | // { |
---|
329 | // PRINTF(3)("This is the Test for one String '%s'\n", s); |
---|
330 | // } |
---|
331 | // |
---|
332 | // void Shell::testB (bool b) |
---|
333 | // { |
---|
334 | // PRINTF(3)("This is the Test for one Bool: "); |
---|
335 | // if (b) |
---|
336 | // PRINTF(3)("true\n"); |
---|
337 | // else |
---|
338 | // PRINTF(3)("false\n"); |
---|
339 | // } |
---|
340 | // |
---|
341 | // void Shell::testF (float f) |
---|
342 | // { |
---|
343 | // PRINTF(3)("This is the Test for one Float '%f'\n", f); |
---|
344 | // } |
---|
345 | // |
---|
346 | // void Shell::testSF (const char* s, float f) |
---|
347 | // { |
---|
348 | // PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f); |
---|
349 | // } |
---|