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

KLines is now playable from keyword again.

Use arrows to move focus rectangle and Space to select/move ball

svn path=/trunk/KDE/kdegames/klines/; revision=619216
parent 258209db
......@@ -128,23 +128,23 @@ void KLines::initKAction()
lPrompt->setPrompt(Prefs::showNext());
KAction *action = new KAction(i18n("Move Left"), actionCollection(), "left");
connect(action, SIGNAL(triggered(bool) ), lsb, SLOT(moveLeft()));
connect(action, SIGNAL(triggered(bool) ), mwidget->scene(), SLOT(moveFocusLeft()));
action->setShortcut(Qt::Key_Left);
addAction(action);
action = new KAction(i18n("Move Right"), actionCollection(), "right");
connect(action, SIGNAL(triggered(bool) ), lsb, SLOT(moveRight()));
connect(action, SIGNAL(triggered(bool) ), mwidget->scene(), SLOT(moveFocusRight()));
action->setShortcut(Qt::Key_Right);
addAction(action);
action = new KAction(i18n("Move Up"), actionCollection(), "up");
connect(action, SIGNAL(triggered(bool) ), lsb, SLOT(moveUp()));
connect(action, SIGNAL(triggered(bool) ), mwidget->scene(), SLOT(moveFocusUp()));
action->setShortcut(Qt::Key_Up);
addAction(action);
action = new KAction(i18n("Move Down"), actionCollection(), "down");
connect(action, SIGNAL(triggered(bool) ), lsb, SLOT(moveDown()));
connect(action, SIGNAL(triggered(bool) ), mwidget->scene(), SLOT(moveFocusDown()));
action->setShortcut(Qt::Key_Down);
addAction(action);
action = new KAction(i18n("Move Ball"), actionCollection(), "place_ball");
connect(action, SIGNAL(triggered(bool) ), lsb, SLOT(placePlayerBall()));
action = new KAction(i18n("Move Ball"), actionCollection(), "select_cell");
connect(action, SIGNAL(triggered(bool) ), mwidget->scene(), SLOT(cellSelected()));
action->setShortcut(Qt::Key_Space);
addAction(action);
......
......@@ -56,6 +56,11 @@ KLinesScene::KLinesScene( QObject* parent )
for(int y=0; y<FIELD_SIZE; ++y)
m_field[x][y] = 0;
// FIXME dimsuz: hardcoded size
m_focusItem = new QGraphicsRectItem( QRectF(0, 0, 32, 32), 0, this );
m_focusItem->setPen( Qt::DashLine );
m_focusItem->hide();
// init m_nextColors
for(int i=0; i<3; i++)
{
......@@ -130,12 +135,15 @@ BallItem* KLinesScene::randomlyPlaceBall(BallColor c)
}
void KLinesScene::mousePressEvent( QGraphicsSceneMouseEvent* ev )
{
selectOrMove( pixToField(ev->scenePos()) );
}
void KLinesScene::selectOrMove( const FieldPos& fpos )
{
if( m_animator->isAnimating() )
return;
FieldPos fpos = pixToField(ev->scenePos());
if( m_field[fpos.x][fpos.y] ) // ball was selected
{
if( m_selPos.isValid() )
......@@ -355,9 +363,85 @@ void KLinesScene::searchAndErase()
}
// after it finishes slot removeAnimFinished() will be called
// if m_itemsToDelete is empty removeAnimFinished() will be called immediately
m_animator->animateRemove( m_itemsToDelete );
}
// FIXME dimsuz: hardcoded size in 5 functions below
void KLinesScene::moveFocusLeft()
{
if( !m_focusItem->isVisible() )
{
m_focusItem->show();
// no action for the first time
return;
}
QPointF pos = m_focusItem->pos();
pos.rx() -= 32;
if( pos.x() < 0 )
pos.setX( (FIELD_SIZE - 1)*32 );
m_focusItem->setPos( pos );
}
void KLinesScene::moveFocusRight()
{
if( !m_focusItem->isVisible() )
{
m_focusItem->show();
// no action for the first time
return;
}
QPointF pos = m_focusItem->pos();
pos.rx() += 32;
if( pos.x() > (FIELD_SIZE-1)*32 )
pos.setX( 0 );
m_focusItem->setPos( pos );
}
void KLinesScene::moveFocusUp()
{
if( !m_focusItem->isVisible() )
{
m_focusItem->show();
// no action for the first time
return;
}
QPointF pos = m_focusItem->pos();
pos.ry() -= 32;
if( pos.y() < 0 )
pos.setY( (FIELD_SIZE - 1)*32 );
m_focusItem->setPos( pos );
}
void KLinesScene::moveFocusDown()
{
if( !m_focusItem->isVisible() )
{
m_focusItem->show();
// no action for the first time
return;
}
QPointF pos = m_focusItem->pos();
pos.ry() += 32;
if( pos.y() > (FIELD_SIZE-1)*32 )
pos.setY( 0 );
m_focusItem->setPos( pos );
}
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) ) );
}
void KLinesScene::drawBackground(QPainter *p, const QRectF&)
{
kDebug() << k_funcinfo << endl;
......
......@@ -34,6 +34,7 @@ static const int FIELD_SIZE=9;
class KLinesRenderer;
class KLinesAnimator;
class BallItem;
class QGraphicsRectItem;
/**
* Displays and drives the game
......@@ -81,6 +82,27 @@ public:
*/
inline FieldPos pixToField( const QPointF& p ) const {
return FieldPos(static_cast<int>(p.x()/32), static_cast<int>(p.y()/32)); }
public slots:
/**
* Moves keyboard-playing focus rect to the left
*/
void moveFocusLeft();
/**
* Moves keyboard-playing focus rect to the right
*/
void moveFocusRight();
/**
* Moves keyboard-playing focus rect to the up
*/
void moveFocusUp();
/**
* Moves keyboard-playing focus rect to the down
*/
void moveFocusDown();
/**
* Takes corresponding action on cell under focus rect
*/
void cellSelected();
signals:
void scoreChanged(int);
void nextColorsChanged();
......@@ -100,6 +122,14 @@ private:
* Searches for 5 or more balls in a row and deletes them from field
*/
void searchAndErase();
/**
* This fuction takes one of two actions:
* If there's a ball at fpos, it will be selected.
* Otherwise if the cell at fpos is empty and there's
* a selected ball in some other cell it will be moved to fpos
* (if the move is possible, of course)
*/
void selectOrMove( const FieldPos& fpos );
virtual void drawBackground( QPainter*, const QRectF& );
virtual void mousePressEvent( QGraphicsSceneMouseEvent* );
......@@ -152,6 +182,10 @@ private:
* Colors of the next turn's balls
*/
QList<BallColor> m_nextColors;
/**
* Keyboard-playing focus indication
*/
QGraphicsRectItem *m_focusItem;
};
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