13 #include <type_traits> 14 #include <unordered_map> 17 #include <SFML/Graphics/Drawable.hpp> 18 #include <SFML/Graphics/Rect.hpp> 19 #include <SFML/System/Vector2.hpp> 21 #include <NasNas/core/data/Introspection.hpp> 36 explicit Layer(std::string name);
53 template <
typename T,
typename = std::enable_if_t<std::is_base_of_v<sf::Drawable, T>>>
54 void add(
const T& dr);
56 template <
typename T,
typename = std::enable_if_t<std::is_po
inter_v<T>>,
typename = std::enable_if_t<std::is_base_of_v<sf::Drawable, std::remove_po
inter_t<T>>>>
66 template <
typename T,
typename = std::enable_if_t<std::is_po
inter_v<T>>,
typename = std::enable_if_t<std::is_base_of_v<sf::Drawable, std::remove_po
inter_t<T>>>>
74 template <
typename T,
typename = std::enable_if_t<std::is_base_of_v<sf::Drawable, T>>>
75 void remove(
const T& dr);
77 template <
typename T,
typename = std::enable_if_t<std::is_po
inter_v<T>>,
typename = std::enable_if_t<std::is_base_of_v<sf::Drawable, std::remove_po
inter_t<T>>>>
85 auto allDrawables()
const ->
const std::vector<const sf::Drawable*>&;
87 auto getDrawablePosition(
const sf::Drawable* dr)
const -> sf::Vector2f;
88 auto getDrawableBounds(
const sf::Drawable* dr)
const -> sf::FloatRect;
102 auto getName()
const ->
const std::string&;
106 std::vector<const sf::Drawable*> m_drawables;
107 std::vector<std::unique_ptr<const sf::Drawable>> m_gc;
108 std::unordered_map<const sf::Drawable*, std::function<sf::Vector2f()>> m_position_getters;
109 std::unordered_map<const sf::Drawable*, std::function<sf::FloatRect()>> m_bounds_getters;
112 template <
typename T,
typename>
114 static constexpr
auto float_min = std::numeric_limits<float>::lowest();
115 static constexpr
auto float_max = std::numeric_limits<float>::max();
117 m_drawables.emplace_back(&dr);
118 if constexpr(introspect::has_getPosition_v<T>)
119 m_position_getters[&dr] = [&dr]() {
return dr.getPosition(); };
121 m_position_getters[&dr] = []() {
return sf::Vector2f{0, float_min}; };
123 if constexpr(introspect::has_getGlobalBounds_v<T>)
124 m_bounds_getters[&dr] = [&dr]() {
return dr.getGlobalBounds(); };
125 else if constexpr(introspect::has_getBounds_v<T>)
126 m_bounds_getters[&dr] = [&dr]() {
return dr.getBounds(); };
128 m_bounds_getters[&dr] = []() {
return sf::FloatRect(float_min/2.f, float_min/2.f, float_max, float_max); };
131 template <
typename T,
typename,
typename>
134 m_gc.emplace_back(dr);
137 template <
typename T,
typename,
typename>
142 template <
typename T,
typename>
144 auto it = std::find(m_drawables.begin(), m_drawables.end(), &dr);
145 if (it != m_drawables.end()) {
146 m_drawables.erase(it);
147 m_position_getters.erase(&dr);
148 m_bounds_getters.erase(&dr);
151 std::cout <<
"Warning : trying to remove a non existent drawable from Layer.\n";
154 template <
typename T,
typename,
typename>
158 std::remove_if(m_gc.begin(), m_gc.end(), [dr](
auto& uptr) {
return uptr.get() == dr; }),
void add(const T &dr)
Add a drawable to the Layer.
void clear()
Removes all the drawables.
void ySort()
Sorts Layer drawables by their Y coordinate.
auto allDrawables() const -> const std::vector< const sf::Drawable *> &
Get a vector of all drawables added to the Layer.
Layer(std::string name)
Construct a Layer object.
void remove(const T &dr)
Remove a drawable from the Layer.
auto getName() const -> const std::string &
Get the name of the Layer.
void addRaw(T dr)
Add a drawable to the layer The pointer is not managed by the Layer, and you have to delete it manual...