- some more work on the new unisolid snow tiles and a test level
[supertux.git] / src / console.cpp
index d6b2238..fc5c1cc 100644 (file)
@@ -19,6 +19,7 @@
 #include <config.h>
 
 #include <iostream>
+#include <SDL_timer.h>
 #include "console.hpp"
 #include "video/drawing_context.hpp"
 #include "video/surface.hpp"
@@ -29,6 +30,7 @@
 #include "main.hpp"
 #include "log.hpp"
 #include "resources.hpp"
+#include "gameconfig.hpp"
 
 /// speed (pixels/s) the console closes
 static const float FADE_SPEED = 1;
@@ -49,8 +51,9 @@ Console::~Console()
 void
 Console::init_graphics()
 {
-  font.reset(new Font("images/engine/fonts/white-small.png",
-                      "images/engine/fonts/shadow-small.png", 8, 9, 1));
+  font.reset(new Font(Font::FIXED,
+                      "images/engine/fonts/andale12.png",
+                      "images/engine/fonts/andale12-shadow.png", 7, 14, 1));
   fontheight = font->get_height();
   background.reset(new Surface("images/engine/console.png"));
   background2.reset(new Surface("images/engine/console2.png"));
@@ -71,9 +74,7 @@ Console::flush(ConsoleStreamBuffer* buffer)
     std::string s = inputBuffer.str();
     if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
       while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
-      addLines("> "+s);
-      parse(s);
-      inputBuffer.str(std::string());
+      enter();
     }
   }
 }
@@ -156,6 +157,15 @@ Console::backspace()
 }
 
 void
+Console::enter()
+{
+  std::string s = inputBuffer.str();
+  addLines("> "+s);
+  parse(s);
+  inputBuffer.str(std::string());
+}
+
+void
 Console::scroll(int numLines)
 {
   offset += numLines;
@@ -181,6 +191,14 @@ Console::show_history(int offset)
   }
 }
 
+void 
+Console::move_cursor(int offset)
+{
+  if (offset == -65535) inputBuffer.pubseekoff(0, std::ios_base::beg, std::ios_base::out);
+  if (offset == +65535) inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
+  inputBuffer.pubseekoff(offset, std::ios_base::cur, std::ios_base::out);
+}
+
 // Helper functions for Console::autocomplete
 // TODO: Fix rough documentation
 namespace {
@@ -255,14 +273,6 @@ Console::autocomplete()
 
   std::list<std::string> cmds;
 
-  // append all known CCRs to list
-  for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
-    std::string cmdKnown = i->first;
-    if (cmdKnown.substr(0, prefix.length()) == prefix) {
-      cmds.push_back(cmdKnown);
-    }
-  }
-
   ready_vm();
 
   // append all keys of the current root table to list
@@ -283,14 +293,23 @@ Console::autocomplete()
   // depending on number of hits, show matches or autocomplete
   if (cmds.size() == 0) addLines("No known command starts with \""+prefix+"\"");
   if (cmds.size() == 1) {
+    // one match: just replace input buffer with full command
     inputBuffer.str(cmds.front());
     inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
   }
   if (cmds.size() > 1) {
+    // multiple matches: show all matches and set input buffer to longest common prefix
+    std::string commonPrefix = cmds.front();
     while (cmds.begin() != cmds.end()) {
-      addLines(cmds.front());
+      std::string cmd = cmds.front();
       cmds.pop_front();
+      addLines(cmd);
+      for (int n = commonPrefix.length(); n >= 1; n--) {
+        if (cmd.compare(0, n, commonPrefix) != 0) commonPrefix.resize(n-1); else break;
+      }
     }
+    inputBuffer.str(commonPrefix);
+    inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
   }
 }
 
@@ -363,48 +382,17 @@ Console::parse(std::string s)
   // ignore if it's an internal command
   if (consoleCommand(command,args)) return;
 
-  // look up registered ccr
-  std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
-  if ((i == commands.end()) || (i->second.size() == 0)) {
-    try {
-      execute_script(s);
-    } catch(std::exception& e) {
-      addLines(e.what());
-    }
-    return;
+  try {
+    execute_script(s);
+  } catch(std::exception& e) {
+    addLines(e.what());
   }
 
-  // send command to the most recently registered ccr
-  ConsoleCommandReceiver* ccr = i->second.front();
-  if (ccr->consoleCommand(command, args) != true) log_warning << "Sent command to registered ccr, but command was unhandled" << std::endl;
 }
 
 bool
