diff --git a/libopenage/console/console.cpp b/libopenage/console/console.cpp index 274cbc087ee..3669ac1869d 100644 --- a/libopenage/console/console.cpp +++ b/libopenage/console/console.cpp @@ -44,79 +44,83 @@ Console::Console(/* presenter::LegacyDisplay *display */) : Console::~Console() {} -// void Console::load_colors(std::vector &colortable) { -// for (auto &c : colortable) { -// this->termcolors.emplace_back(c); -// } +/* +void Console::load_colors(std::vector &colortable) { + for (auto &c : colortable) { + this->termcolors.emplace_back(c); + } -// if (termcolors.size() != 256) { -// throw Error(MSG(err) << "Exactly 256 terminal colors are required."); -// } -// } + if (termcolors.size() != 256) { + throw Error(MSG(err) << "Exactly 256 terminal colors are required."); + } +} +*/ void Console::register_to_engine() { // TODO: Use new renderer + /* + this->display->register_input_action(this); + this->display->register_tick_action(this); + this->display->register_drawhud_action(this); + this->display->register_resize_action(this); + + Bind the console toggle key globally + auto &action = this->display->get_action_manager(); + auto &global = this->display->get_input_manager().get_global_context(); + + global.bind(action.get("TOGGLE_CONSOLE"), [this](const input::legacy::action_arg_t &) { + this->set_visible(!this->visible); + }); + - // this->display->register_input_action(this); - // this->display->register_tick_action(this); - // this->display->register_drawhud_action(this); - // this->display->register_resize_action(this); - - // Bind the console toggle key globally - // auto &action = this->display->get_action_manager(); - // auto &global = this->display->get_input_manager().get_global_context(); - - // global.bind(action.get("TOGGLE_CONSOLE"), [this](const input::legacy::action_arg_t &) { - // this->set_visible(!this->visible); - // }); - - - // TODO: bind any needed input to InputContext - - // toggle console will take highest priority - // this->input_context.bind(action.get("TOGGLE_CONSOLE"), [this](const input::legacy::action_arg_t &) { - // this->set_visible(false); - // }); - // this->input_context.bind(input::legacy::event_class::UTF8, [this](const input::legacy::action_arg_t &arg) { - // // a single char typed into the console - // std::string utf8 = arg.e.as_utf8(); - // this->buf.write(utf8.c_str()); - // command += utf8; - // return true; - // }); - // this->input_context.bind(input::legacy::event_class::NONPRINT, [this](const input::legacy::action_arg_t &arg) { - // switch (arg.e.as_char()) { - // case 8: // remove a single UTF-8 character - // if (this->command.size() > 0) { - // util::utf8_pop_back(this->command); - // this->buf.pop_last_char(); - // } - // return true; - - // case 13: // interpret command - // this->buf.write('\n'); - // this->interpret(this->command); - // this->command = ""; - // return true; - - // default: - // return false; - // } - // }); - // this->input_context.utf8_mode = true; + TODO: bind any needed input to InputContext + + toggle console will take highest priority + this->input_context.bind(action.get("TOGGLE_CONSOLE"), [this](const input::legacy::action_arg_t &) { + this->set_visible(false); + }); + this->input_context.bind(input::legacy::event_class::UTF8, [this](const input::legacy::action_arg_t &arg) { + // a single char typed into the console + std::string utf8 = arg.e.as_utf8(); + this->buf.write(utf8.c_str()); + command += utf8; + return true; + }); + this->input_context.bind(input::legacy::event_class::NONPRINT, [this](const input::legacy::action_arg_t &arg) { + switch (arg.e.as_char()) { + case 8: // remove a single UTF-8 character + if (this->command.size() > 0) { + util::utf8_pop_back(this->command); + this->buf.pop_last_char(); + } + return true; + + case 13: // interpret command + this->buf.write('\n'); + this->interpret(this->command); + this->command = ""; + return true; + + default: + return false; + } + }); + this->input_context.utf8_mode = true; + */ } void Console::set_visible(bool /* make_visible */) { // TODO: Use new renderer - - // if (make_visible) { - // this->display->get_input_manager().push_context(&this->input_context); - // this->visible = true; - // } - // else { - // this->display->get_input_manager().remove_context(&this->input_context); - // this->visible = false; - // } + /* + if (make_visible) { + this->display->get_input_manager().push_context(&this->input_context); + this->visible = true; + } + else { + this->display->get_input_manager().remove_context(&this->input_context); + this->visible = false; + } + */ } void Console::write(const char *text) { @@ -143,8 +147,7 @@ void Console::interpret(const std::string &command) { std::string value = command.substr(second_space + 1, std::string::npos); // TODO: Use new engine class // this->engine->get_cvar_manager().set(name, value); - log::log(MSG(dbg) << "Tried setting cvar " << name << " with " << value - << " but engine is not implemented yet."); + log::log(MSG(dbg) << "Tried setting cvar " << name << " with " << value << " but engine is not implemented yet."); } } else if (command.substr(0, 3) == "get") { @@ -154,60 +157,59 @@ void Console::interpret(const std::string &command) { // TODO: Use new engine class // std::string value = this->engine->get_cvar_manager().get(name); // this->write(value.c_str()); - log::log(MSG(dbg) << "Tried getting cvar " << name - << " but engine is not implemented yet."); + log::log(MSG(dbg) << "Tried getting cvar " << name << " but engine is not implemented yet."); } } } +/* +bool Console::on_tick() { + if (!this->visible) { + return true; + } -// bool Console::on_tick() { -// if (!this->visible) { -// return true; -// } - -// // TODO: handle stuff such as cursor blinking, -// // repeating held-down keys -// return true; -// } - -// bool Console::on_drawhud() { -// if (!this->visible) { -// return true; -// } + // TODO: handle stuff such as cursor blinking, + // repeating held-down keys + return true; +} -// // TODO: Use new renderer +bool Console::on_drawhud() { + if (!this->visible) { + return true; + } -// // draw::to_opengl(this->display, this); + // TODO: Use new renderer -// return true; -// } + // draw::to_opengl(this->display, this); -// bool Console::on_input(SDL_Event *e) { -// // only handle inputs if the console is visible -// if (!this->visible) { -// return true; -// } + return true; +} -// switch (e->type) { -// case SDL_KEYDOWN: -// //TODO handle key inputs +bool Console::on_input(SDL_Event *e) { + // only handle inputs if the console is visible + if (!this->visible) { + return true; + } -// //do not allow anyone else to handle this input -// return false; -// } + switch (e->type) { + case SDL_KEYDOWN: + //TODO handle key inputs -// return true; -// } + //do not allow anyone else to handle this input + return false; + } -// bool Console::on_resize(coord::viewport_delta new_size) { -// coord::pixel_t w = this->buf.get_dims().x * this->charsize.x; -// coord::pixel_t h = this->buf.get_dims().y * this->charsize.y; + return true; +} -// this->bottomleft = {(new_size.x - w) / 2, (new_size.y - h) / 2}; -// this->topright = {this->bottomleft.x + w, this->bottomleft.y - h}; +bool Console::on_resize(coord::viewport_delta new_size) { + coord::pixel_t w = this->buf.get_dims().x * this->charsize.x; + coord::pixel_t h = this->buf.get_dims().y * this->charsize.y; -// return true; -// } + this->bottomleft = {(new_size.x - w) / 2, (new_size.y - h) / 2}; + this->topright = {this->bottomleft.x + w, this->bottomleft.y - h}; + return true; +} +*/ } // namespace console } // namespace openage diff --git a/libopenage/console/draw.cpp b/libopenage/console/draw.cpp index c8835d5bcc3..ba5b4588c9e 100644 --- a/libopenage/console/draw.cpp +++ b/libopenage/console/draw.cpp @@ -17,73 +17,75 @@ namespace openage { namespace console { namespace draw { -// void to_opengl(presenter::LegacyDisplay *engine, Console *console) { -// coord::camhud topleft{ -// console->bottomleft.x, -// // TODO This should probably just be console->topright.y -// console->bottomleft.y + console->charsize.y * console->buf.dims.y}; -// coord::pixel_t ascender = static_cast(console->font.get_ascender()); - -// renderer::TextRenderer *text_renderer = engine->get_text_renderer(); -// text_renderer->set_font(&console->font); - -// int64_t monotime = timing::get_monotonic_time(); - -// bool fastblinking_visible = (monotime % 600000000 < 300000000); -// bool slowblinking_visible = (monotime % 300000000 < 150000000); - -// for (coord::term_t x = 0; x < console->buf.dims.x; x++) { -// coord::camhud chartopleft{topleft.x + console->charsize.x * x, 0}; - -// for (coord::term_t y = 0; y < console->buf.dims.y; y++) { -// chartopleft.y = topleft.y - console->charsize.y * y; -// buf_char p = *(console->buf.chrdataptr({x, y - console->buf.scrollback_pos})); - -// int fgcolid, bgcolid; - -// bool cursor_visible_at_current_pos = (console->buf.cursorpos == coord::term{x, y - console->buf.scrollback_pos}); - -// cursor_visible_at_current_pos &= console->buf.cursor_visible; - -// if (((p.flags & CHR_NEGATIVE) != 0) xor cursor_visible_at_current_pos) { -// bgcolid = p.fgcol; -// fgcolid = p.bgcol; -// } -// else { -// bgcolid = p.bgcol; -// fgcolid = p.fgcol; -// } - -// if ((p.flags & CHR_INVISIBLE) -// or (p.flags & CHR_BLINKING and not slowblinking_visible) -// or (p.flags & CHR_BLINKINGFAST and not fastblinking_visible)) { -// fgcolid = bgcolid; -// } - -// console->termcolors[bgcolid].use(0.8); - -// glBegin(GL_QUADS); -// { -// glVertex3f(chartopleft.x, chartopleft.y, 0); -// glVertex3f(chartopleft.x, chartopleft.y - console->charsize.y, 0); -// glVertex3f(chartopleft.x + console->charsize.x, chartopleft.y - console->charsize.y, 0); -// glVertex3f(chartopleft.x + console->charsize.x, chartopleft.y, 0); -// } -// glEnd(); - -// console->termcolors[fgcolid].use(1); - -// char utf8buf[5]; -// if (util::utf8_encode(p.cp, utf8buf) == 0) { -// //unrepresentable character (question mark in black rhombus) -// text_renderer->draw(chartopleft.x, chartopleft.y - ascender, "\uFFFD"); -// } -// else { -// text_renderer->draw(chartopleft.x, chartopleft.y - ascender, utf8buf); -// } -// } -// } -// } +/* +void to_opengl(presenter::LegacyDisplay *engine, Console *console) { + coord::camhud topleft{ + console->bottomleft.x, + // TODO This should probably just be console->topright.y + console->bottomleft.y + console->charsize.y * console->buf.dims.y}; + coord::pixel_t ascender = static_cast(console->font.get_ascender()); + + renderer::TextRenderer *text_renderer = engine->get_text_renderer(); + text_renderer->set_font(&console->font); + + int64_t monotime = timing::get_monotonic_time(); + + bool fastblinking_visible = (monotime % 600000000 < 300000000); + bool slowblinking_visible = (monotime % 300000000 < 150000000); + + for (coord::term_t x = 0; x < console->buf.dims.x; x++) { + coord::camhud chartopleft{topleft.x + console->charsize.x * x, 0}; + + for (coord::term_t y = 0; y < console->buf.dims.y; y++) { + chartopleft.y = topleft.y - console->charsize.y * y; + buf_char p = *(console->buf.chrdataptr({x, y - console->buf.scrollback_pos})); + + int fgcolid, bgcolid; + + bool cursor_visible_at_current_pos = (console->buf.cursorpos == coord::term{x, y - console->buf.scrollback_pos}); + + cursor_visible_at_current_pos &= console->buf.cursor_visible; + + if (((p.flags & CHR_NEGATIVE) != 0) xor cursor_visible_at_current_pos) { + bgcolid = p.fgcol; + fgcolid = p.bgcol; + } + else { + bgcolid = p.bgcol; + fgcolid = p.fgcol; + } + + if ((p.flags & CHR_INVISIBLE) + or (p.flags & CHR_BLINKING and not slowblinking_visible) + or (p.flags & CHR_BLINKINGFAST and not fastblinking_visible)) { + fgcolid = bgcolid; + } + + console->termcolors[bgcolid].use(0.8); + + glBegin(GL_QUADS); + { + glVertex3f(chartopleft.x, chartopleft.y, 0); + glVertex3f(chartopleft.x, chartopleft.y - console->charsize.y, 0); + glVertex3f(chartopleft.x + console->charsize.x, chartopleft.y - console->charsize.y, 0); + glVertex3f(chartopleft.x + console->charsize.x, chartopleft.y, 0); + } + glEnd(); + + console->termcolors[fgcolid].use(1); + + char utf8buf[5]; + if (util::utf8_encode(p.cp, utf8buf) == 0) { + //unrepresentable character (question mark in black rhombus) + text_renderer->draw(chartopleft.x, chartopleft.y - ascender, "\uFFFD"); + } + else { + text_renderer->draw(chartopleft.x, chartopleft.y - ascender, utf8buf); + } + } + } +} +*/ void to_terminal(Buf *buf, util::FD *fd, bool clear) { //move cursor, draw top left corner