Limit connection offsets to reasonable values

This commit is contained in:
Marcus Huderle 2018-03-11 14:01:29 -07:00
parent 9b0f686781
commit f47e3bf4ea
2 changed files with 30 additions and 2 deletions

View file

@ -257,6 +257,8 @@ void Editor::onConnectionItemSelected(ConnectionPixmapItem* connectionItem) {
current_connection_edit_item->setZValue(0); current_connection_edit_item->setZValue(0);
setConnectionEditControlsEnabled(true); setConnectionEditControlsEnabled(true);
setConnectionEditControlValues(current_connection_edit_item->connection); setConnectionEditControlValues(current_connection_edit_item->connection);
ui->spinBox_ConnectionOffset->setMaximum(current_connection_edit_item->getMaxOffset());
ui->spinBox_ConnectionOffset->setMinimum(current_connection_edit_item->getMinOffset());
} }
void Editor::onConnectionDirectionChanged(QString newDirection) { void Editor::onConnectionDirectionChanged(QString newDirection) {
@ -451,7 +453,7 @@ void Editor::createConnectionItem(Connection* connection, bool hide) {
map->connection_items.append(item); map->connection_items.append(item);
item->setVisible(!hide); item->setVisible(!hide);
ConnectionPixmapItem *connection_edit_item = new ConnectionPixmapItem(pixmap, connection, x, y); ConnectionPixmapItem *connection_edit_item = new ConnectionPixmapItem(pixmap, connection, x, y, map->getWidth(), map->getHeight());
connection_edit_item->setX(x); connection_edit_item->setX(x);
connection_edit_item->setY(y); connection_edit_item->setY(y);
connection_edit_item->setZValue(-1); connection_edit_item->setZValue(-1);
@ -496,6 +498,8 @@ void Editor::updateConnectionOffset(int offset) {
return; return;
current_connection_edit_item->blockSignals(true); current_connection_edit_item->blockSignals(true);
offset = qMin(offset, current_connection_edit_item->getMaxOffset());
offset = qMax(offset, current_connection_edit_item->getMinOffset());
current_connection_edit_item->connection->offset = QString::number(offset); current_connection_edit_item->connection->offset = QString::number(offset);
if (current_connection_edit_item->connection->direction == "up" || current_connection_edit_item->connection->direction == "down") { if (current_connection_edit_item->connection->direction == "up" || current_connection_edit_item->connection->direction == "down") {
current_connection_edit_item->setX(current_connection_edit_item->initialX + (offset - current_connection_edit_item->initialOffset) * 16); current_connection_edit_item->setX(current_connection_edit_item->initialX + (offset - current_connection_edit_item->initialOffset) * 16);
@ -690,6 +694,18 @@ void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
} }
} }
int ConnectionPixmapItem::getMinOffset() {
if (connection->direction == "up" || connection->direction == "down")
return 1 - (this->pixmap().width() / 16);
else
return 1 - (this->pixmap().height() / 16);
}
int ConnectionPixmapItem::getMaxOffset() {
if (connection->direction == "up" || connection->direction == "down")
return baseMapWidth - 1;
else
return baseMapHeight - 1;
}
QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value) QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
{ {
if (change == ItemPositionChange) { if (change == ItemPositionChange) {
@ -700,6 +716,9 @@ QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVari
if (connection->direction == "up" || connection->direction == "down") { if (connection->direction == "up" || connection->direction == "down") {
x = round(newPos.x() / 16) * 16; x = round(newPos.x() / 16) * 16;
newOffset += (x - initialX) / 16; newOffset += (x - initialX) / 16;
newOffset = qMin(newOffset, this->getMaxOffset());
newOffset = qMax(newOffset, this->getMinOffset());
x = newOffset * 16;
} }
else { else {
x = initialX; x = initialX;
@ -708,6 +727,9 @@ QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVari
if (connection->direction == "right" || connection->direction == "left") { if (connection->direction == "right" || connection->direction == "left") {
y = round(newPos.y() / 16) * 16; y = round(newPos.y() / 16) * 16;
newOffset += (y - initialY) / 16; newOffset += (y - initialY) / 16;
newOffset = qMin(newOffset, this->getMaxOffset());
newOffset = qMax(newOffset, this->getMinOffset());
y = newOffset * 16;
} }
else { else {
y = initialY; y = initialY;

View file

@ -270,7 +270,7 @@ protected:
class ConnectionPixmapItem : public QObject, public QGraphicsPixmapItem { class ConnectionPixmapItem : public QObject, public QGraphicsPixmapItem {
Q_OBJECT Q_OBJECT
public: public:
ConnectionPixmapItem(QPixmap pixmap, Connection* connection, int x, int y): QGraphicsPixmapItem(pixmap) { ConnectionPixmapItem(QPixmap pixmap, Connection* connection, int x, int y, int baseMapWidth, int baseMapHeight): QGraphicsPixmapItem(pixmap) {
this->basePixmap = pixmap; this->basePixmap = pixmap;
this->connection = connection; this->connection = connection;
setFlag(ItemIsMovable); setFlag(ItemIsMovable);
@ -278,6 +278,8 @@ public:
this->initialX = x; this->initialX = x;
this->initialY = y; this->initialY = y;
this->initialOffset = connection->offset.toInt(); this->initialOffset = connection->offset.toInt();
this->baseMapWidth = baseMapWidth;
this->baseMapHeight = baseMapHeight;
} }
void render(qreal opacity = 1) { void render(qreal opacity = 1) {
QPixmap newPixmap = basePixmap.copy(0, 0, basePixmap.width(), basePixmap.height()); QPixmap newPixmap = basePixmap.copy(0, 0, basePixmap.width(), basePixmap.height());
@ -289,11 +291,15 @@ public:
} }
this->setPixmap(newPixmap); this->setPixmap(newPixmap);
} }
int getMinOffset();
int getMaxOffset();
QPixmap basePixmap; QPixmap basePixmap;
Connection* connection; Connection* connection;
int initialX; int initialX;
int initialY; int initialY;
int initialOffset; int initialOffset;
int baseMapWidth;
int baseMapHeight;
protected: protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value); QVariant itemChange(GraphicsItemChange change, const QVariant &value);
void mousePressEvent(QGraphicsSceneMouseEvent*); void mousePressEvent(QGraphicsSceneMouseEvent*);