]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/util/enriched_string.cpp
Change typedef to normal definitions in GUI code
[dragonfireclient.git] / src / util / enriched_string.cpp
index 05d7b8c25a6356c4cc1c4c3d31f9875d364f8913..762d094eba0d6b8f1c20f50887f00e3c78dd84c3 100644 (file)
@@ -19,7 +19,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "enriched_string.h"
 #include "util/string.h"
+#include "debug.h"
 #include "log.h"
+
 using namespace irr::video;
 
 EnrichedString::EnrichedString()
@@ -28,32 +30,47 @@ EnrichedString::EnrichedString()
 }
 
 EnrichedString::EnrichedString(const std::wstring &string,
-               const std::vector<SColor> &colors):
-       m_string(string),
-       m_colors(colors)
-{}
+               const std::vector<SColor> &colors)
+{
+       clear();
+       m_string = string;
+       m_colors = colors;
+}
 
 EnrichedString::EnrichedString(const std::wstring &s, const SColor &color)
 {
        clear();
-       addAtEnd(s, color);
+       addAtEnd(translate_string(s), color);
 }
 
 EnrichedString::EnrichedString(const wchar_t *str, const SColor &color)
 {
        clear();
-       addAtEnd(std::wstring(str), color);
+       addAtEnd(translate_string(std::wstring(str)), color);
+}
+
+void EnrichedString::clear()
+{
+       m_string.clear();
+       m_colors.clear();
+       m_has_background = false;
+       m_default_length = 0;
+       m_default_color = irr::video::SColor(255, 255, 255, 255);
+       m_background = irr::video::SColor(0, 0, 0, 0);
 }
 
 void EnrichedString::operator=(const wchar_t *str)
 {
        clear();
-       addAtEnd(std::wstring(str), SColor(255, 255, 255, 255));
+       addAtEnd(translate_string(std::wstring(str)), m_default_color);
 }
 
 void EnrichedString::addAtEnd(const std::wstring &s, const SColor &initial_color)
 {
        SColor color(initial_color);
+       bool use_default = (m_default_length == m_string.size() &&
+               color == m_default_color);
+
        size_t i = 0;
        while (i < s.length()) {
                if (s[i] != L'\x1b') {
@@ -90,6 +107,12 @@ void EnrichedString::addAtEnd(const std::wstring &s, const SColor &initial_color
                                continue;
                        }
                        parseColorString(wide_to_utf8(parts[1]), color, true);
+
+                       // No longer use default color after first escape
+                       if (use_default) {
+                               m_default_length = m_string.size();
+                               use_default = false;
+                       }
                } else if (parts[0] == L"b") {
                        if (parts.size() < 2) {
                                continue;
@@ -97,8 +120,11 @@ void EnrichedString::addAtEnd(const std::wstring &s, const SColor &initial_color
                        parseColorString(wide_to_utf8(parts[1]), m_background, true);
                        m_has_background = true;
                }
-               continue;
        }
+
+       // Update if no escape character was found
+       if (use_default)
+               m_default_length = m_string.size();
 }
 
 void EnrichedString::addChar(const EnrichedString &source, size_t i)
@@ -111,7 +137,7 @@ void EnrichedString::addCharNoColor(wchar_t c)
 {
        m_string += c;
        if (m_colors.empty()) {
-               m_colors.push_back(SColor(255, 255, 255, 255));
+               m_colors.emplace_back(m_default_color);
        } else {
                m_colors.push_back(m_colors[m_colors.size() - 1]);
        }
@@ -119,34 +145,44 @@ void EnrichedString::addCharNoColor(wchar_t c)
 
 EnrichedString EnrichedString::operator+(const EnrichedString &other) const
 {
-       std::vector<SColor> result;
-       result.insert(result.end(), m_colors.begin(), m_colors.end());
-       result.insert(result.end(), other.m_colors.begin(), other.m_colors.end());
-       return EnrichedString(m_string + other.m_string, result);
+       EnrichedString result = *this;
+       result += other;
+       return result;
 }
 
 void EnrichedString::operator+=(const EnrichedString &other)
 {
+       bool update_default_color = m_default_length == m_string.size();
+
        m_string += other.m_string;
        m_colors.insert(m_colors.end(), other.m_colors.begin(), other.m_colors.end());
+
+       if (update_default_color) {
+               m_default_length += other.m_default_length;
+               updateDefaultColor();
+       }
 }
 
 EnrichedString EnrichedString::substr(size_t pos, size_t len) const
 {
-       if (pos == m_string.length()) {
+       if (pos >= m_string.length())
                return EnrichedString();
-       }
-       if (len == std::string::npos || pos + len > m_string.length()) {
-               return EnrichedString(
-                          m_string.substr(pos, std::string::npos),
-                          std::vector<SColor>(m_colors.begin() + pos, m_colors.end())
-                      );
-       } else {
-               return EnrichedString(
-                          m_string.substr(pos, len),
-                          std::vector<SColor>(m_colors.begin() + pos, m_colors.begin() + pos + len)
-                      );
-       }
+
+       if (len == std::string::npos || pos + len > m_string.length())
+               len = m_string.length() - pos;
+
+       EnrichedString str(
+               m_string.substr(pos, len),
+               std::vector<SColor>(m_colors.begin() + pos, m_colors.begin() + pos + len)
+       );
+
+       str.m_has_background = m_has_background;
+       str.m_background = m_background;
+
+       if (pos < m_default_length)
+               str.m_default_length = std::min(m_default_length - pos, str.size());
+       str.setDefaultColor(m_default_color);
+       return str;
 }
 
 const wchar_t *EnrichedString::c_str() const
@@ -163,3 +199,17 @@ const std::wstring &EnrichedString::getString() const
 {
        return m_string;
 }
+
+void EnrichedString::setDefaultColor(const irr::video::SColor &color)
+{
+       m_default_color = color;
+       updateDefaultColor();
+}
+
+void EnrichedString::updateDefaultColor()
+{
+       sanity_check(m_default_length <= m_colors.size());
+
+       for (size_t i = 0; i < m_default_length; ++i)
+               m_colors[i] = m_default_color;
+}