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 <sstream>
22 #include <vector>
23 
24 #include <simplesquirrel/vm.hpp>
25 
26 #include "util/currenton.hpp"
27 #include "video/font_ptr.hpp"
28 #include "video/surface_ptr.hpp"
29 
30 class Console;
32 class DrawingContext;
33 
34 class ConsoleBuffer final : public Currenton<ConsoleBuffer>
35 {
36 public:
37  static std::ostream output;
40 public:
41  std::list<std::string> m_lines;
42  Console* m_console;
43 
44 public:
45  ConsoleBuffer();
46 
47  void addLines(const std::string& s);
48  void addLine(const std::string& s);
50  void flush(ConsoleStreamBuffer& buffer);
52  void set_console(Console* console);
53 
54 private:
55  ConsoleBuffer(const ConsoleBuffer&) = delete;
56  ConsoleBuffer& operator=(const ConsoleBuffer&) = delete;
57 };
58 
59 class Console final : public Currenton<Console>
60 {
61 public:
62  Console(ConsoleBuffer& buffer);
63  ~Console() override;
64 
65  void on_buffer_change(int line_count);
66 
67  void input(char c);
68  void backspace();
69  void eraseChar();
70  void enter();
71  void scroll(int offset);
72  void autocomplete();
73  void show_history(int offset);
74  void move_cursor(int offset);
76  void draw(DrawingContext& context) const;
77  void update(float dt_sec);
78 
79  void show();
80  void open();
81  void hide();
82  void toggle();
84  bool hasFocus() const;
86 private:
87  ConsoleBuffer& m_buffer;
88 
89  std::string m_inputBuffer;
90  int m_inputBufferPosition;
92  std::list<std::string> m_history;
93  std::list<std::string>::iterator m_history_position;
95  SurfacePtr m_background;
96  SurfacePtr m_background2;
98  ssq::VM m_vm;
100  int m_backgroundOffset;
101  float m_height;
102  float m_alpha;
103  int m_offset;
104  bool m_focused;
105  FontPtr m_font;
106 
107  float m_stayOpen;
108 
109  void parse(const std::string& s);
112  void ready_vm();
113 
115  void execute_script(const std::string& s);
116 
117  bool consoleCommand(const std::string& command, const std::vector<std::string>& arguments);
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 */
Definition: console.hpp:124
Definition: console.hpp:59
std::list< std::string > m_lines
backbuffer of lines sent to the console.
Definition: console.hpp:41
static ConsoleStreamBuffer s_outputBuffer
stream buffer used by output stream
Definition: console.hpp:38
void addLine(const std::string &s)
display a line in the console
Definition: console.cpp:60
void addLines(const std::string &s)
display a string of (potentially) multiple lines in the console
Definition: console.cpp:49
A &#39;Currenton&#39; allows access to the currently active instance of a class via the static current() func...
Definition: currenton.hpp:30
void flush(ConsoleStreamBuffer &buffer)
act upon changes in a ConsoleStreamBuffer
Definition: console.cpp:89
static std::ostream output
stream of characters to output to the console.
Definition: console.hpp:37
Definition: console.hpp:34
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42