lxgui
Loading...
Searching...
No Matches
gui_button_parser.cpp
1#include "lxgui/gui_button.hpp"
2#include "lxgui/gui_font_string.hpp"
3#include "lxgui/gui_layout_node.hpp"
4#include "lxgui/gui_localizer.hpp"
5#include "lxgui/gui_manager.hpp"
6#include "lxgui/gui_out.hpp"
7#include "lxgui/gui_parser_common.hpp"
8#include "lxgui/gui_texture.hpp"
9
10namespace lxgui::gui {
13
14 if (const auto attr = node.try_get_attribute_value<std::string>("text"))
15 set_text(utils::utf8_to_unicode(get_manager().get_localizer().localize(attr.value())));
16}
17
20
21 if (const layout_node* special_node = node.try_get_child("NormalTexture")) {
22 auto layer = special_node->get_attribute_value_or<std::string>("layer", "ARTWORK");
23
24 layout_node defaulted = *special_node;
25 defaulted.get_or_set_attribute_value("name", "$parentNormalTexture");
26 defaulted.get_or_set_attribute_value("setAllAnchors", "true");
27
28 auto tex = parse_region_(defaulted, layer, "Texture");
29 if (tex) {
30 tex->set_manually_inherited(true);
31 set_normal_texture(utils::static_pointer_cast<texture>(tex));
32 }
33
35 special_node->bypass_access_check();
36 }
37
38 if (const layout_node* special_node = node.try_get_child("PushedTexture")) {
39 auto layer = special_node->get_attribute_value_or<std::string>("layer", "BORDER");
40
41 layout_node defaulted = *special_node;
42 defaulted.get_or_set_attribute_value("name", "$parentPushedTexture");
43 defaulted.get_or_set_attribute_value("setAllAnchors", "true");
44
45 auto tex = parse_region_(defaulted, layer, "Texture");
46 if (tex) {
47 tex->set_manually_inherited(true);
48 set_pushed_texture(utils::static_pointer_cast<texture>(tex));
49 }
50
52 special_node->bypass_access_check();
53 }
54
55 if (const layout_node* special_node = node.try_get_child("DisabledTexture")) {
56 auto layer = special_node->get_attribute_value_or<std::string>("layer", "BORDER");
57
58 layout_node defaulted = *special_node;
59 defaulted.get_or_set_attribute_value("name", "$parentDisabledTexture");
60 defaulted.get_or_set_attribute_value("setAllAnchors", "true");
61
62 auto tex = parse_region_(defaulted, layer, "Texture");
63 if (tex) {
64 tex->set_manually_inherited(true);
65 set_disabled_texture(utils::static_pointer_cast<texture>(tex));
66 }
67
69 special_node->bypass_access_check();
70 }
71
72 if (const layout_node* special_node = node.try_get_child("HighlightTexture")) {
73 auto layer = special_node->get_attribute_value_or<std::string>("layer", "HIGHLIGHT");
74
75 layout_node defaulted = *special_node;
76 defaulted.get_or_set_attribute_value("name", "$parentHighlightTexture");
77 defaulted.get_or_set_attribute_value("setAllAnchors", "true");
78
79 auto tex = parse_region_(defaulted, layer, "Texture");
80 if (tex) {
81 tex->set_manually_inherited(true);
82 set_highlight_texture(utils::static_pointer_cast<texture>(tex));
83 }
84
86 special_node->bypass_access_check();
87 }
88
89 if (const layout_node* special_node = node.try_get_child("NormalText")) {
90 auto layer = special_node->get_attribute_value_or<std::string>("layer", "ARTWORK");
91
92 layout_node defaulted = *special_node;
93 defaulted.get_or_set_attribute_value("name", "$parentNormalText");
94 defaulted.get_or_set_attribute_value("setAllAnchors", "true");
95
96 auto fstr = parse_region_(defaulted, layer, "FontString");
97 if (fstr) {
98 fstr->set_manually_inherited(true);
99 set_normal_text(utils::static_pointer_cast<font_string>(fstr));
100 }
101
103 special_node->bypass_access_check();
104 }
105
106 if (const layout_node* special_node = node.try_get_child("HighlightText")) {
107 auto layer = special_node->get_attribute_value_or<std::string>("layer", "HIGHLIGHT");
108
109 layout_node defaulted = *special_node;
110 defaulted.get_or_set_attribute_value("name", "$parentHighlightText");
111 defaulted.get_or_set_attribute_value("setAllAnchors", "true");
112
113 auto fstr = parse_region_(defaulted, layer, "FontString");
114 if (fstr) {
115 fstr->set_manually_inherited(true);
116 set_highlight_text(utils::static_pointer_cast<font_string>(fstr));
117 }
118
120 special_node->bypass_access_check();
121 }
122
123 if (const layout_node* special_node = node.try_get_child("DisabledText")) {
124 auto layer = special_node->get_attribute_value_or<std::string>("layer", "BORDER");
125
126 layout_node defaulted = *special_node;
127 defaulted.get_or_set_attribute_value("name", "$parentDisabledText");
128 defaulted.get_or_set_attribute_value("setAllAnchors", "true");
129
130 auto fstr = parse_region_(defaulted, layer, "FontString");
131 if (fstr) {
132 fstr->set_manually_inherited(true);
133 set_disabled_text(utils::static_pointer_cast<font_string>(fstr));
134 }
135
137 special_node->bypass_access_check();
138 }
139
140 if (const layout_node* offset_block = node.try_get_child("PushedTextOffset")) {
141 auto dimensions = parse_dimension_node_(*offset_block);
142 if (dimensions.first == anchor_type::abs) {
144 vector2f(dimensions.second.x.value_or(0.0f), dimensions.second.y.value_or(0.0f)));
145 } else {
146 gui::out << gui::warning << offset_block->get_location() << ": "
147 << "RelDimension for Button:PushedTextOffset is not yet supported. Skipped."
148 << std::endl;
149 }
150 }
151}
152} // namespace lxgui::gui
void set_highlight_text(utils::observer_ptr< font_string > fstr)
Sets this button's highlight text.
void set_normal_texture(utils::observer_ptr< texture > tex)
Sets this button's normal texture.
void set_pushed_texture(utils::observer_ptr< texture > tex)
Sets this button's pushed texture.
void set_disabled_texture(utils::observer_ptr< texture > tex)
Sets this button's disabled texture.
void parse_attributes_(const layout_node &node) override
void set_highlight_texture(utils::observer_ptr< texture > tex)
Sets this button's highlight texture.
void set_text(const utils::ustring &content)
Sets this button's text.
void set_pushed_text_offset(const vector2f &offset)
Sets this button's pushed text offset.
void set_disabled_text(utils::observer_ptr< font_string > fstr)
Sets this button's disabled text.
void set_normal_text(utils::observer_ptr< font_string > fstr)
Sets this button's normal text.
void parse_all_nodes_before_children_(const layout_node &node) override
virtual void parse_all_nodes_before_children_(const layout_node &node)
void parse_attributes_(const layout_node &node) override
utils::observer_ptr< layered_region > parse_region_(const layout_node &node, const std::string &layer_name, const std::string &type)
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::string_view get_or_set_attribute_value(std::string_view name, std::string_view value)
Returns the value of the attribute with the provided name, or set it 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.
manager & get_manager()
Returns this region's manager.
std::pair< anchor_type, vector2< std::optional< float > > > parse_dimension_node_(const layout_node &node)
layer
ID of a layer for rendering inside a frame.
vector2< float > vector2f
Holds 2D coordinates (as floats)
std::ostream out
const std::string warning
Definition gui_out.cpp:6
void warn_for_not_accessed_node(const layout_node &node)
Emit a warning if this node (or any of its attributes/children) was not read.