From 178bf45f08ffbab38e543281ef973d15a77288ca Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Tue, 31 Mar 2020 21:22:36 +0200 Subject: [PATCH] XML visitor for session saving (with toolkit for GLM saving to XML) --- CMakeLists.txt | 7 +- SessionVisitor.cpp | 3 +- SessionVisitor.h | 1 + Visitor.cpp | 207 -------------------------------------------- Visitor.h | 2 - main.cpp | 2 +- tinyxml2Toolkit.cpp | 7 +- tinyxml2Toolkit.h | 6 +- 8 files changed, 15 insertions(+), 220 deletions(-) delete mode 100644 Visitor.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c65dc1c..098da87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -219,15 +219,16 @@ set(VMIX_SRCS ImageShader.cpp Scene.cpp Primitives.cpp - Visitor.cpp + SessionVisitor.cpp Settings.cpp Resource.cpp FileDialog.cpp - ImGuiToolkit.cpp - GstToolkit.cpp MediaPlayer.cpp RenderingManager.cpp UserInterfaceManager.cpp + ImGuiToolkit.cpp + GstToolkit.cpp + tinyxml2Toolkit.cpp ) set(VMIX_RSC_FILES diff --git a/SessionVisitor.cpp b/SessionVisitor.cpp index 691eebb..2d0cf6e 100644 --- a/SessionVisitor.cpp +++ b/SessionVisitor.cpp @@ -1,4 +1,5 @@ -#include "Visitor.h" +#include "SessionVisitor.h" + #include "Log.h" #include "Scene.h" #include "Primitives.h" diff --git a/SessionVisitor.h b/SessionVisitor.h index ce7886b..40149c5 100644 --- a/SessionVisitor.h +++ b/SessionVisitor.h @@ -2,6 +2,7 @@ #define XMLVISITOR_H #include "Visitor.h" +#include "tinyxml2Toolkit.h" class SessionVisitor : public Visitor { diff --git a/Visitor.cpp b/Visitor.cpp deleted file mode 100644 index de29253..0000000 --- a/Visitor.cpp +++ /dev/null @@ -1,207 +0,0 @@ -#include "Visitor.h" -#include "Log.h" -#include "Scene.h" -#include "Primitives.h" -#include "MediaPlayer.h" -#include "GstToolkit.h" - -#include - -#include -#include -#include -#include -#include -#define GLM_ENABLE_EXPERIMENTAL -#include - -#include -using namespace tinyxml2; - -#ifndef XMLCheckResult - #define XMLCheckResult(a_eResult) if (a_eResult != XML_SUCCESS) { Log::Warning("XML error %i\n", a_eResult); return; } -#endif - -SessionVisitor::SessionVisitor(std::string filename) : filename_(filename) -{ - xmlDoc_ = new XMLDocument; - xmlCurrent_ = nullptr; -} - -void SessionVisitor::visit(Group &n) -{ - XMLElement *xmlParent = xmlCurrent_; - xmlCurrent_ = xmlDoc_->NewElement("Group"); - visit( (Node&) n); - - for (int node = 0; node < n.numChildren(); ++node) - { - Node *child = n.getChild(node); - - Group *g = dynamic_cast(child); - if (g != nullptr) { - g->accept(*this); - continue; - } - - TexturedRectangle *tr = dynamic_cast(child); - if (tr != nullptr) { - tr->accept(*this); - continue; - } - - MediaRectangle *mr = dynamic_cast(child); - if (mr != nullptr) { - mr->accept(*this); - continue; - } - - LineStrip *ls = dynamic_cast(child); - if (ls != nullptr) { - ls->accept(*this); - continue; - } - - Primitive *p = dynamic_cast(child); - if (p != nullptr) { - p->accept(*this); - continue; - } - - } - - // recursive - xmlParent->InsertEndChild(xmlCurrent_); - xmlCurrent_ = xmlParent; - -} - - -XMLElement *XMLElementGLM(XMLDocument *doc, glm::vec3 vector) -{ - XMLElement *newelement = doc->NewElement( "vec3" ); - newelement->SetAttribute("x", vector[0]); - newelement->SetAttribute("y", vector[1]); - newelement->SetAttribute("z", vector[2]); - return newelement; -} - -XMLElement *XMLElementGLM(XMLDocument *doc, glm::vec4 vector) -{ - XMLElement *newelement = doc->NewElement( "vec4" ); - newelement->SetAttribute("x", vector[0]); - newelement->SetAttribute("y", vector[1]); - newelement->SetAttribute("z", vector[2]); - newelement->SetAttribute("w", vector[3]); - return newelement; -} - -XMLElement *XMLElementGLM(XMLDocument *doc, glm::mat4 matrix) -{ - XMLElement *newelement = doc->NewElement( "mat4" ); - for (int r = 0 ; r < 4 ; r ++) - { - glm::vec4 row = glm::row(matrix, r); - XMLElement *rowxml = XMLElementGLM(doc, row); - rowxml->SetAttribute("row", r); - newelement->InsertEndChild(rowxml); - } - return newelement; -} - -void SessionVisitor::visit(Node &n) -{ - XMLElement *transform = XMLElementGLM(xmlDoc_, n.transform_); - xmlCurrent_->InsertEndChild(transform); -} - -void SessionVisitor::visit(Primitive &n) -{ - visit( (Node&) n); -} - -void SessionVisitor::visit(TexturedRectangle &n) -{ - // type specific - XMLElement *xmlParent = xmlCurrent_; - xmlCurrent_ = xmlDoc_->NewElement("TexturedRectangle"); - - XMLElement *image = xmlDoc_->NewElement("filename"); - xmlCurrent_->InsertEndChild(image); - XMLText *filename = xmlDoc_->NewText( n.getResourcePath().c_str() ); - image->InsertEndChild(filename); - - // inherited from Primitive - visit( (Primitive&) n); - - // recursive - xmlParent->InsertEndChild(xmlCurrent_); - xmlCurrent_ = xmlParent; -} - -void SessionVisitor::visit(MediaRectangle &n) -{ - // type specific - XMLElement *xmlParent = xmlCurrent_; - xmlCurrent_ = xmlDoc_->NewElement("MediaRectangle"); - - XMLElement *media = xmlDoc_->NewElement("filename"); - xmlCurrent_->InsertEndChild(media); - XMLText *filename = xmlDoc_->NewText( n.getMediaPath().c_str() ); - media->InsertEndChild(filename); - - // TODO : visit MediaPlayer - - // inherited from Primitive - visit( (Primitive&) n); - - // recursive - xmlParent->InsertEndChild(xmlCurrent_); - xmlCurrent_ = xmlParent; -} - -void SessionVisitor::visit(LineStrip &n) -{ - // type specific - XMLElement *xmlParent = xmlCurrent_; - xmlCurrent_ = xmlDoc_->NewElement("LineStrip"); - - XMLElement *color = XMLElementGLM(xmlDoc_, n.getColor()); - color->SetAttribute("type", "RGBA"); - xmlCurrent_->InsertEndChild(color); - - std::vector points = n.getPoints(); - for(size_t i = 0; i < points.size(); ++i) - { - XMLElement *p = XMLElementGLM(xmlDoc_, points[i]); - p->SetAttribute("point", (int) i); - xmlCurrent_->InsertEndChild(p); - } - - // inherited from Primitive - visit( (Primitive&) n); - - // recursive - xmlParent->InsertEndChild(xmlCurrent_); - xmlCurrent_ = xmlParent; -} - -void SessionVisitor::visit(Scene &n) -{ - XMLDeclaration *pDec = xmlDoc_->NewDeclaration(); - xmlDoc_->InsertFirstChild(pDec); - - XMLElement *pRoot = xmlDoc_->NewElement("Session"); - xmlDoc_->InsertEndChild(pRoot); - - std::string s = "Saved on " + GstToolkit::date_time_string(); - XMLComment *pComment = xmlDoc_->NewComment(s.c_str()); - pRoot->InsertEndChild(pComment); - - // save scene - xmlCurrent_ = pRoot; - n.getRoot()->accept(*this); - - XMLError eResult = xmlDoc_->SaveFile(filename_.c_str()); - XMLCheckResult(eResult); -} diff --git a/Visitor.h b/Visitor.h index 49bb0ec..f876b0b 100644 --- a/Visitor.h +++ b/Visitor.h @@ -3,8 +3,6 @@ #include -#include "tinyxml2Toolkit.h" - // Forward declare different kind of Node class Node; class Primitive; diff --git a/main.cpp b/main.cpp index 0398486..665ac62 100644 --- a/main.cpp +++ b/main.cpp @@ -35,7 +35,7 @@ #include "MediaPlayer.h" #include "Scene.h" #include "Primitives.h" -#include "Visitor.h" +#include "SessionVisitor.h" #define PI 3.14159265358979323846 diff --git a/tinyxml2Toolkit.cpp b/tinyxml2Toolkit.cpp index 7453d7c..fbc1ca8 100644 --- a/tinyxml2Toolkit.cpp +++ b/tinyxml2Toolkit.cpp @@ -3,12 +3,13 @@ #include using namespace tinyxml2; +#include #include #include #include -XMLElement *XMLElementGLM(XMLDocument *doc, glm::vec3 vector) +XMLElement *tinyxml2::XMLElementGLM(XMLDocument *doc, glm::vec3 vector) { XMLElement *newelement = doc->NewElement( "vec3" ); newelement->SetAttribute("x", vector[0]); @@ -17,7 +18,7 @@ XMLElement *XMLElementGLM(XMLDocument *doc, glm::vec3 vector) return newelement; } -XMLElement *XMLElementGLM(XMLDocument *doc, glm::vec4 vector) +XMLElement *tinyxml2::XMLElementGLM(XMLDocument *doc, glm::vec4 vector) { XMLElement *newelement = doc->NewElement( "vec4" ); newelement->SetAttribute("x", vector[0]); @@ -27,7 +28,7 @@ XMLElement *XMLElementGLM(XMLDocument *doc, glm::vec4 vector) return newelement; } -XMLElement *XMLElementGLM(XMLDocument *doc, glm::mat4 matrix) +XMLElement *tinyxml2::XMLElementGLM(XMLDocument *doc, glm::mat4 matrix) { XMLElement *newelement = doc->NewElement( "mat4" ); for (int r = 0 ; r < 4 ; r ++) diff --git a/tinyxml2Toolkit.h b/tinyxml2Toolkit.h index af6d03a..06c8f96 100644 --- a/tinyxml2Toolkit.h +++ b/tinyxml2Toolkit.h @@ -8,9 +8,9 @@ namespace tinyxml2 { class XMLDocument; class XMLElement; -tinyxml2::XMLElement *XMLElementGLM(tinyxml2::XMLDocument *doc, glm::vec3 vector); -tinyxml2::XMLElement *XMLElementGLM(tinyxml2::XMLDocument *doc, glm::vec4 vector); -tinyxml2::XMLElement *XMLElementGLM(tinyxml2::XMLDocument *doc, glm::mat4 matrix); +XMLElement *XMLElementGLM(XMLDocument *doc, glm::vec3 vector); +XMLElement *XMLElementGLM(XMLDocument *doc, glm::vec4 vector); +XMLElement *XMLElementGLM(XMLDocument *doc, glm::mat4 matrix); }