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
     current_connection_edit_item->setZValue(0);
257
     current_connection_edit_item->setZValue(0);
258
     setConnectionEditControlsEnabled(true);
258
     setConnectionEditControlsEnabled(true);
259
     setConnectionEditControlValues(current_connection_edit_item->connection);
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
 void Editor::onConnectionDirectionChanged(QString newDirection) {
264
 void Editor::onConnectionDirectionChanged(QString newDirection) {
451
     map->connection_items.append(item);
453
     map->connection_items.append(item);
452
     item->setVisible(!hide);
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
     connection_edit_item->setX(x);
457
     connection_edit_item->setX(x);
456
     connection_edit_item->setY(y);
458
     connection_edit_item->setY(y);
457
     connection_edit_item->setZValue(-1);
459
     connection_edit_item->setZValue(-1);
496
         return;
498
         return;
497
 
499
 
498
     current_connection_edit_item->blockSignals(true);
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
     current_connection_edit_item->connection->offset = QString::number(offset);
503
     current_connection_edit_item->connection->offset = QString::number(offset);
500
     if (current_connection_edit_item->connection->direction == "up" || current_connection_edit_item->connection->direction == "down") {
504
     if (current_connection_edit_item->connection->direction == "up" || current_connection_edit_item->connection->direction == "down") {
501
         current_connection_edit_item->setX(current_connection_edit_item->initialX + (offset - current_connection_edit_item->initialOffset) * 16);
505
         current_connection_edit_item->setX(current_connection_edit_item->initialX + (offset - current_connection_edit_item->initialOffset) * 16);
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
 QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
709
 QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
694
 {
710
 {
695
     if (change == ItemPositionChange) {
711
     if (change == ItemPositionChange) {
700
         if (connection->direction == "up" || connection->direction == "down") {
716
         if (connection->direction == "up" || connection->direction == "down") {
701
             x = round(newPos.x() / 16) * 16;
717
             x = round(newPos.x() / 16) * 16;
702
             newOffset += (x - initialX) / 16;
718
             newOffset += (x - initialX) / 16;
719
+            newOffset = qMin(newOffset, this->getMaxOffset());
720
+            newOffset = qMax(newOffset, this->getMinOffset());
721
+            x = newOffset * 16;
703
         }
722
         }
704
         else {
723
         else {
705
             x = initialX;
724
             x = initialX;
708
         if (connection->direction == "right" || connection->direction == "left") {
727
         if (connection->direction == "right" || connection->direction == "left") {
709
             y = round(newPos.y() / 16) * 16;
728
             y = round(newPos.y() / 16) * 16;
710
             newOffset += (y - initialY) / 16;
729
             newOffset += (y - initialY) / 16;
730
+            newOffset = qMin(newOffset, this->getMaxOffset());
731
+            newOffset = qMax(newOffset, this->getMinOffset());
732
+            y = newOffset * 16;
711
         }
733
         }
712
         else {
734
         else {
713
             y = initialY;
735
             y = initialY;

+ 7
- 1
editor.h View File

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