]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/enriched_string.h
Make Lint Happy
[dragonfireclient.git] / src / util / enriched_string.h
1 /*
2 Copyright (C) 2013 xyz, Ilya Zhuravlev <whatever@xyz.is>
3 Copyright (C) 2016 Nore, NathanaĆ«l Courant <nore@mesecons.net>
4
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.
9
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.
14
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.
18 */
19
20 #pragma once
21
22 #include <string>
23 #include <vector>
24 #include <SColor.h>
25
26 class EnrichedString
27 {
28 public:
29         EnrichedString();
30         EnrichedString(const std::wstring &s,
31                         const irr::video::SColor &color = irr::video::SColor(
32                                         255, 255, 255, 255));
33         EnrichedString(const wchar_t *str,
34                         const irr::video::SColor &color = irr::video::SColor(
35                                         255, 255, 255, 255));
36         EnrichedString(const std::wstring &string,
37                         const std::vector<irr::video::SColor> &colors);
38         void clear();
39         void operator=(const wchar_t *str);
40         void addAtEnd(const std::wstring &s, const irr::video::SColor &color);
41
42         // Adds the character source[i] at the end.
43         // An EnrichedString should always be able to be copied
44         // to the end of an existing EnrichedString that way.
45         void addChar(const EnrichedString &source, size_t i);
46
47         // Adds a single character at the end, without specifying its
48         // color. The color used will be the one from the last character.
49         void addCharNoColor(wchar_t c);
50
51         EnrichedString substr(size_t pos = 0, size_t len = std::string::npos) const;
52         EnrichedString operator+(const EnrichedString &other) const;
53         void operator+=(const EnrichedString &other);
54         const wchar_t *c_str() const;
55         const std::vector<irr::video::SColor> &getColors() const;
56         const std::wstring &getString() const;
57
58         void setDefaultColor(const irr::video::SColor &color);
59         void updateDefaultColor();
60         inline const irr::video::SColor &getDefaultColor() const
61         {
62                 return m_default_color;
63         }
64
65         inline bool operator==(const EnrichedString &other) const
66         {
67                 return (m_string == other.m_string && m_colors == other.m_colors);
68         }
69         inline bool operator!=(const EnrichedString &other) const
70         {
71                 return !(*this == other);
72         }
73         inline bool empty() const { return m_string.empty(); }
74         inline size_t size() const { return m_string.size(); }
75
76         inline bool hasBackground() const { return m_has_background; }
77         inline irr::video::SColor getBackground() const { return m_background; }
78         inline void setBackground(const irr::video::SColor &color)
79         {
80                 m_background = color;
81                 m_has_background = true;
82         }
83
84 private:
85         std::wstring m_string;
86         std::vector<irr::video::SColor> m_colors;
87         bool m_has_background;
88         irr::video::SColor m_default_color;
89         irr::video::SColor m_background;
90         // This variable defines the length of the default-colored text.
91         // Change this to a std::vector if an "end coloring" tag is wanted.
92         size_t m_default_length = 0;
93 };