1 #ifndef LXGUI_GUI_FRAME_HPP
2 #define LXGUI_GUI_FRAME_HPP
4 #include "lxgui/gui_backdrop.hpp"
5 #include "lxgui/gui_event_receiver.hpp"
6 #include "lxgui/gui_frame_core_attributes.hpp"
7 #include "lxgui/gui_layered_region.hpp"
8 #include "lxgui/gui_region.hpp"
9 #include "lxgui/gui_strata.hpp"
10 #include "lxgui/input_keys.hpp"
11 #include "lxgui/lxgui.hpp"
12 #include "lxgui/utils.hpp"
13 #include "lxgui/utils_meta.hpp"
14 #include "lxgui/utils_signal.hpp"
15 #include "lxgui/utils_view.hpp"
20 #include <lxgui/extern_sol2_protected_function.hpp>
21 #include <magic_enum.hpp>
24 #include <unordered_map>
270 adaptor<child_list, utils::view::smart_ptr_dereferencer, utils::view::non_null_filter>;
287 adaptor<region_list, utils::view::smart_ptr_dereferencer, utils::view::non_null_filter>;
300 void render()
const override;
309 void update(
float delta)
final;
316 std::string
serialize(
const std::string& tab)
const override;
323 virtual bool can_use_script(
const std::string& script_name)
const;
574 bool has_script(
const std::string& script_name)
const;
589 typename std::enable_if<std::is_base_of<gui::layered_region, RegionType>::value>::type>
591 return utils::static_pointer_cast<RegionType>(
592 add_region(utils::static_pointer_cast<layered_region>(std::move(reg))));
612 utils::observer_ptr<layered_region>
628 typename std::enable_if<std::is_base_of<gui::layered_region, RegionType>::value>::type>
629 utils::observer_ptr<RegionType>
633 return utils::static_pointer_cast<RegionType>(
650 typename std::enable_if<std::is_base_of<gui::layered_region, RegionType>::value>::type>
656 return utils::static_pointer_cast<RegionType>(
687 typename std::enable_if<std::is_base_of<gui::frame, FrameType>::value>::type>
691 return utils::static_pointer_cast<FrameType>(
create_child(std::move(attr)));
708 typename std::enable_if<std::is_base_of<gui::frame, FrameType>::value>::type>
714 return utils::static_pointer_cast<FrameType>(
create_child(std::move(attr)));
730 typename std::enable_if<std::is_base_of<gui::frame, FrameType>::value>::type>
732 return utils::static_pointer_cast<FrameType>(
733 add_child(utils::static_pointer_cast<frame>(std::move(child))));
764 utils::observer_ptr<const frame>
get_child(
const std::string& name)
const;
775 utils::observer_ptr<frame>
get_child(
const std::string& name) {
776 return utils::const_pointer_cast<frame>(
const_cast<const frame*
>(
this)->
get_child(name));
791 typename std::enable_if<std::is_base_of<gui::frame, FrameType>::value>::type>
792 utils::observer_ptr<const FrameType>
get_child(
const std::string& name)
const {
793 return down_cast<FrameType>(
get_child(name));
808 typename std::enable_if<std::is_base_of<gui::frame, FrameType>::value>::type>
809 utils::observer_ptr<FrameType>
get_child(
const std::string& name) {
810 return down_cast<FrameType>(
get_child(name));
834 utils::observer_ptr<const layered_region>
get_region(
const std::string& name)
const;
845 utils::observer_ptr<layered_region>
get_region(
const std::string& name) {
846 return utils::const_pointer_cast<layered_region>(
862 typename std::enable_if<std::is_base_of<gui::layered_region, RegionType>::value>::type>
863 utils::observer_ptr<RegionType>
get_region(
const std::string& name) {
864 return down_cast<RegionType>(
get_region(name));
903 return utils::const_pointer_cast<frame>(
1022 virtual utils::observer_ptr<const frame>
1033 utils::observer_ptr<frame>
1035 return utils::const_pointer_cast<frame>(
1145 const std::string& script_name,
1146 sol::protected_function handler,
1150 return define_script_(script_name, std::move(handler),
true, info);
1169 return define_script_(script_name, std::move(handler),
true, info);
1196 template<
typename DerivedType =
void,
typename Function>
1203 [handler = std::move(handler)](
frame&
self,
const event_data& data)
mutable {
1204 constexpr
bool use_automatic_cast = std::is_same_v<DerivedType, void>;
1206 using derived_type = std::decay_t<std::conditional_t<
1209 constexpr
bool use_no_cast = std::is_same_v<derived_type, frame>;
1211 if constexpr (use_no_cast) {
1212 handler(
self, data);
1213 }
else if constexpr (use_automatic_cast) {
1214 handler(down_cast<derived_type>(
self), data);
1216 handler(
static_cast<derived_type&
>(
self), data);
1253 const std::string& script_name,
1254 sol::protected_function handler,
1258 return define_script_(script_name, std::move(handler),
false, info);
1277 return define_script_(script_name, std::move(handler),
false, info);
1304 template<
typename DerivedType =
void,
typename Function>
1311 [handler = std::move(handler)](
frame&
self,
const event_data& data)
mutable {
1312 constexpr
bool use_automatic_cast = std::is_same_v<DerivedType, void>;
1314 using derived_type = std::decay_t<std::conditional_t<
1317 constexpr
bool use_no_cast = std::is_same_v<derived_type, frame>;
1319 if constexpr (use_no_cast) {
1320 handler(
self, data);
1321 }
else if constexpr (use_automatic_cast) {
1322 handler(down_cast<derived_type>(
self), data);
1324 handler(
static_cast<derived_type&
>(
self), data);
1353 virtual void fire_script(
const std::string& script_name,
const event_data& data = event_data{});
1452 void set_strata(std::optional<strata> strata_id);
1646 return utils::const_pointer_cast<frame_renderer>(
1666 void set_width(
float abs_width)
override;
1746 utils::observer_ptr<layered_region>
1751 virtual void update_(
float delta);
1760 return utils::const_pointer_cast<frame_renderer>(
1777 void set_parent_(utils::observer_ptr<frame> parent)
override;
1782 const std::string& script_name,
1783 const std::string& content,
1788 const std::string& script_name,
1789 sol::protected_function handler,
1794 const std::string& script_name,
1804 static constexpr std::size_t
num_layers = magic_enum::enum_count<layer>();
Draws borders and background of a frame.
Stores a variable number of arguments for an event.
Utility object to store and manage connections to event signals.
Abstract class for layering and rendering frames.
A region that can contain other regions and react to events.
bool check_script_(const std::string &script_name) const
bool is_auto_focus_enabled() const
Checks if automatic focus is enabled.
utils::observer_ptr< frame_renderer > effective_frame_renderer_
utils::observer_ptr< const region > get_title_region() const
Returns this frame's title region.
utils::observer_ptr< frame > add_child(utils::owner_ptr< frame > child)
Adds a frame to this frame's children.
void enable_keyboard()
Marks this frame as able to receive any keyboard input.
void remove_script(const std::string &script_name)
Removes a script from this frame.
utils::view::adaptor< region_list, utils::view::smart_ptr_dereferencer, utils::view::non_null_filter > region_list_view
std::list< utils::owner_ptr< frame > > child_list
Type of the frame child list (internal).
script_list_view get_script(const std::string &script_name) const
Return a view into this frame's handler scripts, registered for the given event.
void set_height(float abs_height) override
Changes this region's absolute height (in pixels).
void stop_moving()
ends moving this frame.
virtual void parse_all_nodes_before_children_(const layout_node &node)
~frame() override
Destructor.
static void register_on_lua(sol::state &lua)
Registers this region class to the provided Lua state.
void notify_invisible() override
Notifies this region that it is no longer visible on screen.
utils::connection add_script(const std::string &script_name, sol::protected_function handler, script_info info=script_info{})
Adds an additional handler script to this frame (executed after existing scripts).
bounds2f abs_hit_rect_inset_list_
utils::observer_ptr< const frame_renderer > compute_top_level_frame_renderer_() const
vector2f get_min_dimensions() const
Returns this frame's min dimensions.
bool is_mouse_wheel_enabled() const
Checks if this frame can receive mouse wheel input.
void add_level_(int amount)
std::string serialize(const std::string &tab) const override
Prints all relevant information about this region in a string.
utils::view::adaptor< const child_list, utils::view::smart_ptr_dereferencer, utils::view::non_null_filter > const_child_list_view
void render() const override
Renders this region on the current render target.
virtual void parse_hit_rect_insets_node_(const layout_node &node)
void disable_mouse_click()
Marks this frame as unable to receive mouse click input.
float get_scale() const
Returns this frame's scale.
utils::observer_ptr< RegionType > add_region(utils::owner_ptr< RegionType > reg)
Adds a layered_region to this frame's children.
bool is_movable() const
Checks if this frame can be moved.
void set_key_capture_enabled(const std::string &key_name, bool enable)
Marks this frame as able to receive keyboard input from a specific key.
utils::observer_ptr< frame > get_child(const std::string &name)
Returns one of this frame's children.
utils::owner_ptr< region > release_from_parent() override
Removes this region from its parent and return an owning pointer.
utils::observer_ptr< RegionType > create_layered_region(layer layer_id, const std::string &name)
Creates a new region as child of this frame.
utils::observer_ptr< frame_renderer > compute_top_level_frame_renderer_()
virtual void parse_scripts_node_(const layout_node &node)
void enable_auto_focus(bool enable)
Enables automatic focus when this frame is shown or raised.
std::optional< strata > strata_
void stop_sizing()
ends resizing this frame.
child_list_view get_children()
Returns the child list.
const std::vector< std::string > & get_type_list_() const override
bool is_resizable() const
Checks if this frame can be resized.
bool is_user_placed() const
Checks if this frame has been moved by the user.
static std::string get_adjusted_script_name(const std::string &script_name)
Returns the "adjusted" script name: "OnEvent" becomes "on_event".
void set_backdrop(std::unique_ptr< backdrop > bdrop)
Sets this frames' backdrop.
bool is_in_region(const vector2f &position) const override
Checks if the provided coordinates are inside this frame.
void copy_from(const region &obj) override
Copies a region's parameters into this frame (inheritance).
virtual void parse_layers_node_(const layout_node &node)
void set_resizable(bool is_resizable)
Sets if this frame can be resized by the user.
std::set< std::string > reg_drag_list_
void unregister_event(const std::string &event_name)
Tells the frame not to react to a certain event.
utils::connection set_script(const std::string &script_name, sol::protected_function handler, script_info info=script_info{})
Sets a new handler script for this frame (replacing existing scripts).
void disable_drag()
Tells this frame to not react to mouse drag from any mouse button.
std::array< layer_container, num_layers > layer_list_
strata compute_effective_strata_() const
void set_key_capture_enabled(input::key key_id, bool enable)
Marks this frame as able to receive keyboard input from a specific key.
vector2f get_max_dimensions() const
Returns this frame's max dimensions.
utils::observer_ptr< layered_region > get_region(const std::string &name)
Returns one of this frame's region.
void set_clamped_to_screen(bool is_clamped_to_screen)
Sets if this frame is clamped to screen.
utils::connection set_script(const std::string &script_name, Function &&handler, script_info info=script_info{})
Sets a new handler script for this frame (replacing existing scripts).
utils::observer_ptr< RegionType > get_region(const std::string &name)
Returns one of this frame's region.
void set_max_width(float max_width)
Sets this frame's maximum width.
void set_level(int level_id)
Sets this frame's level.
void create_title_region()
Creates a new title region for this frame.
void set_max_height(float max_height)
Sets this frame's maximum height.
backdrop & get_or_create_backdrop()
Returns this frame's backdrop, creating it if needed.
void set_draw_layer_enabled(layer layer_id, bool enable)
Enables or disables a layer.
void notify_layers_need_update()
Tells this frame to rebuild its layer list.
utils::observer_ptr< frame_renderer > frame_renderer_
event_receiver event_receiver_
void set_scale(float scale)
Sets this frame's scale.
void enable_mouse()
Marks this frame as able to receive mouse input (click & move).
void start_sizing(const point &p)
Starts resizing this frame with the mouse.
std::size_t get_layered_region_count() const
Returns the number of layered regions of this frame.
void enable_key_capture(const std::string &key_name)
Marks this frame as able to receive keyboard input from a specific key.
std::size_t get_layered_region_count_upper_bound() const
Returns the approximate number of regions of this frame.
void update(float delta) final
Updates this region's logic.
virtual void parse_title_region_node_(const layout_node &node)
bool has_script(const std::string &script_name) const
Checks if this frame has a script defined.
void notify_visible() override
Notifies this region that it is now visible on screen.
void set_abs_hit_rect_insets(const bounds2f &insets)
Sets this frame's absolute hit rect insets.
utils::observer_ptr< const frame > get_child(const std::string &name) const
Returns one of this frame's children.
void set_min_height(float min_height)
Sets this frame's minimum height.
utils::observer_ptr< FrameType > add_child(utils::owner_ptr< FrameType > child)
Adds a frame to this frame's children.
void enable_mouse_move()
Marks this frame as able to receive mouse move input.
bool is_clamped_to_screen() const
Checks if this frame is clamped to screen.
utils::observer_ptr< const FrameType > get_child(const std::string &name) const
Returns one of this frame's children.
void parse_attributes_(const layout_node &node) override
bool is_drag_enabled(const std::string &button_name) const
Checks if this frame is registered for drag events with the provided mouse button.
void set_frame_renderer(utils::observer_ptr< frame_renderer > rdr)
Flags this object as rendered by another object.
bool is_key_capture_enabled(const std::string &key_name) const
Checks if this frame can receive keyboard input from a specific key.
void set_dimensions(const vector2f &dimensions) override
Changes this region's absolute dimensions (in pixels).
void disable_key_capture()
Marks this frame as unable to receive keyboard input from any key.
void set_top_level(bool is_top_level)
Sets if this frame is at top level.
void on_event_(std::string_view event_name, const event_data &event)
void register_event(const std::string &event_name)
Tells this frame to react to a certain event.
const backdrop * get_backdrop() const
Returns this frame's backdrop.
const bounds2f & get_abs_hit_rect_insets() const
Returns this frame's absolute hit rect insets.
utils::observer_ptr< region > get_title_region()
Returns this frame's title region.
std::set< std::string > reg_key_list_
static constexpr std::size_t num_layers
virtual void fire_script(const std::string &script_name, const event_data &data=event_data{})
Calls a script.
void set_mouse_move_enabled(bool is_mouse_enabled)
Sets if this frame can receive mouse move input.
utils::observer_ptr< layered_region > parse_region_(const layout_node &node, const std::string &layer_name, const std::string &type)
utils::connection add_script(const std::string &script_name, std::string content, script_info info=script_info{})
Adds an additional handler script to this frame (executed after existing scripts).
std::optional< strata > get_strata() const
Returns this frame's strata.
void set_width(float abs_width) override
Changes this region's absolute width (in pixels).
void enable_mouse_click()
Marks this frame as able to receive mouse click input.
bool is_mouse_click_enabled_
void disable_mouse_wheel()
Marks this frame as unable to receive mouse wheel input.
utils::observer_ptr< FrameType > create_child(frame_core_attributes attr)
Creates a new frame as child of this frame.
const bounds2f & get_rel_hit_rect_insets() const
Returns this frame's relative hit rect insets.
void set_mouse_enabled(bool is_mouse_enabled)
Sets if this frame can receive mouse input (click & move).
const utils::observer_ptr< frame_renderer > & get_frame_renderer()
Returns the renderer of this object, nullptr if none.
void enable_mouse_wheel()
Marks this frame as able to receive mouse wheel input.
std::unordered_map< std::string, script_signal > signal_list_
utils::observer_ptr< layered_region > create_layered_region(layer layer_id, region_core_attributes attr)
Creates a new region as child of this frame.
bool is_top_level() const
Checks if this frame is at top level.
virtual void update_(float delta)
utils::view::adaptor< const region_list, utils::view::smart_ptr_dereferencer, utils::view::non_null_filter > const_region_list_view
bounds2f rel_hit_rect_inset_list_
utils::observer_ptr< frame > find_topmost_frame(const std::function< bool(const frame &)> &predicate)
Find the topmost frame matching the provided predicate.
int get_level() const
Returns this frame's level.
void notify_renderer_need_redraw() override
Notifies the renderer of this region that it needs to be redrawn.
utils::observer_ptr< RegionType > create_layered_region(layer layer_id, region_core_attributes attr)
Creates a new region as child of this frame.
void set_min_width(float min_width)
Sets this frame's minimum width.
bool is_clamped_to_screen_
void set_keyboard_enabled(bool is_keyboard_enabled)
Sets if this frame can receive any keyboard input.
virtual bool can_use_script(const std::string &script_name) const
Returns 'true' if this frame can use a script.
utils::observer_ptr< const frame > get_top_level_parent() const
Returns this frame's top-level parent.
utils::connection add_script(const std::string &script_name, script_function handler, script_info info=script_info{})
Adds an additional handler script to this frame (executed after existing scripts).
void set_min_dimensions(const vector2f &min)
Sets this frame's minimum size.
void disable_keyboard()
Marks this frame as unable to receive any keyboard input.
void set_parent_(utils::observer_ptr< frame > parent) override
Changes this region's parent.
bool is_mouse_move_enabled() const
Checks if this frame can receive mouse movement input.
bool is_mouse_click_enabled() const
Checks if this frame can receive mouse click input.
virtual void parse_resize_bounds_node_(const layout_node &node)
void notify_loaded() override
Notifies this region that it has been fully loaded.
bool is_mouse_move_enabled_
bool has_focus() const
Check if this frame currently has focus.
utils::observer_ptr< FrameType > create_child(const std::string &name)
Creates a new frame as child of this frame.
float get_effective_scale() const
Calculates effective scale.
utils::connection set_script(const std::string &script_name, std::string content, script_info info=script_info{})
Sets a new handler script for this frame (replacing existing scripts).
void set_update_rate(float rate)
Sets a maximum update rate (in updates per seconds).
strata get_effective_strata() const
Returns this frame's effective strata.
void parse_layout(const layout_node &node) final
Parses data from a layout_node.
utils::observer_ptr< FrameType > get_child(const std::string &name)
Returns one of this frame's children.
utils::owner_ptr< layered_region > remove_region(const utils::observer_ptr< layered_region > ®)
Removes a layered_region from this frame's children.
virtual void parse_backdrop_node_(const layout_node &node)
void set_rel_hit_rect_insets(const bounds2f &insets)
Sets this frame's relative hit rect insets.
std::size_t get_child_count() const
Returns the number of children of this frame.
std::list< utils::owner_ptr< layered_region > > region_list
Type of the region list (internal).
bool is_mouse_wheel_enabled_
void update_borders_() override
bool is_keyboard_enabled() const
Checks if this frame can receive any keyboard input.
void set_user_placed(bool is_user_placed)
Sets if this frame has been moved by the user.
void set_strata(std::optional< strata > strata_id)
Sets this frame's strata.
void set_mouse_click_enabled(bool is_mouse_enabled)
Sets if this frame can receive mouse click input.
void set_drag_enabled(input::mouse_button button_id, bool enable)
Tells this frame whether to react to mouse drag or not.
virtual void notify_focus(bool focus)
Notifies this frame that it has received or lost focus.
utils::owner_ptr< frame > remove_child(const utils::observer_ptr< frame > &child)
Removes a frame from this frame's children.
void set_drag_enabled(const std::string &button_name, bool enable)
Tells this frame whether to react to mouse drag or not.
void set_focus(bool focus)
Asks for focus for this frame.
utils::observer_ptr< const frame_renderer > get_effective_frame_renderer() const final
Returns the renderer of this object or its parents, nullptr if none.
std::unique_ptr< backdrop > backdrop_
utils::observer_ptr< frame > parse_child_(const layout_node &node, const std::string &type)
utils::observer_ptr< const layered_region > get_region(const std::string &name) const
Returns one of this frame's region.
utils::connection set_script(const std::string &script_name, script_function handler, script_info info=script_info{})
Sets a new handler script for this frame (replacing existing scripts).
utils::owner_ptr< region > title_region_
void disable_mouse()
Marks this frame as unable to receive mouse input (click & move).
float get_update_rate() const
Gets the maximum update rate (in upates per seconds).
void set_mouse_wheel_enabled(bool is_mouse_wheel_enabled)
Sets if this frame can receive mouse wheel input.
utils::observer_ptr< frame > get_top_level_parent()
Returns this frame's top-level parent.
void enable_draw_layer(layer layer_id)
Enables a layer.
utils::observer_ptr< const frame_renderer > get_frame_renderer() const
Returns the renderer of this object, nullptr if none.
void set_movable(bool is_movable)
Sets if this frame can be moved by the user.
void disable_mouse_move()
Marks this frame as unable to receive mouse move input.
virtual void notify_mouse_in_frame(bool mouse_in_frame, const vector2f &mouse_pos)
Tells this frame it is being hovered by the mouse.
utils::observer_ptr< frame > create_child(frame_core_attributes attr)
Creates a new frame as child of this frame.
bool is_keyboard_enabled_
region_list_view get_regions()
Returns the region list.
float time_since_last_update_
utils::connection add_script(const std::string &script_name, Function &&handler, script_info info=script_info{})
Adds an additional handler script to this frame (executed after existing scripts).
virtual void parse_frames_node_(const layout_node &node)
virtual utils::observer_ptr< const frame > find_topmost_frame(const std::function< bool(const frame &)> &predicate) const
Find the topmost frame matching the provided predicate.
void set_max_dimensions(const vector2f &max)
Sets this frame's maximum size.
std::size_t get_child_count_upper_bound() const
Returns the approximate number of children of this frame.
void notify_strata_changed_(strata new_strata_id)
utils::view::adaptor< child_list, utils::view::smart_ptr_dereferencer, utils::view::non_null_filter > child_list_view
static constexpr const char * class_name
utils::observer_ptr< layered_region > add_region(utils::owner_ptr< layered_region > reg)
Adds a layered_region to this frame's children.
void notify_scaling_factor_updated() override
Tells this region that the global interface scaling factor has changed.
void start_moving()
Starts moving this frame with the mouse.
utils::connection define_script_(const std::string &script_name, const std::string &content, bool append, const script_info &info)
void notify_frame_renderer_changed_(const utils::observer_ptr< frame_renderer > &new_renderer)
void disable_draw_layer(layer layer_id)
Disables a layer.
void enable_drag(const std::string &button_name)
Tells this frame to react to mouse drag.
An node in a layout file.
Manages the user interface.
The base class of all elements in the GUI.
region(utils::control_block &block, manager &mgr, const region_core_attributes &attr)
Contructor.
Object representing the connection between a slot and a signal.
Generic class for observing and triggering events.
utils::view::adaptor< slot_list, slot_dereferencer, non_disconnected_filter > slot_list_view
Type of the view returned by slots().
std::function< T > function_type
Type of the callable function stored in a slot. Can use any function/delegate type here,...
Allow iterating over a container without access to the container itself.
layer
ID of a layer for rendering inside a frame.
script_signal::function_type script_function
C++ function type for UI script handlers.
script_signal::slot_list_view script_list_view
View into all the connected scripts for a given event.
void(frame &, const event_data &) script_signature
Signature of frame scripts.
oup::observable_sealed_ptr< T > owner_ptr
typename impl::first_function_argument_< decltype(&std::decay_t< T >::operator())>::type first_function_argument
static const bounds2 zero
Struct holding all the core information about a frame necessary for its creation.
Contains gui::layered_region.
std::vector< utils::observer_ptr< layered_region > > region_list
Struct holding all the core information about a region necessary for its creation.
Holds file/line information for a script.
Convert unique_ptr or shared_ptr to standard pointer.