Réagencement pour le TP XML

This commit is contained in:
trotFunky 2019-05-16 15:19:41 +02:00
parent 2bd65d0ce6
commit 19f7a90a8c
12 changed files with 50 additions and 22 deletions

49
xmlParser/CMakeLists.txt Normal file
View file

@ -0,0 +1,49 @@
cmake_minimum_required(VERSION 3.14)
project(xmlParser)
set(CMAKE_CXX_STANDARD 17)
include_directories(.)
# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML COMPONENTS system window graphics network audio REQUIRED)
if(NOT SFML_FOUND)
message(FATAL_ERROR "SFML could not be found")
endif()
find_package(GTest REQUIRED)
if(GTest_FOUND)
enable_testing()
else()
message(FATAL_ERROR "GTest could not be found")
endif()
find_path(PugiXML_INCLUDE_DIR pugixml.hpp)
add_library(xmlParser SHARED
Circle.cpp
Circle.h
DrawingElement.cpp
DrawingElement.h
Group.cpp
Group.h
xmlParser.cpp
xmlParser.h)
target_link_libraries(xmlParser
sfml-window
sfml-graphics
pugixml)
add_executable(gTestXMLParser gTestXMLParser.cpp)
target_compile_options(gTestXMLParser PRIVATE -pthread)
target_link_libraries(gTestXMLParser
xmlParser
gtest
sfml-graphics
sfml-window
pugixml)
target_link_options(gTestXMLParser PRIVATE -pthread)

65
xmlParser/Circle.cpp Normal file
View file

@ -0,0 +1,65 @@
//
// Created by trotfunky on 14/05/19.
//
#include "Circle.h"
namespace xmlParser
{
Circle::Circle(std::string label, float x, float y, float r, const sf::Color& color) : DrawingElement(std::move(label),x,y,color), r(2)
{
shape = new sf::CircleShape(r);
shape->setPosition(x-r,y-r);
shape->setFillColor(color);
}
Circle::Circle(const pugi::xml_node& node) : DrawingElement(node), r(node.attribute("r").as_int(2))
{
if(label == "Test DrawingElement")
{
label = "Test Circle";
}
shape = new sf::CircleShape(r);
shape->setPosition(x-r,y-r);
shape->setFillColor(color);
}
void Circle::draw(sf::RenderWindow& window)
{
window.draw(*(dynamic_cast<sf::CircleShape*>(shape)));
}
float Circle::getR() const
{
return r;
}
void Circle::setR(float newR)
{
r = newR;
dynamic_cast<sf::CircleShape*>(shape)->setRadius(r);
shape->setPosition(x-r,y-r);
}
const std::string Circle::toString() const
{
std::stringstream string;
string << label << " at x:" << getX() << " y:" << getY() << " | r:" << getR();
string << " color:" << getStringColor();
return(string.str());
}
void Circle::setX(float newX)
{
DrawingElement::setX(newX);
shape->setPosition(x-r,y-r);
}
void Circle::setY(float newY)
{
DrawingElement::setY(newY);
shape->setPosition(x-r,y-r);
}
}

39
xmlParser/Circle.h Normal file
View file

@ -0,0 +1,39 @@
//
// Created by trotfunky on 07/05/19.
//
#ifndef SNIPPETS_CIRCLE_H
#define SNIPPETS_CIRCLE_H
#include <string>
#include <pugixml.hpp>
#include <SFML/Graphics.hpp>
#include <sstream>
#include "DrawingElement.h"
namespace xmlParser
{
class Circle : public DrawingElement
{
public:
explicit Circle(std::string label = "Test Circle", float x = 0, float y = 1, float r = 2,
const sf::Color& color = sf::Color::Yellow);
explicit Circle(const pugi::xml_node&);
void draw(sf::RenderWindow&) override;
void setX(float newX) override;
void setY(float newY) override;
float getR() const;
void setR(float newR);
const std::string toString() const override;
private:
float r;
};
}
#endif //SNIPPETS_CIRCLE_H

View file

@ -0,0 +1,99 @@
#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() const
{
std::stringstream string;
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();
return(ostream);
}
}

View file

