Skip to content

Commit

Permalink
Added shortcuts for finishing and canceling object creation
Browse files Browse the repository at this point in the history
Now the Return / Enter keys will finish object creation, and Escape will
cancel object creation.

Suggested by @noc7c9 in relation to #496 and matches Inkscape behavior
for creation of bezier curves / lines.
  • Loading branch information
bjorn committed Aug 14, 2013
1 parent 8d89a7c commit f85f533
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/tiled/createobjecttool.cpp
Expand Up @@ -34,6 +34,7 @@
#include "utils.h"

#include <QApplication>
#include <QKeyEvent>
#include <QPalette>

using namespace Tiled;
Expand Down Expand Up @@ -100,6 +101,30 @@ void CreateObjectTool::deactivate(MapScene *scene)
AbstractObjectTool::deactivate(scene);
}

void CreateObjectTool::keyPressed(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Enter:
case Qt::Key_Return:
if (mNewMapObjectItem) {
if (mMode == CreatePolygon || mMode == CreatePolyline)
finishOrCancelPolygon();
else
finishNewMapObject();
return;
}
break;
case Qt::Key_Escape:
if (mNewMapObjectItem) {
cancelNewMapObject();
return;
}
break;
}

AbstractObjectTool::keyPressed(event);
}

void CreateObjectTool::mouseEntered()
{
}
Expand Down Expand Up @@ -200,13 +225,7 @@ void CreateObjectTool::mousePressed(QGraphicsSceneMouseEvent *event)
case CreatePolygon:
case CreatePolyline:
if (event->button() == Qt::RightButton) {
// The polygon needs to have at least three points and a
// polyline needs at least two.
int min = mMode == CreatePolygon ? 3 : 2;
if (mNewMapObjectItem->mapObject()->polygon().size() >= min)
finishNewMapObject();
else
cancelNewMapObject();
finishOrCancelPolygon();
} else if (event->button() == Qt::LeftButton) {
QPolygonF current = mNewMapObjectItem->mapObject()->polygon();
QPolygonF next = mOverlayPolygonObject->polygon();
Expand Down Expand Up @@ -377,3 +396,14 @@ void CreateObjectTool::finishNewMapObject()
objectGroup,
newMapObject));
}

void CreateObjectTool::finishOrCancelPolygon()
{
// The polygon needs to have at least three points and a
// polyline needs at least two.
int min = mMode == CreatePolygon ? 3 : 2;
if (mNewMapObjectItem->mapObject()->polygon().size() >= min)
finishNewMapObject();
else
cancelNewMapObject();
}
2 changes: 2 additions & 0 deletions src/tiled/createobjecttool.h
Expand Up @@ -49,6 +49,7 @@ class CreateObjectTool : public AbstractObjectTool

void deactivate(MapScene *scene);

void keyPressed(QKeyEvent *event);
void mouseEntered();
void mouseMoved(const QPointF &pos,
Qt::KeyboardModifiers modifiers);
Expand All @@ -69,6 +70,7 @@ public slots:
MapObject *clearNewMapObjectItem();
void cancelNewMapObject();
void finishNewMapObject();
void finishOrCancelPolygon();

MapObjectItem *mNewMapObjectItem;
ObjectGroup *mOverlayObjectGroup;
Expand Down

0 comments on commit f85f533

Please sign in to comment.