From 0d30e9b57229905f78e7bd60fe5d3cde72851f28 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 16 Mar 2015 16:53:05 -0400 Subject: Rewrote map editor so a single file contains all maps Maps are viewed in a tree control on the left. They can be dragged and dropped. Maps are bolded when they are dirty. Saving saves expansion status and order of maps in tree. --- tools/mapedit/src/map.cpp | 289 ++++++++++++++++++---------------------------- 1 file changed, 113 insertions(+), 176 deletions(-) (limited to 'tools/mapedit/src/map.cpp') diff --git a/tools/mapedit/src/map.cpp b/tools/mapedit/src/map.cpp index f9c07fc..32541e6 100644 --- a/tools/mapedit/src/map.cpp +++ b/tools/mapedit/src/map.cpp @@ -1,106 +1,27 @@ #include "map.h" -#include -#include -#include #include "frame.h" -Map::Map() +Map::Map(int id, World* world) : id(id), world(world) { mapdata = (int*) calloc(MAP_WIDTH * MAP_HEIGHT, sizeof(int)); } -Map::Map(std::string filename) -{ - xmlDocPtr doc = xmlParseFile(filename.c_str()); - if (doc == nullptr) - { - throw MapLoadException(filename); - } - - xmlNodePtr top = xmlDocGetRootElement(doc); - if (top == nullptr) - { - throw MapLoadException(filename); - } - - if (xmlStrcmp(top->name, (const xmlChar*) "map-def")) - { - throw MapLoadException(filename); - } - - for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) - { - if (!xmlStrcmp(node->name, (const xmlChar*) "name")) - { - xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - title = (char*) key; - xmlFree(key); - } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment")) - { - xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); - mapdata[0] = atoi(strtok((char*) key, ",\n")); - for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) - { - mapdata[i] = atoi(strtok(NULL, ",\n")); - } - xmlFree(key); - } else if (!xmlStrcmp(node->name, (const xmlChar*) "leftmap")) - { - xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - leftmap = (char*) key; - xmlFree(key); - } else if (!xmlStrcmp(node->name, (const xmlChar*) "rightmap")) - { - xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - rightmap = (char*) key; - xmlFree(key); - } else if (!xmlStrcmp(node->name, (const xmlChar*) "entities")) - { - for (xmlNodePtr entityNode = node->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next) - { - if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity")) - { - auto data = std::make_shared(); - - for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next) - { - if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) - { - xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); - data->object = MapObject::getAllObjects().at((char*) key).get(); - xmlFree(key); - } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) - { - xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); - sscanf((char*) key, "%lf,%lf", &data->position.first, &data->position.second); - xmlFree(key); - } - } - - objects.push_back(data); - } - } - } - } - - xmlFreeDoc(doc); -} - Map::Map(const Map& map) { mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int)); + id = map.id; title = map.title; leftmap = map.leftmap; rightmap = map.rightmap; - dirty = map.dirty; objects = map.objects; - frame = map.frame; + world = map.world; + treeItemId = map.treeItemId; + children = map.children; } -Map::Map(Map&& map) : Map() +Map::Map(Map&& map) : Map(-1, map.world) { swap(*this, map); } @@ -123,141 +44,157 @@ void swap(Map& first, Map& second) std::swap(first.title, second.title); std::swap(first.leftmap, second.leftmap); std::swap(first.rightmap, second.rightmap); - std::swap(first.dirty, second.dirty); std::swap(first.objects, second.objects); - std::swap(first.frame, second.frame); + std::swap(first.id, second.id); + std::swap(first.world, second.world); + std::swap(first.treeItemId, second.treeItemId); + std::swap(first.children, second.children); } -#define MY_ENCODING "ISO-8859-1" +int Map::getID() const +{ + return id; +} -void Map::save(std::string name) +std::string Map::getTitle() const { - if (!dirty) return; - - int rc; - - xmlTextWriterPtr writer = xmlNewTextWriterFilename(name.c_str(), 0); - if (writer == NULL) throw MapWriteException(name); + return title; +} - rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL); - if (rc < 0) throw MapWriteException(name); - - rc = xmlTextWriterStartElement(writer, (xmlChar*) "map-def"); - if (rc < 0) throw MapWriteException(name); - - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "name", (xmlChar*) title.c_str()); - if (rc < 0) throw MapWriteException(name); - - std::ostringstream mapdata_out; - for (int y=0; y>& Map::getObjects() const +{ + return objects; +} + +std::shared_ptr Map::getLeftmap() const +{ + if (leftmap == -1) { - for (int x=0; x(); + } else { + return world->getMap(leftmap); } - - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "environment", (xmlChar*) mapdata_out.str().c_str()); - if (rc < 0) throw MapWriteException(name); - - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "leftmap", (xmlChar*) leftmap.c_str()); - if (rc < 0) throw MapWriteException(name); +} - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "rightmap", (xmlChar*) rightmap.c_str()); - if (rc < 0) throw MapWriteException(name); - - rc = xmlTextWriterStartElement(writer, (xmlChar*) "entities"); - if (rc < 0) throw MapWriteException(name); - - for (auto object : objects) +std::shared_ptr Map::getRightmap() const +{ + if (rightmap == -1) { - rc = xmlTextWriterStartElement(writer, (xmlChar*) "entity"); - if (rc < 0) throw MapWriteException(name); - - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-type", (xmlChar*) object->object->getType().c_str()); - if (rc < 0) throw MapWriteException(name); - - std::ostringstream entpos_out; - entpos_out << object->position.first << "," << object->position.second; - - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-position", (xmlChar*) entpos_out.str().c_str()); - if (rc < 0) throw MapWriteException(name); - - rc = xmlTextWriterEndElement(writer); - if (rc < 0) throw MapWriteException(name); + return std::shared_ptr(); + } else { + return world->getMap(rightmap); } - - rc = xmlTextWriterEndElement(writer); - if (rc < 0) throw MapWriteException(name); - - rc = xmlTextWriterEndElement(writer); - if (rc < 0) throw MapWriteException(name); - - rc = xmlTextWriterEndDocument(writer); - if (rc < 0) throw MapWriteException(name); - - xmlFreeTextWriter(writer); - - setDirty(false); } -bool Map::hasUnsavedChanges() const +wxTreeItemId Map::getTreeItemId() const { - return dirty; + return treeItemId; } -void Map::setTileAt(int x, int y, int tile) +std::list> Map::getChildren() const { - setDirty(true); - mapdata[x+y*MAP_WIDTH] = tile; + std::list> ret; + + for (auto id : children) + { + ret.push_back(world->getMap(id)); + } + + return ret; } -int Map::getTileAt(int x, int y) const +bool Map::getExpanded() const { - return mapdata[x+y*MAP_WIDTH]; + return expanded; } -std::string Map::getTitle() const +void Map::setTitle(std::string title, bool dirty) { - return title; + this->title = title; + + if (dirty) + { + world->setDirty(true); + } } -void Map::setTitle(std::string title) +void Map::setTileAt(int x, int y, int tile, bool dirty) { - setDirty(true); - this->title = title; + mapdata[x+y*MAP_WIDTH] = tile; + + if (dirty) + { + world->setDirty(true); + } } -const std::list>& Map::getObjects() const +void Map::setMapdata(int* mapdata, bool dirty) { - return objects; + free(this->mapdata); + this->mapdata = mapdata; + + if (dirty) + { + world->setDirty(true); + } } -void Map::addObject(std::shared_ptr& obj) +void Map::addObject(std::shared_ptr& obj, bool dirty) { - setDirty(true); objects.push_back(obj); + + if (dirty) + { + world->setDirty(true); + } } -void Map::removeObject(std::shared_ptr& obj) +void Map::removeObject(std::shared_ptr& obj, bool dirty) { - setDirty(true); objects.remove(obj); + + if (dirty) + { + world->setDirty(true); + } } -bool Map::getDirty() const +void Map::setLeftmap(int id, bool dirty) { - return dirty; + leftmap = id; + + if (dirty) + { + world->setDirty(true); + } } -void Map::setDirty(bool dirty) +void Map::setRightmap(int id, bool dirty) { - this->dirty = dirty; + rightmap = id; - if (frame != nullptr) + if (dirty) { - frame->MapDirtyDidChange(dirty); + world->setDirty(true); } } + +void Map::setTreeItemId(wxTreeItemId id) +{ + this->treeItemId = id; +} + +void Map::addChild(int id) +{ + children.push_back(id); +} + +void Map::setExpanded(bool exp) +{ + expanded = exp; +} -- cgit 1.4.1