lxgui
gui_factory.cpp
1 #include "lxgui/gui_factory.hpp"
2 
3 #include "lxgui/gui_frame.hpp"
4 #include "lxgui/gui_frame_renderer.hpp"
5 #include "lxgui/gui_layered_region.hpp"
6 #include "lxgui/gui_manager.hpp"
7 #include "lxgui/gui_out.hpp"
8 #include "lxgui/gui_registry.hpp"
9 
10 namespace lxgui::gui {
11 
12 factory::factory(manager& mgr) : manager_(mgr) {}
13 
15  if (!reg.check_region_name(attr.name))
16  return nullptr;
17 
18  auto iter = custom_object_list_.find(attr.object_type);
19  if (iter == custom_object_list_.end()) {
20  gui::out << gui::warning << "gui::factory: Unknown object class: \"" << attr.object_type
21  << "\"." << std::endl;
22  return nullptr;
23  }
24 
25  utils::owner_ptr<region> new_object = iter->second(manager_, attr);
26  if (!new_object)
27  return nullptr;
28 
29  if (!finalize_object_(reg, *new_object, attr))
30  return nullptr;
31 
32  return new_object;
33 }
34 
36  if (!reg.check_region_name(attr.name))
37  return nullptr;
38 
39  auto iter = custom_frame_list_.find(attr.object_type);
40  if (iter == custom_frame_list_.end()) {
41  gui::out << gui::warning << "gui::factory: Unknown frame class: \"" << attr.object_type
42  << "\"." << std::endl;
43  return nullptr;
44  }
45 
46  utils::owner_ptr<frame> new_frame = iter->second(manager_, attr);
47  if (!new_frame)
48  return nullptr;
49 
50  if (!finalize_object_(reg, *new_frame, attr))
51  return nullptr;
52 
53  return new_frame;
54 }
55 
58  if (!reg.check_region_name(attr.name))
59  return nullptr;
60 
61  auto iter = custom_region_list_.find(attr.object_type);
62  if (iter == custom_region_list_.end()) {
63  gui::out << gui::warning << "gui::factory: Unknown layered_region class: \""
64  << attr.object_type << "\"." << std::endl;
65  return nullptr;
66  }
67 
68  utils::owner_ptr<layered_region> new_region = iter->second(manager_, attr);
69  if (!new_region)
70  return nullptr;
71 
72  if (!finalize_object_(reg, *new_region, attr))
73  return nullptr;
74 
75  return new_region;
76 }
77 
78 void factory::register_on_lua(sol::state& lua) {
79  for (const auto& reg : custom_lua_regs_)
80  reg.second(lua);
81 }
82 
83 bool factory::finalize_object_(registry& reg, region& object, const region_core_attributes& attr) {
84  if (!object.is_virtual() || attr.parent == nullptr) {
85  if (!reg.add_region(observer_from(&object)))
86  return false;
87  }
88 
89  // NB: This needs to be done after the region is fully constructed, since this
90  // should override defaults set in any constructor.
91  apply_inheritance_(object, attr);
92 
93  return true;
94 }
95 
96 void factory::apply_inheritance_(region& object, const region_core_attributes& attr) {
97  for (const auto& base : attr.inheritance) {
98  if (!object.is_region_type(*base)) {
99  gui::out << gui::warning << "gui::factory: "
100  << "\"" << object.get_name() << "\" (" << object.get_region_type()
101  << ") cannot inherit from \"" << base->get_name() << "\" ("
102  << base->get_region_type() << "). Inheritance skipped." << std::endl;
103  continue;
104  }
105 
106  // Inherit from the other object
107  object.copy_from(*base);
108  }
109 }
110 
111 } // namespace lxgui::gui
void register_on_lua(sol::state &lua)
Registers all region types on the provided Lua state.
Definition: gui_factory.cpp:78
utils::owner_ptr< layered_region > create_layered_region(registry &reg, const region_core_attributes &attr)
Creates a new layered_region.
Definition: gui_factory.cpp:57
factory(manager &mgr)
Constructor.
Definition: gui_factory.cpp:12
utils::owner_ptr< region > create_region(registry &reg, const region_core_attributes &attr)
Creates a new region.
Definition: gui_factory.cpp:14
utils::owner_ptr< frame > create_frame(registry &reg, const frame_core_attributes &attr)
Creates a new frame.
Definition: gui_factory.cpp:35
Manages the user interface.
Definition: gui_manager.hpp:44
The base class of all elements in the GUI.
Definition: gui_region.hpp:161
Keeps track of created UI objects and records their names for lookup.
bool add_region(utils::observer_ptr< region > obj)
Adds a region to be handled by this registry.
bool check_region_name(std::string_view name) const
Checks the provided string is suitable for naming a region.
Definition: gui_registry.cpp:9
utils::observer_ptr< ObjectType > observer_from(ObjectType *self)
Obtain an observer pointer from a raw pointer (typically 'this')
Definition: gui_region.hpp:946
std::ostream out
const std::string warning
Definition: gui_out.cpp:6
oup::observable_sealed_ptr< T > owner_ptr
Struct holding all the core information about a frame necessary for its creation.
Struct holding all the core information about a region necessary for its creation.