10 #include <console.hpp>
11 #include <key_definitions.hpp>
13 static bool singleton_initialized =
false;
20 if (singleton_initialized) {
21 throw std::runtime_error(
"The dev console cannot be initialized more than once!");
23 singleton_initialized =
true;
26 color = {50, 50, 50, 100};
27 color_text = {255, 255, 255, 255};
28 color_info = {10, 50, 255, 255};
29 color_error = {255, 0, 0, 255};
30 color_success = {50, 255, 10, 255};
33 gMessage_bus.
subscribe(LOG_ERROR, &msg_sub);
34 gMessage_bus.
subscribe(LOG_SUCCESS, &msg_sub);
35 gMessage_bus.
subscribe(LOG_INFO, &msg_sub);
36 gMessage_bus.
subscribe(CONSOLE_TOGGLE, &msg_sub);
37 gMessage_bus.
subscribe(MOUSE_SCROLL, &msg_sub);
38 gMessage_bus.
subscribe(KEY_PRESSED, &msg_sub);
39 gMessage_bus.
subscribe(KEY_HELD, &msg_sub);
40 gMessage_bus.
subscribe(KEY_RELEASED, &msg_sub);
41 gMessage_bus.
subscribe(TEXT_STREAM, &msg_sub);
42 gMessage_bus.
subscribe(CONSOLE_CLEAR, &msg_sub);
45 void console::post_init()
const {
46 gMessage_bus.
send_msg(
new message(REGISTER_KEY,
static_cast<uint8_t
>(ST::key::ENTER)));
47 gMessage_bus.
send_msg(
new message(REGISTER_KEY,
static_cast<uint8_t
>(ST::key::TILDE)));
48 gMessage_bus.
send_msg(
new message(REGISTER_KEY,
static_cast<uint8_t
>(ST::key::LEFT)));
49 gMessage_bus.
send_msg(
new message(REGISTER_KEY,
static_cast<uint8_t
>(ST::key::RIGHT)));
50 gMessage_bus.
send_msg(
new message(REGISTER_KEY,
static_cast<uint8_t
>(ST::key::UP)));
51 gMessage_bus.
send_msg(
new message(REGISTER_KEY,
static_cast<uint8_t
>(ST::key::DOWN)));
52 gMessage_bus.
send_msg(
new message(REGISTER_KEY,
static_cast<uint8_t
>(ST::key::BACKSPACE)));
53 gMessage_bus.
send_msg(
new message(REGISTER_KEY,
static_cast<uint8_t
>(ST::key::DELETE)));
60 void console::scroll(int32_t scroll_y) {
61 scroll_offset = scroll_y * 20;
68 void console::handle_messages() {
70 while (temp !=
nullptr) {
71 switch (temp->msg_name) {
73 auto log =
static_cast<std::string *
>(temp->
get_data());
74 if (log_level == 0x07 || log_level == 0x01 || log_level == 0x03 || log_level == 0x05) {
75 write(*log, ST::log_type::ERROR);
80 auto log =
static_cast<std::string *
>(temp->
get_data());
81 if (log_level >= 0x04) {
82 write(*log, ST::log_type::INFO);
87 auto log =
static_cast<std::string *
>(temp->
get_data());
88 if (log_level >= 0x06 || log_level == 0x02 || log_level == 0x03) {
89 write(*log, ST::log_type::SUCCESS);
97 this->entries.clear();
100 scroll(
static_cast<int32_t
>(temp->base_data0));
103 auto key_val =
static_cast<ST::key
>(temp->base_data0);
106 if (hold_counter > 10) {
108 if (key_val == ST::key::ENTER) {
110 }
else if (key_val == ST::key::LEFT) {
112 }
else if (key_val == ST::key::RIGHT) {
114 }
else if (key_val == ST::key::UP) {
116 }
else if (key_val == ST::key::DOWN) {
118 }
else if (key_val == ST::key::BACKSPACE) {
120 }
else if (key_val == ST::key::DELETE) {
130 auto key_val =
static_cast<ST::key
>(temp->base_data0);
131 if (key_val == ST::key::TILDE) {
134 if (key_val == ST::key::ENTER) {
136 }
else if (key_val == ST::key::LEFT) {
138 }
else if (key_val == ST::key::RIGHT) {
140 }
else if (key_val == ST::key::UP) {
142 }
else if (key_val == ST::key::DOWN) {
144 }
else if (key_val == ST::key::BACKSPACE) {
146 }
else if (key_val == ST::key::DELETE) {
154 std::string received_data = *
static_cast<std::string *
>(temp->
get_data());
155 for (
char const &c: received_data) {
156 if (c > 126 || c < 0) {
157 received_data.clear();
160 if (received_data ==
"(") {
161 received_data +=
")";
162 composition.insert(cursor_position--, received_data);
163 }
else if (received_data ==
"\"") {
164 received_data += received_data;
165 composition.insert(cursor_position--, received_data);
166 }
else if (received_data ==
"[" || received_data ==
"{") {
167 received_data +=
static_cast<char>(received_data.at(0) + 2);
168 composition.insert(cursor_position--, received_data);
170 composition.insert(cursor_position, received_data);
172 cursor_position +=
static_cast<uint16_t
>(received_data.size());
191 log_level =
static_cast<uint8_t
>(arg);
197 void console::toggle() {
210 void console::write(
const std::string &arg, ST::log_type type) {
211 if (type == ST::log_type::ERROR) {
212 fprintf(stderr,
"%s\n", arg.c_str());
214 fprintf(stdout,
"%s\n", arg.c_str());
216 entries.emplace_back(type, arg);
218 if (entries.size() > 1000) {
219 entries.erase(entries.begin());
234 void console::hide() {
241 void console::show() {
249 singleton_initialized =
false;
256 void console::backspaceAction() {
257 if (cursor_position > 0) {
258 composition.erase(
static_cast<uint16_t
>(cursor_position - 1), 1);
259 cursor_position -= 1;
267 void console::rightKeyAction() {
268 if (cursor_position < composition.size()) {
269 cursor_position += 1;
277 void console::leftKeyAction() {
278 if (cursor_position > 0) {
279 cursor_position -= 1;
287 void console::downKeyAction() {
288 if (entries_history_index < command_entries.size() - 1) {
289 entries_history_index++;
290 composition = command_entries.at(
static_cast<uint64_t
>(entries_history_index));
292 entries_history_index = -1;
293 composition = composition_history_temp;
296 cursor_position =
static_cast<uint16_t
>(composition.size());
303 void console::upKeyAction() {
304 if (entries_history_index == -1 && !command_entries.empty()) {
305 composition_history_temp = composition;
306 composition = command_entries.back();
307 entries_history_index =
static_cast<int16_t
>(command_entries.size() - 1);
308 }
else if (entries_history_index > 0) {
309 entries_history_index--;
310 composition = command_entries.at(
static_cast<uint64_t
>(entries_history_index));
313 cursor_position =
static_cast<uint16_t
>(composition.size());
323 void console::enterKeyAction() {
324 if (!composition.empty()) {
325 write(composition, ST::log_type::INFO);
326 command_entries.emplace_back(composition);
327 gMessage_bus.
send_msg(
new message(EXECUTE_SCRIPT, make_data(composition)));
331 entries_history_index = -1;
339 void console::deleteKeyAction() {
340 composition.erase(cursor_position, 1);
void set_log_level(ST::log_type arg)
console(message_bus &gMessageBus)
The central messaging system of the engine. All subsystem make extensive use of it.
void subscribe(uint8_t msg, subscriber *sub)
void send_msg(message *msg)
A message object passed around in the message bus. Holds anything created with make_data<>().
message * get_next_message()