Commit 6a13fcfd authored by Dmitry Suzdalev's avatar Dmitry Suzdalev

Hurray, comrades! :)

Svg resizing now works! Looks cool :).
Some fine-tuning needed for viewport size calculation though.

CCMAIL: mauricio@tabuleiro.com


svn path=/trunk/KDE/kdegames/kreversi/; revision=589012
parent ae5b560a
......@@ -72,30 +72,44 @@ void KReversiChip::showLastMoveMarker(bool show)
// -------------------------------------------------------------------------------
// FIXME dimsuz: make member?
static const int NUM_COLS_IN_PIX = 4;
static const int NUM_ROWS_IN_PIX = 3;
KReversiChipFrameSet::KReversiChipFrameSet()
{
m_renderer = new KSvgRenderer;
}
KReversiChipFrameSet::~KReversiChipFrameSet()
{
delete m_renderer;
}
void KReversiChipFrameSet::loadFrames( const QString& chipsPath )
{
m_frames.clear();
m_renderer->load( chipsPath );
//TODO Return meaningful error?
if (!m_renderer->isValid()) return;
setChipSize( m_renderer->defaultSize().width()/NUM_COLS_IN_PIX );
}
void KReversiChipFrameSet::setChipSize( int newSize )
{
QImage baseImg;
//Use the new addition to kdelib/kdecore, KSvgRenderer, so we can use .svgz
KSvgRenderer chips( chipsPath );
//TODO Return meaningful error?
if (!chips.isValid()) return;
if (!m_renderer->isValid()) return;
//Construct an image object to render the contents of the .svgz file
baseImg = QImage(chips.defaultSize(),QImage::Format_ARGB32_Premultiplied);
baseImg = QImage(newSize*NUM_COLS_IN_PIX, newSize*NUM_ROWS_IN_PIX, QImage::Format_ARGB32_Premultiplied);
//Fill the buffer, it is unitialised by default
baseImg.fill(0);
QPainter p(&baseImg);
chips.render(&p);
m_renderer->render(&p);
QPixmap allFrames = QPixmap::fromImage(baseImg);
int frameSize = allFrames.width() / 4;
int frameSize = allFrames.width() / NUM_COLS_IN_PIX;
m_frames.clear();
for(int y=0; y < allFrames.height(); y += frameSize )
for(int x=0; x < allFrames.width(); x += frameSize )
{
......
......@@ -35,6 +35,10 @@ public:
KReversiChip( ChipColor color, const KReversiChipFrameSet *frameSet, QGraphicsScene *scene );
void setFrameSet( const KReversiChipFrameSet *frameSet );
void setColor( ChipColor color );
void setRowCol( int row, int col ) { m_row = row; m_col = col; };
int row() const { return m_row; }
int col() const { return m_col; }
ChipColor color() const { return m_color; }
/**
* Called during animation
......@@ -58,8 +62,12 @@ private:
* Current animation frame
*/
int m_curFrame;
int m_row;
int m_col;
};
class KSvgRenderer;
/**
* This class will load and hold a chip animation frameset.
* As all chips share the same frames it's good to
......@@ -79,6 +87,7 @@ class KReversiChipFrameSet
{
public:
KReversiChipFrameSet();
~KReversiChipFrameSet();
/**
* Loads a chips (svg) pixmap found in path chipsPath, which
* contains chip's animation sequence.
......@@ -108,11 +117,18 @@ public:
* Returns number of frames in animation
*/
int frameCount() const { return m_frames.count(); }
/**
* Sets chip pixmap size to size.
* I.e. re-renders svg image so that each individual chip
* pixmap in m_frames will have size passed as a parameter to this function
*/
void setChipSize( int newSize );
/**
* Returns default chip size
*/
int defaultChipSize() const { return m_frames.at(0).width(); }
private:
QList<QPixmap> m_frames;
KSvgRenderer *m_renderer;
};
#endif
......@@ -30,10 +30,6 @@
#include "kreversigame.h"
#include "kreversichip.h"
// something to remove/give-more-thinking
const int CHIP_SIZE = 36;
KReversiScene::KReversiScene( KReversiGame* game , const QString& chipsPath )
: m_game(0), m_frameSet(0), m_hintChip(0), m_lastMoveChip(0), m_timerDelay(25),
m_showingHint(false), m_demoMode(false), m_showLastMove(false), m_showPossibleMoves(false)
......@@ -42,14 +38,7 @@ KReversiScene::KReversiScene( KReversiGame* game , const QString& chipsPath )
setChipsPixmap(chipsPath);
QFont font; // it'll be initialised to default application font
font.setBold(true);
// NOTE we assume that fontMetrics in drawBackground() will be the same as here
int fontHeight = QFontMetrics(font).height();
m_boardRect = QRectF(fontHeight, fontHeight, CHIP_SIZE*8, CHIP_SIZE*8);
setSceneRect( 0, 0, m_boardRect.width()+2*fontHeight, m_boardRect.height()+2*fontHeight);
resizeScene( m_curChipSize*8, m_curChipSize*8 );
m_animTimer = new QTimer(this);
connect(m_animTimer, SIGNAL(timeout()), SLOT(slotAnimationStep()));
......@@ -62,12 +51,46 @@ KReversiScene::~KReversiScene()
delete m_frameSet;
}
void KReversiScene::resizeScene( int width, int height )
{
QFont font; // it'll be initialised to default application font
font.setBold(true);
// NOTE we assume that fontMetrics in drawBackground() will be the same as here
int fontHeight = QFontMetrics(font).height();
m_curChipSize = qMin( width/8, height/8 );
m_boardRect = QRectF(fontHeight, fontHeight, m_curChipSize*8, m_curChipSize*8);
setSceneRect( 0, 0, m_boardRect.width()+2*fontHeight, m_boardRect.height()+2*fontHeight);
// adopt to new chip size
m_frameSet->setChipSize( m_curChipSize );
QList<QGraphicsItem*> allItems = items();
KReversiChip *chip = 0;
foreach( QGraphicsItem* item, allItems )
{
chip = qgraphicsitem_cast<KReversiChip*>(item);
if( chip )
{
// adjust pos to new one
chip->setPos( cellTopLeft( chip->row(), chip->col() ) );
chip->setColor( chip->color() ); // this will reread pixmap
}
}
// FIXME dimsuz: need to take care of m_possibleMovesItems, m_hintChip as in setGame???
// if yes, maybe move this duplicated functionality to separate function
}
void KReversiScene::setChipsPixmap( const QString& chipsPath )
{
if(!m_frameSet)
m_frameSet = new KReversiChipFrameSet();
m_frameSet->loadFrames( chipsPath );
m_curChipSize = m_frameSet->defaultChipSize();
if(m_game)
{
// FIXME: Qt rc1 bug? there was items( m_boardRect ) here
......@@ -183,6 +206,7 @@ void KReversiScene::updateBoard()
//kDebug() << "No item at (" << row << "," << col << "). Creating." << endl;
chip = new KReversiChip( color, m_frameSet, this );
chip->setPos( cellTopLeft(row, col) );
chip->setRowCol( row, col );
}
}
else
......@@ -227,6 +251,7 @@ void KReversiScene::slotGameMoveFinished()
KReversiMove move = m_changedChips.takeFirst();
KReversiChip *newchip = new KReversiChip( move.color, m_frameSet, this );
newchip->setPos( cellTopLeft( move.row, move.col ) );
newchip->setRowCol( move.row, move.col );
// start animation
if( m_lastMoveChip )
m_lastMoveChip->showLastMoveMarker( false );
......@@ -295,7 +320,7 @@ void KReversiScene::displayLastAndPossibleMoves()
int numtoadd = l.count() - m_possibleMovesItems.count();
for( int i=0; i<numtoadd; ++i)
{
QGraphicsRectItem *item = new QGraphicsRectItem( 0, 0, CHIP_SIZE-1, CHIP_SIZE-1, 0, this );
QGraphicsRectItem *item = new QGraphicsRectItem( 0, 0, m_curChipSize-1, m_curChipSize-1, 0, this );
item->setBrush( Qt::darkGreen );
item->setZValue(-1);
m_possibleMovesItems.append( item );
......@@ -329,6 +354,7 @@ void KReversiScene::slotHint()
if( m_hintChip == 0 )
m_hintChip = new KReversiChip( hint.color, m_frameSet, this );
m_hintChip->setPos( cellTopLeft( hint.row, hint.col ) );
m_hintChip->setRowCol( hint.row, hint.col );
m_showingHint = true;
m_animTimer->start(500);
}
......@@ -340,12 +366,12 @@ void KReversiScene::slotGameOver()
QPointF KReversiScene::cellCenter( int row, int col ) const
{
return QPointF( m_boardRect.x() + col*CHIP_SIZE + CHIP_SIZE/2, m_boardRect.y() + row*CHIP_SIZE + CHIP_SIZE/2 );
return QPointF( m_boardRect.x() + col*m_curChipSize + m_curChipSize/2, m_boardRect.y() + row*m_curChipSize + m_curChipSize/2 );
}
QPointF KReversiScene::cellTopLeft( int row, int col ) const
{
return QPointF( m_boardRect.x() + col*CHIP_SIZE, m_boardRect.y() + row*CHIP_SIZE );
return QPointF( m_boardRect.x() + col*m_curChipSize, m_boardRect.y() + row*m_curChipSize );
}
void KReversiScene::setBackgroundPixmap( const QPixmap& pix )
......@@ -371,10 +397,10 @@ void KReversiScene::drawBackground( QPainter *p, const QRectF& rc)
qreal starty = m_boardRect.y();
qreal endx = m_boardRect.x() + m_boardRect.width();
qreal endy = m_boardRect.y() + m_boardRect.height();
for(qreal x=startx; x<=endx; x+=CHIP_SIZE)
for(qreal x=startx; x<=endx; x+=m_curChipSize)
p->drawLine( QPointF(x, starty), QPointF(x, endy) );
for(qreal y=starty; y<=endy; y+=CHIP_SIZE)
for(qreal y=starty; y<=endy; y+=m_curChipSize)
p->drawLine( QPointF(startx, y), QPointF(endx, y) );
QFont f = p->font();
......@@ -385,21 +411,21 @@ void KReversiScene::drawBackground( QPainter *p, const QRectF& rc)
const char horLabels[] = "ABCDEFGH";
const char verLabels[] = "12345678";
QRect rect;
QRectF rect;
// draw top+bottom labels
for(int c=0; c<8;++c)
{
rect = QRect((int)startx+c*CHIP_SIZE, (int)starty-fontHeight, CHIP_SIZE, fontHeight );
rect = QRectF(startx+c*m_curChipSize, starty-fontHeight, m_curChipSize, fontHeight );
p->drawText( rect, Qt::AlignCenter | Qt::AlignTop, QChar(horLabels[c]) );
rect.moveTop( (int)endy );
rect.moveTop( endy );
p->drawText( rect, Qt::AlignCenter | Qt::AlignTop, QChar(horLabels[c]) );
}
// draw left+right labels
for(int r=0; r<8;++r)
{
rect = QRect( (int)startx-fontHeight, (int)starty+r*CHIP_SIZE, fontHeight, CHIP_SIZE );
rect = QRectF( startx-fontHeight, starty+r*m_curChipSize, fontHeight, m_curChipSize );
p->drawText( rect, Qt::AlignCenter | Qt::AlignVCenter, QChar(verLabels[r]) );
rect.moveLeft( (int)endx );
rect.moveLeft( endx );
p->drawText( rect, Qt::AlignCenter | Qt::AlignVCenter, QChar(verLabels[r]) );
}
}
......@@ -429,8 +455,8 @@ void KReversiScene::mousePressEvent( QGraphicsSceneMouseEvent* ev )
if( !m_boardRect.contains(ev->scenePos()) )
return;
int row = (int)(-m_boardRect.y() + ev->scenePos().y()) / CHIP_SIZE;
int col = (int)(-m_boardRect.x() + ev->scenePos().x()) / CHIP_SIZE;
int row = (int)(-m_boardRect.y() + ev->scenePos().y()) / m_curChipSize;
int col = (int)(-m_boardRect.x() + ev->scenePos().x()) / m_curChipSize;
if( row < 0 ) row = 0;
if( row > 7 ) row = 7;
......
......@@ -57,6 +57,7 @@ public:
void setGame( KReversiGame* game );
void setBackgroundPixmap( const QPixmap& pix );
void setChipsPixmap( const QString& chipsPath );
void resizeScene( int width, int height );
/**
* This function will tell you if the scene is currently performing
* some kind of "better-don't-interrupt-me" operation.
......@@ -145,6 +146,10 @@ private:
* Animation frameset for chips
*/
KReversiChipFrameSet *m_frameSet;
/**
* Current size of chip
*/
int m_curChipSize;
/**
* This list will hold a changed chips
* after each turn. It is received from the game object.
......
......@@ -29,7 +29,7 @@
#include <kdebug.h>
KReversiView::KReversiView( KReversiScene* scene, QWidget *parent )
: QGraphicsView(scene, parent)
: QGraphicsView(scene, parent), m_scene(scene)
{
setCacheMode( QGraphicsView::CacheBackground );
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
......@@ -39,7 +39,13 @@ KReversiView::KReversiView( KReversiScene* scene, QWidget *parent )
resize( sizeHint() );
}
void KReversiView::resizeEvent( QResizeEvent* ev )
{
m_scene->resizeScene( ev->size().width(), ev->size().height() );
QGraphicsView::resizeEvent(ev);
}
QSize KReversiView::sizeHint() const
{
return QSize( (int)scene()->width(), (int)scene()->height() );
return QSize( (int)m_scene->width(), (int)m_scene->height() );
}
......@@ -32,6 +32,9 @@ class KReversiView : public QGraphicsView
public:
KReversiView( KReversiScene* scene, QWidget *parent );
private:
virtual void resizeEvent( QResizeEvent* );
virtual QSize sizeHint() const;
KReversiScene* m_scene;
};
#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