1 #include "main.h"
2 #include "gui2_scrolltext.h"
3
GuiScrollText(GuiContainer * owner,string id,string text)4 GuiScrollText::GuiScrollText(GuiContainer* owner, string id, string text)
5 : GuiElement(owner, id), text(text), text_size(30)
6 {
7 auto_scroll_down = false;
8 scrollbar = new GuiScrollbar(this, id + "_SCROLL", 0, 1, 0, nullptr);
9 scrollbar->setPosition(0, 0, ATopRight)->setSize(50, GuiElement::GuiSizeMax);
10 }
11
setText(string text)12 GuiScrollText* GuiScrollText::setText(string text)
13 {
14 this->text = text;
15 return this;
16 }
17
getText() const18 string GuiScrollText::getText() const
19 {
20 return text;
21 }
22
setScrollbarWidth(float width)23 GuiScrollText* GuiScrollText::setScrollbarWidth(float width)
24 {
25 scrollbar->setSize(width, GuiElement::GuiSizeMax);
26 return this;
27 }
28
onDraw(sf::RenderTarget & window)29 void GuiScrollText::onDraw(sf::RenderTarget& window)
30 {
31 LineWrapResult wrap = doLineWrap(this->text, text_size, rect.width - scrollbar->getSize().x);
32
33 int start_pos = 0;
34 for(int n=0; n<scrollbar->getValue(); n++)
35 {
36 int next = wrap.text.find("\n", start_pos) + 1;
37 if (next > 0)
38 start_pos = next;
39 }
40 if (start_pos > 0)
41 wrap.text = wrap.text.substr(start_pos);
42 int max_lines = rect.height / main_font->getLineSpacing(text_size);
43 if (wrap.line_count - scrollbar->getValue() > max_lines)
44 {
45 int end_pos = 0;
46 for(int n=0; n<max_lines; n++)
47 {
48 int next = wrap.text.find("\n", end_pos) + 1;
49 if (next > 0)
50 end_pos = next;
51 }
52 if (end_pos > 0)
53 wrap.text = wrap.text.substr(0, end_pos);
54 }
55
56 if (scrollbar->getMax() != wrap.line_count)
57 {
58 int diff = wrap.line_count - scrollbar->getMax();
59 scrollbar->setRange(0, wrap.line_count);
60 scrollbar->setValueSize(max_lines);
61 if (auto_scroll_down)
62 scrollbar->setValue(scrollbar->getValue() + diff);
63 }
64
65 if (max_lines >= wrap.line_count)
66 {
67 scrollbar->hide();
68 }
69 else
70 {
71 scrollbar->show();
72 }
73
74 drawText(window, sf::FloatRect(rect.left, rect.top, rect.width - scrollbar->getSize().x, rect.height), wrap.text, ATopLeft, text_size, main_font, selectColor(colorConfig.textbox.forground));
75 }
76