1 /*
2  *  Console.h - console utilities for Aleph One
3 
4 	Copyright (C) 2005 and beyond by Gregory Smith
5 	and the "Aleph One" developers.
6 
7 	This program is free software; you can redistribute it and/or modify
8 	it under the terms of the GNU General Public License as published by
9 	the Free Software Foundation; either version 3 of the License, or
10 	(at your option) any later version.
11 
12 	This program is distributed in the hope that it will be useful,
13 	but WITHOUT ANY WARRANTY; without even the implied warranty of
14 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 	GNU General Public License for more details.
16 
17 	This license is contained in the file "COPYING",
18 	which is included with this source code; it is available online at
19 	http://www.gnu.org/licenses/gpl.html
20 
21 */
22 
23 #ifndef CONSOLE_H
24 #define CONSOLE_H
25 
26 #include <string>
27 #include <map>
28 #include <boost/function.hpp>
29 #include "preferences.h"
30 
31 class CommandParser
32 {
33 public:
CommandParser()34 	CommandParser() { }
35 	virtual ~CommandParser() = default;
36 	void register_command(std::string command, boost::function<void(const std::string&)> f);
37 	void register_command(std::string command, const CommandParser& command_parser);
38 	void unregister_command(std::string command);
39 
40 	virtual void parse_and_execute(const std::string& command_string);
41 private:
42 	typedef std::map<std::string, boost::function<void(const std::string&)> > command_map;
43 	command_map m_commands;
44 };
45 
46 class Console : public CommandParser
47 {
48 public:
49 	static Console* instance();
50 
51 	// called by key handlers
52 	void enter();
53 	void abort(); // callback is called with empty string
54 	void del();
55 	void backspace();
56 	void clear();
57 	void forward_clear();
58 	void transpose();
59 	void delete_word();
60 	void textEvent(const SDL_Event &e);
61 	void up_arrow();
62 	void down_arrow();
63 	void left_arrow();
64 	void right_arrow();
65 	void line_home();
66 	void line_end();
displayBuffer()67 	const std::string &displayBuffer() { return m_displayBuffer; }
68 
69 	void activate_input(boost::function<void (const std::string&)> callback,
70 			    const std::string& prompt);
71 	void deactivate_input(); // like abort, but no callback
72 
input_active()73 	bool input_active() { return m_active; }
74 	int cursor_position();
75 
76 	void register_macro(std::string macro, std::string replacement);
77 	void unregister_macro(std::string macro);
78 	void clear_macros();
79 
80 	// carnage reporting
81 	void set_carnage_message(int16 projectile_type, const std::string& on_kill, const std::string& on_suicide = "");
82 	void report_kill(int16 player_index, int16 aggressor_player_index, int16 projectile_index);
83 	void clear_carnage_messages();
84 
use_lua_console()85 	bool use_lua_console() { return m_use_lua_console || environment_preferences->use_solo_lua; };
use_lua_console(bool f_use)86 	void use_lua_console(bool f_use) { m_use_lua_console = f_use; }
87 
88 	// clear last saved level name
89 	void clear_saves();
90 
91 private:
92 	Console();
93 
94 	boost::function<void (std::string)> m_callback;
95 	std::string m_buffer;
96 	std::string m_displayBuffer;
97 	std::string m_prompt;
98 	bool m_active;
99 
100 	std::vector<std::string> m_prev_commands;
101 	std::vector<std::string>::iterator m_command_iter;
102 	void set_command(std::string command);
103 
104 	int m_cursor_position;
105 
106 	std::map<std::string, std::string> m_macros;
107 
108 	bool m_carnage_messages_exist;
109 	std::vector<std::pair<std::string, std::string> > m_carnage_messages;
110 
111 	bool m_use_lua_console;
112 
113 	void register_save_commands();
114 };
115 
116 class InfoTree;
117 void parse_mml_console(const InfoTree& root);
118 void reset_mml_console();
119 
120 #endif
121 
122 
123