lxgui
Loading...
Searching...
No Matches
gui_texture_parser.cpp
1#include "lxgui/gui_layout_node.hpp"
2#include "lxgui/gui_manager.hpp"
3#include "lxgui/gui_out.hpp"
4#include "lxgui/gui_texture.hpp"
5
6namespace lxgui::gui {
7
10
11 parse_tex_coords_node_(node);
12
13 if (const layout_node* color_node = node.try_get_child("Color"))
15
16 parse_gradient_node_(node);
17}
18
19void texture::parse_attributes_(const layout_node& node) {
21
22 if (const auto attr = node.try_get_attribute_value<material::filter>("filter"))
23 set_filter_mode(attr.value());
24 if (const auto attr = node.try_get_attribute_value<std::string>("file"))
25 set_texture(attr.value());
26}
27
28void texture::parse_tex_coords_node_(const layout_node& node) {
29 if (const layout_node* tex_coords_node = node.try_get_child("TexCoords")) {
31 {tex_coords_node->get_attribute_value_or<float>("left", 0.0f),
32 tex_coords_node->get_attribute_value_or<float>("top", 0.0f),
33 tex_coords_node->get_attribute_value_or<float>("right", 1.0f),
34 tex_coords_node->get_attribute_value_or<float>("bottom", 1.0f)});
35 }
36}
37
38void texture::parse_gradient_node_(const layout_node& node) {
39 if (const layout_node* gradient_node = node.try_get_child("Gradient")) {
40 std::string orientation_name =
41 gradient_node->get_attribute_value_or<std::string>("orientation", "HORIZONTAL");
42
43 orientation orient;
44 if (auto converted = utils::from_string<orientation>(orientation_name);
45 converted.has_value()) {
46 orient = converted.value();
47 } else {
48 gui::out << gui::warning << gradient_node->get_location()
49 << ": Unknown gradient orientation for " << name_ << ": \"" << orientation_name
50 << "\". No gradient will be shown for this texture." << std::endl;
51 return;
52 }
53
54 const layout_node* min_color_node = gradient_node->try_get_child("MinColor");
55 if (!min_color_node) {
56 gui::out << gui::warning << node.get_location()
57 << ": Gradient requires MinColor child node." << std::endl;
58 return;
59 }
60
61 const layout_node* max_color_node = gradient_node->try_get_child("MaxColor");
62 if (!max_color_node) {
63 gui::out << gui::warning << node.get_location()
64 << ": Gradient requires MaxColor child node." << std::endl;
65 return;
66 }
67
68 set_gradient(gradient{
69 orient, parse_color_node_(*min_color_node), parse_color_node_(*max_color_node)});
70 }
71}
72
73} // namespace lxgui::gui
void parse_attributes_(const layout_node &node) override
void parse_layout(const layout_node &node) override
Parses data from a layout_node.
An node in a layout file.
const layout_node * try_get_child(std::string_view name) const noexcept
Returns the first child with a given name, or null if none.
std::optional< std::string_view > try_get_attribute_value(std::string_view name) const noexcept
Returns the value of the attribute with the provided name, nullopt if not found.
color parse_color_node_(const layout_node &node)
void set_gradient(const gradient &g)
Adds a gradient effect to this texture.
void set_tex_rect(const std::array< float, 4 > &texture_rect)
Sets this texture's texture coordinates.
void set_solid_color(const color &c)
Sets this texture's color.
void set_filter_mode(material::filter filt)
Sets this texture's filtering mode.
void parse_layout(const layout_node &node) override
Parses data from a layout_node.
void set_texture(const std::string &file_name)
Sets this texture's texture file.
std::ostream out
const std::string warning
Definition gui_out.cpp:6