Commit 48a30541 authored by Dmitry Suzdalev's avatar Dmitry Suzdalev
Browse files

Furhter QGV porting:

Field is drawn, balls are drawn.
No gameplay. Just testing this and that features.
New clicks bring in new balls. New animating balls.
They're really funny :).
If you'd like to have some fun, update, compile klines
and make several clicks on game area.
They're really cute, believe me :).

But beware! If your clicks will fill the whole board with
ballsies, the Great Hungry Infinite Loop will come and eat
them all :-).

svn path=/trunk/KDE/kdegames/klines/; revision=615446
parent ddc2ff07
......@@ -20,11 +20,46 @@
* Boston, MA 02110-1301, USA.
*
********************************************************************/
#include "ballitem.h"
#include "renderer.h"
BallItem::BallItem( QGraphicsScene* parent, const KLinesRenderer* renderer )
: QGraphicsPixmapItem( 0, parent ), m_renderer(renderer)
{
m_color = NumColors; // = uninitialized
connect(&m_timeLine, SIGNAL(frameChanged(int)), SLOT(animFrameChanged(int)) );
}
void BallItem::setColor( BallColor c )
{
m_color = c;
setPixmap( m_renderer->ballPixmap(c) );
}
void BallItem::startAnimation( BallAnimationType type )
{
m_curAnim = type;
switch(type)
{
case SelectedAnimation:
m_timeLine.setDuration(500);
m_timeLine.setLoopCount(0);
m_timeLine.setFrameRange(0, 9);
m_timeLine.setCurveShape( QTimeLine::LinearCurve );
break;
}
m_timeLine.start();
}
void BallItem::animFrameChanged(int frame)
{
switch(m_curAnim)
{
case SelectedAnimation:
setPixmap(m_renderer->animationFrame( m_color, m_curAnim, frame ));
break;
}
}
#include "ballitem.moc"
......@@ -24,39 +24,59 @@
#define BALL_ITEM_H
#include <QGraphicsPixmapItem>
#include <QTimeLine>
#include "commondefs.h"
class QTimeLine;
class KLinesRenderer;
class BallItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
BallItem( QGraphicsScene* parent, KLinesRenderer* r );
BallItem( QGraphicsScene* parent, const KLinesRenderer* r );
void setColor( BallColor c );
BallColor color() const { return m_color; }
/**
* Starts animation
* Starts animation.
* When animation will finish, animationFinished() signal
* will be emitted (except for animation types which are looped)
*
* @param type type of animation sequence to play
*/
void startAnimation( BallAnimationType type );
/**
* Interrupts current animation
*/
void stopAnimation();
signals:
/**
* Emitted when animation finishes.
* If current animation type is looped, this signal won't be emitted
*/
// FIXME dimsuz: implement
void animationFinished();
private slots:
void animFrameChanged(int);
private:
/**
* Renderer used to render ball's pixmaps
*/
KLinesRenderer* m_renderer;
const KLinesRenderer* m_renderer;
/**
* Timeline for controlling animations
*/
QTimeLine *m_timeLine;
QTimeLine m_timeLine;
/**
* Color of the ball
*/
BallColor m_color;
/**
* Type of running animation
* FIXME dimsuz: reset to something like NoAnimation when it isnt running
*/
BallAnimationType m_curAnim;
};
#endif
......@@ -24,6 +24,6 @@
#define COMMON_DEFINES_H
enum BallAnimationType { SelectedAnimation, BornAnimation, BurnAnimation };
enum BallColor { Red, Green, Blue, Yellow, Violet, Cyan, Brown };
enum BallColor { Blue=0, Brown, Cyan, Green, Red, Violet, Yellow, NumColors };
#endif
......@@ -36,7 +36,10 @@ MainWidget::MainWidget( QWidget* parent )
lsb = new LinesBoard(bPainter, this);
// grid->addWidget( lsb );
lsb->hide();
grid->addWidget( new KLinesView( new KLinesScene(this), this ) );
KLinesScene* klscene = new KLinesScene(this);
KLinesView* klview = new KLinesView( klscene, this );
klview->setCacheMode( QGraphicsView::CacheBackground );
grid->addWidget( klview );
QBoxLayout *right = new QVBoxLayout;
grid->addLayout(right);
......
......@@ -27,11 +27,32 @@
KLinesRenderer::KLinesRenderer()
{
m_fieldPix = QPixmap( KStandardDirs::locate( "appdata", "field.jpg" ));
m_ballsPix = QPixmap( KStandardDirs::locate( "appdata", "balls.jpg" ));
}
QPixmap KLinesRenderer::ballPixmap(BallColor) const
QPixmap KLinesRenderer::ballPixmap(BallColor color) const
{
return QPixmap();
// FIXME dimsuz: copying every time: not very efficient.
// FIXME dimsuz: hardcoded "magic" numbers
// Switching to svg will make this fixmes obsolete
// col, row, width, height - hardcoded. balls.jpg has such a format.
return m_ballsPix.copy( 9*30, static_cast<int>(color)*30, 30, 30 );
}
QPixmap KLinesRenderer::animationFrame( BallColor color, BallAnimationType type, int frameNo ) const
{
// FIXME dimsuz: copying every time: not very efficient.
// FIXME dimsuz: hardcoded "magic" numbers
// Switching to svg will make this fixmes obsolete
switch(type)
{
case SelectedAnimation:
return m_ballsPix.copy( frameNo*30, static_cast<int>(color)*30, 30, 30 );
default:
return QPixmap();
}
}
QPixmap KLinesRenderer::backgroundTilePixmap() const
......
......@@ -25,6 +25,7 @@
#include <kdebug.h>
#include "scene.h"
#include "ballitem.h"
#include "renderer.h"
KLinesView::KLinesView( KLinesScene* scene, QWidget* parent )
......@@ -41,10 +42,14 @@ void KLinesView::resizeEvent( QResizeEvent* ev )
// =============== KLinesScene =======================
KLinesScene::KLinesScene( QObject* parent )
: QGraphicsScene(parent)
: QGraphicsScene(parent), 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;
}
KLinesScene::~KLinesScene()
......@@ -58,6 +63,47 @@ void KLinesScene::resizeScene(int width,int height)
setSceneRect( 0, 0, width, height );
}
void KLinesScene::nextThreeBalls()
{
placeRandomBall();
placeRandomBall();
placeRandomBall();
kDebug() << "=======" << endl;
}
void KLinesScene::placeRandomBall()
{
int posx = -1, posy = -1;
// let's find random free cell
do
{
// FIXME dimsuz: debug temp
if( posx != -1 )
kDebug() << "pos x:" << posx << " y:" << posy << " isn't free!" << endl;
posx = m_randomSeq.getLong(FIELD_SIZE);
posy = m_randomSeq.getLong(FIELD_SIZE);
kDebug() << "trying x:" << posx << " y:" << posy << endl;
} while( m_field[posx][posy] != 0 );
kDebug() << "found x:" << posx << " y:" << posy << endl;
// random color
BallColor c = static_cast<BallColor>(m_randomSeq.getLong(static_cast<int>(NumColors)));
BallItem* newBall = new BallItem( this, m_renderer );
newBall->setColor(c);
newBall->setPos( fieldToPix(posx,posy) );
m_field[posx][posy] = newBall;
//newBall->startAnimation( BornAnimation );
newBall->startAnimation( SelectedAnimation );
m_numBalls++;
}
void KLinesScene::mouseReleaseEvent( QGraphicsSceneMouseEvent* )
{
nextThreeBalls();
}
void KLinesScene::drawBackground(QPainter *p, const QRectF&)
{
kDebug() << k_funcinfo << endl;
......
......@@ -25,11 +25,14 @@
#include <QGraphicsScene>
#include <QGraphicsView>
#include <KRandomSequence>
#include "commondefs.h"
static const int FIELD_SIZE=9;
class KLinesRenderer;
class BallItem;
class KLinesScene : public QGraphicsScene
{
......@@ -38,11 +41,37 @@ public:
~KLinesScene();
void resizeScene( int width, int height );
/**
* Brings in next three balls to scene
*/
void nextThreeBalls();
/**
* Creates a ball of random color and places it in random free cell
*/
void placeRandomBall();
/**
* Field coords to pixel coords
*/
inline QPointF fieldToPix(int fieldX, int fieldY) const {
return QPointF( fieldX*32+2, fieldY*32+2 );
}
private:
virtual void drawBackground( QPainter*, const QRectF& );
virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent* );
BallColor m_field[FIELD_SIZE][FIELD_SIZE];
/**
* This array represents the play field.
* Each cell holds the pointer to BallItem
* or 0 if there's no ball in that cell
*/
BallItem* m_field[FIELD_SIZE][FIELD_SIZE];
KLinesRenderer* m_renderer;
KRandomSequence m_randomSeq;
/**
* Number of balls currently in field
*/
int m_numBalls;
};
class KLinesView : public QGraphicsView
......
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