New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds option to remove an edge from polygon #1685

Merged
merged 6 commits into from Aug 15, 2017
@@ -61,3 +61,14 @@ void ChangePolygon::redo()
{
mMapDocument->mapObjectModel()->setObjectPolygon(mMapObject, mNewPolygon);
}
TogglePolygonPolyline::TogglePolygonPolyline(MapObject *mapObject)
: mMapObject(mapObject)
{
setText(QCoreApplication::translate("Undo Commands", "Toggle Polygon/Polyline"));
}
void TogglePolygonPolyline::toggle()
{
mMapObject->setShape((mMapObject->shape() == MapObject::Polygon) ? MapObject::Polyline : MapObject::Polygon);
}
View
@@ -60,5 +60,19 @@ class ChangePolygon : public QUndoCommand
QPolygonF mNewPolygon;
};
class TogglePolygonPolyline : public QUndoCommand
{
public:
TogglePolygonPolyline(MapObject *MapObject);
void undo() override { toggle(); }
void redo() override { toggle(); }
private:
void toggle();
MapObject *mMapObject;
};
} // namespace Internal
} // namespace Tiled
@@ -44,6 +44,8 @@
#include <QPalette>
#include <QUndoStack>
#include <cstdlib>
using namespace Tiled;
using namespace Tiled::Internal;
@@ -618,6 +620,26 @@ void EditPolygonTool::showHandleContextMenu(PointHandle *clickedHandle,
connect(joinNodesAction, SIGNAL(triggered()), SLOT(joinNodes()));
connect(splitSegmentsAction, SIGNAL(triggered()), SLOT(splitSegments()));
const PointHandle *firstHandle = *mSelectedHandles.begin();
const MapObject *mapObject = firstHandle->mapObjectItem()->mapObject();
if (mapObject->shape() == MapObject::Polygon) {
QAction *deleteSegment = menu.addAction(tr("Delete Segment"));
bool enabled = false;
if (n == 2) {
const PointHandle *secondHandle = *(mSelectedHandles.begin() + 1);
int indexDifference = std::abs(firstHandle->pointIndex() - secondHandle->pointIndex());
if (indexDifference == 1 || indexDifference == mapObject->polygon().size() - 1) {
const MapObject *secondMapObject = secondHandle->mapObjectItem()->mapObject();
enabled = (mapObject == secondMapObject);
}
}
deleteSegment->setEnabled(enabled);
connect(deleteSegment, SIGNAL(triggered()), SLOT(deleteSegment()));
}
menu.exec(screenPos);
}
@@ -870,3 +892,36 @@ void EditPolygonTool::splitSegments()
if (macroStarted)
undoStack->endMacro();
}
void EditPolygonTool::deleteSegment()
{
if (mSelectedHandles.size() != 2)
return;
const auto &firstHandle = *mSelectedHandles.begin();
const auto &secondHandle = *(mSelectedHandles.begin() + 1);
const MapObjectItem *item = firstHandle->mapObjectItem();
MapObject *mapObject = item->mapObject();
QPolygonF polygon = mapObject->polygon();
QPolygonF newPolygon(polygon);
int indexDifference = std::abs(firstHandle->pointIndex() - secondHandle->pointIndex());
if (indexDifference != polygon.size() - 1) {
int maxIndex = std::max(firstHandle->pointIndex(), secondHandle->pointIndex());
for (int i = maxIndex; i < polygon.size(); ++i)
newPolygon[i - maxIndex] = polygon[i];
for (int i = 0; i < maxIndex; ++i)
newPolygon[polygon.size() - maxIndex + i] = polygon[i];
}
setSelectedHandles(QSet<PointHandle*>());
mapDocument()->undoStack()->beginMacro(tr("Delete Segment"));
mapDocument()->undoStack()->push(new ChangePolygon(mapDocument(), mapObject, newPolygon, polygon));
mapDocument()->undoStack()->push(new TogglePolygonPolyline(mapObject));
mapDocument()->undoStack()->endMacro();
}
@@ -68,6 +68,7 @@ private slots:
void joinNodes();
void splitSegments();
void deleteSegment();
private:
enum Mode {
ProTip! Use n and p to navigate between commits in a pull request.