/* client/console.cc This file is part of the Osirion project and is distributed under the terms and conditions of the GNU General Public License version 2 */ #include #include #include #include "auxiliary/functions.h" #include "audio/audio.h" #include "core/application.h" #include "core/core.h" #include "core/gameinterface.h" #include "filesystem/filesystem.h" #include "render/gl.h" #include "ui/console.h" #include "ui/paint.h" namespace ui { const float DEFAULT_CONSOLE_HEIGHT = 0.7f; const size_t DEFAULT_MAX_HISTO_LINES = 512; // the global console buffer object ConsoleBuffer Console::con_buffer; /* -- ConsoleBuffer ------------------------------------------------ */ ConsoleBuffer::ConsoleBuffer() : sys::ConsoleInterface() { //con_print << "^BInitializing console..." << std::endl; } ConsoleBuffer::~ConsoleBuffer() { //con_print << "^BShutting down console..." << std::endl; } //--- Console ----------------------------------------------------- Console::Console(ui::Widget *parent) : ui::Window(parent) { set_visible(false); set_border(false); set_background(true); set_label("console"); history.clear(); history.push_back(""); history_pos = history.rbegin(); console_scrollpane = new ui::ScrollPane(this, con_buffer.log()); console_scrollpane->set_border(false); console_scrollpane->set_scroll(0); console_input = new ui::InputBox(this); console_input->set_focus(); console_input->set_border(false); console_input->set_background(false); console_input->set_prompt("^N>"); load_history(); } Console::~Console() { save_history(); history.clear(); } void Console::show() { ui::Window::show(); SDL_WM_GrabInput(SDL_GRAB_OFF); SDL_ShowCursor(SDL_ENABLE); console_scrollpane->set_scroll(0); history_pos = history.rbegin(); (*history_pos).clear(); console_input->set_text((*history_pos)); audio::play("ui/console"); } void Console::hide() { ui::Window::hide(); SDL_WM_GrabInput(SDL_GRAB_ON); SDL_ShowCursor(SDL_DISABLE); audio::play("ui/console"); } void Console::toggle() { if (visible()) hide(); else show(); } bool Console::on_keypress(const int key, const unsigned int modifier) { // number of lines to scroll const size_t scroll_offset = 3; ui::Text::reverse_iterator upit; switch( key ) { case SDLK_ESCAPE: if (visible()) { hide(); return true; } else { return false; } break; /* case SDLK_TAB: core::CommandBuffer::complete( (*history_pos), input_pos); return true; break; */ case SDLK_RETURN: if (console_input->text().size()) { // store input in history while (history.size() >= DEFAULT_MAX_HISTO_LINES) { history.pop_front(); } core::cmd() << console_input->text() << std::endl; con_print << "^B>" << console_input->text() << std::endl; (*history.rbegin()).assign(console_input->text()); history.push_back(""); history_pos = history.rbegin(); console_input->set_text((*history_pos)); } return true; break; case SDLK_UP: upit = history_pos; ++upit; if (upit != history.rend()) { history_pos = upit; console_input->set_text((*history_pos)); } return true; break; case SDLK_DOWN: if (history_pos != history.rbegin()) { --history_pos; console_input->set_text((*history_pos)); } return true; break; case 512 + SDL_BUTTON_WHEELUP: case SDLK_PAGEUP: console_scrollpane->inc_scroll(scroll_offset); return true; break; case 512 + SDL_BUTTON_WHEELDOWN: case SDLK_PAGEDOWN: console_scrollpane->dec_scroll(scroll_offset); return true; break; } return false; } void Console::draw() { if (core::application()->connected()) { set_size(parent()->size().width(), parent()->size().height() * DEFAULT_CONSOLE_HEIGHT); } else { set_size(parent()->size()); } math::Vector2f s(size()); s[0] -= 8; s[1] -= 8; console_scrollpane->set_location(4, 4); console_scrollpane->set_size(s.width(), s.height() - font()->height()); console_input->set_location(4, height() - font()->height() -4); console_input->set_size(s.width(), font()->height()); math::Color fancy(palette()->fancy()); fancy.a = 0.5f; paint::color(fancy); std::string version(core::name() + ' ' + core::version()); s.assign(version.size() * font()->width(), font()->height()); math::Vector2f l(global_location()); l[0] += width() - s.width() -4; l[1] += height() - s.height() -4; paint::text(l, s, font(), version); } void Console::save_history() { if (history.size() <= 1) return; std::string filename(filesystem::writedir()); filename.append("history.txt"); std::ofstream ofs(filename.c_str()); if (!ofs.is_open()) { con_warn << "Could not write " << filename << std::endl; return; } ui::Text::iterator it; size_t l = 1; for (it = history.begin(); it != history.end(); it++) { if (l < history.size()) ofs << (*it) << std::endl; l++; } ofs.close(); } void Console::load_history() { std::string filename(filesystem::writedir()); filename.append("history.txt"); std::ifstream ifs(filename.c_str(), std::ifstream::in); if (!ifs.is_open()) { con_warn << "Could not read " << filename << std::endl; return; } history.clear(); char line[MAXCMDSIZE]; while (ifs.getline(line, MAXCMDSIZE-1)) { history.push_back(line); } ifs.close(); history.push_back(""); history_pos = history.rbegin(); } }