From d79c0223315beaf55fcd10d6891675c4d57b5e2b Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 21 Oct 2008 19:00:39 +0000 Subject: moved client console into libui --- src/ui/Makefile.am | 9 +- src/ui/console.cc | 251 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ui/console.h | 73 ++++++++++++++++ src/ui/ui.cc | 18 ++++ src/ui/ui.h | 9 ++ 5 files changed, 356 insertions(+), 4 deletions(-) create mode 100644 src/ui/console.cc create mode 100644 src/ui/console.h (limited to 'src/ui') diff --git a/src/ui/Makefile.am b/src/ui/Makefile.am index a3084af..b2126fd 100644 --- a/src/ui/Makefile.am +++ b/src/ui/Makefile.am @@ -7,9 +7,10 @@ else noinst_LTLIBRARIES = libui.la endif -noinst_HEADERS = bitmap.h button.h container.h definitions.h font.h inputbox.h label.h \ +noinst_HEADERS = bitmap.h button.h console.h container.h definitions.h font.h inputbox.h label.h \ menu.h paint.h palette.h scrollpane.h ui.h widget.h window.h -libui_la_SOURCES = bitmap.cc button.cc container.cc font.cc inputbox.cc \ - label.cc menu.cc paint.cc palette.cc scrollpane.cc ui.cc widget.cc window.cc -libui_la_LDFLAGS = -avoid-version -no-undefined \ No newline at end of file +libui_la_SOURCES = bitmap.cc button.cc console.cc console.h container.cc \ + font.cc inputbox.cc label.cc menu.cc paint.cc palette.cc scrollpane.cc ui.cc \ + widget.cc window.cc +libui_la_LDFLAGS = -avoid-version -no-undefined diff --git a/src/ui/console.cc b/src/ui/console.cc new file mode 100644 index 0000000..759d67d --- /dev/null +++ b/src/ui/console.cc @@ -0,0 +1,251 @@ +/* + 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/core.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 SDLK_PAGEUP: + console_scrollpane->inc_scroll(scroll_offset); + return true; + break; + + 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.x -= 8; + s.y -= 8; + + console_scrollpane->set_location(4, 4); + console_scrollpane->set_size(s.x, s.y - font()->height()); + + console_input->set_location(4, height() - font()->height() -4); + console_input->set_size(s.x, font()->height()); + + std::string version(core::name()); + version += ' '; + version.append(core::version()); + + render::gl::color(0.0f, 1.0f, 0.0f, 0.5f); + + s.assign(version.size() * font()->width(), font()->height()); + math::Vector2f l(global_location()); + l.x += width() - s.width() -4; + l.y += height() - s.height() -4; + ui::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(); +} + +} + diff --git a/src/ui/console.h b/src/ui/console.h new file mode 100644 index 0000000..2cec72e --- /dev/null +++ b/src/ui/console.h @@ -0,0 +1,73 @@ +/* + ui/console.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_UI_CONSOLE_H__ +#define __INCLUDED_UI_CONSOLE_H__ + +#include "sys/consoleinterface.h" +#include "ui/inputbox.h" +#include "ui/scrollpane.h" +#include "ui/window.h" + +namespace ui { + +/* -- class ConsoleBuffer ------------------------------------------ */ + +/// client console buffer +/** stores incoming console messages + */ +class ConsoleBuffer : public sys::ConsoleInterface { +public: + ConsoleBuffer(); + virtual ~ConsoleBuffer(); +}; + +/* -- class Console ------------------------------------------------ */ + +/// client system console widget +class Console : public Window { +public: + Console(Widget *parent); + virtual ~Console(); + + /// load input history + void load_history(); + + /// save input history + void save_history(); + + /// show console + virtual void show(); + + /// hide console + virtual void hide(); + + void toggle(); + +protected: + + /// draw the client console + virtual void draw(); + + /// handle keypress events + virtual bool on_keypress(const int key, const unsigned int modifier); + +private: + // input history + Text history; + Text::reverse_iterator history_pos; + + // console widget + InputBox *console_input; + ScrollPane *console_scrollpane; + + // console buffer + static ConsoleBuffer con_buffer; +}; + +} + +#endif // __INCLUDED_UI_CONSOLE_H__ diff --git a/src/ui/ui.cc b/src/ui/ui.cc index bacc753..c5fdfac 100644 --- a/src/ui/ui.cc +++ b/src/ui/ui.cc @@ -43,6 +43,12 @@ void func_ui_restart(std::string const &args) } } + +void func_ui_console(std::string const &args) +{ + console()->toggle(); +} + void func_list_menu(std::string const &args) { if (global_ui) { @@ -171,6 +177,9 @@ void init() func = core::Func::add("ui_restart", func_ui_restart); func->set_info("[command] [options] reload user interface files"); + + func = core::Func::add("ui_console", func_ui_console); + func->set_info("toggle console on or off"); func = core::Func::add("menu", func_menu); func->set_info("[command] menu functions"); @@ -183,6 +192,7 @@ void shutdown() core::Func::remove("list_ui"); core::Func::remove("list_menu"); core::Func::remove("menu"); + core::Func::remove("ui_console"); core::Func::remove("ui"); if (global_ui) { @@ -200,6 +210,9 @@ UI::UI() : Window(0) set_border(false); set_background(false); + // intialize console + ui_console = new Console(this); + // default palette ui_palette = new Palette(); set_palette(ui_palette); @@ -404,6 +417,11 @@ void UI::show_menu(const char *label) ui_active_menu->show(); set_pointer("pointer"); + + // raise console if it is visible + if (ui_console->visible()) + ui_console->show(); + } else { con_warn << "Unknown window '" << label << "'" << std::endl; } diff --git a/src/ui/ui.h b/src/ui/ui.h index 97577c6..f41df24 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -7,6 +7,7 @@ #ifndef __INCLUDED_UI_H__ #define __INCLUDED_UI_H__ +#include "ui/console.h" #include "ui/font.h" #include "ui/palette.h" #include "ui/widget.h" @@ -41,6 +42,9 @@ public: /// show previous window void previous_menu(); + + /// the console + inline Console *console() { return ui_console; } /// return the active menu Window *active() { @@ -111,6 +115,8 @@ private: Window *ui_active_menu; Widget *ui_mouse_focus; Widget *ui_input_focus; + + Console *ui_console; Menus ui_menus; @@ -130,6 +136,9 @@ void shutdown(); /// the global root window UI *root(); +/// the console +inline Console *console() { return root()->console(); } + /// debug mode inline bool debug() { return UI::ui_debug; } -- cgit v1.2.3