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);
setConnectionEditControlsEnabled(true);
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) {
@ -451,7 +453,7 @@ void Editor::createConnectionItem(Connection* connection, bool hide) {
map->connection_items.append(item);
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->setY(y);
connection_edit_item->setZValue(-1);
@ -496,6 +498,8 @@ void Editor::updateConnectionOffset(int offset) {
return;
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);
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);
@ -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)
{
if (change == ItemPositionChange) {
@ -700,6 +716,9 @@ QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVari
if (connection->direction == "up" || connection->direction == "down") {
x = round(newPos.x() / 16) * 16;
newOffset += (x - initialX) / 16;
newOffset = qMin(newOffset, this->getMaxOffset());
newOffset = qMax(newOffset, this->getMinOffset());
x = newOffset * 16;
}
else {
x = initialX;
@ -708,6 +727,9 @@ QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVari
if (connection->direction == "right" || connection->direction == "left") {
y = round(newPos.y() / 16) * 16;
newOffset += (y - initialY) / 16;
newOffset = qMin(newOffset, this->getMaxOffset());
newOffset = qMax(newOffset, this->getMinOffset());
y = newOffset * 16;
}
else {
y = initialY;

View file

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