Commit 06e42626 authored by Dmitry Suzdalev's avatar Dmitry Suzdalev
Browse files

Support resize of playfield.

As currently there is no svg artwork, pixmaps are simply scaled
(and yes, they are ugly-looking because of that). This kind of "svg emulation"
allows to finish some points in code.

Now when svg artwork for klines will be ready  - few changes to methods
of KLinesRenderer class - and you have working SVG-klines :).

svn path=/trunk/KDE/kdegames/klines/; revision=621660
parent 014545c7
......@@ -98,9 +98,8 @@ void KLinesAnimator::animateMove( const FieldPos& from, const FieldPos& to )
// there will be numPoints-1 intervals of
// movement (interval=cell). We want each of them to take 100ms
m_moveTimeLine.setDuration((numPoints-1)*100);
// FIXME dimsuz: 30 <=> m_scene->ballSize() or smth like that
// each interval will take 30 frames
m_moveTimeLine.setFrameRange(0, (numPoints-1)*30);
// each interval will take cellSize frames
m_moveTimeLine.setFrameRange(0, (numPoints-1)*KLinesRenderer::self()->cellSize());
m_moveTimeLine.setCurrentTime(0);
m_moveTimeLine.start();
}
......@@ -126,7 +125,8 @@ void KLinesAnimator::animateBorn( const QList<BallItem*>& list )
void KLinesAnimator::moveAnimationFrame(int frame)
{
int intervalNum = frame/30;
int cellSize = KLinesRenderer::self()->cellSize();
int intervalNum = frame/cellSize;
if(intervalNum == m_foundPath.count()-1)
{
......@@ -153,7 +153,7 @@ void KLinesAnimator::moveAnimationFrame(int frame)
else
ky = 0;
int frameWithinInterval = frame%30;
int frameWithinInterval = frame%cellSize;
QPointF pos = m_scene->fieldToPix(from);
m_movingBall->setPos( pos.x()+kx*frameWithinInterval,
pos.y()+ky*frameWithinInterval );
......
......@@ -108,9 +108,6 @@ void KLinesMainWindow::initKAction()
action = actionCollection()->addAction("select_cell");
action->setText(i18n("Move Ball"));
connect(action, SIGNAL(triggered(bool) ), mwidget->scene(), SLOT(cellSelected()));
action = actionCollection()->addAction("place_ball");
action->setText(i18n("Move Ball"));
action->setShortcut(Qt::Key_Space);
addAction(action);
......
......@@ -52,6 +52,11 @@ MainWidget::~MainWidget()
{
}
void MainWidget::resizeEvent( QResizeEvent* )
{
m_preview->updateGeometry();
}
void MainWidget::updateNextColors()
{
m_preview->setColors( m_scene->nextColors() );
......
......@@ -35,6 +35,7 @@ public slots:
void updateNextColors();
void setShowNextColors(bool);
private:
virtual void resizeEvent( QResizeEvent* );
KLinesScene* m_scene;
BallsPreview* m_preview;
};
......
......@@ -32,20 +32,18 @@ BallsPreview::BallsPreview( QWidget* parent )
QSize BallsPreview::sizeHint() const
{
return QSize( KLinesRenderer::self()->backgroundTilePixmap().size().width()*3, KLinesRenderer::self()->backgroundTilePixmap().height() );
return QSize( KLinesRenderer::self()->cellSize()*3, KLinesRenderer::self()->cellSize() );
}
void BallsPreview::paintEvent( QPaintEvent* )
{
QPainter p(this);
QPixmap pix = KLinesRenderer::self()->backgroundTilePixmap();
// FIXME dimsuz: use smth like KLinesRenderer::self()->elementSize()
int width = pix.size().width();
for(int i=0; i < 3; i++)
{
p.drawPixmap( i*width, 0, pix );
if( !m_colors.isEmpty() && m_showColors)
// FIXME dimsuz: "2" - hardcoded
p.drawPixmap( i*width+2, 2, KLinesRenderer::self()->ballPixmap( m_colors.at(i) ) );
p.drawPixmap( i*width, 2, KLinesRenderer::self()->ballPixmap( m_colors.at(i) ) );
}
}
......@@ -42,7 +42,6 @@ public:
*/
void setShowColors( bool show ) { m_showColors = show; update(); }
// FIXME dimsuz: this won't be needed when graphics will be SVG
virtual QSize sizeHint() const;
private:
void paintEvent( QPaintEvent* );
......
......@@ -31,48 +31,43 @@ KLinesRenderer* KLinesRenderer::self()
}
KLinesRenderer::KLinesRenderer()
: m_cellSize(32)
{
m_fieldPix = QPixmap( KStandardDirs::locate( "appdata", "field.jpg" ));
m_firePix = QPixmap( KStandardDirs::locate( "appdata", "fire.jpg" ));
m_ballsPix = QPixmap( KStandardDirs::locate( "appdata", "balls.jpg" ));
}
// these are the notes for all *Pixmap() functions below:
// FIXME dimsuz: copying every time: not very efficient.
// FIXME dimsuz: hardcoded "magic" numbers
// FIXME dimsuz: scale to emulate future svg support
// Switching to svg will make this fixmes obsolete
QPixmap KLinesRenderer::ballPixmap(BallColor color) const
{
// 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( 7*30, static_cast<int>(color)*30, 30, 30 );
return m_ballsPix.copy( 7*30, static_cast<int>(color)*30, 30, 30 ).scaled( m_cellSize, m_cellSize );
}
QPixmap KLinesRenderer::firePixmap(int frame) const
{
// 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_firePix.copy(frame*30, 0, 30, 30 );
return m_firePix.copy(frame*30, 0, 30, 30 ).scaled( m_cellSize, m_cellSize );
}
QPixmap KLinesRenderer::bornPixmap(BallColor color, int frame) const
{
// 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( 13*30 + frame*30, static_cast<int>(color)*30, 30, 30 );
return m_ballsPix.copy( 13*30 + frame*30, static_cast<int>(color)*30, 30, 30 ).scaled( m_cellSize, m_cellSize );
}
QPixmap KLinesRenderer::selectedPixmap( BallColor color, int frame ) const
{
return m_ballsPix.copy( frame*30, static_cast<int>(color)*30, 30, 30 );
return m_ballsPix.copy( frame*30, static_cast<int>(color)*30, 30, 30 ).scaled( m_cellSize, m_cellSize );
}
QPixmap KLinesRenderer::backgroundTilePixmap() const
{
return m_fieldPix;
return m_fieldPix.scaled( m_cellSize, m_cellSize );
}
......@@ -34,13 +34,14 @@ public:
static KLinesRenderer* self();
QPixmap ballPixmap( BallColor c ) const;
QPixmap firePixmap( int frame ) const;
QPixmap bornPixmap( BallColor c, int frame ) const;
QPixmap selectedPixmap( BallColor c, int frame ) const;
QPixmap backgroundTilePixmap() const;
void setCellSize(int size) { m_cellSize = size; }
int cellSize() const { return m_cellSize; }
inline int numFireFrames() const { return 5; }
inline int numBornFrames() const { return 5; }
inline int numSelectedFrames() const { return 13; }
......@@ -54,6 +55,12 @@ private:
QPixmap m_ballsPix; // to be removed when SVG comes to us
QPixmap m_fieldPix; // to be removed when SVG comes to us
QPixmap m_firePix; // to be removed when SVG comes to us
/**
* This is the size of the scene's cell.
* All rendered pixmaps will have this size
*/
int m_cellSize;
};
#endif
......@@ -44,15 +44,15 @@ void KLinesView::resizeEvent( QResizeEvent* ev )
// =============== KLinesScene =======================
KLinesScene::KLinesScene( QObject* parent )
: QGraphicsScene(parent), m_numFreeCells(FIELD_SIZE*FIELD_SIZE), m_score(0), m_bonusScore(0)
: QGraphicsScene(parent), m_numFreeCells(FIELD_SIZE*FIELD_SIZE), m_score(0), m_bonusScore(0), m_cellSize(32)
{
m_animator = new KLinesAnimator(this);
connect( m_animator, SIGNAL(moveFinished()), SLOT(moveAnimFinished() ) );
connect( m_animator, SIGNAL(removeFinished()), SLOT(removeAnimFinished() ) );
connect( m_animator, SIGNAL(bornFinished()), SLOT(bornAnimFinished() ) );
// FIXME dimsuz: hardcoded size
m_focusItem = new QGraphicsRectItem( QRectF(0, 0, 32, 32), 0, this );
m_focusItem = new QGraphicsRectItem( QRectF(0, 0, m_cellSize, m_cellSize), 0, this );
m_focusItem->setZValue(1.0);
m_focusItem->setPen( Qt::DashLine );
startNewGame();
......@@ -106,8 +106,30 @@ KLinesScene::~KLinesScene()
void KLinesScene::resizeScene(int width,int height)
{
kDebug() << "resize:" << width << "," << height << endl;
// store focus item field pos (calculated using old cellSize)
FieldPos focusRectFieldPos = pixToField( m_focusItem->pos() );
m_cellSize = qMin(width, height)/FIELD_SIZE;
setSceneRect( 0, 0, width, height );
KLinesRenderer::self()->setCellSize( m_cellSize );
// re-render && recalc positions for all balls
for( int x=0; x<FIELD_SIZE; ++x)
for(int y=0; y< FIELD_SIZE; ++y)
{
if( m_field[x][y] )
{
// this will update pixmap
m_field[x][y]->setColor( m_field[x][y]->color() );
m_field[x][y]->setPos( fieldToPix( FieldPos(x,y) ) );
}
}
m_focusItem->setRect( QRect(0,0, m_cellSize, m_cellSize) );
m_focusItem->setPos( fieldToPix( focusRectFieldPos ) );
kDebug() << "resize:" << width << "," << height << "; cellSize: " << m_cellSize << endl;
}
void KLinesScene::endTurn()
......@@ -407,7 +429,6 @@ void KLinesScene::searchAndErase()
m_animator->animateRemove( m_itemsToDelete );
}
// FIXME dimsuz: hardcoded size in 5 functions below
void KLinesScene::moveFocusLeft()
{
if( !m_focusItem->isVisible() )
......@@ -418,9 +439,9 @@ void KLinesScene::moveFocusLeft()
}
QPointF pos = m_focusItem->pos();
pos.rx() -= 32;
pos.rx() -= m_cellSize;
if( pos.x() < 0 )
pos.setX( (FIELD_SIZE - 1)*32 );
pos.setX( (FIELD_SIZE - 1)*m_cellSize );
m_focusItem->setPos( pos );
}
......@@ -434,8 +455,8 @@ void KLinesScene::moveFocusRight()
}
QPointF pos = m_focusItem->pos();
pos.rx() += 32;
if( pos.x() > (FIELD_SIZE-1)*32 )
pos.rx() += m_cellSize;
if( pos.x() > (FIELD_SIZE-1)*m_cellSize )
pos.setX( 0 );
m_focusItem->setPos( pos );
}
......@@ -450,9 +471,9 @@ void KLinesScene::moveFocusUp()
}
QPointF pos = m_focusItem->pos();
pos.ry() -= 32;
pos.ry() -= m_cellSize;
if( pos.y() < 0 )
pos.setY( (FIELD_SIZE - 1)*32 );
pos.setY( (FIELD_SIZE - 1)*m_cellSize );
m_focusItem->setPos( pos );
}
......@@ -466,8 +487,8 @@ void KLinesScene::moveFocusDown()
}
QPointF pos = m_focusItem->pos();
pos.ry() += 32;
if( pos.y() > (FIELD_SIZE-1)*32 )
pos.ry() += m_cellSize;
if( pos.y() > (FIELD_SIZE-1)*m_cellSize )
pos.setY( 0 );
m_focusItem->setPos( pos );
}
......@@ -477,9 +498,8 @@ void KLinesScene::cellSelected()
if( !m_focusItem->isVisible() )
m_focusItem->show();
// FIXME dimsuz: hardcoded
// we're taking the center of the cell
selectOrMove( pixToField( m_focusItem->pos() + QPointF(16,16) ) );
selectOrMove( pixToField( m_focusItem->pos() + QPointF(m_cellSize/2,m_cellSize/2) ) );
}
void KLinesScene::saveUndoInfo()
......@@ -547,8 +567,8 @@ void KLinesScene::undo()
void KLinesScene::drawBackground(QPainter *p, const QRectF&)
{
// FIXME dimsuz: temp
for(int x=0; x<32*FIELD_SIZE;x+=32)
for(int y=0; y<32*FIELD_SIZE;y+=32)
for(int x=0; x<m_cellSize*FIELD_SIZE;x+=m_cellSize)
for(int y=0; y<m_cellSize*FIELD_SIZE;y+=m_cellSize)
p->drawPixmap( x, y, KLinesRenderer::self()->backgroundTilePixmap() );
}
......
......@@ -77,13 +77,13 @@ public:
* Field coords to pixel coords
*/
inline QPointF fieldToPix(const FieldPos& fpos) const {
return QPointF( fpos.x*32+2, fpos.y*32+2 );
return QPointF( fpos.x*m_cellSize, fpos.y*m_cellSize );
}
/**
* Pixel coords to field coords
*/
inline FieldPos pixToField( const QPointF& p ) const {
return FieldPos(static_cast<int>(p.x()/32), static_cast<int>(p.y()/32)); }
return FieldPos(static_cast<int>(p.x()/m_cellSize), static_cast<int>(p.y()/m_cellSize)); }
public slots:
/**
* Starts new game
......@@ -185,6 +185,10 @@ private:
* @see setBonusScorePoints()
*/
int m_bonusScore;
/**
* Cell size in pixels
*/
int m_cellSize;
/**
* Varable which is needed for little trick (tm).
* Read more about it in removeAnimFinished() slot
......
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