@ -0,0 +1,58 @@
//
// Created by trotfunky on 15/05/19.
//
#ifndef SNIPPETS_DRAWINGELEMENT_H
#define SNIPPETS_DRAWINGELEMENT_H
#include <string>
#include <sstream>
#include <ostream>
#include <SFML/Graphics.hpp>
#include <pugixml.hpp>
#include "xmlParser.h"
namespace xmlParser
{
class DrawingElement
{
public:
explicit DrawingElement(std::string label = "Test DrawingElement", float x = 0, float y = 1,
const sf::Color& = sf::Color::Yellow);
explicit DrawingElement(const pugi::xml_node&);
virtual void draw(sf::RenderWindow&) = 0;
void draw(sf::RenderWindow&, float referenceX, float referenceY);
std::string label;
virtual float getX() const;
virtual void setX(float newX);
virtual float getY() const;
virtual void setY(float newY);
const sf::Color& getColor() const;
const std::string getStringColor() const;
void setColor(const sf::Color& newColor);
void setColor(const std::string& stringColor);
virtual const std::string toString() const;
friend std::ostream& operator<<(std::ostream&,const DrawingElement&);
protected:
float x;
float y;
sf::Color color;
sf::Shape* shape;
};
}
#endif //SNIPPETS_DRAWINGELEMENT_H

84
xmlParser/Group.cpp Normal file
View file

@ -0,0 +1,84 @@
//
// Created by trotfunky on 15/05/19.
//
#include "Group.h"
namespace xmlParser
{
Group::Group(std::string label, float x, float y, const sf::Color& color) : DrawingElement(std::move(label),x,y,color), drawingElements({})
{}
Group::Group(const pugi::xml_node& node) : DrawingElement(node)
{
if(label == "Test DrawingElement")
{
label = "Test Group";
}
if(!strncmp(node.name(),"Drawing",7))
{
x = 0;
y = 0;
label = "Drawing";
color = sf::Color::Transparent;
}
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 != nullptr)
{
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(const auto& element : drawingElements)
{
element.second->draw(window,x,y);
}
}
const std::string Group::toString() const
{
std::stringstream string;
string << label << " at x:" << getX() << " y:" << getY();
string << " color:" << getStringColor() << std::endl;
for(const auto& element : drawingElements)
{
string << "\t" << *(element.second) << std::endl;
}
return(string.str());
}
}

44
xmlParser/Group.h Normal file
View file

@ -0,0 +1,44 @@
//
// Created by trotfunky on 15/05/19.
//
#ifndef SNIPPETS_GROUP_H
#define SNIPPETS_GROUP_H
#include <pugixml.hpp>
#include <map>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include "Circle.h"
#include "DrawingElement.h"
namespace xmlParser
{
class Group : public DrawingElement
{
public:
explicit Group(std::string label = "Test Group", float x = 0, float y = 1,
const sf::Color& color = sf::Color::Transparent);
explicit Group(const pugi::xml_node&);
explicit Group(const std::map<std::string, DrawingElement*>&);
bool addDrawingElement(DrawingElement* drawingElement);
bool removeDrawingElement(const std::string& label);
DrawingElement* getDrawingElement(const std::string&);
void draw(sf::RenderWindow&) override;
const std::string toString() const override;
private:
std::map<std::string,DrawingElement*> drawingElements;
};
}
#endif //SNIPPETS_GROUP_H

View file

