Commit 6dba0c48 authored by Roney Gomes's avatar Roney Gomes

Improved the setMousePosition() slot of KGrView and added, but still waiting...

Improved the setMousePosition() slot of KGrView and added, but still waiting for improvements, the makeSprite() slot to KGrScene.

svn path=/branches/work/kgoldrunner-qgv/; revision=1311413
parent 2f055e98
......@@ -154,8 +154,9 @@ void KGrLevelPlayer::init (KGrView * view,
// Connect to code that paints grid cells and start-positions of sprites.
connect (this, SIGNAL (paintCell(int,int,char)),
view->gameScene(), SLOT (paintCell(int,int,char)));
/* connect (this, SIGNAL (makeSprite(char,int,int)),
view, SLOT (makeSprite(char,int,int))); */
connect (this, SIGNAL (makeSprite(char,int,int)),
view->gameScene(), SLOT (makeSprite(char,int,int)));
// Connect to the mouse-positioning code in the graphics.
connect (this, SIGNAL (getMousePos(int&,int&)),
......@@ -240,13 +241,13 @@ void KGrLevelPlayer::init (KGrView * view,
// Connect the hero's and enemies' efforts to the graphics.
/* connect (this, SIGNAL (gotGold(int,int,int,bool,bool)),
view, SLOT (gotGold(int,int,int,bool,bool)));
view, SLOT (gotGold(int,int,int,bool,bool))); */
// Connect mouse-clicks from KGrCanvas to digging slot.
connect (view, SIGNAL (mouseClick(int)), SLOT (doDig(int)));
// Connect the hero and enemies (if any) to the animation code.
connect (hero, SIGNAL (startAnimation (int, bool, int, int, int,
/* connect (hero, SIGNAL (startAnimation (int, bool, int, int, int,
Direction, AnimationType)),
view, SLOT (startAnimation (int, bool, int, int, int,
Direction, AnimationType)));
......@@ -745,7 +746,9 @@ void KGrLevelPlayer::tick (bool missed, int scaledTime)
{
int i, j;
emit getMousePos (i, j);
if (i == -2) {
return; // The KGoldRunner window is inactive.
}
if ((i == -1) && (playback || (controlMode != KEYBOARD))) {
return; // The pointer is outside the level layout.
}
......
......@@ -188,4 +188,53 @@ void KGrScene::paintCell (const int i, const int j, const char type)
}
}
int KGrScene::makeSprite (const char type, int i, int j)
{
int id;
int index = i * m_tilesHigh + j;
KGameRenderedItem * sprite = 0;
if (m_tiles.at(index) == 0) {
// Occupy the slot corresponding to the given coordinates case it's
// free.
id = index;
}
else if (m_tiles.lastIndexOf(0) >= 0) {
// Otherwise, occupy the last empty slot.
id = m_tiles.lastIndexOf(0);
}
else {
// Case the list is completely occupied, add one free slot to it.
id = m_tiles.size();
m_tiles.reserve (id + 1);
m_tileTypes.reserve (id + 1);
}
sprite = m_renderer->getTileItem (type, m_tiles.at(id));
m_tiles[id] = sprite;
m_tileTypes[id] = type;
setTileSize (sprite, m_tileSize);
sprite->setPos (i, j);
// TODO: Set the starting frame of the sprite.
switch (type) {
case HERO:
sprite->setZValue (1);
break;
case ENEMY:
sprite->setZValue (2);
break;
case BRICK:
paintCell (i, j, FREE);
break;
default:
break;
}
return id;
}
#include "kgrscene.moc"
......@@ -87,6 +87,7 @@ public:
KGrRenderer * renderer () const { return m_renderer; }
public slots:
int makeSprite (const char type, int i, int j);
void paintCell (const int i, const int j, const char type);
private:
......
......@@ -40,20 +40,34 @@ void KGrView::getMousePos (int & i, int & j)
{
QPointF pos = mapToScene (mapFromGlobal (m_mouse->pos()));
i = pos.x();
j = pos.y();
// The window lacks keyboard focus or is minimized.
if (! isActiveWindow()) {
i = -2;
j = -2;
return;
}
// The pointer is outside the level layout.
if (i < 1 || i > FIELDWIDTH || j < 1 || j > FIELDHEIGHT) {
i = -1;
j = -1;
return;
}
i = pos.x();
j = pos.y();
}
void KGrView::setMousePos (const int i, const int j)
{
QPoint pos = mapToGlobal (mapFromScene (i, j));
m_mouse->setPos(pos);
QPoint pos = mapFromScene (i, j);
// Puts the cursor at the middle of the starting cell.
int s = m_scene->tileSize().width() / 2;
int x = pos.x() + s;
int y = pos.y() + s;
m_mouse->setPos( mapToGlobal ( QPoint (x, y)));
}
void KGrView::resizeEvent (QResizeEvent *)
......@@ -63,3 +77,15 @@ void KGrView::resizeEvent (QResizeEvent *)
fitInView (scene()->sceneRect(), Qt::KeepAspectRatio);
}
}
void KGrView::mousePressEvent (QMouseEvent * mouseEvent)
{
emit mouseClick (mouseEvent->button());
}
void KGrView::mouseReleaseEvent (QMouseEvent * mouseEvent)
{
emit mouseLetGo (mouseEvent->button());
}
#include "kgrview.moc"
......@@ -19,6 +19,7 @@
#define KGRVIEW_H
#include <QCursor>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QGraphicsView>
......@@ -45,8 +46,14 @@ public slots:
void getMousePos (int & i, int & j);
void setMousePos (const int i, const int j);
signals:
void mouseClick (int);
void mouseLetGo (int);
protected:
virtual void resizeEvent (QResizeEvent *);
virtual void resizeEvent (QResizeEvent *);
virtual void mousePressEvent (QMouseEvent * mouseEvent);
virtual void mouseReleaseEvent (QMouseEvent * mouseEvent);
private:
QCursor * m_mouse;
......
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