Luanti 5.15.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, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
22#include <string>
23#include <string_view>
24#include <vector>
25#include <SColor.h>
26
27
29public:
31 EnrichedString(std::wstring_view s,
32 const video::SColor &color = video::SColor(255, 255, 255, 255));
33 EnrichedString(std::wstring_view string,
34 const std::vector<video::SColor> &colors);
35 EnrichedString &operator=(std::wstring_view s);
36
37 void clear();
38
39 void addAtEnd(std::wstring_view s, video::SColor color);
40
41 // Adds the character source[i] at the end.
42 // An EnrichedString should always be able to be copied
43 // to the end of an existing EnrichedString that way.
44 void addChar(const EnrichedString &source, size_t i);
45
46 // Adds a single character at the end, without specifying its
47 // color. The color used will be the one from the last character.
48 void addCharNoColor(wchar_t c);
49
50 EnrichedString getNextLine(size_t *pos) const;
51 EnrichedString substr(size_t pos = 0, size_t len = std::string::npos) const;
52
53 EnrichedString operator+(const EnrichedString &other) const;
54 void operator+=(const EnrichedString &other);
55 void operator+=(std::wstring_view other)
56 {
57 *this += EnrichedString(other);
58 }
59
60 const wchar_t *c_str() const
61 {
62 return getString().c_str();
63 }
64 const std::vector<video::SColor> &getColors() const;
65 const std::wstring &getString() const;
66
67 inline void setDefaultColor(video::SColor color)
68 {
69 m_default_color = color;
71 }
72 void updateDefaultColor();
73 inline const video::SColor &getDefaultColor() const
74 {
75 return m_default_color;
76 }
77
78 inline bool operator==(const EnrichedString &other) const
79 {
80 return (m_string == other.m_string && m_colors == other.m_colors);
81 }
82 inline bool operator!=(const EnrichedString &other) const
83 {
84 return !(*this == other);
85 }
86 inline bool empty() const
87 {
88 return m_string.empty();
89 }
90 inline size_t size() const
91 {
92 return m_string.size();
93 }
94
95 inline bool hasBackground() const
96 {
97 return m_has_background;
98 }
99 inline video::SColor getBackground() const
100 {
101 return m_background;
102 }
103 inline void setBackground(video::SColor color)
104 {
105 m_background = color;
106 m_has_background = true;
107 }
108
109private:
110 std::wstring m_string;
111 std::vector<video::SColor> m_colors;
113 video::SColor m_default_color;
114 video::SColor m_background;
115 // This variable defines the length of the default-colored text.
117};
Definition enriched_string.h:28
video::SColor getBackground() const
Definition enriched_string.h:99
EnrichedString getNextLine(size_t *pos) const
Definition enriched_string.cpp:163
void addAtEnd(std::wstring_view s, video::SColor color)
Definition enriched_string.cpp:62
void setBackground(video::SColor color)
Definition enriched_string.h:103
bool empty() const
Definition enriched_string.h:86
void addChar(const EnrichedString &source, size_t i)
Definition enriched_string.cpp:127
EnrichedString(std::wstring_view string, const std::vector< video::SColor > &colors)
void clear()
Definition enriched_string.cpp:45
bool operator!=(const EnrichedString &other) const
Definition enriched_string.h:82
const std::vector< video::SColor > & getColors() const
Definition enriched_string.cpp:200
bool operator==(const EnrichedString &other) const
Definition enriched_string.h:78
bool hasBackground() const
Definition enriched_string.h:95
const std::wstring & getString() const
Definition enriched_string.cpp:205
void operator+=(std::wstring_view other)
Definition enriched_string.h:55
std::wstring m_string
Definition enriched_string.h:110
const wchar_t * c_str() const
Definition enriched_string.h:60
video::SColor m_default_color
Definition enriched_string.h:113
std::vector< video::SColor > m_colors
Definition enriched_string.h:111
void operator+=(const EnrichedString &other)
Definition enriched_string.cpp:150
const video::SColor & getDefaultColor() const
Definition enriched_string.h:73
void addCharNoColor(wchar_t c)
Definition enriched_string.cpp:133
size_t size() const
Definition enriched_string.h:90
bool m_has_background
Definition enriched_string.h:112
EnrichedString operator+(const EnrichedString &other) const
Definition enriched_string.cpp:143
EnrichedString()
Definition enriched_string.cpp:26
EnrichedString(std::wstring_view s, const video::SColor &color=video::SColor(255, 255, 255, 255))
void updateDefaultColor()
Definition enriched_string.cpp:210
size_t m_default_length
Definition enriched_string.h:116
EnrichedString substr(size_t pos=0, size_t len=std::string::npos) const
Definition enriched_string.cpp:178
void setDefaultColor(video::SColor color)
Definition enriched_string.h:67
video::SColor m_background
Definition enriched_string.h:114
EnrichedString & operator=(std::wstring_view s)
Definition enriched_string.cpp:55