Commit 7c3a8255 authored by Dmitry Suzdalev's avatar Dmitry Suzdalev

Now it looks prettier, because the chips aren't simple

black and white ellipses, but a fancy pixmaps, which
are taken from the original KReversi's set.
So it looks much the same now.
Although chips are not animated yet.

svn path=/branches/work/kreversi_rewrite/; revision=572966
parent cde435cd
......@@ -2,8 +2,10 @@
#include <QPainter>
#include <QPixmap>
KReversiChip::KReversiChip( ChipColor color, QGraphicsScene* scene )
: QGraphicsPixmapItem( 0, scene ), m_color(color)
#include <kdebug.h>
KReversiChip::KReversiChip( ChipColor color, const KReversiChipFrameSet* frameSet, QGraphicsScene* scene )
: QGraphicsPixmapItem( 0, scene ), m_color(color), m_frameSet(frameSet)
{
setColor(m_color);
}
......@@ -11,11 +13,27 @@ KReversiChip::KReversiChip( ChipColor color, QGraphicsScene* scene )
void KReversiChip::setColor( ChipColor color )
{
m_color = color;
QPixmap pix(32, 32);
pix.fill( Qt::white );
QPainter p(&pix);
p.setBrush( color == White ? Qt::white : Qt::black );
p.drawEllipse( QRectF(0,0,32,32) );
p.end();
setPixmap( pix );
setPixmap( m_frameSet->chipPixmap( m_color ) );
}
KReversiChipFrameSet::KReversiChipFrameSet( const QPixmap& allFrames, int frameSize )
{
// we skip x,y = (0,0) case, because allFrames has a transparent frame as the
// the first one. Just for symmetry I guess (so the pix remains square)
for(int y=0; y < allFrames.height(); y += frameSize )
for(int x=0; x < allFrames.width(); x += frameSize )
{
if( x==0 && y==0 ) continue;
m_frames.append( allFrames.copy(x,y, frameSize, frameSize) );
}
}
QPixmap KReversiChipFrameSet::frame( int frameNo ) const
{
return m_frames.at(frameNo);
}
QPixmap KReversiChipFrameSet::chipPixmap( ChipColor color ) const
{
return ( color == White ? m_frames.at(frameCount()-1) : m_frames.at(0) );
}
......@@ -5,14 +5,62 @@
#include "commondefs.h"
class QGraphicsScene;
class KReversiChipFrameSet;
class KReversiChip : public QGraphicsPixmapItem
{
public:
KReversiChip( ChipColor color, QGraphicsScene *scene );
KReversiChip( ChipColor color, const KReversiChipFrameSet *frameSet, QGraphicsScene *scene );
void setColor( ChipColor color );
ChipColor color() const { return m_color; }
private:
ChipColor m_color;
const KReversiChipFrameSet* m_frameSet;
};
/**
* This class will hold a chip animation frameset.
* As all chips share the same frames it's good to
* put them in a single storage class like this,
* so any chip can retrieve any frame any time :).
*
* This class is based on a number of assumptions which
* reflect the current allchips.png pixmap format.
* Here they are:
* 1. Animation sequence goes from black to white colored chips
* 2a. as a consequence of 1: frame(0) is black chip
* 2b. as a consequence of 1: frame(frameCount()) is white chip
* But 2a and 2b shouldn't matter, because there's chipPixmap()
*
* These are subject to changing/rethinking ;)
*/
class KReversiChipFrameSet
{
public:
/**
* Initializes this class with 'allFrames' QPixmap, which
* contains chip's animation sequence.
* The chips frames are extracted from it and put into
* m_frames QList
* Supposes that allFrames represents an animation sequence
* going from black to white.
* @param allFrames a pixmap containing whole animation sequence
* @param chipSize size of each frame's pixmap. Frames are squares.
*/
KReversiChipFrameSet( const QPixmap& allFrames, int frameSize );
/**
* Retruns a pixmap which corresponds to frame with number frameNo.
*/
QPixmap frame( int frameNo ) const;
/**
* Returns a pixmap with a chip of corresponding color
*/
QPixmap chipPixmap(ChipColor color) const;
/**
* Returns number of frames in animation
*/
int frameCount() const { return m_frames.count(); }
private:
QList<QPixmap> m_frames;
};
#endif
......@@ -10,15 +10,17 @@
// FIXME dimsuz: something to remove/give-more-thinking
const int CHIP_SIZE = 32;
const int CHIP_SIZE = 36;
KReversiScene::KReversiScene( KReversiGame* game )
KReversiScene::KReversiScene( KReversiGame* game , const QPixmap& chipsPixmap )
{
setBackgroundBrush( Qt::lightGray );
m_boardRect = QRectF(5, 5, CHIP_SIZE*8, CHIP_SIZE*8);
setSceneRect( 0, 0, m_boardRect.width()+10, m_boardRect.height()+10);
m_frameSet = new KReversiChipFrameSet( chipsPixmap, CHIP_SIZE );
setGame(game);
}
......@@ -61,7 +63,7 @@ void KReversiScene::updateBoard()
else
{
kDebug() << "No item at (" << row << "," << col << "). Creating." << endl;
chip = new KReversiChip( color, this );
chip = new KReversiChip( color, m_frameSet, this );
chip->setPos( cellTopLeft(row, col) );
}
}
......
......@@ -5,6 +5,7 @@
#include <QPixmap>
class KReversiGame;
class KReversiChipFrameSet;
class QPainter;
/**
......@@ -20,7 +21,10 @@ class KReversiScene : public QGraphicsScene
{
Q_OBJECT
public:
KReversiScene( KReversiGame* game=0 );
/**
* @param chipsPixmap the pixmap with animation frames
*/
KReversiScene( KReversiGame* game, const QPixmap& chipsPixmap );
void setGame( KReversiGame* game );
void setBackgroundPixmap( const QPixmap& pix );
public slots:
......@@ -50,5 +54,9 @@ private:
* Background pixmap
*/
QPixmap m_bkgndPix;
/**
* Animation frameset for chips
*/
KReversiChipFrameSet *m_frameSet;
};
#endif
......@@ -7,6 +7,7 @@
KReversiView::KReversiView( QGraphicsScene* scene, QWidget *parent )
: QGraphicsView(scene, parent)
{
setCacheMode( QGraphicsView::CacheBackground );
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
//setFixedSize( sceneRect().toRect().size() );
......
......@@ -17,13 +17,15 @@ KReversiMainWindow::KReversiMainWindow(QWidget* parent)
: KMainWindow(parent)
{
m_game = new KReversiGame;
m_scene = new KReversiScene(m_game);
KReversiView *view = new KReversiView(m_scene, this);
view->show();
// FIXME dimsuz: if chips.png not found give error end exit
m_scene = new KReversiScene(m_game, KStandardDirs::locate("appdata", "pics/chips.png"));
m_view = new KReversiView(m_scene, this);
m_view->show();
setupActions();
setCentralWidget(view);
setCentralWidget(m_view);
setupGUI();
}
......@@ -63,7 +65,10 @@ void KReversiMainWindow::slotBackgroundChanged( const QString& text )
file.remove('&');
QPixmap pix( KStandardDirs::locate("appdata", QString("pics/background/") + file ) );
if(!pix.isNull())
{
m_view->resetCachedContent();
m_scene->setBackgroundPixmap( pix );
}
}
void KReversiMainWindow::newGame()
......
......@@ -5,6 +5,7 @@
class KReversiScene;
class KReversiGame;
class KReversiView;
class KReversiMainWindow : public KMainWindow
{
......@@ -18,6 +19,7 @@ private:
void setupActions();
KReversiScene *m_scene;
KReversiView *m_view;
KReversiGame *m_game;
};
#endif
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