/*
This file is part of "bounce" by Gunnstein Lye <gl@ez.no>
Copyright (C) 2000 eZ systems as, Norway
(http://www.ez.no)
bounce is part of a QCanvas tutorial on www.zez.org.
bounce is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
You should have received a copy of the GNU General Public License
along with bounce; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include "bouncecanvasview.hpp"
#include "ball.hpp"
#include <qwidget.h>
#include <qevent.h>
#include <qtimer.h>
BounceCanvasView::BounceCanvasView( QCanvas *canvas, QWidget *parent, const char *name )
: QCanvasView( canvas, parent, name )
{
// create and setup a timer
Timer = new QTimer( this, "Timer" );
connect( Timer, SIGNAL( timeout() ), this, SLOT( timeout() ) );
MousePosQueue.setAutoDelete( true ); // delete removed items
Moving = 0; // we are not holding the ball
}
BounceCanvasView::~BounceCanvasView()
{
}
void BounceCanvasView::contentsMousePressEvent( QMouseEvent *e )
{
// a list of all items underneath the mouse when it was pressed
QCanvasItemList list = canvas()->collisions(e->pos());
// the RTTI of a QCanvasSprite is 1...
if ( !list.isEmpty() && list.first()->rtti() == 1 )
{
// ...which means we can safely cast to a Ball pointer.
Ball *sphere = (Ball*)list.first();
// hit() will return false it we hit the transparent corners
// of the planet image.
if ( sphere->hit( e->pos() ) )
{
Moving = sphere;
canvas()->setAdvancePeriod( -1 ); // stops the animation when dragging
MovingStart = e->pos();
Moving->setZ( 2 ); // raises the current item above other items when dragging.
// (This in not really necessary here, since we have just one item.)
Timer->start( 10, false ); // start the timer
}
}
}
void BounceCanvasView::contentsMouseMoveEvent( QMouseEvent *e )
{
if ( Moving ) // if we are holding the ball...
{
// ...then move it
Moving->moveBy( e->pos().x() - MovingStart.x(),
e->pos().y() - MovingStart.y() );
MovingStart = e->pos();
canvas()->update(); // update visual change to canvas
}
}
void BounceCanvasView::contentsMouseReleaseEvent( QMouseEvent* )
{
if ( Moving ) // if we are holding the ball
{
Timer->stop(); // stop the timer
if ( MousePosQueue.count() ) // if we have recorded mouse positions...
{
// ...then calculate average velocity
int count = MousePosQueue.count();
QPoint velocity( 0, 0 );
QPoint pos;
QPoint prevPos = *MousePosQueue.head();
while ( MousePosQueue.count() )
{
pos = *MousePosQueue.dequeue();
velocity += pos - prevPos;
prevPos = pos;
}
// set the balls new velocity
Moving->setXVelocity( velocity.x() / count );
Moving->setYVelocity( velocity.y() / count );
}
else // if we haven't recorded anything (fast click)
{
// set zero velocity (we drop the ball)
Moving->setXVelocity( 0 );
Moving->setYVelocity( 0 );
}
Moving->setZ( 1 );
canvas()->setAdvancePeriod( 10 ); // restart the animation
Moving = 0; // clear the pointer, we are no longer holding the ball
}
}
void BounceCanvasView::timeout()
{
MousePosQueue.enqueue( new QPoint( MovingStart ) ); // record position
if ( MousePosQueue.count() > 10 ) // if larger than 10 items
MousePosQueue.remove(); // remove one item
}
没有合适的资源?快使用搜索试试~ 我知道了~
一个非常有趣的qt小实例
1星 需积分: 50 33 下载量 84 浏览量
2013-05-08
22:50:10
上传
评论 2
收藏 28KB GZ 举报
温馨提示
共12个文件
cpp:4个
hpp:3个
png:2个
一个用qt 画布制作的qt弹球实例,非常值得初学者学习研究。
资源推荐
资源详情
资源评论
收起资源包目录
bounce.tar.gz (12个子文件)
bounce
earth.png 14KB
COPYING 18KB
bouncecanvasview.hpp 1KB
stars.png 2KB
bounce.pro 1KB
bouncewidget.cpp 2KB
main.cpp 934B
ball.hpp 1KB
bouncewidget.hpp 1KB
ball.cpp 3KB
README 473B
bouncecanvasview.cpp 4KB
共 12 条
- 1
资源评论
- qq_498308342022-01-26不能用,不建议下载
wg_chn
- 粉丝: 16
- 资源: 69
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功