lxgui
gui_factory.hpp
1 #ifndef LXGUI_GUI_FACTORY_HPP
2 #define LXGUI_GUI_FACTORY_HPP
3 
4 #include "lxgui/lxgui.hpp"
5 #include "lxgui/utils_observer.hpp"
6 
7 #include <functional>
8 #include <string>
9 #include <unordered_map>
10 
13 namespace sol {
14 
15 class state;
16 
17 }
21 namespace lxgui::gui {
22 
23 class region;
24 struct region_core_attributes;
25 struct frame_core_attributes;
26 class layered_region;
27 class frame;
28 class registry;
29 class frame_renderer;
30 class manager;
31 
39 class factory {
40 private:
41  template<typename T>
43  create_new_object(manager& mgr, const region_core_attributes& attr) {
44  return utils::make_owned<T>(mgr, attr);
45  }
46 
47  template<typename T>
49  create_new_frame(manager& mgr, const frame_core_attributes& attr) {
50  return utils::make_owned<T>(mgr, attr);
51  }
52 
53  template<typename T>
55  create_new_layered_region(manager& mgr, const region_core_attributes& attr) {
56  return utils::make_owned<T>(mgr, attr);
57  }
58 
59 public:
64  explicit factory(manager& mgr);
65 
66  // Non-copiable, non-movable
67  factory(const factory&) = delete;
68  factory(factory&&) = delete;
69  factory& operator=(const factory&) = delete;
70  factory& operator=(factory&&) = delete;
71 
81 
94 
105 
110  template<
111  typename ObjectType,
112  typename Enable =
113  typename std::enable_if<std::is_base_of<gui::region, ObjectType>::value>::type>
115  if constexpr (std::is_base_of_v<gui::layered_region, ObjectType>) {
116  custom_region_list_[ObjectType::class_name] = &create_new_layered_region<ObjectType>;
117  } else if constexpr (std::is_base_of_v<gui::frame, ObjectType>) {
118  custom_frame_list_[ObjectType::class_name] = &create_new_frame<ObjectType>;
119  } else {
120  custom_object_list_[ObjectType::class_name] = &create_new_object<ObjectType>;
121  }
122 
123  custom_lua_regs_[ObjectType::class_name] = &ObjectType::register_on_lua;
124  }
125 
131  template<
132  typename ObjectType,
133  typename FunctionType,
134  typename Enable =
135  typename std::enable_if<std::is_base_of<gui::region, ObjectType>::value>::type>
136  void register_region_type(FunctionType&& factory_func) {
137  if constexpr (std::is_base_of_v<gui::layered_region, ObjectType>) {
138  custom_region_list_[ObjectType::class_name] =
139  [factory_func =
140  std::move(factory_func)](manager& mgr, const region_core_attributes& attr) {
141  return factory_func(mgr, attr);
142  };
143  } else if constexpr (std::is_base_of_v<gui::frame, ObjectType>) {
144  custom_frame_list_[ObjectType::class_name] =
145  [factory_func =
146  std::move(factory_func)](manager& mgr, const frame_core_attributes& attr) {
147  return factory_func(mgr, attr);
148  };
149  } else {
150  custom_object_list_[ObjectType::class_name] =
151  [factory_func =
152  std::move(factory_func)](manager& mgr, const region_core_attributes& attr) {
153  return factory_func(mgr, attr);
154  };
155  }
156 
157  custom_lua_regs_[ObjectType::class_name] = &ObjectType::register_on_lua;
158  }
159 
164  void register_on_lua(sol::state& lua);
165 
166 private:
167  bool finalize_object_(registry& reg, region& object, const region_core_attributes& attr);
168 
169  void apply_inheritance_(region& object, const region_core_attributes& attr);
170 
171  manager& manager_;
172 
173  template<typename T>
174  using string_map = std::unordered_map<std::string, T>;
175 
176  string_map<std::function<utils::owner_ptr<region>(manager&, const region_core_attributes&)>>
177  custom_object_list_;
178 
179  string_map<std::function<utils::owner_ptr<frame>(manager&, const frame_core_attributes&)>>
180  custom_frame_list_;
181 
182  string_map<
183  std::function<utils::owner_ptr<layered_region>(manager&, const region_core_attributes&)>>
184  custom_region_list_;
185 
186  string_map<std::function<void(sol::state&)>> custom_lua_regs_;
187 };
188 
189 } // namespace lxgui::gui
190 
191 #endif
Handles the creation of new UI objects.
Definition: gui_factory.hpp:39
factory(factory &&)=delete
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 & operator=(const factory &)=delete
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
factory(const factory &)=delete
factory & operator=(factory &&)=delete
void register_region_type()
Registers a new object type.
utils::owner_ptr< frame > create_frame(registry &reg, const frame_core_attributes &attr)
Creates a new frame.
Definition: gui_factory.cpp:35
void register_region_type(FunctionType &&factory_func)
Registers a new object type.
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.
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.