-Console::consoleCommand(std::string command, std::vector<std::string> arguments)
+Console::consoleCommand(std::string /*command*/, std::vector<std::string> /*arguments*/)
 {
-  if (command == "ccrs") {
-    if (arguments.size() != 1) {
-      log_info << "Usage: ccrs <command>" << std::endl;
-      return true;
-    }
-    std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(arguments[0]);
-    if ((i == commands.end()) || (i->second.size() == 0)) {
-      log_info << "unknown command: \"" << arguments[0] << "\"" << std::endl;
-      return true;
-    }
-
-    std::ostringstream ccr_list;
-    std::list<ConsoleCommandReceiver*> &ccrs = i->second;
-    std::list<ConsoleCommandReceiver*>::iterator j;
-    for (j = ccrs.begin(); j != ccrs.end(); j++) {
-      if (j != ccrs.begin()) ccr_list << ", ";
-      ccr_list << "[" << *j << "]";
-    }
-
-    log_info << "registered ccrs for \"" << arguments[0] << "\": " << ccr_list.str() << std::endl;
-    return true;
-  }
-
   return false;
 }
 
@@ -417,6 +405,9 @@ Console::hasFocus()
 void
 Console::show()
 {
+  if(!config->console_enabled)
+    return;
+
   focused = true;
   height = 256;
   alpha = 1.0;
@@ -482,56 +473,25 @@ Console::draw(DrawingContext& context)
 
   if (focused) {
     lineNo++;
-    float py = height-4-1*9;
-    context.draw_text(font.get(), "> "+inputBuffer.str()+"_", Vector(4, py), LEFT_ALLIGN, layer);
+    float py = height-4-1 * font->get_height();
+    context.draw_text(font.get(), "> "+inputBuffer.str(), Vector(4, py), ALIGN_LEFT, layer);
+    if (SDL_GetTicks() % 1000 < 750) {
+      int cursor_px = 2 + inputBuffer.pubseekoff(0, std::ios_base::cur, std::ios_base::out);
+      context.draw_text(font.get(), "_", Vector(4 + (cursor_px * font->get_text_width("X")), py), ALIGN_LEFT, layer);
+    }
   }
 
   int skipLines = -offset;
   for (std::list<std::string>::iterator i = lines.begin(); i != lines.end(); i++) {
     if (skipLines-- > 0) continue;
     lineNo++;
-    float py = height-4-lineNo*9;
-    if (py < -9) break;
-    context.draw_text(font.get(), *i, Vector(4, py), LEFT_ALLIGN, layer);
+    float py = height - 4 - lineNo*font->get_height();
+    if (py < -font->get_height()) break;
+    context.draw_text(font.get(), *i, Vector(4, py), ALIGN_LEFT, layer);
   }
-
   context.pop_transform();
 }
 
-void
-Console::registerCommand(std::string command, ConsoleCommandReceiver* ccr)
-{
-  commands[command].push_front(ccr);
-}
-
-void
-Console::unregisterCommand(std::string command, ConsoleCommandReceiver* ccr)
-{
-  std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
-  if ((i == commands.end()) || (i->second.size() == 0)) {
-    log_warning << "Command \"" << command << "\" not associated with a command receiver. Not dissociated." << std::endl;
-    return;
-  }
-  std::list<ConsoleCommandReceiver*>::iterator j = find(i->second.begin(), i->second.end(), ccr);
-  if (j == i->second.end()) {
-    log_warning << "Command \"" << command << "\" not associated with given command receiver. Not dissociated." << std::endl;
-    return;
-  }
-  i->second.erase(j);
-}
-
-void
-Console::unregisterCommands(ConsoleCommandReceiver* ccr)
-{
-  for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
-    std::list<ConsoleCommandReceiver*> &ccrs = i->second;
-    std::list<ConsoleCommandReceiver*>::iterator j;
-    while ((j = find(ccrs.begin(), ccrs.end(), ccr)) != ccrs.end()) {
-      ccrs.erase(j);
-    }
-  }
-}
-
 Console* Console::instance = NULL;
 ConsoleStreamBuffer Console::inputBuffer;
 ConsoleStreamBuffer Console::outputBuffer;