lxgui
gui_parser_common.cpp
1 #include "lxgui/gui_parser_common.hpp"
2 
3 #include "lxgui/gui_frame.hpp"
4 #include "lxgui/gui_layout_node.hpp"
5 #include "lxgui/gui_manager.hpp"
6 #include "lxgui/gui_out.hpp"
7 #include "lxgui/gui_root.hpp"
8 #include "lxgui/gui_virtual_registry.hpp"
9 #include "lxgui/gui_virtual_root.hpp"
10 
11 namespace lxgui::gui {
12 
14  registry& reg,
15  virtual_registry& vreg,
16  const layout_node& node,
17  utils::observer_ptr<frame> parent) {
19  attr.object_type = node.get_name();
20  attr.name = node.get_attribute_value<std::string>("name");
21 
22  if (parent) {
23  attr.parent = std::move(parent);
24 
25  if (node.has_attribute("virtual")) {
26  gui::out << gui::warning << node.get_location() << ": "
27  << "Cannot use the \"virtual\" attribute on \"" << attr.name
28  << "\", because it is a nested region. Attribute ignored." << std::endl;
29  }
30  if (node.has_attribute("parent")) {
31  gui::out << gui::warning << node.get_location() << ": "
32  << "Cannot use the \"parent\" attribute on \"" << attr.name
33  << "\", because it is a nested region. Attribute ignored." << std::endl;
34  }
35  } else {
36  attr.is_virtual = node.get_attribute_value_or<bool>("virtual", false);
37 
38  if (const auto parent_attr = node.try_get_attribute_value<std::string>("parent")) {
39  std::string parent_name = parent_attr.value();
40  auto parent_obj = reg.get_region_by_name(parent_name);
41  if (!parent_name.empty() && !parent_obj) {
42  gui::out << gui::warning << node.get_location() << ": "
43  << "Cannot find \"" << attr.name << "\"'s parent: \"" << parent_name
44  << "\". No parent given to this region." << std::endl;
45  }
46 
47  attr.parent = down_cast<frame>(parent_obj);
48  if (parent_obj != nullptr && attr.parent == nullptr) {
49  gui::out << gui::warning << node.get_location() << ": "
50  << "Cannot set \"" << attr.name << "\"'s parent: \"" << parent_name
51  << "\". This is not a frame." << std::endl;
52  }
53  }
54  }
55 
56  if (const auto inh_attr = node.try_get_attribute_value<std::string>("inherits")) {
57  attr.inheritance = vreg.get_virtual_region_list(inh_attr.value());
58  }
59 
60  return attr;
61 }
62 
64  if (node.is_access_check_bypassed())
65  return;
66 
67  if (!node.was_accessed()) {
68  gui::out << gui::warning << node.get_location() << ": "
69  << "node '" << node.get_name()
70  << "' was not read by parser; check its name is spelled correctly and that it is "
71  "at the right location."
72  << std::endl;
73  return;
74  }
75 
76  for (const auto& attr : node.get_attributes()) {
77  if (attr.is_access_check_bypassed())
78  continue;
79 
80  if (!attr.was_accessed()) {
81  gui::out << gui::warning << attr.get_location() << ": "
82  << "attribute '" << attr.get_name()
83  << "' was not read by parser; check its name is spelled correctly and that it "
84  "is at the right location."
85  << std::endl;
86  }
87  }
88 
89  for (const auto& child : node.get_children())
91 }
92 
93 } // namespace lxgui::gui
bool is_access_check_bypassed() const noexcept
Check if this node should be bypassed for access checks.
std::string_view get_name() const noexcept
Returns this node's name.
bool was_accessed() const noexcept
Check if this node was accessed by the parser.
std::string_view get_location() const noexcept
Returns this node's location in the file as {file}:{line}.
An node in a layout file.
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.
bool has_attribute(std::string_view name) const noexcept
Checks if a given attribute has been specified.
children_view get_children() const noexcept
Returns a view to the list of children.
attribute_view get_attributes() const noexcept
Returns a view to the list of attributes.
std::string_view get_attribute_value(std::string_view name) const
Returns the value of the attribute with the provided name, throws if none.
T get_attribute_value_or(std::string_view name, T fallback) const noexcept
Returns the value of the attribute with the provided name, or a default if not found or parsing faile...
Keeps track of created UI objects and records their names for lookup.
utils::observer_ptr< const region > get_region_by_name(std::string_view name) const
Returns the region associated with the given name.
Keeps track of virtual UI objects and records their names for lookup.
std::vector< utils::observer_ptr< const region > > get_virtual_region_list(std::string_view names) const
Return a list of virtual regions matching the provided comma-separated list.
std::ostream out
region_core_attributes parse_core_attributes(registry &reg, virtual_registry &vreg, const layout_node &node, utils::observer_ptr< frame > parent)
Parse "core" attributes from a layout node, before creating a frame.
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.
Struct holding all the core information about a region necessary for its creation.
std::vector< utils::observer_ptr< const region > > inheritance