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