Beispiel #1
0
bool Clickable::updateAndGetClicked(sf::Vector2f mousePosition, bool isLeftDown)
{
	if(rect.contains(mousePosition.x, mousePosition.y))	// Mouse is over the clicking area
	{
		if(wasClicked)
		{
			if(!isLeftDown)	// Clicked and released inside the clicking area
			{
				wasHovered = true;
				wasClicked = false;
				hovered();

				return true;
			}
		}
		else
		{
			if(isLeftDown)	// Clicked in the clicking area
			{
				wasClicked = true;
				clicked();
			}
			else if(!wasHovered)
			{
				wasHovered = true;
				hovered();
			}
		}
	}
	else	// Mouse is not over the clicking area
	{
		if(wasHovered || wasClicked)
		{
			wasClicked = false;
			wasHovered = false;
			unhovered();
		}

		if(isLeftDown)
		{
			clickedOutside();
		}
	}

	return false;
}
bool MInverseMouseArea::eventFilter(QObject *obj, QEvent *ev)
{
    Q_UNUSED(obj);
    if (!m_enabled || !isVisible())
        return false;
    switch (ev->type()) {
    case QEvent::MouseButtonPress: {
        QMouseEvent *me = static_cast<QMouseEvent *>(ev);
        QPointF mappedPos = me->screenPos();//??is mapping to root item needed still
        m_pressed = !isUnderMouse() && !isClickedOnSoftwareInputPanel();

        if (m_pressed) {
            m_lastsceenPos = me->screenPos();
            emit pressedOutside(mappedPos.x(), mappedPos.y());
        }
        break;
    }
    case QEvent::MouseMove: {
        if (m_pressed) {
            QMouseEvent *me = static_cast<QMouseEvent *>(ev);
            const QPointF &dist = me->screenPos() - m_lastsceenPos;

            if (dist.x() * dist.x() + dist.y() * dist.y() > FlickThresholdSquare)
                m_pressed = false;
        }
        break;
    }
    case QEvent::MouseButtonRelease: {
        QMouseEvent *me = static_cast<QMouseEvent *>(ev);
        QPointF mappedPos = mapToRootItem(me->screenPos());

        if (m_pressed) {
            m_pressed = false;
            emit clickedOutside(mappedPos.x(), mappedPos.y());
        }
        break;
    }
    default:
        break;
    }

    return false;
}
bool MInverseMouseArea::eventFilter(QObject *obj, QEvent *ev)
{
    Q_UNUSED(obj);
    if (!m_enabled || !isVisible())
        return false;    
    switch (ev->type()) {
    case QEvent::GraphicsSceneMousePress: {
        QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(ev);
        QPointF mappedPos = mapToRootItem(me->scenePos());
        m_pressed = !contains(mapFromScene(me->scenePos())) && !isClickedOnSoftwareInputPanel(me);

        if (m_pressed)
            emit pressedOutside(mappedPos.x(), mappedPos.y());
        break;
    }
    case QEvent::GraphicsSceneMouseMove: {
        if (m_pressed) {
            QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(ev);
            const QPointF &dist = me->scenePos() - me->buttonDownScenePos(Qt::LeftButton);

            if (dist.x() * dist.x() + dist.y() * dist.y() > FlickThresholdSquare)
                m_pressed = false;
        }
        break;
    }
    case QEvent::GraphicsSceneMouseRelease: {
        QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(ev);
        QPointF mappedPos = mapToRootItem(me->scenePos());

        if (m_pressed) {
            m_pressed = false;
            emit clickedOutside(mappedPos.x(), mappedPos.y());
        }
        break;
    }
    default:
        break;
    }

    return false;
}