3 Copyright (C) 2019 rubenwardy
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.
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.
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.
20 #include "client/tile.h" // ITextureSource
21 #include "client/fontengine.h"
23 #include "irrlichttypes_extrabloated.h"
24 #include "util/string.h"
38 BGCOLOR_HOVERED, // Note: Deprecated property
39 BGCOLOR_PRESSED, // Note: Deprecated property
43 BGIMG_HOVERED, // Note: Deprecated property
45 BGIMG_PRESSED, // Note: Deprecated property
47 FGIMG_HOVERED, // Note: Deprecated property
48 FGIMG_PRESSED, // Note: Deprecated property
66 STATE_HOVERED = 1 << 0,
67 STATE_PRESSED = 1 << 1,
69 STATE_INVALID = 1 << 3,
73 std::array<bool, NUM_PROPERTIES> property_set{};
74 std::array<std::string, NUM_PROPERTIES> properties;
75 State state_map = STATE_DEFAULT;
78 static Property GetPropertyByName(const std::string &name)
80 if (name == "textcolor") {
82 } else if (name == "bgcolor") {
84 } else if (name == "bgcolor_hovered") {
85 return BGCOLOR_HOVERED;
86 } else if (name == "bgcolor_pressed") {
87 return BGCOLOR_PRESSED;
88 } else if (name == "noclip") {
90 } else if (name == "border") {
92 } else if (name == "bgimg") {
94 } else if (name == "bgimg_hovered") {
96 } else if (name == "bgimg_middle") {
98 } else if (name == "bgimg_pressed") {
100 } else if (name == "fgimg") {
102 } else if (name == "fgimg_hovered") {
103 return FGIMG_HOVERED;
104 } else if (name == "fgimg_pressed") {
105 return FGIMG_PRESSED;
106 } else if (name == "alpha") {
108 } else if (name == "content_offset") {
109 return CONTENT_OFFSET;
110 } else if (name == "padding") {
112 } else if (name == "font") {
114 } else if (name == "font_size") {
116 } else if (name == "colors") {
118 } else if (name == "bordercolors") {
120 } else if (name == "borderwidths") {
122 } else if (name == "sound") {
124 } else if (name == "spacing") {
126 } else if (name == "size") {
133 std::string get(Property prop, std::string def) const
135 const auto &val = properties[prop];
136 return val.empty() ? def : val;
139 void set(Property prop, const std::string &value)
141 properties[prop] = value;
142 property_set[prop] = true;
145 //! Parses a name and returns the corresponding state enum
146 static State getStateByName(const std::string &name)
148 if (name == "default") {
149 return STATE_DEFAULT;
150 } else if (name == "hovered") {
151 return STATE_HOVERED;
152 } else if (name == "pressed") {
153 return STATE_PRESSED;
155 return STATE_INVALID;
159 //! Gets the state that this style is intended for
160 State getState() const
165 //! Set the given state on this style
166 void addState(State state)
168 FATAL_ERROR_IF(state >= NUM_STATES, "Out-of-bounds state received");
170 state_map = static_cast<State>(state_map | state);
173 //! Using a list of styles mapped to state values, calculate the final
174 // combined style for a state by propagating values in its component states
175 static StyleSpec getStyleFromStatePropagation(const std::array<StyleSpec, NUM_STATES> &styles, State state)
177 StyleSpec temp = styles[StyleSpec::STATE_DEFAULT];
178 temp.state_map = state;
179 for (int i = StyleSpec::STATE_DEFAULT + 1; i <= state; i++) {
180 if ((state & i) != 0) {
181 temp = temp | styles[i];
188 video::SColor getColor(Property prop, video::SColor def) const
190 const auto &val = properties[prop];
195 parseColorString(val, def, false, 0xFF);
199 video::SColor getColor(Property prop) const
201 const auto &val = properties[prop];
202 FATAL_ERROR_IF(val.empty(), "Unexpected missing property");
205 parseColorString(val, color, false, 0xFF);
209 std::array<video::SColor, 4> getColorArray(Property prop,
210 std::array<video::SColor, 4> def) const
212 const auto &val = properties[prop];
216 std::vector<std::string> strs;
217 if (!parseArray(val, strs))
220 for (size_t i = 0; i <= 3; i++) {
222 if (parseColorString(strs[i], color, false, 0xff))
229 std::array<s32, 4> getIntArray(Property prop, std::array<s32, 4> def) const
231 const auto &val = properties[prop];
235 std::vector<std::string> strs;
236 if (!parseArray(val, strs))
239 for (size_t i = 0; i <= 3; i++)
240 def[i] = stoi(strs[i]);
245 irr::core::rect<s32> getRect(Property prop, irr::core::rect<s32> def) const
247 const auto &val = properties[prop];
251 irr::core::rect<s32> rect;
252 if (!parseRect(val, &rect))
258 irr::core::rect<s32> getRect(Property prop) const
260 const auto &val = properties[prop];
261 FATAL_ERROR_IF(val.empty(), "Unexpected missing property");
263 irr::core::rect<s32> rect;
264 parseRect(val, &rect);
268 v2f32 getVector2f(Property prop, v2f32 def) const
270 const auto &val = properties[prop];
275 if (!parseVector2f(val, &vec))
281 v2s32 getVector2i(Property prop, v2s32 def) const
283 const auto &val = properties[prop];
288 if (!parseVector2f(val, &vec))
291 return v2s32(vec.X, vec.Y);
294 v2s32 getVector2i(Property prop) const
296 const auto &val = properties[prop];
297 FATAL_ERROR_IF(val.empty(), "Unexpected missing property");
300 parseVector2f(val, &vec);
301 return v2s32(vec.X, vec.Y);
304 gui::IGUIFont *getFont() const
306 FontSpec spec(FONT_SIZE_UNSPECIFIED, FM_Standard, false, false);
308 const std::string &font = properties[FONT];
309 const std::string &size = properties[FONT_SIZE];
311 if (font.empty() && size.empty())
314 std::vector<std::string> modes = split(font, ',');
316 for (size_t i = 0; i < modes.size(); i++) {
317 if (modes[i] == "normal")
318 spec.mode = FM_Standard;
319 else if (modes[i] == "mono")
321 else if (modes[i] == "bold")
323 else if (modes[i] == "italic")
330 if (size[0] == '*') {
331 std::string new_size = size.substr(1); // Remove '*' (invalid for stof)
332 calc_size = stof(new_size) * g_fontengine->getFontSize(spec.mode);
333 } else if (size[0] == '+' || size[0] == '-') {
334 calc_size = stoi(size) + g_fontengine->getFontSize(spec.mode);
336 calc_size = stoi(size);
339 spec.size = (unsigned)std::min(std::max(calc_size, 1), 999);
342 return g_fontengine->getFont(spec);
345 video::ITexture *getTexture(Property prop, ISimpleTextureSource *tsrc,
346 video::ITexture *def) const
348 const auto &val = properties[prop];
353 video::ITexture *texture = tsrc->getTexture(val);
358 video::ITexture *getTexture(Property prop, ISimpleTextureSource *tsrc) const
360 const auto &val = properties[prop];
361 FATAL_ERROR_IF(val.empty(), "Unexpected missing property");
363 video::ITexture *texture = tsrc->getTexture(val);
368 bool getBool(Property prop, bool def) const
370 const auto &val = properties[prop];
378 inline bool isNotDefault(Property prop) const
380 return !properties[prop].empty();
383 inline bool hasProperty(Property prop) const { return property_set[prop]; }
385 StyleSpec &operator|=(const StyleSpec &other)
387 for (size_t i = 0; i < NUM_PROPERTIES; i++) {
388 auto prop = (Property)i;
389 if (other.hasProperty(prop)) {
390 set(prop, other.get(prop, ""));
397 StyleSpec operator|(const StyleSpec &other) const
399 StyleSpec newspec = *this;
405 bool parseArray(const std::string &value, std::vector<std::string> &arr) const
407 std::vector<std::string> strs = split(value, ',');
409 if (strs.size() == 1) {
410 arr = {strs[0], strs[0], strs[0], strs[0]};
411 } else if (strs.size() == 2) {
412 arr = {strs[0], strs[1], strs[0], strs[1]};
413 } else if (strs.size() == 4) {
416 warningstream << "Invalid array size (" << strs.size()
417 << " arguments): \"" << value << "\"" << std::endl;
423 bool parseRect(const std::string &value, irr::core::rect<s32> *parsed_rect) const
425 irr::core::rect<s32> rect;
426 std::vector<std::string> v_rect = split(value, ',');
428 if (v_rect.size() == 1) {
429 s32 x = stoi(v_rect[0]);
430 rect.UpperLeftCorner = irr::core::vector2di(x, x);
431 rect.LowerRightCorner = irr::core::vector2di(-x, -x);
432 } else if (v_rect.size() == 2) {
433 s32 x = stoi(v_rect[0]);
434 s32 y = stoi(v_rect[1]);
435 rect.UpperLeftCorner = irr::core::vector2di(x, y);
436 rect.LowerRightCorner = irr::core::vector2di(-x, -y);
437 // `-x` is interpreted as `w - x`
438 } else if (v_rect.size() == 4) {
439 rect.UpperLeftCorner = irr::core::vector2di(
440 stoi(v_rect[0]), stoi(v_rect[1]));
441 rect.LowerRightCorner = irr::core::vector2di(
442 stoi(v_rect[2]), stoi(v_rect[3]));
444 warningstream << "Invalid rectangle string format: \"" << value
445 << "\"" << std::endl;
454 bool parseVector2f(const std::string &value, v2f32 *parsed_vec) const
457 std::vector<std::string> v_vector = split(value, ',');
459 if (v_vector.size() == 1) {
460 f32 x = stof(v_vector[0]);
463 } else if (v_vector.size() == 2) {
464 vec.X = stof(v_vector[0]);
465 vec.Y = stof(v_vector[1]);
467 warningstream << "Invalid 2d vector string format: \"" << value
468 << "\"" << std::endl;