tsp_cpp/snippets/Group.cpp
trotFunky 71ce85cec4 Continuing vector graphics exercise
New classes :
 - DrawingElement (Base class for every other element)
 - Group : Groups DrawingElements together

Modified Circle to work with DrawingElement
Corrected a *maybe* failed vector copy in Polynomial.tpp
2019-05-16 01:16:19 +02:00

65 lines
No EOL
1.9 KiB
C++

//
// Created by trotfunky on 15/05/19.
//
#include "Group.h"
namespace xmlParser
{
Group::Group() : DrawingElement(), drawingElements({})
{
label = "Test Group";
}
Group::Group(const pugi::xml_node& node) : DrawingElement(node)
{
if(label == "Test DrawingElement")
{
label = "Test Group";
}
pugi::xml_object_range circleChildren = node.children();
for(pugi::xml_node_iterator child : circleChildren)
{
pugi::xml_node& childNode = (*child);
DrawingElement* newElement = !strncmp(childNode.name(),"Circle",6) ? dynamic_cast<DrawingElement*>(new Circle(childNode)) :
!strncmp(childNode.name(),"Group",5) ? dynamic_cast<DrawingElement*>(new Group(childNode)) : nullptr;
if(newElement)
{
drawingElements.insert(std::make_pair(newElement->label,newElement));
}
}
}
Group::Group(const std::map<std::string, DrawingElement*>& elements) : Group()
{
std::transform(elements.begin(),elements.end(),std::inserter(drawingElements,drawingElements.begin()),
[](std::pair<std::string,DrawingElement*> pair)
{
return(pair);
});
}
bool Group::addDrawingElement(DrawingElement* drawingElement)
{
return(drawingElements.insert(std::make_pair(drawingElement->label,drawingElement)).second);
}
bool Group::removeDrawingElement(const std::string& label)
{
return(drawingElements.erase(label));
}
DrawingElement* Group::getDrawingElement(const std::string& label)
{
return(drawingElements.at(label));
}
void Group::draw(sf::RenderWindow& window)
{
for(auto element : drawingElements)
{
element.second->draw(window);
}
}
}