1 #ifndef LXGUI_GUI_REGION_HPP
2 #define LXGUI_GUI_REGION_HPP
4 #include "lxgui/gui_anchor.hpp"
5 #include "lxgui/gui_bounds2.hpp"
6 #include "lxgui/gui_color.hpp"
7 #include "lxgui/gui_exception.hpp"
8 #include "lxgui/gui_region_core_attributes.hpp"
9 #include "lxgui/gui_vector2.hpp"
10 #include "lxgui/lxgui.hpp"
11 #include "lxgui/utils.hpp"
12 #include "lxgui/utils_maths.hpp"
13 #include "lxgui/utils_observer.hpp"
16 #include <lxgui/extern_sol2_object.hpp>
18 #include <unordered_map>
185 virtual void render()
const;
191 virtual void update(
float delta);
198 virtual std::string
serialize(
const std::string& tab)
const;
216 const std::string&
get_name()
const;
422 template<
typename ObjectType>
502 template<
typename... Args>
504 constexpr
auto set_anchor_overload =
506 (this->*set_anchor_overload)(
anchor_data{std::forward<Args>(args)...});
542 const std::array<std::optional<anchor>, 9>&
get_anchors()
const;
616 return utils::const_pointer_cast<frame_renderer>(
729 template<
typename ObjectType>
732 template<
typename ObjectType>
746 std::pair<anchor_type, vector2<std::optional<float>>>
750 float& left,
float& right,
float& top,
float& bottom,
float& x_center,
float& y_center)
753 bool make_borders_(
float& min,
float& max,
float center,
float size)
const;
790 virtual void set_parent_(utils::observer_ptr<frame> parent);
836 template<
typename ObjectType>
838 const ObjectType*
object =
dynamic_cast<const ObjectType*
>(
self);
839 if (
self && !
object && self->is_region_type(ObjectType::class_name)) {
841 self->get_region_type(),
"cannot use down_cast() to " +
842 std::string(ObjectType::class_name) +
843 " as object is being destroyed");
857 template<
typename ObjectType>
859 const ObjectType*
object =
dynamic_cast<const ObjectType*
>(
self);
860 if (
self && !
object) {
861 if (
self.is_region_type(ObjectType::class_name)) {
863 self.get_region_type(),
"cannot use down_cast() to " +
864 std::string(ObjectType::class_name) +
865 " as object is being destroyed");
868 self.get_region_type(),
"cannot use down_cast() to " +
869 std::string(ObjectType::class_name) +
870 " as object is not of the right type");
885 template<
typename ObjectType>
887 return const_cast<ObjectType*
>(down_cast<ObjectType>(
const_cast<const region*
>(
self)));
899 template<
typename ObjectType>
901 return const_cast<ObjectType&
>(down_cast<ObjectType>(
const_cast<const region&
>(
self)));
910 template<
typename ObjectType>
921 template<
typename ObjectType>
922 utils::observer_ptr<ObjectType>
down_cast(
const utils::observer_ptr<region>&
object) {
923 return utils::observer_ptr<ObjectType>(
object, down_cast<ObjectType>(
object.get()));
932 template<
typename ObjectType>
933 utils::observer_ptr<ObjectType>
down_cast(utils::observer_ptr<region>&&
object) {
934 return utils::observer_ptr<ObjectType>(std::move(
object), down_cast<ObjectType>(
object.get()));
945 template<
typename ObjectType>
948 return utils::static_pointer_cast<ObjectType>(self->region::observer_from_this());
Stores a position for a UI region.
Holds a single color (float RGBA, 128 bits)
Exception to be thrown by GUI code.
Handles the creation of new UI objects.
A region that can contain other regions and react to events.
An node in a layout file.
Manages the user interface.
The base class of all elements in the GUI.
void set_shown(bool is_shown)
shows/hides this region.
virtual void set_width(float abs_width)
Changes this region's absolute width (in pixels).
void set_all_anchors(const utils::observer_ptr< region > &obj)
Adjusts this regions anchors to fit the provided region.
bool is_region_type() const
Checks if this region is of the provided type.
bool is_valid() const
Checks if this region has all its borders correctly defined.
const std::vector< utils::observer_ptr< region > > & get_anchored_objects() const
Returns the list of all objects that are anchored to this one.
void set_lua_member_(std::string key, sol::stack_object value)
const anchor & get_anchor(point p) const
Returns one of this region's anchor.
virtual void set_parent_(utils::observer_ptr< frame > parent)
Changes this region's parent.
const std::string & get_region_type() const
Returns the type of this region.
void set_anchor(const anchor_data &a)
Adds/replaces an anchor.
virtual utils::observer_ptr< const frame_renderer > get_effective_frame_renderer() const
Returns the renderer of this object or its parents.
void create_glue_(T &self)
color parse_color_node_(const layout_node &node)
virtual void parse_layout(const layout_node &node)
Parses data from a layout_node.
const bounds2f & get_borders() const
Returns this region's borders.
~region() override
Destructor.
void initialize_(T &self, const region_core_attributes &attr)
Set up function to call in all derived class constructors.
bool is_virtual() const
Checks if this region is virtual.
float get_effective_alpha() const
Returns this region's effective alpha (opacity).
bool is_apparent_width_defined() const
Checks if this region's apparent width is defined.
registry & get_registry()
Returns the UI object registry, which keeps track of all objects in the UI.
region & operator=(region &&)=delete
Non-movable.
virtual void parse_size_node_(const layout_node &node)
std::pair< anchor_type, vector2< std::optional< float > > > parse_dimension_node_(const layout_node &node)
bool is_visible() const
Checks if this region can be seen on the screen.
utils::observer_ptr< frame > parent_
friend const ObjectType * down_cast(const region *self)
Obtain a pointer to a derived class.
virtual utils::owner_ptr< region > release_from_parent()
Removes this region from its parent and return an owning pointer.
const utils::observer_ptr< frame > & get_parent()
Returns this region's parent.
manager & get_manager()
Returns this region's manager.
virtual void notify_loaded()
Notifies this region that it has been fully loaded.
void hide()
hides this region.
virtual void notify_renderer_need_redraw()
Notifies the renderer of this region that it needs to be redrawn.
float round_to_pixel(float value, utils::rounding_method method=utils::rounding_method::nearest) const
Round an absolute position on screen to the nearest physical pixel.
static void register_on_lua(sol::state &lua)
Registers this region class to the provided Lua state.
vector2f get_apparent_dimensions() const
Returns this region's apparent width and height (in pixels).
void read_anchors_(float &left, float &right, float &top, float &bottom, float &x_center, float &y_center) const
virtual void notify_visible()
Notifies this region that it is now visible on screen.
void set_manually_inherited(bool manually_inherited)
Flags this region as manually inherited or not.
virtual void render() const
Renders this region on the current render target.
virtual void set_height(float abs_height)
Changes this region's absolute height (in pixels).
void show()
shows this region.
void set_relative_height(float rel_height)
Changes this region's height (relative to its parent).
const vector2f & get_dimensions() const
Returns this region's explicitly-defined width and height (in pixels).
bool depends_on(const region &obj) const
Checks if this region depends on another.
void set_alpha(float alpha)
Changes this region's alpha (opacity).
vector2f get_center() const
Returns the position of this region's center.
const std::string & get_name() const
Returns this region's name.
bool is_shown() const
Checks if this region is shown.
virtual void parse_attributes_(const layout_node &node)
void destroy()
Forcefully removes this region from the GUI.
float get_right() const
Returns the horizontal position of this region's right border.
vector2< std::optional< float > > parse_offset_node_(const layout_node &node)
float get_left() const
Returns the horizontal position of this region's left border.
virtual void notify_invisible()
Notifies this region that it is no longer visible on screen.
void add_anchored_object(region &obj)
Notifies this region that another one is anchored to it.
static const std::vector< std::string > & get_type_list_impl_()
const std::string & get_raw_name() const
Returns this region's raw name.
void set_addon(const addon *a)
Sets the addon this frame belongs to.
std::array< std::optional< anchor >, 9 > anchor_list_
region(region &&)=delete
Non-movable.
void set_virtual_()
Makes this region virtual.
anchor & modify_anchor(point p)
Returns one of this region's anchor to modify it.
bool is_manually_inherited() const
Checks if this object is manually inherited.
static constexpr const char * class_name
virtual bool is_in_region(const vector2f &position) const
Checks if the provided coordinates are inside this region.
const manager & get_manager() const
Returns this region's manager.
virtual void set_dimensions(const vector2f &dimensions)
Changes this region's absolute dimensions (in pixels).
utils::observer_ptr< frame_renderer > get_effective_frame_renderer()
Returns the renderer of this object or its parents, nullptr if none.
float get_bottom() const
Returns the vertical position of this region's bottom border.
virtual void update(float delta)
Updates this region's logic.
void set_relative_width(float rel_width)
Changes this region's width (relative to its parent).
void clear_all_anchors()
Removes all anchors.
virtual const std::vector< std::string > & get_type_list_() const
utils::observer_ptr< const frame > get_parent() const
Returns this region's parent.
region & operator=(const region &)=delete
Non-copiable.
bool is_loaded() const
Checks if this region has been fully loaded.
region(utils::control_block &block, manager &mgr, const region_core_attributes &attr)
Contructor.
virtual void notify_borders_need_update()
Tells this region that its borders need updating.
bounds2< bool > defined_borders_
bool is_region_type(const region &obj) const
Checks if this region is of a type equal or derived from the supplied region.
void set_anchor(Args &&... args)
Adds/replaces an anchor.
bool make_borders_(float &min, float &max, float center, float size) const
void set_relative_dimensions(const vector2f &dimensions)
Changes this region's dimensions (relative to its parent).
vector2< float > parse_offset_node_or_(const layout_node &node, float fallback)
float get_top() const
Returns the vertical position of this region's top border.
bool is_apparent_height_defined() const
Checks if this region's apparent height is defined.
const std::array< std::optional< anchor >, 9 > & get_anchors() const
Returns all of this region's anchors.
bool is_manually_inherited_
virtual void copy_from(const region &obj)
Copies a region's parameters into this region (inheritance).
const addon * get_addon() const
Returns this frame's addon.
sol::object get_lua_member_(const std::string &key) const
virtual void parse_anchor_node_(const layout_node &node)
std::string parse_file_name(const std::string &file_name) const
Convert an addon-relative file path to a application-relative path.
region(const region &)=delete
Non-copiable.
float get_alpha() const
Returns this region's alpha (opacity).
std::vector< utils::observer_ptr< region > > anchored_object_list_
virtual std::string serialize(const std::string &tab) const
Prints all relevant information about this region in a string.
void remove_anchored_object(region &obj)
Notifies this region that another one is no longer anchored to it.
virtual void notify_scaling_factor_updated()
Tells this region that the global interface scaling factor has changed.
std::size_t get_anchor_count() const
Returns the number of defined anchors.
virtual void update_borders_()
void set_name_(const std::string &name)
Sets this region's name.
void remove_glue()
Removes the Lua glue.
Keeps track of created UI objects and records their names for lookup.
const ObjectType * down_cast(const region *self)
Obtain a pointer to a derived class.
utils::observer_ptr< ObjectType > observer_from(ObjectType *self)
Obtain an observer pointer from a raw pointer (typically 'this')
oup::enable_observer_from_this_sealed< T > enable_observer_from_this
rounding_method
Rounding method for points to pixels conversions.
@ nearest
Equivalent to round()
oup::observable_sealed_ptr< T > owner_ptr
A piece of the user interface.
Raw data of an anchor (value type)
Struct holding all the core information about a region necessary for its creation.