20 #if defined(_DEBUG) && defined(_WINDOWS)
21 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
27 Console_State::Console_State()
28 : m_virtual_screen(Point2f(0.0
f, 0.0
f), Point2f(float(
get_Window().get_width() * 600.0
f /
get_Window().get_height()), 600.0
f)),
29 m_projector(m_virtual_screen),
30 m_bg(Vertex2f_Color(Point2f(0.0
f, 0.0
f),
get_Colors()[
"console_border"]),
31 Vertex2f_Color(Point2f(0.0
f, 54.0
f + 7.0
f *
get_Fonts()[
"system_36_x600"].get_text_height()),
get_Colors()[
"console_border"]),
32 Vertex2f_Color(Point2f(m_virtual_screen.second.
x, 54.0
f + 7.0
f *
get_Fonts()[
"system_36_x600"].get_text_height()),
get_Colors()[
"console_border"]),
33 Vertex2f_Color(Point2f(m_virtual_screen.second.
x, 0.0
f),
get_Colors()[
"console_border"])),
34 m_log(Point2f(18.0
f, 18.0
f),
35 Point2f(m_virtual_screen.second.
x - 18.0
f,
36 18.0
f + 6.0
f *
get_Fonts()[
"system_36_x600"].get_text_height()),
41 m_prompt(Point2f(18.0
f, 36.0
f + 6.0
f *
get_Fonts()[
"system_36_x600"].get_text_height()),
42 Point2f(m_virtual_screen.second.
x - 18.0
f,
43 36.0
f + 7.0
f *
get_Fonts()[
"system_36_x600"].get_text_height()),
51 m_functions[
"args"] =
new Console_Function;
53 m_log.give_BG_Renderer(
new Widget_Renderer_Color(
get_Colors()[
"console_background"]));
54 m_prompt.give_BG_Renderer(
new Widget_Renderer_Color(
get_Colors()[
"console_background"]));
57 Console_State::~Console_State() {
58 for(std::map<String, Console_Function *>::iterator it = m_functions.begin(); it != m_functions.end(); ++it)
62 Gamestate & Console_State::get_child() {
67 void Console_State::set_child(Gamestate child) {
73 void Console_State::clear_child() {
83 const unsigned int max_lines =
static_cast<unsigned int>(m_log.
get_max_lines());
84 for(
unsigned int endl_count = 0
u, pos = static_cast<unsigned int>(m_text.
size() - 1
u); pos !=
static_cast<unsigned int>(-1); --pos)
85 if(m_text[pos] ==
'\n' && ++endl_count == max_lines) {
86 m_text = m_text.
substr(pos + 1);
101 if(m_functions[name])
102 delete m_functions[
name];
103 m_functions[
name] =
function;
106 void Console_State::fire_command(
const String &text) {
111 std::vector<String> args;
114 bool in_quotes =
false;
115 bool escaped =
false;
124 if(in_quotes || !token.
empty()) {
128 args.push_back(token);
131 in_quotes = !in_quotes;
138 else if(!token.
empty()) {
142 args.push_back(token);
162 args.push_back(token);
163 else if(command.
empty())
166 std::map<String, Console_Function *>::iterator
func = m_functions.find(command);
167 if(func != m_functions.end())
168 (*(func->second))(*
this, command, args);
174 m_handled_event =
false;
176 if(!m_handled_event && m_child) {
184 const bool mod_nonshift = gr.get_key_state(
SDLK_LALT) || gr.get_key_state(
SDLK_RALT) ||
186 #if SDL_VERSION_ATLEAST(1,3,0)
189 gr.get_key_state(SDLK_LMETA) || gr.get_key_state(SDLK_RMETA) ||
190 gr.get_key_state(SDLK_LSUPER) || gr.get_key_state(SDLK_RSUPER);
206 const String backup_text = m_prompt.
get_text();
214 m_prompt.
seek(backup_pos);
225 m_prompt.
on_event(event, m_virtual_screen);
230 m_prompt.
on_event(event, m_virtual_screen);
234 void Console_State::perform_logic() {
235 m_virtual_screen = std::make_pair(Point2f(0.0
f, 0.0
f), Point2f(
float(
get_Window().get_width() * 600.0
f /
get_Window().get_height()), 600.0
f));
236 m_projector = Projector2D(m_virtual_screen);
238 if(m_bg.c.position.x != m_virtual_screen.second.x) {
239 m_bg.c.position.x = m_virtual_screen.second.x;
240 m_bg.d.position.x = m_virtual_screen.second.x;
242 const float right = m_virtual_screen.second.x - 18.0f;
250 void Console_State::prerender() {
260 if(num_lines > max_lines)
261 m_log.
erase_lines(0, static_cast<unsigned int>(num_lines - max_lines));
268 void Console_State::render() {
274 vr.clear_depth_buffer();
275 vr.set_2d(m_virtual_screen);
284 const std::vector<String> &args)
289 std::vector<String>::const_iterator it = args.begin();
291 for(++it; it != args.end(); ++it)
void on_event(const SDL_Event &event)
Colors & get_Colors()
Get access to the singleton.
void give_function(const String &name, Console_Function *const &function)
EGLImageKHR EGLint * name
Game & get_Game()
Get access to the singleton.
String substr(size_t pos=0, size_t n=npos) const
size_t get_max_lines() const
void erase_lines(const unsigned int &begin, const unsigned int &end)
Keyboard button event structure (event.key.*)
virtual void set_lower_right(const Point2f &lower_right_)
virtual void on_event(const SDL_Event &event)
First check for events. Called by Game as part of the main gameloop.
size_t get_num_lines() const
Window & get_Window()
Get access to the singleton.
Mouse motion event structure (event.motion.*)
static const int & get_height()
Get the height of the window.
const int & get_edit_pos() const
void set_text(const String &text_)
const String & get_text() const
void write_to_log(const String &text)
Provide a text console to process commands.
Fonts & get_Fonts()
Get access to the singleton.
virtual void operator()(Console_State &console, const String &name, const std::vector< String > &args)
Video & get_Video()
Get access to the singleton.
void seek(const int &edit_pos)