From 2bd65d0ce6cebca1ec9c519df4d23cc92b391ed3 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Thu, 16 May 2019 13:07:29 +0200 Subject: [PATCH] =?UTF-8?q?Continuation=20du=20TP=20:=20affichage=20foncti?= =?UTF-8?q?onnel=20Texte=20pas=20beau=20mais=20fonctionnel=20=C3=80=20fair?= =?UTF-8?q?e=20:=20Interaction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- snippets/Circle.cpp | 33 ++++++++++--- snippets/Circle.h | 16 +++++-- snippets/DrawingElement.cpp | 92 ++++++++++++++++++------------------- snippets/DrawingElement.h | 29 ++++++++---- snippets/Group.cpp | 33 ++++++++++--- snippets/Group.h | 8 +++- snippets/gTestXMLParser.cpp | 65 +++++++++++++++++++++++++- snippets/xmlParser.cpp | 75 ++++++++++++++++++++++++++++++ snippets/xmlParser.h | 8 +++- 9 files changed, 283 insertions(+), 76 deletions(-) diff --git a/snippets/Circle.cpp b/snippets/Circle.cpp index a8d60ee..d320994 100644 --- a/snippets/Circle.cpp +++ b/snippets/Circle.cpp @@ -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(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(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); } } \ No newline at end of file diff --git a/snippets/Circle.h b/snippets/Circle.h index 4b362ff..2211297 100644 --- a/snippets/Circle.h +++ b/snippets/Circle.h @@ -8,6 +8,7 @@ #include #include #include +#include #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 diff --git a/snippets/DrawingElement.cpp b/snippets/DrawingElement.cpp index 1b6bccc..fd3476c 100644 --- a/snippets/DrawingElement.cpp +++ b/snippets/DrawingElement.cpp @@ -1,3 +1,5 @@ +#include + // // 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; - } - else if(stringColor == "White") - { - color = sf::Color::White; - } - else if(stringColor == "Red") - { - 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; - } + 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); } } \ No newline at end of file diff --git a/snippets/DrawingElement.h b/snippets/DrawingElement.h index 5a4bfa8..acb5356 100644 --- a/snippets/DrawingElement.h +++ b/snippets/DrawingElement.h @@ -5,9 +5,14 @@ #ifndef SNIPPETS_DRAWINGELEMENT_H #define SNIPPETS_DRAWINGELEMENT_H +#include +#include +#include + #include #include -#include + +#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; diff --git a/snippets/Group.cpp b/snippets/Group.cpp index aec9d44..9ac432f 100644 --- a/snippets/Group.cpp +++ b/snippets/Group.cpp @@ -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(new Circle(childNode)) : !strncmp(childNode.name(),"Group",5) ? dynamic_cast(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()); + } } \ No newline at end of file diff --git a/snippets/Group.h b/snippets/Group.h index b5d75d2..99c7a42 100644 --- a/snippets/Group.h +++ b/snippets/Group.h @@ -9,6 +9,8 @@ #include #include #include +#include +#include #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&); @@ -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 drawingElements; }; diff --git a/snippets/gTestXMLParser.cpp b/snippets/gTestXMLParser.cpp index ecaa347..23534d3 100644 --- a/snippets/gTestXMLParser.cpp +++ b/snippets/gTestXMLParser.cpp @@ -3,6 +3,7 @@ // #include +#include #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 = "\n" + "\n" + "\n" + " \n" + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "\t\n" + "\t \n" + "\t \n" + "\t\n" + "\t\n" + "\t \n" + "\t \n" + "\t\n" + " \n" + ""; + + 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); diff --git a/snippets/xmlParser.cpp b/snippets/xmlParser.cpp index e078e73..da8f4ce 100644 --- a/snippets/xmlParser.cpp +++ b/snippets/xmlParser.cpp @@ -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); + } + } } \ No newline at end of file diff --git a/snippets/xmlParser.h b/snippets/xmlParser.h index 5882ba4..abd3131 100644 --- a/snippets/xmlParser.h +++ b/snippets/xmlParser.h @@ -5,9 +5,15 @@ #ifndef SNIPPETS_XMLPARSER_H #define SNIPPETS_XMLPARSER_H +#include + +#include + + namespace xmlParser { - + const std::string colorToStr(const sf::Color&); + const sf::Color& strToColor(const std::string&); } #endif //SNIPPETS_XMLPARSER_H