diff --git a/src/canvaswidget.h b/src/canvaswidget.h --- a/src/canvaswidget.h +++ b/src/canvaswidget.h @@ -51,12 +51,12 @@ void updateBarDirection(); void startGame(); void updateCursor(); - void resetCursor(); void resetMousePosition(); protected: void focusOutEvent(QFocusEvent *event) override; void resizeEvent(QResizeEvent *event) override; + bool event(QEvent *event) override; private: //used when moving the bar with the keys diff --git a/src/canvaswidget.cpp b/src/canvaswidget.cpp --- a/src/canvaswidget.cpp +++ b/src/canvaswidget.cpp @@ -46,8 +46,7 @@ connect(rootObject(), SIGNAL(mousePressed()), this, SIGNAL(mousePressed())); // for handling mouse cursor - connect(rootObject(), SIGNAL(pausedChanged()), this, SLOT(updateCursor())); - connect(this, &CanvasWidget::gameEnded, this, &CanvasWidget::resetCursor); + connect(rootObject(), SIGNAL(ballMovingChanged()), this, SLOT(updateCursor())); } CanvasWidget::~CanvasWidget() @@ -68,11 +67,17 @@ QMetaObject::invokeMethod(rootObject(), "updateGeometry"); } +bool CanvasWidget::event(QEvent *event) +{ + if (event->type() == QEvent::Leave) { + updateCursor(); + } + return KgDeclarativeView::event(event); +} + void CanvasWidget::newGame() { QMetaObject::invokeMethod(rootObject(), "reset"); - - setCursor(QCursor(Qt::BlankCursor)); } void CanvasWidget::showLine(const QString &line, int lineNumber) @@ -122,33 +127,34 @@ Q_ARG(QVariant, m_barDirection)); } -void CanvasWidget::resetCursor() -{ - setCursor(QCursor(Qt::ArrowCursor)); -} - void CanvasWidget::updateCursor() { - bool paused = rootObject()->property("paused").toBool(); + const bool ballMoving = rootObject()->property("ballMoving").toBool(); - if (paused) { - resetCursor(); - } else { - // FIXME: move the cursor to where the bar is + if (ballMoving) { resetMousePosition(); - QCursor newCursor(Qt::BlankCursor); - newCursor.setPos(cursor().pos()); - setCursor(newCursor); + setCursor(Qt::BlankCursor); + } else { + setCursor(Qt::ArrowCursor); } } void CanvasWidget::resetMousePosition() { - // FIXME: the cursor's position is supposed to be reset, - // just doesn't work! - int barPosition = rootObject()->property("barCenter").toInt(); - QPoint p = mapToGlobal(QPoint(barPosition, 0)); - cursor().setPos(p.x(), cursor().pos().y()); + const QQuickItem *jailItem = rootObject()->property("jailItem").value(); + const QPoint jailItemTopLeft = mapToGlobal(QPoint(jailItem->x(), jailItem->y())); + QPoint p = QCursor::pos(); + if (p.x() < jailItemTopLeft.x()) { + QCursor::setPos(jailItemTopLeft.x(), p.y()); + } else if (p.x() > jailItemTopLeft.x() + jailItem->width()) { + QCursor::setPos(jailItemTopLeft.x() + jailItem->width(), p.y()); + } + + if (p.y() < jailItemTopLeft.y()) { + QCursor::setPos(p.x(), jailItemTopLeft.y()); + } else if (p.y() > jailItemTopLeft.y() + jailItem->height()) { + QCursor::setPos(p.x(), jailItemTopLeft.y() + jailItem->height()); + } } void CanvasWidget::focusOutEvent(QFocusEvent *event) diff --git a/src/qml/main.qml b/src/qml/main.qml --- a/src/qml/main.qml +++ b/src/qml/main.qml @@ -31,6 +31,8 @@ property bool paused: false property bool gameOver: false property bool gameWon: false + readonly property Item jailItem: bgOverlay + readonly property bool ballMoving: !paused && fireBallMessage.opacity !== 1 function updateGeometry() { var bw = Globals.BRICK_WIDTH*Globals.WIDTH + 1;