Exclude end sequence from focus lose code
[supertux.git] / src / supertux / console.hpp
index d81bbf3..d1c1da9 100644 (file)
@@ -23,6 +23,7 @@
 #include <sstream>
 #include <vector>
 
+#include "util/currenton.hpp"
 #include "video/font_ptr.hpp"
 #include "video/surface_ptr.hpp"
 
@@ -31,17 +32,35 @@ class ConsoleStreamBuffer;
 class ConsoleCommandReceiver;
 class DrawingContext;
 
-class Console
+class ConsoleBuffer : public Currenton<ConsoleBuffer>
 {
 public:
-  Console();
-  ~Console();
+  static std::ostream output; /**< stream of characters to output to the console. Do not forget to send std::endl or to flush the stream. */
+  static ConsoleStreamBuffer s_outputBuffer; /**< stream buffer used by output stream */
 
-  static Console* instance;
+public:
+  std::list<std::string> m_lines; /**< backbuffer of lines sent to the console. New lines get added to front. */
 
-  static std::ostream output; /**< stream of characters to output to the console. Do not forget to send std::endl or to flush the stream. */
+public:
+  ConsoleBuffer();
+
+  void addLines(const std::string& s); /**< display a string of (potentially) multiple lines in the console */
+  void addLine(const std::string& s); /**< display a line in the console */
+
+  void flush(ConsoleStreamBuffer& buffer); /**< act upon changes in a ConsoleStreamBuffer */
+
+private:
+  ConsoleBuffer(const ConsoleBuffer&) = delete;
+  ConsoleBuffer& operator=(const ConsoleBuffer&) = delete;
+};
 
-  void init_graphics();
+class Console : public Currenton<Console>
+{
+public:
+  Console(ConsoleBuffer& buffer);
+  ~Console();
+
+  void on_buffer_change(int line_count);
 
   void input(char c); /**< add character to inputBuffer */
   void backspace(); /**< delete character left of inputBufferPosition */
@@ -62,53 +81,30 @@ public:
 
   bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */
 
-  template<typename T> static bool string_is(std::string s) {
-    std::istringstream iss(s);
-    T i;
-    if ((iss >> i) && iss.eof()) {
-      return true;
-    } else {
-      return false;
-    }
-  }
-
-  template<typename T> static T string_to(std::string s) {
-    std::istringstream iss(s);
-    T i;
-    if ((iss >> i) && iss.eof()) {
-      return i;
-    } else {
-      return T();
-    }
-  }
-
 private:
-  std::list<std::string> history; /**< command history. New lines get added to back. */
-  std::list<std::string>::iterator history_position; /**< item of command history that is currently displayed */
-  std::list<std::string> lines; /**< backbuffer of lines sent to the console. New lines get added to front. */
+  ConsoleBuffer& m_buffer;
 
-  SurfacePtr background; /**< console background image */
-  SurfacePtr background2; /**< second, moving console background image */
+  std::string m_inputBuffer; /**< string used for keyboard input */
+  int m_inputBufferPosition; /**< position in inputBuffer before which to append new characters */
 
-  HSQUIRRELVM vm; /**< squirrel thread for the console (with custom roottable) */
-  HSQOBJECT vm_object;
+  std::list<std::string> m_history; /**< command history. New lines get added to back. */
+  std::list<std::string>::iterator m_history_position; /**< item of command history that is currently displayed */
 
-  int backgroundOffset; /**< current offset of scrolling background image */
-  float height; /**< height of the console in px */
-  float alpha;
-  int offset; /**< decrease to scroll text up */
-  bool focused; /**< true if console has input focus */
-  FontPtr font;
-  float fontheight; /**< height of the font (this is a separate var, because the font could not be initialized yet but is needed in the addLine message */
+  SurfacePtr m_background; /**< console background image */
+  SurfacePtr m_background2; /**< second, moving console background image */
 
-  float stayOpen;
+  HSQUIRRELVM m_vm; /**< squirrel thread for the console (with custom roottable) */
+  HSQOBJECT m_vm_object;
 
-  static int inputBufferPosition; /**< position in inputBuffer before which to append new characters */
-  static std::string inputBuffer; /**< string used for keyboard input */
-  static ConsoleStreamBuffer outputBuffer; /**< stream buffer used by output stream */
+  int m_backgroundOffset; /**< current offset of scrolling background image */
+  float m_height; /**< height of the console in px */
+  float m_alpha;
+  int m_offset; /**< decrease to scroll text up */
+  bool m_focused; /**< true if console has input focus */
+  FontPtr m_font;
+
+  float m_stayOpen;
 
-  void addLines(std::string s); /**< display a string of (potentially) multiple lines in the console */
-  void addLine(std::string s); /**< display a line in the console */
   void parse(std::string s); /**< react to a given command */
 
   /** ready a virtual machine instance, creating a new thread and loading default .nut files if needed */
@@ -119,9 +115,6 @@ private:
 
   bool consoleCommand(std::string command, std::vector<std::string> arguments); /**< process internal command; return false if command was unknown, true otherwise */
 
-  friend class ConsoleStreamBuffer;
-  void flush(ConsoleStreamBuffer* buffer); /**< act upon changes in a ConsoleStreamBuffer */
-
 private:
   Console(const Console&);
   Console & operator=(const Console&);
@@ -133,8 +126,8 @@ public:
   int sync()
   {
     int result = std::stringbuf::sync();
-    if(Console::instance != NULL)
-      Console::instance->flush(this);
+    if(ConsoleBuffer::current())
+      ConsoleBuffer::current()->flush(*this);
     return result;
   }
 };