From 184c6305a677d968e60eb6cba9b29dd840cdce3c Mon Sep 17 00:00:00 2001 From: neoraider Date: Sun, 13 Apr 2008 01:59:01 +0000 Subject: zoomedit: * New data interfaces, yay! Levels should now be completely editable * Fixed some -Wextra warnings --- src/Data/Gate.cpp | 38 +++++++++++++++++- src/Data/Gate.h | 16 ++++++-- src/Data/Level.cpp | 112 +++++++++++++++++++++++++--------------------------- src/Data/Level.h | 40 ++++++++++--------- src/Data/Room.cpp | 38 +++++++++++++++++- src/Data/Room.h | 16 ++++++-- src/Data/Texture.h | 6 +-- src/Data/Triangle.h | 4 ++ 8 files changed, 182 insertions(+), 88 deletions(-) (limited to 'src/Data') diff --git a/src/Data/Gate.cpp b/src/Data/Gate.cpp index 2aa89ee..62794c2 100644 --- a/src/Data/Gate.cpp +++ b/src/Data/Gate.cpp @@ -18,6 +18,7 @@ */ #include "Gate.h" +#include "Triangle.h" namespace ZoomEdit { namespace Data { @@ -29,7 +30,7 @@ Gate::Gate(xmlpp::Element *node) : gateNode(node) { xmlpp::Element *tNode = dynamic_cast(*t); if(tNode) - triangles.push_back(Triangle(tNode)); + triangles.push_back(new Triangle(tNode)); } id = node->get_attribute_value("id"); @@ -37,5 +38,40 @@ Gate::Gate(xmlpp::Element *node) : gateNode(node) { room2 = node->get_attribute_value("room2"); } +Gate::~Gate() { + for(std::list::iterator t = triangles.begin(); t != triangles.end(); ++t) + delete *t; +} + +Triangle* Gate::addTriangle() { + xmlpp::Element *tNode = gateNode->add_child("triangle"); + + xmlpp::Element *node = tNode->add_child("vertex"); + node->set_attribute("x", "0.0"); + node->set_attribute("y", "0.0"); + node->set_attribute("z", "0.0"); + + node = tNode->add_child("vertex"); + node->set_attribute("x", "0.0"); + node->set_attribute("y", "0.0"); + node->set_attribute("z", "0.0"); + + node = tNode->add_child("vertex"); + node->set_attribute("x", "0.0"); + node->set_attribute("y", "0.0"); + node->set_attribute("z", "0.0"); + + Triangle *t = new Triangle(tNode); + triangles.push_back(t); + + return t; +} + +void Gate::removeTriangle(Triangle *t) { + gateNode->remove_child(t->getNode()); + triangles.remove(t); + delete t; +} + } } diff --git a/src/Data/Gate.h b/src/Data/Gate.h index b1cde71..a6fb958 100644 --- a/src/Data/Gate.h +++ b/src/Data/Gate.h @@ -20,27 +20,37 @@ #ifndef ZOOMEDIT_DATA_GATE_H_ #define ZOOMEDIT_DATA_GATE_H_ -#include "Triangle.h" #include +#include namespace ZoomEdit { namespace Data { +class Triangle; + class Gate { private: - std::list triangles; + std::list triangles; xmlpp::Element *gateNode; Glib::ustring id; Glib::ustring room1, room2; + // Prevent shallow copy + Gate(const Gate &o); + Gate& operator=(const Gate &o); + public: Gate(xmlpp::Element *node); + virtual ~Gate(); - const std::list& getTriangles() { + const std::list& getTriangles() { return triangles; } + Triangle* addTriangle(); + void removeTriangle(Triangle *t); + const Glib::ustring& getId() const { return id; } diff --git a/src/Data/Level.cpp b/src/Data/Level.cpp index 6689107..283bf66 100644 --- a/src/Data/Level.cpp +++ b/src/Data/Level.cpp @@ -18,6 +18,9 @@ */ #include "Level.h" +#include "Room.h" +#include "Gate.h" +#include "Texture.h" namespace ZoomEdit { namespace Data { @@ -32,11 +35,8 @@ Level::Level(xmlpp::Element *levelNode) : info(dynamic_cast(lev for(xmlpp::Node::NodeList::iterator room = nodeList.begin(); room != nodeList.end(); ++room) { xmlpp::Element *roomNode = dynamic_cast(*room); - if(roomNode) { - Room room(roomNode); - - rooms.insert(std::make_pair(room.getId(), room)); - } + if(roomNode) + rooms.push_back(new Room(roomNode)); } nodeList = gatesNode->get_children("gate"); @@ -44,11 +44,8 @@ Level::Level(xmlpp::Element *levelNode) : info(dynamic_cast(lev for(xmlpp::Node::NodeList::iterator gate = nodeList.begin(); gate != nodeList.end(); ++gate) { xmlpp::Element *gateNode = dynamic_cast(*gate); - if(gateNode) { - Gate gate(gateNode); - - gates.insert(std::make_pair(gate.getId(), gate)); - } + if(gateNode) + gates.push_back(new Gate(gateNode)); } nodeList = texturesNode->get_children("texture"); @@ -56,22 +53,23 @@ Level::Level(xmlpp::Element *levelNode) : info(dynamic_cast(lev for(xmlpp::Node::NodeList::iterator tex = nodeList.begin(); tex != nodeList.end(); ++tex) { xmlpp::Element *texNode = dynamic_cast(*tex); - if(texNode) { - Texture tex(texNode); - - textures.insert(std::make_pair(tex.getId(), tex)); - } + if(texNode) + textures.push_back(new Texture(texNode)); } } -Room* Level::getRoom(const Glib::ustring &id) { - std::map::iterator room = rooms.find(id); - return (room != rooms.end()) ? &room->second : NULL; +Level::~Level() { + for(std::list::iterator room = rooms.begin(); room != rooms.end(); ++room) + delete *room; } -const Room* Level::getRoom(const Glib::ustring &id) const { - std::map::const_iterator room = rooms.find(id); - return (room != rooms.end()) ? &room->second : NULL; +Room* Level::getRoom(const Glib::ustring &id) const { + for(std::list::const_iterator room = rooms.begin(); room != rooms.end(); ++room) { + if((*room)->getId() == id) + return *room; + } + + return NULL; } Room* Level::addRoom(const Glib::ustring &id) { @@ -79,26 +77,25 @@ Room* Level::addRoom(const Glib::ustring &id) { node->set_attribute("id", id); - return &rooms.insert(std::make_pair(id, Room(node))).first->second; -} - -void Level::removeRoom(const Glib::ustring &id) { - Room *room = getRoom(id); - if(!room) - return; + Room *room = new Room(node); + rooms.push_back(room); - roomsNode->remove_child(room->getNode()); - rooms.erase(id); + return room; } -Gate* Level::getGate(const Glib::ustring &id) { - std::map::iterator gate = gates.find(id); - return (gate != gates.end()) ? &gate->second : NULL; +void Level::removeRoom(Room *room) { + roomsNode->remove_child(room->getNode()); + rooms.remove(room); + delete room; } -const Gate* Level::getGate(const Glib::ustring &id) const { - std::map::const_iterator gate = gates.find(id); - return (gate != gates.end()) ? &gate->second : NULL; +Gate* Level::getGate(const Glib::ustring &id) const { + for(std::list::const_iterator gate = gates.begin(); gate != gates.end(); ++gate) { + if((*gate)->getId() == id) + return *gate; + } + + return NULL; } Gate* Level::addGate(const Glib::ustring &id) { @@ -108,26 +105,25 @@ Gate* Level::addGate(const Glib::ustring &id) { node->set_attribute("room1", ""); node->set_attribute("room2", ""); - return &gates.insert(std::make_pair(id, Gate(node))).first->second; -} - -void Level::removeGate(const Glib::ustring &id) { - Gate *gate = getGate(id); - if(!gate) - return; + Gate *gate = new Gate(node); + gates.push_back(gate); - gatesNode->remove_child(gate->getNode()); - gates.erase(id); + return gate; } -Texture* Level::getTexture(const Glib::ustring &id) { - std::map::iterator tex = textures.find(id); - return (tex != textures.end()) ? &tex->second : NULL; +void Level::removeGate(Gate *gate) { + gatesNode->remove_child(gate->getNode()); + gates.remove(gate); + delete gate; } -const Texture* Level::getTexture(const Glib::ustring &id) const { - std::map::const_iterator tex = textures.find(id); - return (tex != textures.end()) ? &tex->second : NULL; +Texture* Level::getTexture(const Glib::ustring &id) const { + for(std::list::const_iterator tex = textures.begin(); tex != textures.end(); ++tex) { + if((*tex)->getId() == id) + return *tex; + } + + return NULL; } Texture* Level::addTexture(const Glib::ustring &id) { @@ -136,16 +132,16 @@ Texture* Level::addTexture(const Glib::ustring &id) { node->set_attribute("id", id); node->set_attribute("name", ""); - return &textures.insert(std::make_pair(id, Texture(node))).first->second; + Texture *tex = new Texture(node); + textures.push_back(tex); + + return tex; } -void Level::removeTexture(const Glib::ustring &id) { - Texture *tex = getTexture(id); - if(!tex) - return; - +void Level::removeTexture(Texture *tex) { texturesNode->remove_child(tex->getNode()); - textures.erase(id); + textures.remove(tex); + delete tex; } } diff --git a/src/Data/Level.h b/src/Data/Level.h index c72b503..34b5b1a 100644 --- a/src/Data/Level.h +++ b/src/Data/Level.h @@ -21,54 +21,56 @@ #define ZOOMEDIT_DATA_LEVEL_H_ #include "Info.h" -#include "Room.h" -#include "Gate.h" -#include "Texture.h" #include -#include namespace ZoomEdit { namespace Data { +class Room; +class Gate; +class Texture; + class Level { private: Info info; - std::map rooms; + std::list rooms; xmlpp::Element *roomsNode; - std::map gates; + std::list gates; xmlpp::Element *gatesNode; - std::map textures; + std::list textures; xmlpp::Element *texturesNode; + // Prevent shallow copy + Level(const Level &o); + Level& operator=(const Level &o); + public: Level(xmlpp::Element *levelNode); + virtual ~Level(); - const std::map& getRooms() const {return rooms;} + const std::list& getRooms() const {return rooms;} - Room* getRoom(const Glib::ustring &id); - const Room* getRoom(const Glib::ustring &id) const; + Room* getRoom(const Glib::ustring &id) const; Room* addRoom(const Glib::ustring &id); - void removeRoom(const Glib::ustring &id); + void removeRoom(Room *room); - const std::map& getGates() const {return gates;} + const std::list& getGates() const {return gates;} - Gate* getGate(const Glib::ustring &id); - const Gate* getGate(const Glib::ustring &id) const; + Gate* getGate(const Glib::ustring &id) const; Gate* addGate(const Glib::ustring &id); - void removeGate(const Glib::ustring &id); + void removeGate(Gate *gate); - const std::map& getTextures() const {return textures;} + const std::list& getTextures() const {return textures;} - Texture* getTexture(const Glib::ustring &id); - const Texture* getTexture(const Glib::ustring &id) const; + Texture* getTexture(const Glib::ustring &id) const; Texture* addTexture(const Glib::ustring &id); - void removeTexture(const Glib::ustring &id); + void removeTexture(Texture *tex); }; } diff --git a/src/Data/Room.cpp b/src/Data/Room.cpp index 9749b22..acc79be 100644 --- a/src/Data/Room.cpp +++ b/src/Data/Room.cpp @@ -18,6 +18,7 @@ */ #include "Room.h" +#include "Triangle.h" namespace ZoomEdit { namespace Data { @@ -29,11 +30,46 @@ Room::Room(xmlpp::Element *node) : roomNode(node) { xmlpp::Element *tNode = dynamic_cast(*t); if(tNode) - triangles.push_back(Triangle(tNode)); + triangles.push_back(new Triangle(tNode)); } id = node->get_attribute_value("id"); } +Room::~Room() { + for(std::list::iterator t = triangles.begin(); t != triangles.end(); ++t) + delete *t; +} + +Triangle* Room::addTriangle() { + xmlpp::Element *tNode = roomNode->add_child("triangle"); + + xmlpp::Element *node = tNode->add_child("vertex"); + node->set_attribute("x", "0.0"); + node->set_attribute("y", "0.0"); + node->set_attribute("z", "0.0"); + + node = tNode->add_child("vertex"); + node->set_attribute("x", "0.0"); + node->set_attribute("y", "0.0"); + node->set_attribute("z", "0.0"); + + node = tNode->add_child("vertex"); + node->set_attribute("x", "0.0"); + node->set_attribute("y", "0.0"); + node->set_attribute("z", "0.0"); + + Triangle *t = new Triangle(tNode); + triangles.push_back(t); + + return t; +} + +void Room::removeTriangle(Triangle *t) { + roomNode->remove_child(t->getNode()); + triangles.remove(t); + delete t; +} + } } diff --git a/src/Data/Room.h b/src/Data/Room.h index 1c1f9e8..e290e36 100644 --- a/src/Data/Room.h +++ b/src/Data/Room.h @@ -20,26 +20,36 @@ #ifndef ZOOMEDIT_DATA_ROOM_H_ #define ZOOMEDIT_DATA_ROOM_H_ -#include "Triangle.h" #include +#include namespace ZoomEdit { namespace Data { +class Triangle; + class Room { private: - std::list triangles; + std::list triangles; xmlpp::Element *roomNode; Glib::ustring id; + // Prevent shallow copy + Room(const Room &o); + Room& operator=(const Room &o); + public: Room(xmlpp::Element *node); + virtual ~Room(); - const std::list& getTriangles() const { + const std::list& getTriangles() const { return triangles; } + Triangle* addTriangle(); + void removeTriangle(Triangle *t); + const Glib::ustring& getId() const { return id; } diff --git a/src/Data/Texture.h b/src/Data/Texture.h index b301bbf..8bc2c82 100644 --- a/src/Data/Texture.h +++ b/src/Data/Texture.h @@ -38,9 +38,9 @@ class Texture { return id; } - void setId(const Glib::ustring &nid) { - id = nid; - texNode->set_attribute("id", nid); + void setId(const Glib::ustring &id0) { + id = id0; + texNode->set_attribute("id", id0); } const Glib::ustring& getName() const { diff --git a/src/Data/Triangle.h b/src/Data/Triangle.h index 3a235ce..2267f36 100644 --- a/src/Data/Triangle.h +++ b/src/Data/Triangle.h @@ -78,6 +78,10 @@ class Triangle { visible = vis; triangleNode->set_attribute("visible", vis ? "true" : "false"); } + + xmlpp::Element* getNode() const { + return triangleNode; + } }; } -- cgit v1.2.3