cpp_drawing/shapes/group.hpp

53 lines
1.4 KiB
C++

#pragma once
#include "shape.hpp"
#include <memory>
#include <vector>
namespace shapes
{
/**
* @brief Group shape
*
* Represents a group of shapes that can be used as a canvas
*/
class Group : public Shape
{
public:
Group() = default;
Group(const Group&) = delete;
Group& operator=(const Group&) = delete;
Group(Group&&) = default;
Group& operator=(Group&&) = default;
template <typename TShape, typename... TArgs>
requires std::derived_from<TShape, Shape>
void AddShape(TArgs&&... args)
{
m_shapes.emplace_back(std::make_unique<TShape>(std::forward<TArgs>(args)...));
}
void Translate(const math::Vector& offset) override { ShapesCall<&Shape::Translate>(offset); }
void Rotate(const math::Vector& center, float angle) override { ShapesCall<&Shape::Rotate>(center, angle); }
void Scale(const math::Vector& center, float factor) override { ShapesCall<&Shape::Scale>(center, factor); }
void Draw(Renderer& renderer) const override { ShapesCall<&Shape::Draw>(renderer); }
~Group() override = default;
private:
// calls a member function on all shapes in the group
template <auto Fun, typename TSelf, typename... TArgs>
void ShapesCall(this TSelf&& self, TArgs&&... args)
{
for (auto& shape : self.m_shapes)
(shape.get()->*Fun)(std::forward<TArgs>(args)...);
}
private:
std::vector<std::unique_ptr<Shape>> m_shapes;
};
} // namespace shapes