2019-05-16 01:16:19 +02:00
|
|
|
//
|
|
|
|
// Created by trotfunky on 15/05/19.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Group.h"
|
|
|
|
|
|
|
|
namespace xmlParser
|
|
|
|
{
|
2019-05-22 18:47:48 +02:00
|
|
|
Group::Group(std::string label, float x, float y, const sf::Color& color) : DrawingElement(std::move(label),x,y,color), drawingElements()
|
2019-05-16 13:07:29 +02:00
|
|
|
{}
|
2019-05-16 01:16:19 +02:00
|
|
|
|
|
|
|
Group::Group(const pugi::xml_node& node) : DrawingElement(node)
|
|
|
|
{
|
|
|
|
if(label == "Test DrawingElement")
|
|
|
|
{
|
|
|
|
label = "Test Group";
|
|
|
|
}
|
|
|
|
|
2019-05-16 13:07:29 +02:00
|
|
|
if(!strncmp(node.name(),"Drawing",7))
|
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
label = "Drawing";
|
|
|
|
color = sf::Color::Transparent;
|
|
|
|
}
|
|
|
|
|
2019-05-16 01:16:19 +02:00
|
|
|
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;
|
2019-05-16 13:07:29 +02:00
|
|
|
if(newElement != nullptr)
|
2019-05-16 01:16:19 +02:00
|
|
|
{
|
2019-05-22 18:47:48 +02:00
|
|
|
drawingElements.emplace(std::make_pair(newElement->label,std::move(std::unique_ptr<DrawingElement>(newElement))));
|
2019-05-16 01:16:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2019-05-22 18:47:48 +02:00
|
|
|
return(std::make_pair(pair.first,std::move(std::unique_ptr<DrawingElement>(pair.second))));
|
2019-05-16 01:16:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Group::addDrawingElement(DrawingElement* drawingElement)
|
|
|
|
{
|
2019-05-22 18:47:48 +02:00
|
|
|
return(drawingElements.insert(std::make_pair(drawingElement->label,std::move(std::unique_ptr<DrawingElement>(drawingElement)))).second);
|
2019-05-16 01:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Group::removeDrawingElement(const std::string& label)
|
|
|
|
{
|
|
|
|
return(drawingElements.erase(label));
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawingElement* Group::getDrawingElement(const std::string& label)
|
|
|
|
{
|
2019-05-22 18:47:48 +02:00
|
|
|
return(drawingElements.at(label).get());
|
2019-05-16 01:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Group::draw(sf::RenderWindow& window)
|
|
|
|
{
|
2019-05-16 13:07:29 +02:00
|
|
|
for(const auto& element : drawingElements)
|
|
|
|
{
|
|
|
|
element.second->draw(window,x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 12:48:08 +02:00
|
|
|
const std::string Group::toString(int indent) const
|
2019-05-16 13:07:29 +02:00
|
|
|
{
|
|
|
|
std::stringstream string;
|
2019-05-21 12:48:08 +02:00
|
|
|
for(int i = 0;i<indent;i++)
|
|
|
|
{
|
|
|
|
string << "\t";
|
|
|
|
}
|
|
|
|
|
2019-05-16 13:07:29 +02:00
|
|
|
string << label << " at x:" << getX() << " y:" << getY();
|
|
|
|
string << " color:" << getStringColor() << std::endl;
|
|
|
|
|
|
|
|
for(const auto& element : drawingElements)
|
2019-05-16 01:16:19 +02:00
|
|
|
{
|
2019-05-21 12:48:08 +02:00
|
|
|
string << element.second->toString(indent+1) << std::endl;
|
2019-05-16 01:16:19 +02:00
|
|
|
}
|
2019-05-16 13:07:29 +02:00
|
|
|
return(string.str());
|
2019-05-16 01:16:19 +02:00
|
|
|
}
|
|
|
|
}
|