lxgui
Loading...
Searching...
No Matches
gui_region.hpp
1#ifndef LXGUI_GUI_REGION_HPP
2#define LXGUI_GUI_REGION_HPP
3
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"
14
15#include <array>
16#include <lxgui/extern_sol2_object.hpp>
17#include <optional>
18#include <unordered_map>
19#include <vector>
20
23namespace sol {
24
25class state;
26
27}
31namespace lxgui::gui {
32
33struct addon;
34class manager;
35class registry;
36class factory;
37class layout_node;
38
39class frame;
40class frame_renderer;
41
162 friend factory;
163 friend frame;
164
165public:
167 explicit region(utils::control_block& block, manager& mgr, const region_core_attributes& attr);
168
170 ~region() override;
171
173 region(const region&) = delete;
174
176 region(region&&) = delete;
177
179 region& operator=(const region&) = delete;
180
182 region& operator=(region&&) = delete;
183
185 virtual void render() const;
186
191 virtual void update(float delta);
192
198 virtual std::string serialize(const std::string& tab) const;
199
204 virtual void copy_from(const region& obj);
205
207 virtual void notify_borders_need_update();
208
210 virtual void notify_scaling_factor_updated();
211
216 const std::string& get_name() const;
217
223 const std::string& get_raw_name() const;
224
229 utils::observer_ptr<const frame> get_parent() const {
230 return parent_;
231 }
232
237 const utils::observer_ptr<frame>& get_parent() {
238 return parent_;
239 }
240
246
253 void destroy();
254
260 void set_alpha(float alpha);
261
266 float get_alpha() const;
267
273 float get_effective_alpha() const;
274
280 void show();
281
287 void hide();
288
294 void set_shown(bool is_shown);
295
300 bool is_shown() const;
301
306 bool is_visible() const;
307
317 bool is_valid() const;
318
323 virtual void set_dimensions(const vector2f& dimensions);
324
329 virtual void set_width(float abs_width);
330
335 virtual void set_height(float abs_height);
336
341 void set_relative_dimensions(const vector2f& dimensions);
342
347 void set_relative_width(float rel_width);
348
353 void set_relative_height(float rel_height);
354
364 const vector2f& get_dimensions() const;
365
375
385 bool is_apparent_width_defined() const;
386
396 bool is_apparent_height_defined() const;
397
403 virtual bool is_in_region(const vector2f& position) const;
404
409 const std::string& get_region_type() const;
410
416 bool is_region_type(const std::string& type_name) const;
417
422 template<typename ObjectType>
423 bool is_region_type() const {
424 return is_region_type(ObjectType::class_name);
425 }
426
431 bool is_region_type(const region& obj) const {
432 return is_region_type(obj.get_region_type());
433 }
434
439 float get_bottom() const;
440
445 vector2f get_center() const;
446
451 float get_left() const;
452
457 float get_right() const;
458
463 float get_top() const;
464
469 const bounds2f& get_borders() const;
470
476 void clear_all_anchors();
477
483 void set_all_anchors(const utils::observer_ptr<region>& obj);
484
490 void set_all_anchors(const std::string& obj_name);
491
496 void set_anchor(const anchor_data& a);
497
502 template<typename... Args>
503 void set_anchor(Args&&... args) {
504 constexpr auto set_anchor_overload =
505 static_cast<void (region::*)(const anchor_data&)>(&region::set_anchor);
506 (this->*set_anchor_overload)(anchor_data{std::forward<Args>(args)...});
507 }
508
514 bool depends_on(const region& obj) const;
515
520 std::size_t get_anchor_count() const;
521
530
536 const anchor& get_anchor(point p) const;
537
542 const std::array<std::optional<anchor>, 9>& get_anchors() const;
543
550 float round_to_pixel(
551 float value, utils::rounding_method method = utils::rounding_method::nearest) const;
552
560 const vector2f& position,
562
569 void add_anchored_object(region& obj);
570
577
585 bool is_virtual() const;
586
594 void set_manually_inherited(bool manually_inherited);
595
601 bool is_manually_inherited() const;
602
608 virtual utils::observer_ptr<const frame_renderer> get_effective_frame_renderer() const;
609
615 utils::observer_ptr<frame_renderer> get_effective_frame_renderer() {
616 return utils::const_pointer_cast<frame_renderer>(
617 const_cast<const region*>(this)->get_effective_frame_renderer());
618 }
619
624 virtual void notify_renderer_need_redraw();
625
630 const std::vector<utils::observer_ptr<region>>& get_anchored_objects() const;
631
636 virtual void notify_loaded();
637
648 bool is_loaded() const;
649
654 virtual void notify_visible();
655
660 virtual void notify_invisible();
661
666 void set_addon(const addon* a);
667
674 const addon* get_addon() const;
675
687 std::string parse_file_name(const std::string& file_name) const;
688
694 return manager_;
695 }
696
701 const manager& get_manager() const {
702 return manager_;
703 }
704
710
715 const registry& get_registry() const;
716
718 void remove_glue();
719
724 virtual void parse_layout(const layout_node& node);
725
727 static void register_on_lua(sol::state& lua);
728
729 template<typename ObjectType>
730 friend const ObjectType* down_cast(const region* self);
731
732 template<typename ObjectType>
733 friend ObjectType* down_cast(region* self);
734
735 static constexpr const char* class_name = "Region";
736
737protected:
738 // Layout parsing
739 virtual void parse_attributes_(const layout_node& node);
740 virtual void parse_size_node_(const layout_node& node);
741 virtual void parse_anchor_node_(const layout_node& node);
744 vector2<float> parse_offset_node_or_(const layout_node& node, float fallback);
745
746 std::pair<anchor_type, vector2<std::optional<float>>>
748
749 void read_anchors_(
750 float& left, float& right, float& top, float& bottom, float& x_center, float& y_center)
751 const;
752
753 bool make_borders_(float& min, float& max, float center, float size) const;
754
755 virtual void update_borders_();
756
757 sol::state& get_lua_();
758 const sol::state& get_lua_() const;
759
760 template<typename T>
761 void create_glue_(T& self);
762
763 template<typename T>
764 static const std::vector<std::string>& get_type_list_impl_();
765
766 virtual const std::vector<std::string>& get_type_list_() const;
767
768 void set_lua_member_(std::string key, sol::stack_object value);
769 sol::object get_lua_member_(const std::string& key) const;
770
775 void set_virtual_();
776
783 void set_name_(const std::string& name);
784
790 virtual void set_parent_(utils::observer_ptr<frame> parent);
791
797 template<typename T>
798 void initialize_(T& self, const region_core_attributes& attr);
799
801
802 const addon* addon_ = nullptr;
803
804 std::string name_;
805 std::string raw_name_;
806
807 utils::observer_ptr<frame> parent_ = nullptr;
808
810 bool is_virtual_ = false;
811 bool is_loaded_ = false;
812 bool is_valid_ = true;
813
814 std::array<std::optional<anchor>, 9> anchor_list_;
817
818 float alpha_ = 1.0f;
819 bool is_shown_ = true;
820 bool is_visible_ = true;
821
823
824 std::vector<utils::observer_ptr<region>> anchored_object_list_;
825};
826
836template<typename ObjectType>
837const ObjectType* down_cast(const region* self) {
838 const ObjectType* object = dynamic_cast<const ObjectType*>(self);
839 if (self && !object && self->is_region_type(ObjectType::class_name)) {
840 throw gui::exception(
841 self->get_region_type(), "cannot use down_cast() to " +
842 std::string(ObjectType::class_name) +
843 " as object is being destroyed");
844 }
845 return object;
846}
847
857template<typename ObjectType>
858const ObjectType& down_cast(const region& self) {
859 const ObjectType* object = dynamic_cast<const ObjectType*>(self);
860 if (!object) {
861 if (self.is_region_type(ObjectType::class_name)) {
862 throw gui::exception(
863 self.get_region_type(), "cannot use down_cast() to " +
864 std::string(ObjectType::class_name) +
865 " as object is being destroyed");
866 } else {
867 throw gui::exception(
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");
871 }
872 }
873 return *object;
874}
875
885template<typename ObjectType>
886ObjectType* down_cast(region* self) {
887 return const_cast<ObjectType*>(down_cast<ObjectType>(const_cast<const region*>(self)));
888}
889
899template<typename ObjectType>
900ObjectType& down_cast(region& self) {
901 return const_cast<ObjectType&>(down_cast<ObjectType>(const_cast<const region&>(self)));
902}
903
910template<typename ObjectType>
912 return utils::owner_ptr<ObjectType>(std::move(object), down_cast<ObjectType>(object.get()));
913}
914
921template<typename ObjectType>
922utils::observer_ptr<ObjectType> down_cast(const utils::observer_ptr<region>& object) {
923 return utils::observer_ptr<ObjectType>(object, down_cast<ObjectType>(object.get()));
924}
925
932template<typename ObjectType>
933utils::observer_ptr<ObjectType> down_cast(utils::observer_ptr<region>&& object) {
934 return utils::observer_ptr<ObjectType>(std::move(object), down_cast<ObjectType>(object.get()));
935}
936
945template<typename ObjectType>
946utils::observer_ptr<ObjectType> observer_from(ObjectType* self) {
947 if (self)
948 return utils::static_pointer_cast<ObjectType>(self->region::observer_from_this());
949 else
950 return nullptr;
951}
952
953} // namespace lxgui::gui
954
955#endif
Stores a position for a UI region.
Holds a single color (float RGBA, 128 bits)
Definition gui_color.hpp:13
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.
region & operator=(const region &)=delete
Non-copiable.
manager & get_manager()
Returns this region's manager.
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).
utils::observer_ptr< const frame > get_parent() const
Returns this region's parent.
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.
const utils::observer_ptr< frame > & get_parent()
Returns this region's parent.
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_
virtual utils::owner_ptr< region > release_from_parent()
Removes this region from its parent and return an owning pointer.
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.
const addon * addon_
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.
bool is_region_type(const std::string &type_name) const
Checks if this region is of the provided type.
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.
region & operator=(region &&)=delete
Non-movable.
friend const ObjectType * down_cast(const region *self)
Obtain a pointer to a derived class.
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.
const manager & get_manager() const
Returns this region's manager.
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.
virtual void set_dimensions(const vector2f &dimensions)
Changes this region's absolute dimensions (in pixels).
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
bool is_loaded() const
Checks if this region has been fully loaded.
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.
std::string raw_name_
bool is_apparent_height_defined() const
Checks if this region's apparent height is defined.
utils::observer_ptr< frame_renderer > get_effective_frame_renderer()
Returns the renderer of this object or its parents, nullptr if none.
const std::array< std::optional< anchor >, 9 > & get_anchors() const
Returns all of this region's anchors.
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).
sol::state & get_lua_()
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.
utils::observer_ptr< ObjectType > observer_from(ObjectType *self)
Obtain an observer pointer from a raw pointer (typically 'this')
const ObjectType * down_cast(const region *self)
Obtain a pointer to a derived class.
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.
Definition gui_addon.hpp:12
Raw data of an anchor (value type)
Struct holding all the core information about a region necessary for its creation.