f81727e93ecfc9c3a227c1846922c580fc957644
[oweals/minetest.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
22 #pragma once
23
24
25 class StyleSpec
26 {
27 public:
28         enum Property {
29                 NONE = 0,
30                 TEXTCOLOR,
31                 BGCOLOR,
32                 NUM_PROPERTIES
33         };
34
35 private:
36         std::unordered_map<Property, std::string> properties;
37
38 public:
39         static Property GetPropertyByName(const std::string &name) {
40                 if (name == "textcolor") {
41                         return TEXTCOLOR;
42                 } else if (name == "bgcolor") {
43                         return BGCOLOR;
44                 } else {
45                         return NONE;
46                 }
47         }
48
49         std::string get(Property prop, std::string def) const {
50                 auto it = properties.find(prop);
51                 if (it == properties.end()) {
52                         return def;
53                 }
54
55                 return it->second;
56         }
57
58         void set(Property prop, std::string value) {
59                 properties[prop] = std::move(value);
60         }
61
62         video::SColor getColor(Property prop, video::SColor def) const {
63                 auto it = properties.find(prop);
64                 if (it == properties.end()) {
65                         return def;
66                 }
67
68                 parseColorString(it->second, def, false, 0xFF);
69                 return def;
70         }
71
72         video::SColor getColor(Property prop) const {
73                 auto it = properties.find(prop);
74                 FATAL_ERROR_IF(it == properties.end(), "Unexpected missing property");
75
76                 video::SColor color;
77                 parseColorString(it->second, color, false, 0xFF);
78                 return color;
79         }
80
81         bool hasProperty(Property prop) const {
82                 return properties.find(prop) != properties.end();
83         }
84
85         StyleSpec &operator|=(const StyleSpec &other) {
86                 for (size_t i = 1; i < NUM_PROPERTIES; i++) {
87                         auto prop = (Property)i;
88                         if (other.hasProperty(prop)) {
89                                 properties[prop] = other.get(prop, "");
90                         }
91                 }
92
93                 return *this;
94         }
95
96         StyleSpec operator|(const StyleSpec &other) const {
97                 StyleSpec newspec = *this;
98                 newspec |= other;
99                 return newspec;
100         }
101 };
102