Update CMake to 3.2.1 in .travis.yml
[supertux.git] / src / supertux / console.hpp
1 //  SuperTux - Console
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #ifndef HEADER_SUPERTUX_SUPERTUX_CONSOLE_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_CONSOLE_HPP
19
20 #include <list>
21 #include <memory>
22 #include <squirrel.h>
23 #include <sstream>
24 #include <vector>
25
26 #include "util/currenton.hpp"
27 #include "video/font_ptr.hpp"
28 #include "video/surface_ptr.hpp"
29
30 class Console;
31 class ConsoleStreamBuffer;
32 class ConsoleCommandReceiver;
33 class DrawingContext;
34
35 class ConsoleBuffer : public Currenton<ConsoleBuffer>
36 {
37 public:
38   static std::ostream output; /**< stream of characters to output to the console. Do not forget to send std::endl or to flush the stream. */
39   static ConsoleStreamBuffer s_outputBuffer; /**< stream buffer used by output stream */
40
41 public:
42   std::list<std::string> m_lines; /**< backbuffer of lines sent to the console. New lines get added to front. */
43
44 public:
45   ConsoleBuffer();
46
47   void addLines(const std::string& s); /**< display a string of (potentially) multiple lines in the console */
48   void addLine(const std::string& s); /**< display a line in the console */
49
50   void flush(ConsoleStreamBuffer& buffer); /**< act upon changes in a ConsoleStreamBuffer */
51
52 private:
53   ConsoleBuffer(const ConsoleBuffer&) = delete;
54   ConsoleBuffer& operator=(const ConsoleBuffer&) = delete;
55 };
56
57 class Console : public Currenton<Console>
58 {
59 public:
60   Console(ConsoleBuffer& buffer);
61   ~Console();
62
63   void on_buffer_change(int line_count);
64
65   void input(char c); /**< add character to inputBuffer */
66   void backspace(); /**< delete character left of inputBufferPosition */
67   void eraseChar(); /**< delete character at inputBufferPosition */
68   void enter(); /**< process and clear input stream */
69   void scroll(int offset); /**< scroll console text up or down by @c offset lines */
70   void autocomplete(); /**< autocomplete current command */
71   void show_history(int offset); /**< move @c offset lines forward through history; Negative offset moves backward */
72   void move_cursor(int offset); /**< move the cursor @c offset chars to the right; Negative offset moves backward; 0xFFFF moves to the end */
73
74   void draw(DrawingContext& context); /**< draw the console in a DrawingContext */
75   void update(float elapsed_time);
76
77   void show(); /**< display the console */
78   void open(); /**< open the console for viewing for 6 seconds */
79   void hide(); /**< hide the console */
80   void toggle(); /**< display the console if hidden, hide otherwise */
81
82   bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */
83
84 private:
85   ConsoleBuffer& m_buffer;
86
87   std::string m_inputBuffer; /**< string used for keyboard input */
88   int m_inputBufferPosition; /**< position in inputBuffer before which to append new characters */
89
90   std::list<std::string> m_history; /**< command history. New lines get added to back. */
91   std::list<std::string>::iterator m_history_position; /**< item of command history that is currently displayed */
92
93   SurfacePtr m_background; /**< console background image */
94   SurfacePtr m_background2; /**< second, moving console background image */
95
96   HSQUIRRELVM m_vm; /**< squirrel thread for the console (with custom roottable) */
97   HSQOBJECT m_vm_object;
98
99   int m_backgroundOffset; /**< current offset of scrolling background image */
100   float m_height; /**< height of the console in px */
101   float m_alpha;
102   int m_offset; /**< decrease to scroll text up */
103   bool m_focused; /**< true if console has input focus */
104   FontPtr m_font;
105
106   float m_stayOpen;
107
108   void parse(std::string s); /**< react to a given command */
109
110   /** ready a virtual machine instance, creating a new thread and loading default .nut files if needed */
111   void ready_vm();
112
113   /** execute squirrel script and output result */
114   void execute_script(const std::string& s);
115
116   bool consoleCommand(std::string command, std::vector<std::string> arguments); /**< process internal command; return false if command was unknown, true otherwise */
117
118 private:
119   Console(const Console&);
120   Console & operator=(const Console&);
121 };
122
123 class ConsoleStreamBuffer : public std::stringbuf
124 {
125 public:
126   int sync()
127   {
128     int result = std::stringbuf::sync();
129     if(ConsoleBuffer::current())
130       ConsoleBuffer::current()->flush(*this);
131     return result;
132   }
133 };
134
135 #endif
136
137 /* EOF */