From 56cdfd3822d2800abdd2f912ab7f76a5764793a7 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 19 Oct 2008 13:45:07 +0000 Subject: scrollpane widget, updated chatbox --- src/ui/scrollpane.cc | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 src/ui/scrollpane.cc (limited to 'src/ui/scrollpane.cc') diff --git a/src/ui/scrollpane.cc b/src/ui/scrollpane.cc new file mode 100644 index 0000000..69bc159 --- /dev/null +++ b/src/ui/scrollpane.cc @@ -0,0 +1,172 @@ +/* + ui/scrollpane.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include + +#include "ui/scrollpane.h" +#include "render/text.h" +#include "render/gl.h" + +namespace ui +{ + +ScrollPane::ScrollPane(Widget *parent, ui::Text &text) : Widget(parent), scrollpane_text(text) +{ + set_label("scrollpane"); +} + +ScrollPane::~ScrollPane() +{ +} + +void ScrollPane::set_scroll(int scroll) +{ + scrollpane_scroll = scroll; + + if (scrollpane_scroll > (int) scrollpane_text.size()) + scrollpane_scroll = (int) scrollpane_text.size(); + else if (scrollpane_scroll < 0) + scrollpane_scroll = 0; +} + +void ScrollPane::inc_scroll(int scroll) +{ + scrollpane_scroll += scroll; + + if (scrollpane_scroll > (int) scrollpane_text.size()) + scrollpane_scroll = (int) scrollpane_text.size(); +} + +void ScrollPane::dec_scroll(int scroll) +{ + scrollpane_scroll -= scroll; + + if (scrollpane_scroll < 0) + scrollpane_scroll = 0; +} + +void ScrollPane::draw() +{ + render::Text::setfont(font()->name().c_str(), font()->width(), font()->height()); + render::gl::enable(GL_TEXTURE_2D); + + // text size + int text_height = (int) floorf(height() / font()->height()); + int text_width = (int) floorf(width() / font()->width()); + + // validate scroll position + if (scrollpane_scroll > (int) scrollpane_text.size()) + scrollpane_scroll = (int) scrollpane_text.size(); + else if (scrollpane_scroll < 0) + scrollpane_scroll = 0; + + int bottom = (int) scrollpane_text.size() - scrollpane_scroll; + int current_line = 0; + + ui::Text lines; + + for (ui::Text::const_iterator it = scrollpane_text.begin(); it != scrollpane_text.end() && current_line < bottom; it++) { + if (current_line >= bottom - text_height) { + std::string linedata(*it); + linedata += '\n'; + + std::string word; + size_t word_length = 0; + + std::string line; + size_t line_length = 0; + + const char *c = linedata.c_str(); + char pen = 'N'; + char wordpen = 'N'; + + while (*c) { + + // color code + if (aux::is_color_code(c)) { + c++; + pen = *c; + word += '^'; + word += pen; + } + + // new word, wrap if necessary + else if ((*c == '\n' ) || ( *c == ' ')) { + + if (line_length + word_length > (size_t) text_width) { + if (line.size()) { + lines.push_back(line); + line.clear(); + line += '^'; + line += wordpen; + line_length = 0; + } + } + + line.append(word); + line_length += word_length; + + word.clear(); + word_length = 0; + wordpen = pen; + + // new line + if (*c == '\n' ) { + lines.push_back(line); + line.clear(); + line_length = 0; + // new word + } else if (*c == ' ' ) { + line += ' '; + line_length++; + } + } + + // new character + else { + word += *c; + word_length++; + + if (word_length == (size_t) text_width) { + if (line.size()) { + lines.push_back(line); + line.clear(); + line += '^'; + line += wordpen; + line_length = 0; + } + + line.append(word); + line_length = word_length; + + word.clear(); + word_length = 0; + wordpen = pen; + } + } + + c++; + } + + } + current_line++; + } + + render::gl::color(palette()->text()); + const math ::Vector2f gl(global_location()); + float y = height() - font()->height(); + + for (ui::Text::reverse_iterator rit = lines.rbegin(); (y >= 0) && (rit != lines.rend()); ++rit) { + render::Text::draw(gl.x, gl.y + y, (*rit)); + y -= font()->height(); + } + + render::gl::disable(GL_TEXTURE_2D); +} + +} + + -- cgit v1.2.3