tsp_cpp/xmlParser/DrawingElement.cpp

104 lines
No EOL
2.3 KiB
C++

#include <utility>
//
// Created by trotfunky on 15/05/19.
//
#include "DrawingElement.h"
namespace xmlParser
{
DrawingElement::DrawingElement(std::string label, float x, float y, const sf::Color& color) : x(x), y(-y), label(std::move(label)),
color(color), shape(nullptr)
{}
DrawingElement::DrawingElement(const pugi::xml_node& node) : x(node.attribute("x").as_int(0)),
y(-node.attribute("y").as_int(1)), label(node.attribute("label").as_string("Test DrawingElement")), shape(nullptr)
{
setColor(node.attribute("color").as_string("Yellow"));
}
void DrawingElement::draw(sf::RenderWindow& window, float referenceX, float referenceY)
{
setX(x+referenceX);
setY(y-referenceY);
draw(window);
setX(x-referenceX);
setY(y+referenceY);
}
float xmlParser::DrawingElement::getX() const
{
return x;
}
void xmlParser::DrawingElement::setX(float newX)
{
x = newX;
if(shape != nullptr)
{
shape->setPosition(x,-y);
}
}
float xmlParser::DrawingElement::getY() const
{
return -y;
}
void xmlParser::DrawingElement::setY(float newY)
{
y = newY;
if(shape != nullptr)
{
shape->setPosition(x,-y);
}
}
const sf::Color& xmlParser::DrawingElement::getColor() const
{
return color;
}
const std::string DrawingElement::getStringColor() const
{
return(colorToStr(color));
}
void xmlParser::DrawingElement::setColor(const sf::Color& newColor)
{
color = newColor;
if(shape != nullptr)
{
shape->setFillColor(color);
}
}
void DrawingElement::setColor(const std::string& stringColor)
{
color = strToColor(stringColor);
}
const std::string DrawingElement::toString(int indent) const
{
std::stringstream string;
for(int i = 0;i<indent;i++)
{
string << "\t";
}
string << label << " at x:" << getX() << " y:" << getY();
string << " | color:" << getStringColor();
return(string.str());
}
std::ostream& operator<<(std::ostream& ostream, const DrawingElement& element)
{
ostream << element.toString(0);
return(ostream);
}
}