Skip to content

Commit

Permalink
Just rotate/flip active currently moved block (improvement for #72 - …
Browse files Browse the repository at this point in the history
…second scenario)
  • Loading branch information
ElTh0r0 committed Jun 29, 2024
1 parent 7107c25 commit 91c97db
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
74 changes: 46 additions & 28 deletions block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,28 +234,30 @@ void Block::moveBlock(const bool bRelease) {

void Block::rotateBlock(const int nDelta) {
static const quint8 RIGHTANGLE = 90;
qint8 nAngle(0);
qreal nTranslateX(0);
qreal nTranslateY(0);

if (nDelta < 0) {
nAngle = RIGHTANGLE;
nTranslateX = this->boundingRect().height();
nTranslateY = 0;
} else {
nAngle = -RIGHTANGLE;
nTranslateX = 0;
nTranslateY = this->boundingRect().width();
if (m_bActive || !this->isAnyBlockActive()) {
qint8 nAngle(0);
qreal nTranslateX(0);
qreal nTranslateY(0);

if (nDelta < 0) {
nAngle = RIGHTANGLE;
nTranslateX = this->boundingRect().height();
nTranslateY = 0;
} else {
nAngle = -RIGHTANGLE;
nTranslateX = 0;
nTranslateY = this->boundingRect().width();
}
this->prepareGeometryChange();
// qDebug() << "Before rot.:" << m_nID << nAngle << "\n" << m_PolyShape;
m_pTransform->reset();
m_pTransform->rotate(nAngle);
m_PolyShape = m_pTransform->map(m_PolyShape); // Rotate
m_PolyShape.translate(nTranslateX, nTranslateY); // Move back
// qDebug() << "After rot.:" << m_PolyShape;
}
this->prepareGeometryChange();
// qDebug() << "Before rot.:" << m_nID << nAngle << "\n" << m_PolyShape;
m_pTransform->reset();
m_pTransform->rotate(nAngle);
m_PolyShape = m_pTransform->map(m_PolyShape); // Rotate
m_PolyShape.translate(nTranslateX, nTranslateY); // Move back
// qDebug() << "After rot.:" << m_PolyShape;

if (!m_bActive) {

if (!this->isAnyBlockActive()) {
this->checkBlockIntersection();
}
}
Expand All @@ -264,14 +266,16 @@ void Block::rotateBlock(const int nDelta) {
// ---------------------------------------------------------------------------

void Block::flipBlock() {
this->prepareGeometryChange();
// qDebug() << "Before flip" << m_nID << "-" << m_PolyShape;
QTransform transform = QTransform::fromScale(-1, 1);
m_PolyShape = transform.map(m_PolyShape); // Flip
m_PolyShape.translate(this->boundingRect().width(), 0); // Move back
// qDebug() << "After flip:" << m_PolyShape;
if (m_bActive || !this->isAnyBlockActive()) {
this->prepareGeometryChange();
// qDebug() << "Before flip" << m_nID << "-" << m_PolyShape;
QTransform transform = QTransform::fromScale(-1, 1);
m_PolyShape = transform.map(m_PolyShape); // Flip
m_PolyShape.translate(this->boundingRect().width(), 0); // Move back
// qDebug() << "After flip:" << m_PolyShape;
}

if (!m_bActive) {
if (!this->isAnyBlockActive()) {
this->checkBlockIntersection();
}
}
Expand Down Expand Up @@ -383,6 +387,18 @@ void Block::rescaleBlock(const quint16 nNewScale) {
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

auto Block::isAnyBlockActive() -> bool {
for (auto &pBlock : *m_pListBlocks) {
if (pBlock->isActive()) {
return true;
}
}
return false;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

auto Block::type() const -> int {
// Enable the use of qgraphicsitem_cast with this item
return Type;
Expand All @@ -392,6 +408,8 @@ auto Block::getIndex() const -> quint16 { return m_nID; }

auto Block::isBarrier() const -> bool { return m_bBarrier; }

auto Block::isActive() const -> bool { return m_bActive; }

auto Block::getPosition() const -> QPointF { return this->pos(); }

auto Block::getPolygon() const -> QPolygonF { return this->m_PolyShape; }
2 changes: 2 additions & 0 deletions block.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Block : public QGraphicsObject {

auto getPolygon() const -> QPolygonF;
auto isBarrier() const -> bool;
auto isActive() const -> bool;
void bringToForeground();
void rescaleBlock(const quint16 nNewScale);
auto getIndex() const -> quint16;
Expand All @@ -81,6 +82,7 @@ class Block : public QGraphicsObject {
void checkBlockIntersection();
auto snapToGrid(const QPointF point) const -> QPointF;
void resetBrushStyle() const;
auto isAnyBlockActive() -> bool;

void moveBlock(const bool bRelease = false);
void rotateBlock(const int nDelta = -1);
Expand Down

0 comments on commit 91c97db

Please sign in to comment.