2 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "supertux/console.hpp"
22 #include "physfs/ifile_stream.hpp"
23 #include "scripting/scripting.hpp"
24 #include "scripting/squirrel_util.hpp"
25 #include "supertux/gameconfig.hpp"
26 #include "supertux/globals.hpp"
27 #include "video/drawing_context.hpp"
29 /// speed (pixels/s) the console closes
30 static const float FADE_SPEED = 1;
32 ConsoleBuffer::ConsoleBuffer() :
38 ConsoleBuffer::addLines(const std::string& s)
40 std::istringstream iss(s);
42 while (std::getline(iss, line, '\n'))
49 ConsoleBuffer::addLine(const std::string& s_)
53 // output line to stderr
54 std::cerr << s << std::endl;
60 m_lines.push_front(Font::wrap_to_chars(s, 99, &overflow));
63 } while (s.length() > 0);
65 // trim scrollback buffer
66 while (m_lines.size() >= 1000)
71 if (Console::current())
73 Console::current()->on_buffer_change(line_count);
78 ConsoleBuffer::flush(ConsoleStreamBuffer& buffer)
80 if (&buffer == &s_outputBuffer)
82 std::string s = s_outputBuffer.str();
83 if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')))
85 while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))
87 s.erase(s.length()-1);
90 s_outputBuffer.str(std::string());
95 Console::Console(ConsoleBuffer& buffer) :
98 m_inputBufferPosition(0),
100 m_history_position(m_history.end()),
101 m_background(Surface::create("images/engine/console.png")),
102 m_background2(Surface::create("images/engine/console2.png")),
105 m_backgroundOffset(0),
110 m_font(new Font(Font::FIXED, "fonts/andale12.stf", 1)),
119 sq_release(scripting::global_vm, &m_vm_object);
124 Console::on_buffer_change(int line_count)
126 // increase console height if necessary
127 if (m_stayOpen > 0 && m_height < 64)
133 m_height += m_font->get_height() * line_count;
136 // reset console to full opacity
144 m_vm = scripting::global_vm;
145 HSQUIRRELVM new_vm = sq_newthread(m_vm, 16);
147 throw scripting::SquirrelError(m_vm, "Couldn't create new VM thread for console");
149 // store reference to thread
150 sq_resetobject(&m_vm_object);
151 if(SQ_FAILED(sq_getstackobj(m_vm, -1, &m_vm_object)))
152 throw scripting::SquirrelError(m_vm, "Couldn't get vm object for console");
153 sq_addref(m_vm, &m_vm_object);
156 // create new roottable for thread
158 sq_pushroottable(new_vm);
159 if(SQ_FAILED(sq_setdelegate(new_vm, -2)))
160 throw scripting::SquirrelError(new_vm, "Couldn't set console_table delegate");
162 sq_setroottable(new_vm);
167 std::string filename = "scripts/console.nut";
168 IFileStream stream(filename);
169 scripting::compile_and_run(m_vm, stream, filename);
170 } catch(std::exception& e) {
171 log_warning << "Couldn't load console.nut: " << e.what() << std::endl;
177 Console::execute_script(const std::string& command)
179 using namespace scripting;
183 SQInteger oldtop = sq_gettop(m_vm);
185 if(SQ_FAILED(sq_compilebuffer(m_vm, command.c_str(), command.length(),
187 throw SquirrelError(m_vm, "Couldn't compile command");
189 sq_pushroottable(m_vm);
190 if(SQ_FAILED(sq_call(m_vm, 1, SQTrue, SQTrue)))
191 throw SquirrelError(m_vm, "Problem while executing command");
193 if(sq_gettype(m_vm, -1) != OT_NULL)
194 m_buffer.addLines(squirrel2string(m_vm, -1));
195 } catch(std::exception& e) {
196 m_buffer.addLines(e.what());
198 SQInteger newtop = sq_gettop(m_vm);
199 if(newtop < oldtop) {
200 log_fatal << "Script destroyed squirrel stack..." << std::endl;
202 sq_settop(m_vm, oldtop);
207 Console::input(char c)
209 m_inputBuffer.insert(m_inputBufferPosition, 1, c);
210 m_inputBufferPosition++;
216 if ((m_inputBufferPosition > 0) && (m_inputBuffer.length() > 0)) {
217 m_inputBuffer.erase(m_inputBufferPosition-1, 1);
218 m_inputBufferPosition--;
225 if (m_inputBufferPosition < (int)m_inputBuffer.length()) {
226 m_inputBuffer.erase(m_inputBufferPosition, 1);
233 m_buffer.addLines("> " + m_inputBuffer);
234 parse(m_inputBuffer);
236 m_inputBufferPosition = 0;
240 Console::scroll(int numLines)
242 m_offset += numLines;
243 if (m_offset > 0) m_offset = 0;
247 Console::show_history(int offset_)
249 while ((offset_ > 0) && (m_history_position != m_history.end())) {
250 m_history_position++;
253 while ((offset_ < 0) && (m_history_position != m_history.begin())) {
254 m_history_position--;
257 if (m_history_position == m_history.end()) {
259 m_inputBufferPosition = 0;
261 m_inputBuffer = *m_history_position;
262 m_inputBufferPosition = m_inputBuffer.length();
267 Console::move_cursor(int offset_)
269 if (offset_ == -65535) m_inputBufferPosition = 0;
270 if (offset_ == +65535) m_inputBufferPosition = m_inputBuffer.length();
271 m_inputBufferPosition+=offset_;
272 if (m_inputBufferPosition < 0) m_inputBufferPosition = 0;
273 if (m_inputBufferPosition > (int)m_inputBuffer.length()) m_inputBufferPosition = m_inputBuffer.length();
276 // Helper functions for Console::autocomplete
277 // TODO: Fix rough documentation
280 void sq_insert_commands(std::list<std::string>& cmds, HSQUIRRELVM vm, std::string table_prefix, std::string search_prefix);
283 * Acts upon key,value on top of stack:
284 * Appends key (plus type-dependent suffix) to cmds if table_prefix+key starts with search_prefix;
285 * Calls sq_insert_commands if search_prefix starts with table_prefix+key (and value is a table/class/instance);
288 sq_insert_command(std::list<std::string>& cmds, HSQUIRRELVM vm, std::string table_prefix, std::string search_prefix)
290 const SQChar* key_chars;
291 if (SQ_FAILED(sq_getstring(vm, -2, &key_chars))) return;
292 std::string key_string = table_prefix + key_chars;
294 switch (sq_gettype(vm, -1)) {
297 if (search_prefix.substr(0, key_string.length()) == key_string) {
299 sq_insert_commands(cmds, vm, key_string, search_prefix);
306 if (search_prefix.substr(0, key_string.length()) == key_string) {
307 sq_insert_commands(cmds, vm, key_string, search_prefix);
311 case OT_NATIVECLOSURE:
318 if (key_string.substr(0, search_prefix.length()) == search_prefix) {
319 cmds.push_back(key_string);
325 * calls sq_insert_command for all entries of table/class on top of stack
328 sq_insert_commands(std::list<std::string>& cmds, HSQUIRRELVM vm, std::string table_prefix, std::string search_prefix)
330 sq_pushnull(vm); // push iterator
331 while (SQ_SUCCEEDED(sq_next(vm,-2))) {
332 sq_insert_command(cmds, vm, table_prefix, search_prefix);
333 sq_pop(vm, 2); // pop key, val
335 sq_pop(vm, 1); // pop iterator
339 // End of Console::autocomplete helper functions
342 Console::autocomplete()
344 //int autocompleteFrom = m_inputBuffer.find_last_of(" ();+", m_inputBufferPosition);
345 int autocompleteFrom = m_inputBuffer.find_last_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_->.", m_inputBufferPosition);
346 if (autocompleteFrom != (int)std::string::npos) {
347 autocompleteFrom += 1;
349 autocompleteFrom = 0;
351 std::string prefix = m_inputBuffer.substr(autocompleteFrom, m_inputBufferPosition - autocompleteFrom);
352 m_buffer.addLines("> " + prefix);
354 std::list<std::string> cmds;
358 // append all keys of the current root table to list
359 sq_pushroottable(m_vm); // push root table
361 // check all keys (and their children) for matches
362 sq_insert_commands(cmds, m_vm, "", prefix);
364 // cycle through parent(delegate) table
365 SQInteger oldtop = sq_gettop(m_vm);
366 if(SQ_FAILED(sq_getdelegate(m_vm, -1)) || oldtop == sq_gettop(m_vm)) {
369 sq_remove(m_vm, -2); // remove old table
371 sq_pop(m_vm, 1); // remove table
373 // depending on number of hits, show matches or autocomplete
376 m_buffer.addLines("No known command starts with \"" + prefix + "\"");
379 if (cmds.size() == 1)
381 // one match: just replace input buffer with full command
382 std::string replaceWith = cmds.front();
383 m_inputBuffer.replace(autocompleteFrom, prefix.length(), replaceWith);
384 m_inputBufferPosition += (replaceWith.length() - prefix.length());
389 // multiple matches: show all matches and set input buffer to longest common prefix
390 std::string commonPrefix = cmds.front();
391 while (cmds.begin() != cmds.end()) {
392 std::string cmd = cmds.front();
394 m_buffer.addLines(cmd);
395 for (int n = commonPrefix.length(); n >= 1; n--) {
396 if (cmd.compare(0, n, commonPrefix) != 0) commonPrefix.resize(n-1); else break;
399 std::string replaceWith = commonPrefix;
400 m_inputBuffer.replace(autocompleteFrom, prefix.length(), replaceWith);
401 m_inputBufferPosition += (replaceWith.length() - prefix.length());
406 Console::parse(std::string s)
408 // make sure we actually have something to parse
409 if (s.length() == 0) return;
411 // add line to history
412 m_history.push_back(s);
413 m_history_position = m_history.end();
415 // split line into list of args
416 std::vector<std::string> args;
420 start = s.find_first_not_of(" ,", end);
421 end = s.find_first_of(" ,", start);
422 if (start == s.npos) break;
423 args.push_back(s.substr(start, end-start));
426 // command is args[0]
427 if (args.size() == 0) return;
428 std::string command = args.front();
429 args.erase(args.begin());
431 // ignore if it's an internal command
432 if (consoleCommand(command,args)) return;
436 } catch(std::exception& e) {
437 m_buffer.addLines(e.what());
442 Console::consoleCommand(std::string /*command*/, std::vector<std::string> /*arguments*/)
456 if(!g_config->console_enabled)
462 // SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); // Useless in SDL2 : if you want to disable repeat, then you need to check if the key was repeated and ignore it.
479 // clear input buffer
481 m_inputBufferPosition = 0;
482 // SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
488 if (Console::hasFocus()) {
497 Console::update(float elapsed_time)
500 m_stayOpen -= elapsed_time;
503 } else if(!m_focused && m_height > 0) {
504 m_alpha -= elapsed_time * FADE_SPEED;
513 Console::draw(DrawingContext& context)
518 int layer = LAYER_GUI + 1;
520 context.push_transform();
521 context.set_alpha(m_alpha);
522 context.draw_surface(m_background2,
523 Vector(SCREEN_WIDTH/2 - m_background->get_width()/2 - m_background->get_width() + m_backgroundOffset,
524 m_height - m_background->get_height()),
526 context.draw_surface(m_background2,
527 Vector(SCREEN_WIDTH/2 - m_background->get_width()/2 + m_backgroundOffset,
528 m_height - m_background->get_height()),
530 for (int x = (SCREEN_WIDTH/2 - m_background->get_width()/2
531 - (static_cast<int>(ceilf((float)SCREEN_WIDTH /
532 (float)m_background->get_width()) - 1) * m_background->get_width()));
534 x += m_background->get_width())
536 context.draw_surface(m_background, Vector(x, m_height - m_background->get_height()), layer);
538 m_backgroundOffset+=10;
539 if (m_backgroundOffset > (int)m_background->get_width()) m_backgroundOffset -= (int)m_background->get_width();
545 float py = m_height-4-1 * m_font->get_height();
546 context.draw_text(m_font, "> "+m_inputBuffer, Vector(4, py), ALIGN_LEFT, layer);
547 if (SDL_GetTicks() % 1000 < 750) {
548 int cursor_px = 2 + m_inputBufferPosition;
549 context.draw_text(m_font, "_", Vector(4 + (cursor_px * m_font->get_text_width("X")), py), ALIGN_LEFT, layer);
553 int skipLines = -m_offset;
554 for (std::list<std::string>::iterator i = m_buffer.m_lines.begin(); i != m_buffer.m_lines.end(); i++)
556 if (skipLines-- > 0) continue;
558 float py = m_height - 4 - lineNo * m_font->get_height();
559 if (py < -m_font->get_height()) break;
560 context.draw_text(m_font, *i, Vector(4, py), ALIGN_LEFT, layer);
562 context.pop_transform();
565 ConsoleStreamBuffer ConsoleBuffer::s_outputBuffer;
566 std::ostream ConsoleBuffer::output(&ConsoleBuffer::s_outputBuffer);