Browse Source

Limit connection offsets to reasonable values

Marcus Huderle 6 years ago
parent
commit
f47e3bf4ea
2 changed files with 30 additions and 2 deletions
  1. 23
    1
      editor.cpp
  2. 7
    1
      editor.h

+ 23
- 1
editor.cpp View File

@@ -257,6 +257,8 @@ void Editor::onConnectionItemSelected(ConnectionPixmapItem* connectionItem) {
257 257
     current_connection_edit_item->setZValue(0);
258 258
     setConnectionEditControlsEnabled(true);
259 259
     setConnectionEditControlValues(current_connection_edit_item->connection);
260
+    ui->spinBox_ConnectionOffset->setMaximum(current_connection_edit_item->getMaxOffset());
261
+    ui->spinBox_ConnectionOffset->setMinimum(current_connection_edit_item->getMinOffset());
260 262
 }
261 263
 
262 264
 void Editor::onConnectionDirectionChanged(QString newDirection) {
@@ -451,7 +453,7 @@ void Editor::createConnectionItem(Connection* connection, bool hide) {
451 453
     map->connection_items.append(item);
452 454
     item->setVisible(!hide);
453 455
 
454
-    ConnectionPixmapItem *connection_edit_item = new ConnectionPixmapItem(pixmap, connection, x, y);
456
+    ConnectionPixmapItem *connection_edit_item = new ConnectionPixmapItem(pixmap, connection, x, y, map->getWidth(), map->getHeight());
455 457
     connection_edit_item->setX(x);
456 458
     connection_edit_item->setY(y);
457 459
     connection_edit_item->setZValue(-1);
@@ -496,6 +498,8 @@ void Editor::updateConnectionOffset(int offset) {
496 498
         return;
497 499
 
498 500
     current_connection_edit_item->blockSignals(true);
501
+    offset = qMin(offset, current_connection_edit_item->getMaxOffset());
502
+    offset = qMax(offset, current_connection_edit_item->getMinOffset());
499 503
     current_connection_edit_item->connection->offset = QString::number(offset);
500 504
     if (current_connection_edit_item->connection->direction == "up" || current_connection_edit_item->connection->direction == "down") {
501 505
         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) {
690 694
     }
691 695
 }
692 696
 
697
+int ConnectionPixmapItem::getMinOffset() {
698
+    if (connection->direction == "up" || connection->direction == "down")
699
+        return 1 - (this->pixmap().width() / 16);
700
+    else
701
+        return 1 - (this->pixmap().height() / 16);
702
+}
703
+int ConnectionPixmapItem::getMaxOffset() {
704
+    if (connection->direction == "up" || connection->direction == "down")
705
+        return baseMapWidth - 1;
706
+    else
707
+        return baseMapHeight - 1;
708
+}
693 709
 QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
694 710
 {
695 711
     if (change == ItemPositionChange) {
@@ -700,6 +716,9 @@ QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVari
700 716
         if (connection->direction == "up" || connection->direction == "down") {
701 717
             x = round(newPos.x() / 16) * 16;
702 718
             newOffset += (x - initialX) / 16;
719
+            newOffset = qMin(newOffset, this->getMaxOffset());
720
+            newOffset = qMax(newOffset, this->getMinOffset());
721
+            x = newOffset * 16;
703 722
         }
704 723
         else {
705 724
             x = initialX;
@@ -708,6 +727,9 @@ QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVari
708 727
         if (connection->direction == "right" || connection->direction == "left") {
709 728
             y = round(newPos.y() / 16) * 16;
710 729
             newOffset += (y - initialY) / 16;
730
+            newOffset = qMin(newOffset, this->getMaxOffset());
731
+            newOffset = qMax(newOffset, this->getMinOffset());
732
+            y = newOffset * 16;
711 733
         }
712 734
         else {
713 735
             y = initialY;

+ 7
- 1
editor.h View File

@@ -270,7 +270,7 @@ protected:
270 270
 class ConnectionPixmapItem : public QObject, public QGraphicsPixmapItem {
271 271
     Q_OBJECT
272 272
 public:
273
-    ConnectionPixmapItem(QPixmap pixmap, Connection* connection, int x, int y): QGraphicsPixmapItem(pixmap) {
273
+    ConnectionPixmapItem(QPixmap pixmap, Connection* connection, int x, int y, int baseMapWidth, int baseMapHeight): QGraphicsPixmapItem(pixmap) {
274 274
         this->basePixmap = pixmap;
275 275
         this->connection = connection;
276 276
         setFlag(ItemIsMovable);
@@ -278,6 +278,8 @@ public:
278 278
         this->initialX = x;
279 279
         this->initialY = y;
280 280
         this->initialOffset = connection->offset.toInt();
281
+        this->baseMapWidth = baseMapWidth;
282
+        this->baseMapHeight = baseMapHeight;
281 283
     }
282 284
     void render(qreal opacity = 1) {
283 285
         QPixmap newPixmap = basePixmap.copy(0, 0, basePixmap.width(), basePixmap.height());
@@ -289,11 +291,15 @@ public:
289 291
         }
290 292
         this->setPixmap(newPixmap);
291 293
     }
294
+    int getMinOffset();
295
+    int getMaxOffset();
292 296
     QPixmap basePixmap;
293 297
     Connection* connection;
294 298
     int initialX;
295 299
     int initialY;
296 300
     int initialOffset;
301
+    int baseMapWidth;
302
+    int baseMapHeight;
297 303
 protected:
298 304
     QVariant itemChange(GraphicsItemChange change, const QVariant &value);
299 305
     void mousePressEvent(QGraphicsSceneMouseEvent*);