/* 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 "client/client.h" #include "client/console.h" #include "core/core.h" #include "render/render.h" #include "sys/sys.h" #include "ui/ui.h" namespace client { 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_input = new ui::Input(this); chat_input->set_border(true); 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()) { (*history_pos).assign(chat_input->text()); // store input into history while (history.size() >= MAXHISTOLINES) { history.pop_front(); } if ((*history_pos).c_str()[0] == '/' || (*history_pos).c_str()[0] == '\\') { core::cmd() << &(*history_pos).c_str()[1] << std::endl; } else { core::cmd() << "say " << (*history_pos) << std::endl; } (*history.rbegin()) = (*history_pos); 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 (!client()->connected()) { hide(); return; } if (ui::root()->active()) return; else Widget::event_draw(); } } // namespace client