Loading...
Searching...
No Matches
DrawableWrapper.h
Go to the documentation of this file.
1//
2// Created by Dave Smith on 3/14/26.
3//
4
5#ifndef DRAWABLEWRAPPER_H
6#define DRAWABLEWRAPPER_H
7
8namespace ml
9{
10 // ── Detection traits ─────────────────────────────────────────────────────
11
13
15 template<typename T, typename = void>
16 struct HasSetPosition : std::false_type {};
17 template<typename T>
18 struct HasSetPosition<T, std::void_t<
19 decltype(std::declval<T>().setPosition(std::declval<sf::Vector2f>()))
20 >> : std::true_type {};
21
23 template<typename T, typename = void>
24 struct HasGetPosition : std::false_type {};
25 template<typename T>
26 struct HasGetPosition<T, std::void_t<
27 decltype(std::declval<T>().getPosition())
28 >> : std::true_type {};
29
31 template<typename T, typename = void>
32 struct HasGetGlobalBounds : std::false_type {};
33 template<typename T>
34 struct HasGetGlobalBounds<T, std::void_t<
35 decltype(std::declval<T>().getGlobalBounds())
36 >> : std::true_type {};
37
39
40 // ── DrawableWrapper ───────────────────────────────────────────────────────
41
64 template<typename ENTITY>
65 class DrawableWrapper : public ENTITY, public ml::Core
66 {
67 public:
68 using ENTITY::ENTITY;
69
78 void setPosition(const sf::Vector2f& pos) override
79 {
80 if constexpr (HasSetPosition<ENTITY>::value)
81 ENTITY::setPosition(pos);
82 }
83
91 sf::Vector2f getPosition() const override
92 {
93 if constexpr (HasGetPosition<ENTITY>::value)
94 return ENTITY::getPosition();
95 return {};
96 }
97
109 {
110 if constexpr (HasGetGlobalBounds<ENTITY>::value)
111 return ENTITY::getGlobalBounds();
113 return sf::FloatRect({0.f, 0.f}, sf::Vector2f(size));
114 }
115 };
116
117} // namespace ml
118
119#endif // DRAWABLEWRAPPER_H
Virtual base class for all Malena framework objects.
Definition Core.h:67
Adapts any sf::Drawable into a first-class ml::Core object.
void setPosition(const sf::Vector2f &pos) override
Set the world-space position, delegating to ENTITY if supported.
sf::Vector2f getPosition() const override
Return the world-space position, delegating to ENTITY if supported.
sf::FloatRect getGlobalBounds() const override
Return the axis-aligned bounding box, delegating to ENTITY if supported.
Vector2u getSize() const override
sf::RenderWindow & getWindow()
Return the framework's shared sf::RenderWindow.
Definition Component.h:18
Rect< float > FloatRect
Vector2< float > Vector2f