void Editor::update( float dt )
{
	vec2f scrollDir( 0.0f, 0.0f );

	// Continuous (key state) keys
	Uint8 *keyState = SDL_GetKeyState( NULL );
	if ((keyState[SDLK_LEFT]) && (!keyState[SDLK_RIGHT]))
	{
		scrollDir.x = -1.0;
	}
	else if ((!keyState[SDLK_LEFT]) && (keyState[SDLK_RIGHT]))
	{
		scrollDir.x = 1.0;
	}
	if ((keyState[SDLK_UP]) && (!keyState[SDLK_DOWN]))
	{
		scrollDir.y = 1.0;
	}
	else if ((!keyState[SDLK_UP]) && (keyState[SDLK_DOWN]))
	{
		scrollDir.y = -1.0;
	}

	// update view
	if (m_useEditView)
	{
		m_viewport += scrollDir * _TV( 0.6f ) * m_viewsize.x * dt;
	}
	else
	{
		m_gameview += scrollDir * _TV( 200.0f ) * dt;
	}

	// update mouse
	int mx, my;
	SDL_GetMouseState( &mx, &my );
	my = 600-my;
	if (m_activeShape)
	{
		if (m_useEditView)
		{
			m_activeShape->pos.x = m_viewport.x + ((float)mx / 800.0)*m_viewsize.x;
			m_activeShape->pos.y = m_viewport.y + ((float)my / 600.0)*m_viewsize.y;			
		}
		else
		{
			m_activeShape->pos.x = mx + m_gameview.x;
			m_activeShape->pos.y = my + m_gameview.y;
		}
		
		// remember this pos for otehr use
		m_mousePos = m_activeShape->pos;

		// offset so active shape is centered
		m_activeShape->pos.x -= m_activeShape->m_size.x/2;
		m_activeShape->pos.y -= m_activeShape->m_size.y/2;
	}
}
Ejemplo n.º 2
0
/*!
    Starts scrolling the widget so that the point \a pos is visible inside
    the viewport.

    If the specified point cannot be reached, the contents are scrolled to the
    nearest valid position (in this case the scroller might or might not overshoot).

    The scrolling speed will be calculated so that the given position will
    be reached after a platform-defined time span (1 second for Maemo 5).
    The final speed at the end position is not guaranteed to be zero.

    \sa ensureVisible(), maximumContentPosition()
*/
void QKineticScroller::scrollTo(const QPointF &pos, int scrollTime)
{
    Q_D(QKineticScroller);

    if (scrollTime <= 0)
        scrollTime = 1;

    qKSDebug() << "QKS::scrollTo(" << pos << " [pix], " << scrollTime << " [ms])";

    qreal time = qreal(scrollTime) / 1000;

    if ((pos == contentPosition()) ||
            (d->state == QKineticScroller::StatePressed) ||
            (d->state == QKineticScroller::StateDragging)) {
        return;
    }

    // estimate the minimal start velocity
    // if the start velocity is below that then the scrolling would stop before scrollTime.
    //qreal vMin = d->linearDecelerationFactor * time / qPow(d->exponentialDecelerationBase, time);

    /*
    // estimate of the distance passed within the vMin time during scrollTime
    qreal distMin = qreal(scrollTime) * vMin / 2.0;

    QPointF v = QPointF((pos.x()-contentPosition().x()) / distMin * vMin,
                        (pos.y()-contentPosition().y()) / distMin * vMin);

    */

    // v(t) = vstart * exponentialDecelerationBase ^ t - linearDecelerationFactor * t
    // pos(t) = integrate(v(t) * dt)
    // pos(t) = vstart * (eDB ^ t / ln(eDB) + C) -  lDF / 2 * t ^ 2
    //
    // pos(time) = pos - contentsPos()
    // vstart = ((lDF / 2) * time ^ 2 + (pos - contentPos())) / (eDB ^ time / ln(eDB) + C)
    // (for C = -1/ln(eDB) )

    QPointF scrollDir(qSign(pos.x() - contentPosition().x()),
                      qSign(pos.y() - contentPosition().y()));

    QPointF v = (scrollDir * (d->linearDecelerationFactor / qreal(2)) * qreal(time) * qreal(time) + (pos - contentPosition()) / d->pixelPerMeter);
    if (d->exponentialDecelerationBase != qreal(1))
        v /= (qPow(d->exponentialDecelerationBase, time) - 1) / qLn(d->exponentialDecelerationBase);
    else
        v /= time;

    d->scrollToPosition = pos;
    d->scrollToX = true;
    d->scrollToY = true;
    d->releaseVelocity = v;
    d->scrollRelativeTimer.restart();
    d->scrollAbsoluteTimer.restart();
    d->setState(QKineticScroller::StateScrolling);
}