Continuation du TP : affichage fonctionnel

Texte pas beau mais fonctionnel
À faire : Interaction
This commit is contained in:
trotFunky 2019-05-16 13:07:29 +02:00
parent 71ce85cec4
commit 2bd65d0ce6
9 changed files with 283 additions and 76 deletions

View file

@ -7,11 +7,10 @@
namespace xmlParser
{
Circle::Circle() : DrawingElement(), r(2)
Circle::Circle(std::string label, float x, float y, float r, const sf::Color& color) : DrawingElement(std::move(label),x,y,color), r(2)
{
label = "Test Circle";
shape = new sf::CircleShape(r);
shape->setPosition(x,y);
shape->setPosition(x-r,y-r);
shape->setFillColor(color);
}
@ -22,7 +21,7 @@ namespace xmlParser
label = "Test Circle";
}
shape = new sf::CircleShape(r);
shape->setPosition(x,y);
shape->setPosition(x-r,y-r);
shape->setFillColor(color);
}
@ -31,14 +30,36 @@ namespace xmlParser
window.draw(*(dynamic_cast<sf::CircleShape*>(shape)));
}
int Circle::getR() const
float Circle::getR() const
{
return r;
}
void Circle::setR(int newR)
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);
}
}

View file

@ -8,6 +8,7 @@
#include <string>
#include <pugixml.hpp>
#include <SFML/Graphics.hpp>
#include <sstream>
#include "DrawingElement.h"
@ -16,16 +17,23 @@ namespace xmlParser
class Circle : public DrawingElement
{
public:
Circle();
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;
int getR() const;
void setR(int newR);
void setX(float newX) override;
void setY(float newY) override;
float getR() const;
void setR(float newR);
const std::string toString() const override;
private:
int r;
float r;
};
}
#endif //SNIPPETS_CIRCLE_H

View file

@ -1,3 +1,5 @@
#include <utility>
//
// Created by trotfunky on 15/05/19.
//
@ -7,42 +9,52 @@
namespace xmlParser
{
DrawingElement::DrawingElement() : x(0), y(1), label("Test DrawingElement"), color(sf::Color::Yellow), shape(nullptr)
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)
y(-node.attribute("y").as_int(1)), label(node.attribute("label").as_string("Test DrawingElement")), shape(nullptr)
{
setColor(node.attribute("color").as_string("Yellow"));
}
int xmlParser::DrawingElement::getX() const
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(int newX)
void xmlParser::DrawingElement::setX(float newX)
{
x = newX;
if(shape)
if(shape != nullptr)
{
shape->setPosition(x,y);
shape->setPosition(x,-y);
}
}
int xmlParser::DrawingElement::getY() const
float xmlParser::DrawingElement::getY() const
{
return y;
return -y;
}
void xmlParser::DrawingElement::setY(int newY)
void xmlParser::DrawingElement::setY(float newY)
{
y = newY;
if(shape)
if(shape != nullptr)
{
shape->setPosition(x,y);
shape->setPosition(x,-y);
}
}
@ -51,11 +63,16 @@ namespace xmlParser
return color;
}
const std::string DrawingElement::getStringColor() const
{
return(colorToStr(color));
}
void xmlParser::DrawingElement::setColor(const sf::Color& newColor)
{
color = newColor;
if(shape)
if(shape != nullptr)
{
shape->setFillColor(color);
}
@ -63,41 +80,20 @@ namespace xmlParser
void DrawingElement::setColor(const std::string& stringColor)
{
if(stringColor == "Black")
{
color = sf::Color::Black;
color = strToColor(stringColor);
}
else if(stringColor == "White")
const std::string DrawingElement::toString() const
{
color = sf::Color::White;
std::stringstream string;
string << label << " at x:" << getX() << " y:" << getY();
string << " | color:" << getStringColor();
return(string.str());
}
else if(stringColor == "Red")
std::ostream& operator<<(std::ostream& ostream, const DrawingElement& element)
{
color = sf::Color::Red;
}
else if(stringColor == "Green")
{
color = sf::Color::Green;
}
else if(stringColor == "Blue")
{
color = sf::Color::Blue;
}
else if(stringColor == "Yellow")
{
color = sf::Color::Yellow;
}
else if(stringColor == "Magenta")
{
color = sf::Color::Magenta;
}
else if(stringColor == "Cyan")
{
color = sf::Color::Cyan;
}
else
{
color = sf::Color::Green;
}
ostream << element.toString();
return(ostream);
}
}

View file

@ -5,9 +5,14 @@
#ifndef SNIPPETS_DRAWINGELEMENT_H
#define SNIPPETS_DRAWINGELEMENT_H
#include <string>
#include <sstream>
#include <ostream>
#include <SFML/Graphics.hpp>
#include <pugixml.hpp>
#include <string>
#include "xmlParser.h"
namespace xmlParser
{
@ -15,26 +20,34 @@ namespace xmlParser
{
public:
DrawingElement();
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;
int getX() const;
void setX(int newX);
virtual float getX() const;
virtual void setX(float newX);
int getY() const;
void setY(int newY);
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:
int x;
int y;
float x;
float y;
sf::Color color;

View file

@ -6,10 +6,8 @@
namespace xmlParser
{
Group::Group() : DrawingElement(), drawingElements({})
{
label = "Test Group";
}
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)
{
@ -18,13 +16,21 @@ namespace xmlParser
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)
if(newElement != nullptr)
{
drawingElements.insert(std::make_pair(newElement->label,newElement));
}
@ -57,9 +63,22 @@ namespace xmlParser
void Group::draw(sf::RenderWindow& window)
{
for(auto element : drawingElements)
for(const auto& element : drawingElements)
{
element.second->draw(window);
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());
}
}

View file

@ -9,6 +9,8 @@
#include <map>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include "Circle.h"
#include "DrawingElement.h"
@ -19,7 +21,9 @@ namespace xmlParser
class Group : public DrawingElement
{
public:
Group();
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*>&);
@ -29,6 +33,8 @@ namespace xmlParser
DrawingElement* getDrawingElement(const std::string&);
void draw(sf::RenderWindow&) override;
const std::string toString() const override;
private:
std::map<std::string,DrawingElement*> drawingElements;
};

View file

@ -3,6 +3,7 @@
//
#include <gtest/gtest.h>
#include <iostream>
#include "Circle.h"
#include "xmlParser.h"
@ -23,8 +24,8 @@ TEST(xmlParserInits,initEmptyCircle)
TEST(xmlParserInits,initEmptyCirclePugiXML)
{
xmlParser::Circle cercle = xmlParser::Circle();
pugi::xml_node node;
xmlParser::Circle cercle = xmlParser::Circle(node);
EXPECT_EQ(cercle.getX(),0);
EXPECT_EQ(cercle.getY(),1);
@ -128,6 +129,68 @@ TEST(parseXML,parseXMLNestGroup)
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);

View file

@ -7,5 +7,80 @@
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);
}
}
}

View file

@ -5,9 +5,15 @@
#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