]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/StyleSpec.h
Inventory: Send dirty lists where appropriate (#8742)
[dragonfireclient.git] / src / gui / StyleSpec.h
1 /*
2 Minetest
3 Copyright (C) 2019 rubenwardy
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 #include "irrlichttypes_extrabloated.h"
21 #include <array>
22
23 #pragma once
24
25 class StyleSpec
26 {
27 public:
28         enum Property
29         {
30                 TEXTCOLOR,
31                 BGCOLOR,
32                 NOCLIP,
33                 BORDER,
34                 BGIMG,
35                 BGIMG_PRESSED,
36                 ALPHA,
37                 NUM_PROPERTIES,
38                 NONE
39         };
40
41 private:
42         std::array<bool, NUM_PROPERTIES> property_set;
43         std::array<std::string, NUM_PROPERTIES> properties;
44
45 public:
46         static Property GetPropertyByName(const std::string &name)
47         {
48                 if (name == "textcolor") {
49                         return TEXTCOLOR;
50                 } else if (name == "bgcolor") {
51                         return BGCOLOR;
52                 } else if (name == "noclip") {
53                         return NOCLIP;
54                 } else if (name == "border") {
55                         return BORDER;
56                 } else if (name == "bgimg") {
57                         return BGIMG;
58                 } else if (name == "bgimg_pressed") {
59                         return BGIMG_PRESSED;
60                 } else if (name == "alpha") {
61                         return ALPHA;
62                 } else {
63                         return NONE;
64                 }
65         }
66
67         std::string get(Property prop, std::string def) const
68         {
69                 const auto &val = properties[prop];
70                 return val.empty() ? def : val;
71         }
72
73         void set(Property prop, const std::string &value)
74         {
75                 properties[prop] = value;
76                 property_set[prop] = true;
77         }
78
79         video::SColor getColor(Property prop, video::SColor def) const
80         {
81                 const auto &val = properties[prop];
82                 if (val.empty()) {
83                         return def;
84                 }
85
86                 parseColorString(val, def, false, 0xFF);
87                 return def;
88         }
89
90         video::SColor getColor(Property prop) const
91         {
92                 const auto &val = properties[prop];
93                 FATAL_ERROR_IF(val.empty(), "Unexpected missing property");
94
95                 video::SColor color;
96                 parseColorString(val, color, false, 0xFF);
97                 return color;
98         }
99
100         bool getBool(Property prop, bool def) const
101         {
102                 const auto &val = properties[prop];
103                 if (val.empty()) {
104                         return def;
105                 }
106
107                 return is_yes(val);
108         }
109
110         inline bool isNotDefault(Property prop) const
111         {
112                 return !properties[prop].empty();
113         }
114
115         inline bool hasProperty(Property prop) const { return property_set[prop]; }
116
117         StyleSpec &operator|=(const StyleSpec &other)
118         {
119                 for (size_t i = 0; i < NUM_PROPERTIES; i++) {
120                         auto prop = (Property)i;
121                         if (other.hasProperty(prop)) {
122                                 set(prop, other.get(prop, ""));
123                         }
124                 }
125
126                 return *this;
127         }
128
129         StyleSpec operator|(const StyleSpec &other) const
130         {
131                 StyleSpec newspec = *this;
132                 newspec |= other;
133                 return newspec;
134         }
135 };