/* client/chat.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 "auxiliary/functions.h" #include "client/chat.h" #include "core/core.h" #include "sys/sys.h" #include "ui/ui.h" namespace client { const size_t DEFAULT_MAX_HISTO_LINES = 512; Chat::Chat(ui::Widget *parent) : ui::Window(parent) { set_label("chat"); history.clear(); history.push_back(""); history_pos = history.rbegin(); chat_label = new ui::Label(this, "^BSay^F:^B"); chat_label->set_alignment(ui::AlignLeft | ui::AlignVCenter); chat_label->set_border(false); chat_input = new ui::InputBox(this); chat_input->set_border(false); chat_input->set_focus(); set_background(true); set_visible(false); } Chat::~Chat() { history.clear(); } void Chat::show() { Window::show(); history_pos = history.rbegin(); (*history_pos).clear(); chat_input->set_text((*history_pos)); } void Chat::toggle() { if (visible()) hide(); else show(); } void Chat::resize() { chat_label->set_location(font()->width(), height() / 5.0f); chat_label->set_size(width() - font()->width() * 2, height() / 5.0f); chat_input->set_location(font()->width(), height() / 5.0f * 3.0f); chat_input->set_size(width() - font()->width() * 2, height() / 5.0f); } bool Chat::on_keypress(const int key, const unsigned int modifier) { History::reverse_iterator upit; switch( key ) { case SDLK_ESCAPE: if (visible()) { hide(); return true; } else { return false; } case SDLK_RETURN: if (chat_input->text().size()) { // store input into history while (history.size() >= DEFAULT_MAX_HISTO_LINES) { history.pop_front(); } if (chat_input->text().c_str()[0] == '/' || chat_input->text().c_str()[0] == '\\') { core::cmd() << &chat_input->text().c_str()[1] << std::endl; } else { core::cmd() << "say " << chat_input->text() << std::endl; } (*history.rbegin()) = chat_input->text(); history.push_back(""); history_pos = history.rbegin(); chat_input->set_text((*history_pos)); } hide(); return true; break; case SDLK_UP: upit = history_pos; ++upit; if (upit != history.rend()) { history_pos = upit; chat_input->set_text((*history_pos)); } return true; break; case SDLK_DOWN: if (history_pos != history.rbegin()) { --history_pos; chat_input->set_text((*history_pos)); } return true; break; } return false; } void Chat::event_draw() { if (!core::application()->connected()) { hide(); return; } if (ui::root()->active()) return; else Widget::event_draw(); } } // namespace client