@ -0,0 +1,198 @@
//
// Created by trotfunky on 07/05/19.
//
#include <gtest/gtest.h>
#include <iostream>
#include "Circle.h"
#include "xmlParser.h"
#include "SFML/Graphics.hpp"
#include "Group.h"
TEST(xmlParserInits,initEmptyCircle)
{
xmlParser::Circle cercle = xmlParser::Circle();
EXPECT_EQ(cercle.getX(),0);
EXPECT_EQ(cercle.getY(),1);
EXPECT_EQ(cercle.getR(),2);
EXPECT_EQ(cercle.label,"Test Circle");
EXPECT_EQ(cercle.getColor(),sf::Color::Yellow);
}
TEST(xmlParserInits,initEmptyCirclePugiXML)
{
pugi::xml_node node;
xmlParser::Circle cercle = xmlParser::Circle(node);
EXPECT_EQ(cercle.getX(),0);
EXPECT_EQ(cercle.getY(),1);
EXPECT_EQ(cercle.getR(),2);
EXPECT_EQ(cercle.label,"Test Circle");
EXPECT_EQ(cercle.getColor(),sf::Color::Yellow);
}
TEST(xmlParserInits,initEmptyGroup)
{
xmlParser::Group group = xmlParser::Group();
EXPECT_EQ(group.getX(),0);
EXPECT_EQ(group.getY(),1);
EXPECT_EQ(group.label,"Test Group");
}
TEST(xmlParserInits,initEmptyGroupPugiXML)
{
pugi::xml_node node;
xmlParser::Group group = xmlParser::Group(node);
EXPECT_EQ(group.getX(),0);
EXPECT_EQ(group.getY(),1);
EXPECT_EQ(group.label,"Test Group");
}
TEST(parseXML,parseXMLCircle)
{
std::string xml = "<?xml version = \"1.0\"?>\n"
"<Circle label=\"testCircle\" x=\"0\" y=\"1\" r=\"2\" color=\"Black\"/>";
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_string(xml.c_str());
ASSERT_NE(0,result);
pugi::xml_node node = doc.child("Circle");
xmlParser::Circle cercle = xmlParser::Circle(node);
EXPECT_EQ(cercle.getX(),0);
EXPECT_EQ(cercle.getY(),1);
EXPECT_EQ(cercle.getR(),2);
EXPECT_EQ(cercle.label,"testCircle");
EXPECT_EQ(cercle.getColor(),sf::Color::Black);
}
TEST(parseXML,parseXMLGroup)
{
std::string xml = "<?xml version = \"1.0\"?>\n"
"<Group label=\"testGroup\" x=\"0\" y=\"1\">\n"
" <Circle label=\"testCircle1\" x=\"2\" y=\"3\" r=\"4\" color=\"Black\"/>\n"
" <Circle label=\"testCircle2\" x=\"5\" y=\"6\" r=\"7\" color=\"Black\"/>\n"
"</Group>";
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_string(xml.c_str());
ASSERT_NE(0,result);
pugi::xml_node node = doc.child("Group");
xmlParser::Group group = xmlParser::Group(node);
EXPECT_EQ(group.getX(),0);
EXPECT_EQ(group.getY(),1);
EXPECT_EQ(group.label,"testGroup");
ASSERT_TRUE(group.getDrawingElement("testCircle1"));
xmlParser::Circle* testCircle1 = dynamic_cast<xmlParser::Circle*>(group.getDrawingElement("testCircle1"));
EXPECT_EQ(testCircle1->getX(),2);
EXPECT_EQ(testCircle1->getY(),3);
EXPECT_EQ(testCircle1->getR(),4);
EXPECT_EQ(testCircle1->getColor(),sf::Color::Black);
ASSERT_TRUE(group.getDrawingElement("testCircle2"));
xmlParser::Circle* testCircle2 = dynamic_cast<xmlParser::Circle*>(group.getDrawingElement("testCircle2"));
EXPECT_EQ(testCircle2->getX(),5);
EXPECT_EQ(testCircle2->getY(),6);
EXPECT_EQ(testCircle2->getR(),7);
EXPECT_EQ(testCircle2->getColor(),sf::Color::Black);
}
TEST(parseXML,parseXMLNestGroup)
{
std::string xml = "<?xml version = \"1.0\"?>\n"
"<Group label=\"testGroup1\" x=\"0\" y=\"1\">\n"
" <Circle label=\"testCircle\" x=\"2\" y=\"3\" r=\"4\" color=\"Black\"/>\n"
" <Group label=\"testGroup2\" x=\"5\" y=\"6\"/>\n"
"</Group>";
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_string(xml.c_str());
ASSERT_NE(0,result);
pugi::xml_node node = doc.child("Group");
xmlParser::Group group = xmlParser::Group(node);
ASSERT_TRUE(group.getDrawingElement("testGroup2"));
xmlParser::Group* testGroup2 = dynamic_cast<xmlParser::Group*>(group.getDrawingElement("testGroup2"));
EXPECT_EQ(testGroup2->getX(),5);
EXPECT_EQ(testGroup2->getY(),6);
EXPECT_EQ(testGroup2->label,"testGroup2");
}
TEST(drawFace,drawFace)
{
std::string xml = "<?xml version=\"1.0\"?>\n"
"<!-- Dans cette version de format, les coordonnees (x,y) sont exprimees dans un -->\n"
"<!-- repere dont le centre est au centre de l'ecran, les valeurs positives de x -->\n"
"<!-- etant vers la droite, les valeurs positives de y etant vers le haut. --> \n"
"<Drawing>\n"
" <!-- Le noeud suivant cree un cercle dont le centre est en (0,0), -->\n"
" <!-- de rayon 200, de couleur noir, qui a pour etiquette -->\n"
" <!-- \"contourVisage\" -->\n"
" <Circle label=\"contourVisage\" x=\"0\" y=\"0\" r=\"200\" color=\"Black\"/>\n"
" <Circle label=\"nez\" x=\"0\" y=\"0\" r=\"20\" color=\"Red\"/>\n"
" <!-- Le noeud suivant cree un groupe \"oreilles\" positionne en (0,210) -->\n"
" <Group label=\"oreilles\" x=\"0\" y=\"210\">\n"
"\t<Group label=\"oreille\" x=\"-210\" y=\"0\">\n"
"\t <Circle label=\"c1\" x=\"0\" y=\"0\" r=\"100\" color=\"Black\"/>\n"
"\t <Circle label=\"c2\" x=\"0\" y=\"0\" r=\"70\" color=\"Magenta\"/>\n"
"\t</Group>\n"
"\t<Group label=\"oreille2\" x=\"210\" y=\"0\">\n"
"\t <Circle label=\"c1\" x=\"0\" y=\"0\" r=\"100\" color=\"Black\"/>\n"
"\t <Circle label=\"c2\" x=\"0\" y=\"0\" r=\"70\" color=\"Magenta\"/>\n"
"\t</Group>\n"
" </Group>\n"
"</Drawing>";
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_string(xml.c_str());
ASSERT_NE(result,0);
xmlParser::Group drawing = xmlParser::Group(doc.child("Drawing"));
drawing.setX(1280/2.0);
drawing.setY(-1024/2.0);
std::cout << drawing << std::endl;
sf::RenderWindow renderWindow(sf::VideoMode(1280,1024),"DA FACE");
sf::RectangleShape rectangleGhost = sf::RectangleShape(sf::Vector2f(660,1024));
rectangleGhost.setPosition(310,202);
renderWindow.draw(rectangleGhost);
sf::CircleShape circleGhost = sf::CircleShape(330,100);
circleGhost.setPosition(310,202-330);
renderWindow.draw(circleGhost);
drawing.draw(renderWindow);
renderWindow.display();
while (renderWindow.isOpen())
{
sf::Event event;
while (renderWindow.pollEvent(event))
{
if (event.type == sf::Event::Closed)
renderWindow.close();
if (event.type == sf::Event::KeyPressed)
{
renderWindow.close();
}
}
}
}
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}

