Layer *ObjectGroup::mergedWith(Layer *other) const { Q_ASSERT(canMergeWith(other)); const ObjectGroup *og = static_cast<ObjectGroup*>(other); ObjectGroup *merged = static_cast<ObjectGroup*>(clone()); for (const MapObject *mapObject : og->objects()) merged->addObject(mapObject->clone()); return merged; }
/** * Convenience method to copy the current selection to the clipboard. * Deals with either tile selection or object selection. */ void ClipboardManager::copySelection(const MapDocument *mapDocument) { const Layer *currentLayer = mapDocument->currentLayer(); if (!currentLayer) return; const Map *map = mapDocument->map(); const QRegion &selectedArea = mapDocument->selectedArea(); const QList<MapObject*> &selectedObjects = mapDocument->selectedObjects(); const TileLayer *tileLayer = dynamic_cast<const TileLayer*>(currentLayer); Layer *copyLayer = nullptr; if (!selectedArea.isEmpty() && tileLayer) { const QRegion area = selectedArea.intersected(tileLayer->bounds()); // Copy the selected part of the layer copyLayer = tileLayer->copy(area.translated(-tileLayer->position())); copyLayer->setPosition(area.boundingRect().topLeft()); } else if (!selectedObjects.isEmpty()) { // Create a new object group with clones of the selected objects ObjectGroup *objectGroup = new ObjectGroup; for (const MapObject *mapObject : selectedObjects) objectGroup->addObject(mapObject->clone()); copyLayer = objectGroup; } else { return; } // Create a temporary map to write to the clipboard Map copyMap(map->orientation(), 0, 0, map->tileWidth(), map->tileHeight()); copyMap.setRenderOrder(map->renderOrder()); // Resolve the set of tilesets used by this layer foreach (const SharedTileset &tileset, copyLayer->usedTilesets()) copyMap.addTileset(tileset); copyMap.addLayer(copyLayer); setMap(copyMap); }
void TiledMapLoader::loadObjectGroupObjects(Map &map, ObjectGroup &objectGroup, rapidxml::xml_node<> &objectGroupNode) { rapidxml::xml_node<> *objectNode = nullptr; rapidxml::xml_node<> *objectNature = nullptr; objectNode = objectGroupNode.first_node("object"); while (objectNode) { Object::Ptr object(new Object); XMLElement objectGroupElement(*objectNode); object->setGid(objectGroupElement.getInt("gid", -1)); object->setId(objectGroupElement.getInt("id", -1)); object->setName(objectGroupElement.getString("name")); object->setType(objectGroupElement.getString("type")); object->setX(objectGroupElement.getFloat("x")); object->setY(objectGroupElement.getFloat("y")); object->setWidth(objectGroupElement.getFloat("width")); object->setHeight(objectGroupElement.getFloat("height")); object->setRotation(objectGroupElement.getFloat("rotation")); object->setVisible(objectGroupElement.getInt("visible", 1)); objectNature = objectNode->first_node("ellipse"); if (objectNature) { object->setPolygonType("ellipse"); } objectNature = objectNode->first_node("polygon"); if (objectNature) { XMLElement objectNatureElement(*objectNature); object->setPolygonType("polygon"); object->setVertices(objectNatureElement.getString("points")); } objectNature = objectNode->first_node("polyline"); if (objectNature) { XMLElement objectNatureElement(*objectNature); object->setPolygonType("polyline"); object->setVertices(objectNatureElement.getString("points")); } object->parseProperties(*objectNode); objectGroup.addObject(std::move(object)); objectNode = objectNode->next_sibling("object"); } }
void ClipboardManager::copySelection(const MapDocument *mapDocument) { const Layer *currentLayer = mapDocument->currentLayer(); if (!currentLayer) return; const Map *map = mapDocument->map(); const QRegion &selectedArea = mapDocument->selectedArea(); const QList<MapObject*> &selectedObjects = mapDocument->selectedObjects(); const TileLayer *tileLayer = dynamic_cast<const TileLayer*>(currentLayer); Layer *copyLayer = 0; if (!selectedArea.isEmpty() && tileLayer) { // Copy the selected part of the layer copyLayer = tileLayer->copy(selectedArea.translated(-tileLayer->x(), -tileLayer->y())); } else if (!selectedObjects.isEmpty()) { // Create a new object group with clones of the selected objects ObjectGroup *objectGroup = new ObjectGroup; foreach (const MapObject *mapObject, selectedObjects) objectGroup->addObject(mapObject->clone()); copyLayer = objectGroup; } else {
void TemplatesDock::setTemplate(ObjectTemplate *objectTemplate) { if (mObjectTemplate == objectTemplate) return; mObjectTemplate = objectTemplate; mMapScene->setSelectedTool(nullptr); MapDocumentPtr previousDocument = mDummyMapDocument; mMapView->setEnabled(objectTemplate); if (objectTemplate && objectTemplate->object()) { mDummyMapDocument = ourDummyDocuments.value(objectTemplate); if (!mDummyMapDocument) { Map::Orientation orientation = Map::Orthogonal; std::unique_ptr<Map> map { new Map(orientation, 1, 1, 1, 1) }; MapObject *dummyObject = objectTemplate->object()->clone(); dummyObject->markAsTemplateBase(); if (Tileset *tileset = dummyObject->cell().tileset()) { map->addTileset(tileset->sharedPointer()); dummyObject->setPosition({-dummyObject->width() / 2, dummyObject->height() / 2}); } else { dummyObject->setPosition({-dummyObject->width() / 2, -dummyObject->height() /2}); } ObjectGroup *objectGroup = new ObjectGroup; objectGroup->addObject(dummyObject); map->addLayer(objectGroup); mDummyMapDocument = MapDocumentPtr::create(std::move(map)); mDummyMapDocument->setAllowHidingObjects(false); mDummyMapDocument->setCurrentLayer(objectGroup); ourDummyDocuments.insert(objectTemplate, mDummyMapDocument); } mDummyMapDocument->setCurrentObject(dummyObject()); mUndoAction->setEnabled(mDummyMapDocument->undoStack()->canUndo()); mRedoAction->setEnabled(mDummyMapDocument->undoStack()->canRedo()); connect(mDummyMapDocument->undoStack(), &QUndoStack::indexChanged, this, &TemplatesDock::applyChanges); checkTileset(); } else { mDummyMapDocument.reset(); } mMapScene->setMapDocument(mDummyMapDocument.data()); mToolManager->setMapDocument(mDummyMapDocument.data()); mPropertiesDock->setDocument(mDummyMapDocument.data()); mMapScene->setSelectedTool(mToolManager->selectedTool()); if (previousDocument) previousDocument->undoStack()->disconnect(this); }
ObjectGroup *VariantToMapConverter::toObjectGroup(const QVariantMap &variantMap) { ObjectGroup *objectGroup = new ObjectGroup(variantMap["name"].toString(), variantMap["x"].toInt(), variantMap["y"].toInt(), variantMap["width"].toInt(), variantMap["height"].toInt()); const qreal opacity = variantMap["opacity"].toReal(); const bool visible = variantMap["visible"].toBool(); objectGroup->setOpacity(opacity); objectGroup->setVisible(visible); objectGroup->setColor(variantMap.value("color").value<QColor>()); const PixelToTileCoordinates toTile(mMap); foreach (const QVariant &objectVariant, variantMap["objects"].toList()) { const QVariantMap objectVariantMap = objectVariant.toMap(); const QString name = objectVariantMap["name"].toString(); const QString type = objectVariantMap["type"].toString(); const int gid = objectVariantMap["gid"].toInt(); const int x = objectVariantMap["x"].toInt(); const int y = objectVariantMap["y"].toInt(); const int width = objectVariantMap["width"].toInt(); const int height = objectVariantMap["height"].toInt(); const QPointF pos = toTile(x, y); const QPointF size = toTile(width, height); MapObject *object = new MapObject(name, type, pos, QSizeF(size.x(), size.y())); if (gid) { bool ok; const Cell cell = mGidMapper.gidToCell(gid, ok); object->setTile(cell.tile); } if (objectVariantMap.contains("visible")) object->setVisible(objectVariantMap["visible"].toBool()); object->setProperties(toProperties(objectVariantMap["properties"])); objectGroup->addObject(object); const QVariant polylineVariant = objectVariantMap["polyline"]; const QVariant polygonVariant = objectVariantMap["polygon"]; if (polygonVariant.isValid()) { object->setShape(MapObject::Polygon); object->setPolygon(toPolygon(polygonVariant)); } if (polylineVariant.isValid()) { object->setShape(MapObject::Polyline); object->setPolygon(toPolygon(polylineVariant)); } if (objectVariantMap.contains("ellipse")) object->setShape(MapObject::Ellipse); } return objectGroup; }