Loading...
Searching...
No Matches
Panel.h
Go to the documentation of this file.
1//
2// Created by Dave Smith on 4/1/26.
3//
4
5#ifndef MALENA_PANEL_H
6#define MALENA_PANEL_H
7
8#pragma once
9
14#include <functional>
15#include <unordered_map>
16
17namespace ml {
18
20
43 class MALENA_API Panel : public RectangleWith<PanelManifest>, public Themeable
44 {
45 // Maps each child pointer to its position relative to this panel's origin
46 std::unordered_map<Core*, sf::Vector2f> _relativePositions;
47
48 // Resize lambdas for fill-enabled children (empty function = no setSize)
49 std::unordered_map<Core*, std::function<void(sf::Vector2f)>> _fillChildren;
50
51 public:
53
66 template<typename T>
67 void addComponent(T& child, bool fill = false)
68 {
69 static_assert(std::is_base_of_v<Core, T>,
70 "Panel::addComponent requires a Core-derived type");
71
72 if (fill)
73 {
74 if constexpr (detail::has_setSize<T>::value)
75 {
76 // Fill child: snap to panel origin, fill its size
77 child.setPosition(getPosition());
78 child.setSize(getSize());
79 _relativePositions[&child] = {0.f, 0.f};
80 _fillChildren[&child] = [&child](sf::Vector2f sz){ child.setSize(sz); };
81 }
82 else
83 {
84 _relativePositions[&child] = child.getPosition() - getPosition();
85 }
86 }
87 else
88 {
89 _relativePositions[&child] = child.getPosition() - getPosition();
90 }
91
92 Core::addComponent(child);
93 }
94
118 void addUntracked(Core& child);
119
121 void addRef(Core& child);
122
126 bool removeComponent(Core& child);
127
131 void clear();
132
136 void setSize(const sf::Vector2f& size);
137
141 void setPosition(const sf::Vector2f& position) override;
142
143 void setVisible(bool visible) override;
144 void setActive(bool active) override;
145
146 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
147
148 protected:
149 void onThemeApplied(const Theme& theme) override;
150 };
151
152} // ml
153
154#endif //PANEL_H
Virtual base class for all Malena framework objects.
Definition Core.h:97
void addComponent(Core &child)
sf::Vector2f getPosition() const override
Base class for all Malena manifests.
Definition Manifest.h:51
void setActive(bool active) override
void addComponent(T &child, bool fill=false)
Add a child component to this panel.
Definition Panel.h:67
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw this graphic to an SFML render target.
void setSize(const sf::Vector2f &size)
Resize the panel and propagate to fill-enabled children.
void addUntracked(Core &child)
Add a child that will NOT follow the panel when it moves.
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
void clear()
Remove all child components from this panel.
void setPosition(const sf::Vector2f &position) override
Move the panel and shift all children by the same delta.
bool removeComponent(Core &child)
Remove a child component from this panel.
void setVisible(bool visible) override
void addRef(Core &child)
Alias for addUntracked(). Kept for compatibility.
Rectangle with an attached manifest.
Definition Rectangle.h:68
const sf::Vector2f & getSize() const
#define MALENA_API
Definition Component.h:22
Vector2< float > Vector2f
Universal design token set applied across all Themeable components.
Definition Theme.h:70