86
xmlParser/xmlParser.cpp Normal file
View file

@ -0,0 +1,86 @@
//
// Created by trotfunky on 07/05/19.
//
#include <pugixml.hpp>
#include "xmlParser.h"
namespace xmlParser
{
const std::string colorToStr(const sf::Color& color)
{
if(color == sf::Color::Black)
{
return("Black");
}
else if(color == sf::Color::White)
{
return("White");
}
else if(color == sf::Color::Red)
{
return("Red");
}
else if(color == sf::Color::Green)
{
return("Green");
}
else if(color == sf::Color::Blue)
{
return("Blue");
}
else if(color == sf::Color::Yellow)
{
return("Yellow");
}
else if(color == sf::Color::Magenta)
{
return("Magenta");
}
else if(color == sf::Color::Cyan)
{
return("Cyan");
}
return("Transparent");
}
const sf::Color& strToColor(const std::string& stringColor)
{
if(stringColor == "Black")
{
return(sf::Color::Black);
}
else if(stringColor == "White")
{
return(sf::Color::White);
}
else if(stringColor == "Red")
{
return(sf::Color::Red);
}
else if(stringColor == "Green")
{
return(sf::Color::Green);
}
else if(stringColor == "Blue")
{
return(sf::Color::Blue);
}
else if(stringColor == "Yellow")
{
return(sf::Color::Yellow);
}
else if(stringColor == "Magenta")
{
return(sf::Color::Magenta);
}
else if(stringColor == "Cyan")
{
return(sf::Color::Cyan);
}
else
{
return(sf::Color::Green);
}
}
}

19
xmlParser/xmlParser.h Normal file
View file

@ -0,0 +1,19 @@
//
// Created by trotfunky on 07/05/19.
//
#ifndef SNIPPETS_XMLPARSER_H
#define SNIPPETS_XMLPARSER_H
#include <string>
#include <SFML/Graphics.hpp>
namespace xmlParser
{
const std::string colorToStr(const sf::Color&);
const sf::Color& strToColor(const std::string&);
}
#endif //SNIPPETS_XMLPARSER_H