Luanti 5.17.0-dev
Loading...
Searching...
No Matches
enriched_string.h
Go to the documentation of this file.
1/*
2Copyright (C) 2013 xyz, Ilya Zhuravlev <whatever@xyz.is>
3Copyright (C) 2016 Nore, Nathanaƫlle Courant <nore@mesecons.net>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, see <https://www.gnu.org/licenses/>.
17*/
18
19#pragma once
20
21#include <string>
22#include <string_view>
23#include <vector>
24#include <SColor.h>
25
26
28public:
30 EnrichedString(std::wstring_view s,
31 const video::SColor &color = video::SColor(255, 255, 255, 255));
32 EnrichedString(std::wstring_view string,
33 const std::vector<video::SColor> &colors);
34 EnrichedString &operator=(std::wstring_view s);
35
36 void clear();
37
38 void addAtEnd(std::wstring_view s, video::SColor color);
39
40 // Adds the character source[i] at the end.
41 // An EnrichedString should always be able to be copied
42 // to the end of an existing EnrichedString that way.
43 void addChar(const EnrichedString &source, size_t i);
44
45 // Adds a single character at the end, without specifying its
46 // color. The color used will be the one from the last character.
47 void addCharNoColor(wchar_t c);
48
49 EnrichedString getNextLine(size_t *pos) const;
50 EnrichedString substr(size_t pos = 0, size_t len = std::string::npos) const;
51
52 EnrichedString operator+(const EnrichedString &other) const;
53 void operator+=(const EnrichedString &other);
54 void operator+=(std::wstring_view other)
55 {
56 *this += EnrichedString(other);
57 }
58
59 const wchar_t *c_str() const
60 {
61 return getString().c_str();
62 }
63 const std::vector<video::SColor> &getColors() const;
64 const std::wstring &getString() const;
65
66 inline void setDefaultColor(video::SColor color)
67 {
68 m_default_color = color;
70 }
71 void updateDefaultColor();
72 inline const video::SColor &getDefaultColor() const
73 {
74 return m_default_color;
75 }
76
77 inline bool operator==(const EnrichedString &other) const
78 {
79 return (m_string == other.m_string && m_colors == other.m_colors);
80 }
81 inline bool operator!=(const EnrichedString &other) const
82 {
83 return !(*this == other);
84 }
85 inline bool empty() const
86 {
87 return m_string.empty();
88 }
89 inline size_t size() const
90 {
91 return m_string.size();
92 }
93
94 inline bool hasBackground() const
95 {
96 return m_has_background;
97 }
98 inline video::SColor getBackground() const
99 {
100 return m_background;
101 }
102 inline void setBackground(video::SColor color)
103 {
104 m_background = color;
105 m_has_background = true;
106 }
107
108private:
109 std::wstring m_string;
110 std::vector<video::SColor> m_colors;
112 video::SColor m_default_color;
113 video::SColor m_background;
114 // This variable defines the length of the default-colored text.
116};
video::SColor getBackground() const
Definition enriched_string.h:98
EnrichedString getNextLine(size_t *pos) const
Definition enriched_string.cpp:162
void addAtEnd(std::wstring_view s, video::SColor color)
Definition enriched_string.cpp:61
void setBackground(video::SColor color)
Definition enriched_string.h:102
bool empty() const
Definition enriched_string.h:85
void addChar(const EnrichedString &source, size_t i)
Definition enriched_string.cpp:126
EnrichedString(std::wstring_view string, const std::vector< video::SColor > &colors)
void clear()
Definition enriched_string.cpp:44
bool operator!=(const EnrichedString &other) const
Definition enriched_string.h:81
const std::vector< video::SColor > & getColors() const
Definition enriched_string.cpp:199
bool operator==(const EnrichedString &other) const
Definition enriched_string.h:77
bool hasBackground() const
Definition enriched_string.h:94
const std::wstring & getString() const
Definition enriched_string.cpp:204
void operator+=(std::wstring_view other)
Definition enriched_string.h:54
std::wstring m_string
Definition enriched_string.h:109
const wchar_t * c_str() const
Definition enriched_string.h:59
video::SColor m_default_color
Definition enriched_string.h:112
std::vector< video::SColor > m_colors
Definition enriched_string.h:110
void operator+=(const EnrichedString &other)
Definition enriched_string.cpp:149
const video::SColor & getDefaultColor() const
Definition enriched_string.h:72
void addCharNoColor(wchar_t c)
Definition enriched_string.cpp:132
size_t size() const
Definition enriched_string.h:89
bool m_has_background
Definition enriched_string.h:111
EnrichedString operator+(const EnrichedString &other) const
Definition enriched_string.cpp:142
EnrichedString()
Definition enriched_string.cpp:25
EnrichedString(std::wstring_view s, const video::SColor &color=video::SColor(255, 255, 255, 255))
void updateDefaultColor()
Definition enriched_string.cpp:209
size_t m_default_length
Definition enriched_string.h:115
EnrichedString substr(size_t pos=0, size_t len=std::string::npos) const
Definition enriched_string.cpp:177
void setDefaultColor(video::SColor color)
Definition enriched_string.h:66
video::SColor m_background
Definition enriched_string.h:113
EnrichedString & operator=(std::wstring_view s)
Definition enriched_string.cpp:54