Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 20325d8c01d9077a9f784ee8cda299af53bf93f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
   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");
	set_alignment(AlignBottom);
}

ScrollPane::~ScrollPane()
{
}
	
void ScrollPane::set_alignment(const unsigned int alignment)
{
	scrollpane_alignment = alignment;
}

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());
	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;
						pen = 'N';
						wordpen = 'N';
					// 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++;		
	}

	gl::color(palette()->text());
	const math ::Vector2f gl(global_location());
	float y = 0;
	
	if ((alignment() & AlignTop) == AlignTop) {
		int i = (int) lines.size();
		for (ui::Text::iterator it = lines.begin(); it != lines.end(); it++) {
			if (i <= text_height) {
				render::Text::draw(gl.x(), gl.y() + y, (*it));
				y += font()->height();
			}
			i--;
		}
	} else {
		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();
		}
	}
	gl::disable(GL_TEXTURE_2D);
}

}