lxgui
gui_root.hpp
1 #ifndef LXGUI_GUI_ROOT_HPP
2 #define LXGUI_GUI_ROOT_HPP
3 
4 #include "lxgui/gui_anchor.hpp"
5 #include "lxgui/gui_frame_container.hpp"
6 #include "lxgui/gui_frame_renderer.hpp"
7 #include "lxgui/gui_key_binder.hpp"
8 #include "lxgui/gui_registry.hpp"
9 #include "lxgui/gui_vector2.hpp"
10 #include "lxgui/input_signals.hpp"
11 #include "lxgui/lxgui.hpp"
12 #include "lxgui/utils_observer.hpp"
13 #include "lxgui/utils_signal.hpp"
14 
15 #include <list>
16 #include <memory>
17 
18 namespace lxgui::input {
19 
20 class world_dispatcher;
21 
22 }
23 
24 namespace lxgui::gui {
25 
26 class region;
27 class frame;
28 class manager;
29 class renderer;
30 
36 class root :
38  public frame_renderer,
39  public frame_container {
40 public:
46  explicit root(utils::control_block& block, manager& mgr);
47 
49  ~root() override;
50 
51  // Non-copiable, non-movable
52  root(const root&) = delete;
53  root(root&&) = delete;
54  root& operator=(const root&) = delete;
55  root& operator=(root&&) = delete;
56 
61  vector2f get_target_dimensions() const override;
62 
64  void render() const;
65 
71  void enable_caching(bool enable);
72 
80  void toggle_caching();
81 
87  bool is_caching_enabled() const;
88 
93  void update(float delta);
94 
97 
100 
105  const utils::observer_ptr<frame>& get_hovered_frame() {
106  return hovered_frame_;
107  }
108 
113  utils::observer_ptr<const frame> get_hovered_frame() const {
114  return hovered_frame_;
115  }
116 
121  bool is_hovered(const frame& obj) const {
122  return hovered_frame_.get() == &obj;
123  }
124 
129  const utils::observer_ptr<frame>& get_dragged_frame() {
130  return dragged_frame_;
131  }
132 
137  utils::observer_ptr<const frame> get_dragged_frame() const {
138  return dragged_frame_;
139  }
140 
145  bool is_dragged(const frame& obj) const {
146  return dragged_frame_.get() == &obj;
147  }
148 
158  void start_moving(
159  utils::observer_ptr<region> obj,
160  anchor* a = nullptr,
162  std::function<void()> apply_constraint_func = nullptr);
163 
168  void stop_moving();
169 
175  bool is_moving(const region& obj) const;
176 
184  void start_sizing(utils::observer_ptr<region> obj, point p);
185 
190  void stop_sizing();
191 
197  bool is_sizing(const region& obj) const;
198 
209  void request_focus(utils::observer_ptr<frame> receiver);
210 
215  void release_focus(const frame& receiver);
216 
218  void clear_focus();
219 
225  bool is_focused() const;
226 
231  utils::observer_ptr<const frame> get_focused_frame() const;
232 
237  utils::observer_ptr<frame> get_focused_frame() {
238  return utils::const_pointer_cast<frame>(const_cast<const root*>(this)->get_focused_frame());
239  }
240 
246  return manager_;
247  }
248 
253  const manager& get_manager() const {
254  return manager_;
255  }
256 
262  return object_registry_;
263  }
264 
269  const registry& get_registry() const {
270  return object_registry_;
271  }
272 
278  return key_binder_;
279  }
280 
285  const key_binder& get_key_binder() const {
286  return key_binder_;
287  }
288 
289 private:
290  void create_caching_render_target_();
291  void create_strata_cache_render_target_(strata_data& strata_obj);
292 
293  void clear_hovered_frame_();
294  void update_hovered_frame_();
295  void
296  set_hovered_frame_(utils::observer_ptr<frame> obj, const vector2f& mouse_pos = vector2f::zero);
297 
298  void on_window_resized_(const vector2ui& dimensions);
299  bool on_mouse_moved_(const input::mouse_moved_data& args);
300  bool on_mouse_wheel_(const input::mouse_wheel_data& args);
301  bool on_drag_start_(const input::mouse_drag_start_data& args);
302  bool on_drag_stop_(const input::mouse_drag_stop_data& args);
303  bool on_text_entered_(const input::text_entered_data& args);
304  bool on_key_state_changed_(input::key key, bool is_down, bool is_repeat);
305  bool on_mouse_button_state_changed_(
306  input::mouse_button button_id,
307  bool is_down,
308  bool is_double_click,
309  bool was_dragged,
310  const vector2f& mouse_pos);
311 
312  manager& manager_;
313  renderer& renderer_;
314  registry object_registry_;
315  key_binder key_binder_;
316  input::world_dispatcher& world_input_dispatcher_;
317 
318  // Rendering
319  vector2ui screen_dimensions_;
320 
321  bool caching_enabled_ = false;
322 
323  std::shared_ptr<render_target> target_;
324  quad screen_quad_;
325 
326  // IO
327  std::vector<utils::scoped_connection> connections_;
328 
329  // Mouse IO
330  utils::observer_ptr<frame> hovered_frame_ = nullptr;
331  utils::observer_ptr<frame> dragged_frame_ = nullptr;
332  utils::observer_ptr<frame> start_click_frame_ = nullptr;
333 
334  utils::observer_ptr<region> moved_object_ = nullptr;
335  utils::observer_ptr<region> sized_object_ = nullptr;
336  vector2f mouse_movement_;
337 
338  anchor* moved_anchor_ = nullptr;
339  vector2f movement_start_position_;
340  constraint constraint_ = constraint::none;
341  std::function<void()> apply_constraint_func_;
342 
343  vector2f resize_start_;
344  bool is_resizing_width_ = false;
345  bool is_resizing_height_ = false;
346  bool is_resizing_from_right_ = false;
347  bool is_resizing_from_bottom_ = false;
348 
349  // Keyboard IO
350  std::vector<utils::observer_ptr<frame>> focus_stack_;
351 };
352 
353 } // namespace lxgui::gui
354 
355 #endif
Stores a position for a UI region.
Definition: gui_anchor.hpp:97
Abstract class for layering and rendering frames.
A region that can contain other regions and react to events.
Definition: gui_frame.hpp:255
Binds global actions to key presses.
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.
Abstract type for implementation specific management.
Root of the UI object hierarchy.
Definition: gui_root.hpp:39
const utils::observer_ptr< frame > & get_hovered_frame()
Returns the currently hovered frame, if any.
Definition: gui_root.hpp:105
void render() const
Renders the UI into the current render target.
Definition: gui_root.cpp:121
root(utils::control_block &block, manager &mgr)
Constructor.
Definition: gui_root.cpp:19
void notify_scaling_factor_updated()
Tells this object that the global interface scaling factor has changed.
Definition: gui_root.cpp:275
root & operator=(root &&)=delete
bool is_moving(const region &obj) const
Checks if the given object is allowed to move.
Definition: gui_root.cpp:336
const key_binder & get_key_binder() const
Returns the key_binder object, which enables binding global actions to key presses.
Definition: gui_root.hpp:285
root(const root &)=delete
void start_moving(utils::observer_ptr< region > obj, anchor *a=nullptr, constraint constraint=constraint::none, std::function< void()> apply_constraint_func=nullptr)
Start manually moving a region with the mouse.
Definition: gui_root.cpp:303
bool is_hovered(const frame &obj) const
Check if a given frame is being hovered.
Definition: gui_root.hpp:121
void stop_sizing()
Stops sizing for the current object.
Definition: gui_root.cpp:406
const utils::observer_ptr< frame > & get_dragged_frame()
Returns the currently dragged frame, if any.
Definition: gui_root.hpp:129
void request_focus(utils::observer_ptr< frame > receiver)
Sets whether keyboard input should be focused.
Definition: gui_root.cpp:447
root & operator=(const root &)=delete
vector2f get_target_dimensions() const override
Returns the width and height of this renderer's main render target (e.g., screen).
Definition: gui_root.cpp:117
void toggle_caching()
Toggles interface caching.
Definition: gui_root.cpp:257
key_binder & get_key_binder()
Returns the key_binder object, which enables binding global actions to key presses.
Definition: gui_root.hpp:277
bool is_dragged(const frame &obj) const
Check if a given frame is being dragged.
Definition: gui_root.hpp:145
void notify_hovered_frame_dirty()
Notifies the root that it should update the hovered frame.
Definition: gui_root.cpp:299
bool is_caching_enabled() const
Checks if interface caching is enabled.
Definition: gui_root.cpp:271
const manager & get_manager() const
Returns the manager instance associated with this root.
Definition: gui_root.hpp:253
utils::observer_ptr< frame > get_focused_frame()
Returns the currently focused frame (nullptr if none).
Definition: gui_root.hpp:237
void clear_focus()
Release all requested focus.
Definition: gui_root.cpp:475
bool is_sizing(const region &obj) const
Checks if the given object is allowed to be resized.
Definition: gui_root.cpp:410
bool is_focused() const
Checks whether keyboard input is focused somewhere, to prevent multiple inputs.
Definition: gui_root.cpp:483
const registry & get_registry() const
Returns the UI object registry, which keeps track of all objects in the UI.
Definition: gui_root.hpp:269
utils::observer_ptr< const frame > get_hovered_frame() const
Returns the currently hovered frame, if any.
Definition: gui_root.hpp:113
utils::observer_ptr< const frame > get_dragged_frame() const
Returns the currently dragged frame, if any.
Definition: gui_root.hpp:137
void update(float delta)
updates this root and its regions.
Definition: gui_root.cpp:184
registry & get_registry()
Returns the UI object registry, which keeps track of all objects in the UI.
Definition: gui_root.hpp:261
void start_sizing(utils::observer_ptr< region > obj, point p)
Starts manually resizing a region with the mouse.
Definition: gui_root.cpp:340
void stop_moving()
Stops movement for the current object.
Definition: gui_root.cpp:331
root(root &&)=delete
void release_focus(const frame &receiver)
Give up focus of keyboard input.
Definition: gui_root.cpp:461
utils::observer_ptr< const frame > get_focused_frame() const
Returns the currently focused frame (nullptr if none).
Definition: gui_root.cpp:487
~root() override
Destructor.
Definition: gui_root.cpp:112
manager & get_manager()
Returns the manager instance associated with this root.
Definition: gui_root.hpp:245
void enable_caching(bool enable)
Enables or disables interface caching.
Definition: gui_root.cpp:266
Generates input events for the world, after filtering by the UI.
oup::enable_observer_from_this_sealed< T > enable_observer_from_this
Simple structure holding four vertices and a material.
Definition: gui_quad.hpp:18
Contains frames sorted by level.
Definition: gui_strata.hpp:19
static const vector2 zero
Data for on_mouse_drag_start signal.
Data for on_mouse_drag_stop signal.
Data for on_mouse_moved signal.
Data for on_mouse_wheel signal.
Data for on_text_entered signal.