src/supertux/console.hpp

Go to the documentation of this file.
00001 //  SuperTux - Console
00002 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #ifndef HEADER_SUPERTUX_SUPERTUX_CONSOLE_HPP
00018 #define HEADER_SUPERTUX_SUPERTUX_CONSOLE_HPP
00019 
00020 #include <list>
00021 #include <memory>
00022 #include <squirrel.h>
00023 #include <sstream>
00024 #include <vector>
00025 
00026 #include "video/font_ptr.hpp"
00027 #include "video/surface_ptr.hpp"
00028 
00029 class Console;
00030 class ConsoleStreamBuffer;
00031 class ConsoleCommandReceiver;
00032 class DrawingContext;
00033 
00034 class Console
00035 {
00036 public:
00037   Console();
00038   ~Console();
00039 
00040   static Console* instance;
00041 
00042   static std::ostream output; 
00044   void init_graphics();
00045 
00046   void input(char c); 
00047   void backspace(); 
00048   void eraseChar(); 
00049   void enter(); 
00050   void scroll(int offset); 
00051   void autocomplete(); 
00052   void show_history(int offset); 
00053   void move_cursor(int offset); 
00055   void draw(DrawingContext& context); 
00056   void update(float elapsed_time);
00057 
00058   void show(); 
00059   void open(); 
00060   void hide(); 
00061   void toggle(); 
00063   bool hasFocus(); 
00065   template<typename T> static bool string_is(std::string s) {
00066     std::istringstream iss(s);
00067     T i;
00068     if ((iss >> i) && iss.eof()) {
00069       return true;
00070     } else {
00071       return false;
00072     }
00073   }
00074 
00075   template<typename T> static T string_to(std::string s) {
00076     std::istringstream iss(s);
00077     T i;
00078     if ((iss >> i) && iss.eof()) {
00079       return i;
00080     } else {
00081       return T();
00082     }
00083   }
00084 
00085 private:
00086   std::list<std::string> history; 
00087   std::list<std::string>::iterator history_position; 
00088   std::list<std::string> lines; 
00090   SurfacePtr background; 
00091   SurfacePtr background2; 
00093   HSQUIRRELVM vm; 
00094   HSQOBJECT vm_object;
00095 
00096   int backgroundOffset; 
00097   float height; 
00098   float alpha;
00099   int offset; 
00100   bool focused; 
00101   FontPtr font;
00102   float fontheight; 
00104   float stayOpen;
00105 
00106   static int inputBufferPosition; 
00107   static std::string inputBuffer; 
00108   static ConsoleStreamBuffer outputBuffer; 
00110   void addLines(std::string s); 
00111   void addLine(std::string s); 
00112   void parse(std::string s); 
00115   void ready_vm();
00116 
00118   void execute_script(const std::string& s);
00119 
00120   bool consoleCommand(std::string command, std::vector<std::string> arguments); 
00122   friend class ConsoleStreamBuffer;
00123   void flush(ConsoleStreamBuffer* buffer); 
00125 private:
00126   Console(const Console&);
00127   Console & operator=(const Console&);
00128 };
00129 
00130 class ConsoleStreamBuffer : public std::stringbuf
00131 {
00132 public:
00133   int sync()
00134   {
00135     int result = std::stringbuf::sync();
00136     if(Console::instance != NULL)
00137       Console::instance->flush(this);
00138     return result;
00139   }
00140 };
00141 
00142 #endif
00143 
00144 /* EOF */

Generated on Mon Jun 9 03:38:22 2014 for SuperTux by  doxygen 1.5.1