Commit aff7d4be authored by Dmitry Suzdalev's avatar Dmitry Suzdalev
Browse files

Balls became selectable

svn path=/trunk/KDE/kdegames/klines/; revision=615693
parent 48a30541
......@@ -20,6 +20,7 @@
* Boston, MA 02110-1301, USA.
*
********************************************************************/
#include <kdebug.h>
#include "ballitem.h"
#include "renderer.h"
......@@ -43,20 +44,43 @@ void BallItem::startAnimation( BallAnimationType type )
switch(type)
{
case SelectedAnimation:
m_timeLine.setDuration(500);
m_timeLine.setDuration(400);
m_timeLine.setLoopCount(0);
m_timeLine.setFrameRange(0, 9);
m_timeLine.setCurveShape( QTimeLine::LinearCurve );
// in a balls.jpg we have 9 effective frames
// of raising the ball up. lowing down frames are the
// same 9 frames only played in opposite direction.
// So I setup timeline for 0-18 frame range and in animFrameChanged()
// i'll make sure that after frame number will go past 9 it
// will be recalculated to be 8,7,6,...,0 etc
m_timeLine.setFrameRange(0, 9*2);
// starting by going lower
m_timeLine.setCurrentTime( m_timeLine.duration()/2 );
break;
}
m_timeLine.start();
}
void BallItem::stopAnimation()
{
switch(m_curAnim)
{
case SelectedAnimation:
m_timeLine.stop();
setPixmap( m_renderer->ballPixmap(m_color) );
break;
}
}
void BallItem::animFrameChanged(int frame)
{
switch(m_curAnim)
{
case SelectedAnimation:
// if frame > 9, make it go in opposite direction
// See also comment in startAnimation()
if(frame > 9)
frame = frame - (frame-9)*2;
setPixmap(m_renderer->animationFrame( m_color, m_curAnim, frame ));
break;
}
......
......@@ -21,6 +21,7 @@
*
********************************************************************/
#include <QResizeEvent>
#include <QGraphicsSceneMouseEvent>
#include <kdebug.h>
......@@ -42,14 +43,15 @@ void KLinesView::resizeEvent( QResizeEvent* ev )
// =============== KLinesScene =======================
KLinesScene::KLinesScene( QObject* parent )
: QGraphicsScene(parent), m_numBalls(0)
: QGraphicsScene(parent), m_selectedBall(0), m_numBalls(0)
{
m_renderer = new KLinesRenderer;
nextThreeBalls();
for(int x=0; x<FIELD_SIZE; ++x)
for(int y=0; y<FIELD_SIZE; ++y)
m_field[x][y] = 0;
nextThreeBalls();
}
KLinesScene::~KLinesScene()
......@@ -95,13 +97,22 @@ void KLinesScene::placeRandomBall()
m_field[posx][posy] = newBall;
//newBall->startAnimation( BornAnimation );
newBall->startAnimation( SelectedAnimation );
m_numBalls++;
}
void KLinesScene::mouseReleaseEvent( QGraphicsSceneMouseEvent* )
void KLinesScene::mousePressEvent( QGraphicsSceneMouseEvent* ev )
{
nextThreeBalls();
int fx = pixToFieldX(ev->scenePos());
int fy = pixToFieldY(ev->scenePos());
if( m_field[fx][fy] )
{
if( m_selectedBall )
m_selectedBall->stopAnimation();
m_field[fx][fy]->startAnimation( SelectedAnimation );
m_selectedBall = m_field[fx][fy];
}
}
void KLinesScene::drawBackground(QPainter *p, const QRectF&)
......
......@@ -56,9 +56,11 @@ public:
inline QPointF fieldToPix(int fieldX, int fieldY) const {
return QPointF( fieldX*32+2, fieldY*32+2 );
}
inline int pixToFieldX( const QPointF& p ) const { return static_cast<int>(p.x()/32); }
inline int pixToFieldY( const QPointF& p ) const { return static_cast<int>(p.y()/32); }
private:
virtual void drawBackground( QPainter*, const QRectF& );
virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent* );
virtual void mousePressEvent( QGraphicsSceneMouseEvent* );
/**
* This array represents the play field.
......@@ -68,6 +70,10 @@ private:
BallItem* m_field[FIELD_SIZE][FIELD_SIZE];
KLinesRenderer* m_renderer;
KRandomSequence m_randomSeq;
/**
* Currently selected ball (0 if none)
*/
BallItem *m_selectedBall;
/**
* Number of balls currently in field
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment