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 <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;
31 class DrawingContext;
32 
33 class ConsoleBuffer final : public Currenton<ConsoleBuffer>
34 {
35 public:
36  static std::ostream output;
39 public:
40  std::list<std::string> m_lines;
41  Console* m_console;
42 
43 public:
44  ConsoleBuffer();
45 
46  void addLines(const std::string& s);
47  void addLine(const std::string& s);
49  void flush(ConsoleStreamBuffer& buffer);
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);
67  void backspace();
68  void eraseChar();
69  void enter();
70  void scroll(int offset);
71  void autocomplete();
72  void show_history(int offset);
73  void move_cursor(int offset);
75  void draw(DrawingContext& context) const;
76  void update(float dt_sec);
77 
78  void show();
79  void open();
80  void hide();
81  void toggle();
83  bool hasFocus() const;
85 private:
86  ConsoleBuffer& m_buffer;
87 
88  std::string m_inputBuffer;
89  int m_inputBufferPosition;
91  std::list<std::string> m_history;
92  std::list<std::string>::iterator m_history_position;
94  SurfacePtr m_background;
95  SurfacePtr m_background2;
97  HSQUIRRELVM m_vm;
98  HSQOBJECT m_vm_object;
99 
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:58
std::list< std::string > m_lines
backbuffer of lines sent to the console.
Definition: console.hpp:40
static ConsoleStreamBuffer s_outputBuffer
stream buffer used by output stream
Definition: console.hpp:37
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:36
Definition: console.hpp:33
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42