diff options
Diffstat (limited to 'src/client/chat.cc')
-rw-r--r-- | src/client/chat.cc | 202 |
1 files changed, 72 insertions, 130 deletions
diff --git a/src/client/chat.cc b/src/client/chat.cc index 6f63a6d..bdeebe2 100644 --- a/src/client/chat.cc +++ b/src/client/chat.cc @@ -5,129 +5,86 @@ */ #include "auxiliary/functions.h" -#include "core/core.h" #include "client/chat.h" +#include "client/client.h" #include "client/console.h" -#include "client/keyboard.h" -#include "client/video.h" +#include "core/core.h" #include "render/render.h" +#include "sys/sys.h" +#include "ui/ui.h" namespace client { -namespace chat { - -// input history -std::deque<std::string> history; -std::deque<std::string>::reverse_iterator history_pos; -size_t input_pos = 0; - -// chatbox visibility -bool chat_visible = false; - -//--- public ------------------------------------------------------ - -void init() +Chat::Chat(ui::Widget *parent) : ui::Widget(parent) { - // add engine functions + set_label("chat"); history.clear(); history.push_back(""); history_pos = history.rbegin(); - input_pos = 0; + + 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); } -void shutdown() +Chat::~Chat() { history.clear(); - input_pos = 0; } -bool visible() -{ - return chat_visible; -} -void draw() +void Chat::show() { - using namespace render; - - if (console()->visible() || !visible()) - return; + Widget::show(); - size_t width = (size_t) (video::width / Text::fontwidth()) - 8; - float y = video::height*8/10; - Text::draw(4, y, "^BSay^F:^B "); + raise(); + set_focus(); - std::string firstpart((*history_pos).substr(0, input_pos)); - size_t draw_width = 0; - const char *c = firstpart.c_str(); - - while (*c) { - if (aux::is_color_code(c)) { - c++; - } else { - draw_width++; - } - c++; - } - - c = firstpart.c_str(); - while (*c && draw_width > width - 2) { - if (aux::is_color_code(c)) { - c++; - Text::setcolor(*c); - } else { - draw_width--; - } - c++; - } - - if (*c) { - Text::draw(4+5*Text::fontwidth(), y, c); - } - - if (input_pos < (*history_pos).size()) { - // FIXME limit to width - if (input_pos > 1 && aux::is_color_code((*history_pos).c_str() + input_pos -1)) { - Text::setcolor((*history_pos)[input_pos]); - } - c = (*history_pos).c_str() + input_pos; - Text::draw(4+Text::fontwidth()*(draw_width+5), y, c); - } + history_pos = history.rbegin(); + (*history_pos).clear(); + chat_input->set_text((*history_pos)); +} - // draw cursor - if ((core::application()->time() - ::floorf(core::application()->time())) < 0.5f) { - std::string cursor("^B"); - cursor += (char) 11; - Text::draw(4+Text::fontwidth()*(draw_width+5), y , cursor); - } +void Chat::toggle() +{ + if (visible()) + hide(); + else + show(); } -void toggle() +void Chat::resize() { - chat_visible = !chat_visible; - if (chat_visible) { - input_pos = 0; - history_pos = history.rbegin(); - (*history_pos).clear(); - } + chat_label->set_location(font()->width(), height() / 5.0f); + chat_label->set_size(width() - font()->width() * 2, height() / 5.0f); - setkeyboardmode(console()->visible() || (core::application()->connected() && chat::visible())); + chat_input->set_location(font()->width(), height() / 5.0f * 3.0f); + chat_input->set_size(width() - font()->width() * 2, height() / 5.0f); } -void keypressed(unsigned int key) +bool Chat::on_keypress(const int key, const unsigned int modifier) { - std::deque<std::string>::reverse_iterator upit; + History::reverse_iterator upit; switch( key ) { case SDLK_ESCAPE: - toggle(); - break; + if (visible()) { + hide(); + return true; + } else { + return false; + } + case SDLK_RETURN: + if (chat_input->text().size()) { + (*history_pos).assign(chat_input->text()); - case SDLK_TAB: - core::CommandBuffer::complete( (*history_pos), input_pos); - break; - case SDLK_RETURN: - if ((*history_pos).size()) { // store input into history while (history.size() >= MAXHISTOLINES) { history.pop_front(); @@ -142,61 +99,46 @@ void keypressed(unsigned int key) history.push_back(""); history_pos = history.rbegin(); - input_pos = 0; + chat_input->set_text((*history_pos)); } - toggle(); + + hide(); + return true; break; + case SDLK_UP: upit = history_pos; ++upit; if (upit != history.rend()) { history_pos = upit; - input_pos = (*history_pos).size(); + chat_input->set_text((*history_pos)); } + return true; break; + case SDLK_DOWN: if (history_pos != history.rbegin()) { --history_pos; - input_pos = (*history_pos).size(); - } - break; - case SDLK_HOME: - input_pos = 0; - break; - case SDLK_END: - input_pos = (*history_pos).size(); - break; - case SDLK_LEFT: - if (input_pos > 0) - input_pos--; - break; - case SDLK_RIGHT: - if (input_pos < (*history_pos).size()) - input_pos++; - break; - case SDLK_DELETE: - if ((*history_pos).size() && input_pos < (*history_pos).size()) { - (*history_pos).erase(input_pos, 1); - } - break; - case SDLK_BACKSPACE: - if ((*history_pos).size() && input_pos) { - (*history_pos).erase(input_pos-1, 1); - input_pos--; - } - break; - default: - if ((key >= 32 ) && (key <175)) { - if (input_pos == (*history_pos).size()) - (*history_pos) += (char)key; - else - (*history_pos).insert(input_pos, 1, (char)key); - input_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 |