incrementaltp: respect physics overrides
[waspsaliva.git] / src / util / enriched_string.cpp
blob762d094eba0d6b8f1c20f50887f00e3c78dd84c3
1 /*
2 Copyright (C) 2013 xyz, Ilya Zhuravlev <whatever@xyz.is>
3 Copyright (C) 2016 Nore, Nathanaƫl Courant <nore@mesecons.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "enriched_string.h"
21 #include "util/string.h"
22 #include "debug.h"
23 #include "log.h"
25 using namespace irr::video;
27 EnrichedString::EnrichedString()
29 clear();
32 EnrichedString::EnrichedString(const std::wstring &string,
33 const std::vector<SColor> &colors)
35 clear();
36 m_string = string;
37 m_colors = colors;
40 EnrichedString::EnrichedString(const std::wstring &s, const SColor &color)
42 clear();
43 addAtEnd(translate_string(s), color);
46 EnrichedString::EnrichedString(const wchar_t *str, const SColor &color)
48 clear();
49 addAtEnd(translate_string(std::wstring(str)), color);
52 void EnrichedString::clear()
54 m_string.clear();
55 m_colors.clear();
56 m_has_background = false;
57 m_default_length = 0;
58 m_default_color = irr::video::SColor(255, 255, 255, 255);
59 m_background = irr::video::SColor(0, 0, 0, 0);
62 void EnrichedString::operator=(const wchar_t *str)
64 clear();
65 addAtEnd(translate_string(std::wstring(str)), m_default_color);
68 void EnrichedString::addAtEnd(const std::wstring &s, const SColor &initial_color)
70 SColor color(initial_color);
71 bool use_default = (m_default_length == m_string.size() &&
72 color == m_default_color);
74 size_t i = 0;
75 while (i < s.length()) {
76 if (s[i] != L'\x1b') {
77 m_string += s[i];
78 m_colors.push_back(color);
79 ++i;
80 continue;
82 ++i;
83 size_t start_index = i;
84 size_t length;
85 if (i == s.length()) {
86 break;
88 if (s[i] == L'(') {
89 ++i;
90 ++start_index;
91 while (i < s.length() && s[i] != L')') {
92 if (s[i] == L'\\') {
93 ++i;
95 ++i;
97 length = i - start_index;
98 ++i;
99 } else {
100 ++i;
101 length = 1;
103 std::wstring escape_sequence(s, start_index, length);
104 std::vector<std::wstring> parts = split(escape_sequence, L'@');
105 if (parts[0] == L"c") {
106 if (parts.size() < 2) {
107 continue;
109 parseColorString(wide_to_utf8(parts[1]), color, true);
111 // No longer use default color after first escape
112 if (use_default) {
113 m_default_length = m_string.size();
114 use_default = false;
116 } else if (parts[0] == L"b") {
117 if (parts.size() < 2) {
118 continue;
120 parseColorString(wide_to_utf8(parts[1]), m_background, true);
121 m_has_background = true;
125 // Update if no escape character was found
126 if (use_default)
127 m_default_length = m_string.size();
130 void EnrichedString::addChar(const EnrichedString &source, size_t i)
132 m_string += source.m_string[i];
133 m_colors.push_back(source.m_colors[i]);
136 void EnrichedString::addCharNoColor(wchar_t c)
138 m_string += c;
139 if (m_colors.empty()) {
140 m_colors.emplace_back(m_default_color);
141 } else {
142 m_colors.push_back(m_colors[m_colors.size() - 1]);
146 EnrichedString EnrichedString::operator+(const EnrichedString &other) const
148 EnrichedString result = *this;
149 result += other;
150 return result;
153 void EnrichedString::operator+=(const EnrichedString &other)
155 bool update_default_color = m_default_length == m_string.size();
157 m_string += other.m_string;
158 m_colors.insert(m_colors.end(), other.m_colors.begin(), other.m_colors.end());
160 if (update_default_color) {
161 m_default_length += other.m_default_length;
162 updateDefaultColor();
166 EnrichedString EnrichedString::substr(size_t pos, size_t len) const
168 if (pos >= m_string.length())
169 return EnrichedString();
171 if (len == std::string::npos || pos + len > m_string.length())
172 len = m_string.length() - pos;
174 EnrichedString str(
175 m_string.substr(pos, len),
176 std::vector<SColor>(m_colors.begin() + pos, m_colors.begin() + pos + len)
179 str.m_has_background = m_has_background;
180 str.m_background = m_background;
182 if (pos < m_default_length)
183 str.m_default_length = std::min(m_default_length - pos, str.size());
184 str.setDefaultColor(m_default_color);
185 return str;
188 const wchar_t *EnrichedString::c_str() const
190 return m_string.c_str();
193 const std::vector<SColor> &EnrichedString::getColors() const
195 return m_colors;
198 const std::wstring &EnrichedString::getString() const
200 return m_string;
203 void EnrichedString::setDefaultColor(const irr::video::SColor &color)
205 m_default_color = color;
206 updateDefaultColor();
209 void EnrichedString::updateDefaultColor()
211 sanity_check(m_default_length <= m_colors.size());
213 for (size_t i = 0; i < m_default_length; ++i)
214 m_colors[i] = m_default_color;