Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2008-10-19 13:45:07 +0000
committerStijn Buys <ingar@osirion.org>2008-10-19 13:45:07 +0000
commit56cdfd3822d2800abdd2f912ab7f76a5764793a7 (patch)
tree2656c7ef694117e0554ae4a47bb09629c78ed8af /src/ui/scrollpane.cc
parente6f1fad441a7737549f463ebac1c9de062b5173d (diff)
scrollpane widget, updated chatbox
Diffstat (limited to 'src/ui/scrollpane.cc')
-rw-r--r--src/ui/scrollpane.cc172
1 files changed, 172 insertions, 0 deletions
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 <cmath>
+
+#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);
+}
+
+}
+
+