Skip to content

Commit

Permalink
add limits for resizing layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
garakmon committed Nov 12, 2024
1 parent c83474b commit 1163969
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
5 changes: 0 additions & 5 deletions include/core/editcommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,6 @@ class ShiftMetatiles : public QUndoCommand {
/// Implements a command to commit a map or border resize action.
class ResizeLayout : public QUndoCommand {
public:
// ResizeLayout(Layout *layout, QSize oldLayoutDimensions, QSize newLayoutDimensions,
// const Blockdata &oldMetatiles, const Blockdata &newMetatiles,
// QSize oldBorderDimensions, QSize newBorderDimensions,
// const Blockdata &oldBorder, const Blockdata &newBorder,
// QUndoCommand *parent = nullptr);
ResizeLayout(Layout *layout, QSize oldLayoutDimensions, QMargins newLayoutMargins,
const Blockdata &oldMetatiles, const Blockdata &newMetatiles,
QSize oldBorderDimensions, QSize newBorderDimensions,
Expand Down
3 changes: 3 additions & 0 deletions include/ui/movablerect.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ResizableRect : public QObject, public MovableRect
}

void updatePosFromRect(QRect newPos);
void setLimit(QRect limit) { this->limit = limit; }

protected:
void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
Expand All @@ -71,6 +72,8 @@ class ResizableRect : public QObject, public MovableRect
QPointF clickedPos = QPointF();
QRect clickedRect;

QRect limit = QRect();

int lineWidth = 8;

signals:
Expand Down
27 changes: 19 additions & 8 deletions src/ui/movablerect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,24 @@ void ResizableRect::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
break;
}

// lower bounds limits
if (resizedRect.width() < 16)
resizedRect.setWidth(16);
if (resizedRect.height() < 16)
resizedRect.setHeight(16);

// TODO: upper bound limits
// lower bounds limits, smallest possible size is 16x16 square
if (resizedRect.width() < 16) {
if (dx < 0) { // right sided adjustment made
resizedRect.setWidth(16);
} else { // left sided adjustment slightly more complicated
int dxMax = this->clickedRect.right() - this->clickedRect.left() - 16;
resizedRect.adjust(dxMax - dx, 0, 0, 0);
}
}
if (resizedRect.height() < 16) {
if (dy < 0) { // bottom
resizedRect.setHeight(16);
} else { // top
int dyMax = this->clickedRect.bottom() - this->clickedRect.top() - 16;
resizedRect.adjust(0, dyMax - dy, 0, 0);
}
}

this->updatePosFromRect(resizedRect);
// Upper bounds: clip resized to limit rect
this->updatePosFromRect(resizedRect & this->limit);
}
5 changes: 3 additions & 2 deletions src/ui/resizelayoutpopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ void CheckeredBgScene::drawBackground(QPainter *painter, const QRectF &rect) {
for (int x = xMin, xTile = 0; x <= xMax; x += this->gridSize, xTile++) {
for (int y = yMin, yTile = 0; y <= yMax; y += this->gridSize, yTile++) {
if (!((xTile ^ yTile) & 1)) { // tile numbers have same parity (evenness)
if (this->validRect.contains(x, y)) // check if inside validRect
if (this->validRect.contains(x, y))
paintColor = QColor(132, 217, 165); // green light color
else
paintColor = 0xbcbcbc; // normal light color
}
else {
if (this->validRect.contains(x, y)) // check if inside validRect
if (this->validRect.contains(x, y))
paintColor = QColor(76, 178, 121); // green dark color
else
paintColor = 0x969696; // normal dark color
Expand Down Expand Up @@ -142,6 +142,7 @@ void ResizeLayoutPopup::setupLayoutView() {
static bool layoutSizeRectVisible = true;

this->outline = new ResizableRect(this, &layoutSizeRectVisible, this->editor->layout->getWidth(), this->editor->layout->getHeight(), qRgb(255, 0, 255));
this->outline->setLimit(cover->rect().toAlignedRect());
connect(outline, &ResizableRect::rectUpdated, [=](QRect rect){
this->scene->setValidRect(rect);
this->ui->spinBox_width->setValue(rect.width() / 16);
Expand Down

0 comments on commit 1163969

Please sign in to comment.