1// SuperTux
2// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#include "object/text_object.hpp"
18
19#include "supertux/globals.hpp"
20#include "supertux/resources.hpp"
21#include "video/drawing_context.hpp"
22
23TextObject::TextObject(const std::string& name) :
24 GameObject(name),
25 ExposedObject<TextObject, scripting::Text>(this),
26 m_font(Resources::normal_font),
27 m_text(),
28 m_wrapped_text(),
29 m_fading(0),
30 m_fadetime(0),
31 m_visible(false),
32 m_centered(false),
33 m_anchor(ANCHOR_MIDDLE),
34 m_pos(0, 0)
35{
36}
37
38TextObject::~TextObject()
39{
40}
41
42void
43TextObject::set_font(const std::string& name_)
44{
45 if (name_ == "normal") {
46 m_font = Resources::normal_font;
47 } else if (name_ == "big") {
48 m_font = Resources::big_font;
49 } else if (name_ == "small") {
50 m_font = Resources::small_font;
51 } else {
52 log_warning << "Unknown font '" << name_ << "'." << std::endl;
53 m_font = Resources::normal_font;
54 }
55
56 wrap_text();
57}
58
59void
60TextObject::wrap_text()
61{
62 std::string rest;
63
64 // strip all newlines except double ones (markdown'ish)
65 char prev_c = ' ';
66 for(char& c : m_text) {
67 if (c == '\n') {
68 if (prev_c == '\n') {
69 rest += '\n';
70 } else {
71 rest += ' ';
72 }
73 } else {
74 rest += c;
75 }
76 }
77
78 m_wrapped_text.clear();
79
80 do {
81 std::string overflow;
82 m_wrapped_text += m_font->wrap_to_width(rest, 500, &overflow);
83 if (!overflow.empty()) {
84 m_wrapped_text += "\n";
85 }
86 rest = overflow;
87 } while (!rest.empty());
88}
89
90void
91TextObject::set_text(const std::string& text_)
92{
93 m_text = text_;
94 wrap_text();
95}
96
97void
98TextObject::fade_in(float fadetime_)
99{
100 m_fadetime = fadetime_;
101 m_fading = fadetime_;
102}
103
104void
105TextObject::fade_out(float fadetime_)
106{
107 m_fadetime = fadetime_;
108 m_fading = -fadetime_;
109}
110
111void
112TextObject::set_visible(bool visible_)
113{
114 m_visible = visible_;
115 m_fading = 0;
116}
117
118void
119TextObject::set_centered(bool centered_)
120{
121 m_centered = centered_;
122}
123
124void
125TextObject::draw(DrawingContext& context)
126{
127 context.push_transform();
128 context.set_translation(Vector(0, 0));
129 if (m_fading > 0) {
130 context.set_alpha((m_fadetime - m_fading) / m_fadetime);
131 } else if (m_fading < 0) {
132 context.set_alpha(-m_fading / m_fadetime);
133 } else if (!m_visible) {
134 context.pop_transform();
135 return;
136 }
137
138 float width = m_font->get_text_width(m_wrapped_text) + 20.0f;
139 float height = m_font->get_text_height(m_wrapped_text) + 20.0f;
140 Vector spos = m_pos + get_anchor_pos(Rectf(0, 0, static_cast<float>(context.get_width()), static_cast<float>(context.get_height())),
141 width, height, m_anchor);
142
143 context.color().draw_filled_rect(Rectf(spos, Sizef(width, height)),
144 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-50);
145 if (m_centered) {
146 context.color().draw_center_text(m_font, m_wrapped_text, spos, LAYER_GUI-40, TextObject::default_color);
147 } else {
148 context.color().draw_text(m_font, m_wrapped_text, spos + Vector(10, 10), ALIGN_LEFT, LAYER_GUI-40, TextObject::default_color);
149 }
150
151 context.pop_transform();
152}
153
154void
155TextObject::update(float dt_sec)
156{
157 if (m_fading > 0) {
158 m_fading -= dt_sec;
159 if (m_fading <= 0) {
160 m_fading = 0;
161 m_visible = true;
162 }
163 } else if (m_fading < 0) {
164 m_fading += dt_sec;
165 if (m_fading >= 0) {
166 m_fading = 0;
167 m_visible = false;
168 }
169 }
170}
171
172/* EOF */
173