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