/** * Shows the context menu for map objects. The menu allows you to duplicate and * remove the map object, or do edit its properties. */ void MapObjectItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { if (!mIsEditable) return; QMenu menu; QIcon dupIcon(QLatin1String(":images/16x16/stock-duplicate-16.png")); QIcon delIcon(QLatin1String(":images/16x16/edit-delete.png")); QIcon propIcon(QLatin1String(":images/16x16/document-properties.png")); QAction *dupAction = menu.addAction(dupIcon, tr("&Duplicate")); QAction *removeAction = menu.addAction(delIcon, tr("&Remove")); menu.addSeparator(); QAction *propertiesAction = menu.addAction(propIcon, tr("&Properties...")); Utils::setThemeIcon(removeAction, "edit-delete"); Utils::setThemeIcon(propertiesAction, "document-properties"); QAction *selectedAction = menu.exec(event->screenPos()); if (selectedAction == dupAction) { MapDocument *doc = mMapDocument; doc->undoStack()->push(new AddMapObject(doc, mObject->objectGroup(), mObject->clone())); } else if (selectedAction == removeAction) { MapDocument *doc = mMapDocument; doc->undoStack()->push(new RemoveMapObject(doc, mObject)); } else if (selectedAction == propertiesAction) { ObjectPropertiesDialog propertiesDialog(mMapDocument, mObject, event->widget()); propertiesDialog.exec(); } }
void SelectSameTileTool::mousePressed(QGraphicsSceneMouseEvent *event) { if (event->button() != Qt::LeftButton) return; const Qt::KeyboardModifiers modifiers = event->modifiers(); MapDocument *document = mapDocument(); QRegion selection = document->selectedArea(); if (modifiers == Qt::ShiftModifier) selection += mSelectedRegion; else if (modifiers == Qt::ControlModifier) selection -= mSelectedRegion; else if (modifiers == (Qt::ControlModifier | Qt::ShiftModifier)) selection &= mSelectedRegion; else selection = mSelectedRegion; if (selection != document->selectedArea()) { QUndoCommand *cmd = new ChangeSelectedArea(document, selection); document->undoStack()->push(cmd); } }
void TileSelectionTool::mouseReleased(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::LeftButton) { mSelecting = false; MapDocument *document = mapDocument(); QRegion selection = document->selectedArea(); const QRect area = selectedArea(); switch (mSelectionMode) { case Replace: selection = area; break; case Add: selection += area; break; case Subtract: selection -= area; break; case Intersect: selection &= area; break; } if (selection != document->selectedArea()) { QUndoCommand *cmd = new ChangeSelectedArea(document, selection); document->undoStack()->push(cmd); } brushItem()->setTileRegion(QRegion()); updateStatusInfo(); } }
void AbstractObjectTool::detachSelectedObjects() { MapDocument *currentMapDocument = mapDocument(); QList<MapObject *> templateInstances; /** * Stores the unique tilesets used by the templates * to avoid creating multiple undo commands for the same tileset */ QSet<SharedTileset> sharedTilesets; for (MapObject *object : mapDocument()->selectedObjects()) { if (object->templateObject()) { templateInstances.append(object); if (Tile *tile = object->cell().tile()) sharedTilesets.insert(tile->tileset()->sharedPointer()); } } auto changeMapObjectCommand = new DetachObjects(currentMapDocument, templateInstances); // Add any missing tileset used by the templates to the map map before detaching for (SharedTileset sharedTileset : sharedTilesets) { if (!currentMapDocument->map()->tilesets().contains(sharedTileset)) new AddTileset(currentMapDocument, sharedTileset, changeMapObjectCommand); } currentMapDocument->undoStack()->push(changeMapObjectCommand); }
void ResizeHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { Handle::mouseReleaseEvent(event); // If we resized the object, create an undo command MapObject *obj = mMapObjectItem->mapObject(); if (event->button() == Qt::LeftButton && mOldSize != obj->size()) { MapDocument *document = mMapObjectItem->mapDocument(); QUndoCommand *cmd = new ResizeMapObject(document, obj, mOldSize); document->undoStack()->push(cmd); } }
void MapObjectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { QGraphicsItem::mouseReleaseEvent(event); // If we got moved, create an undo command if (event->button() == Qt::LeftButton && mOldObjectPos != mObject->position()) { MapDocument *document = mMapDocument; QUndoCommand *cmd = new MoveMapObject(document, mObject, mOldObjectPos); document->undoStack()->push(cmd); } }
void AbstractObjectTool::changeTile() { QList<MapObject*> tileObjects; MapDocument *currentMapDocument = mapDocument(); for (auto object : currentMapDocument->selectedObjects()) if (object->isTileObject()) tileObjects.append(object); auto changeMapObjectCommand = new ChangeMapObjectsTile(currentMapDocument, tileObjects, tile()); // Make sure the tileset is part of the document SharedTileset sharedTileset = tile()->tileset()->sharedPointer(); if (!currentMapDocument->map()->tilesets().contains(sharedTileset)) new AddTileset(currentMapDocument, sharedTileset, changeMapObjectCommand); currentMapDocument->undoStack()->push(changeMapObjectCommand); }
void DocumentManager::currentIndexChanged() { if (mSceneWithTool) { mSceneWithTool->disableSelectedTool(); mSceneWithTool = 0; } MapDocument *mapDocument = currentDocument(); if (mapDocument) mUndoGroup->setActiveStack(mapDocument->undoStack()); emit currentDocumentChanged(mapDocument); if (MapScene *mapScene = currentMapScene()) { mapScene->setSelectedTool(mSelectedTool); mapScene->enableSelectedTool(); mSceneWithTool = mapScene; } }
void TileCollisionDock::setTile(Tile *tile) { if (mTile == tile) return; mTile = tile; mMapScene->disableSelectedTool(); MapDocument *previousDocument = mDummyMapDocument; mMapView->setEnabled(tile); if (tile) { Map::Orientation orientation = Map::Orthogonal; QSize tileSize = tile->size(); if (tile->tileset()->orientation() == Tileset::Isometric) { orientation = Map::Isometric; tileSize = tile->tileset()->gridSize(); } Map *map = new Map(orientation, 1, 1, tileSize.width(), tileSize.height()); map->addTileset(tile->sharedTileset()); TileLayer *tileLayer = new TileLayer(QString(), 0, 0, 1, 1); tileLayer->setCell(0, 0, Cell(tile)); map->addLayer(tileLayer); ObjectGroup *objectGroup; if (tile->objectGroup()) objectGroup = tile->objectGroup()->clone(); else objectGroup = new ObjectGroup; objectGroup->setDrawOrder(ObjectGroup::IndexOrder); map->setNextObjectId(objectGroup->highestObjectId() + 1); map->addLayer(objectGroup); mDummyMapDocument = new MapDocument(map); mDummyMapDocument->setCurrentLayer(objectGroup); mMapScene->setMapDocument(mDummyMapDocument); mToolManager->setMapDocument(mDummyMapDocument); mMapScene->enableSelectedTool(); connect(mDummyMapDocument->undoStack(), &QUndoStack::indexChanged, this, &TileCollisionDock::applyChanges); connect(mDummyMapDocument, &MapDocument::selectedObjectsChanged, this, &TileCollisionDock::selectedObjectsChanged); } else { mDummyMapDocument = nullptr; mMapScene->setMapDocument(nullptr); mToolManager->setMapDocument(nullptr); } emit dummyMapDocumentChanged(mDummyMapDocument); setHasSelectedObjects(false); if (previousDocument) { // Explicitly disconnect early from this signal, since it can get fired // from the QUndoStack destructor. disconnect(previousDocument->undoStack(), &QUndoStack::indexChanged, this, &TileCollisionDock::applyChanges); delete previousDocument; } }