Commit 2f055e98 authored by Roney Gomes's avatar Roney Gomes

Fixed the bug related to KGrScene::paintCell() and theme changes. The slots...

Fixed the bug related to KGrScene::paintCell() and theme changes. The slots getMousePos() and setMousePos() were added to KGrView and connected to the game engine.

svn path=/branches/work/kgoldrunner-qgv/; revision=1310771
parent d98b1a47
......@@ -216,7 +216,7 @@ private:
int resizeCount; // =0 until the main window has resized.
QTime t; // IDW
unsigned int level;
// Keep current score and lives
int lives;
int score;
......
......@@ -152,16 +152,17 @@ void KGrLevelPlayer::init (KGrView * view,
grid->calculateAccess (rules->runThruHole());
// 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 (paintCell(int,int,char)),
view->gameScene(), SLOT (paintCell(int,int,char)));
/* connect (this, SIGNAL (makeSprite(char,int,int)),
view, SLOT (makeSprite(char,int,int)));
view, SLOT (makeSprite(char,int,int))); */
// Connect to the mouse-positioning code in the graphics.
connect (this, SIGNAL (getMousePos(int&,int&)),
view, SLOT (getMousePos(int&,int&)));
connect (this, SIGNAL (setMousePos(int,int)),
view, SLOT (setMousePos(int,int))); */
view, SLOT (setMousePos(int,int)));
// Show the layout of this level in the view (KGrCanvas).
int wall = ConcreteWall;
......@@ -744,11 +745,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 KGoldrunner viewport.
return; // The pointer is outside the level layout.
}
if (playback) { // Replay a recorded move.
......
......@@ -16,34 +16,37 @@
****************************************************************************/
#include <QDebug>
#include <QGraphicsView>
#include <kdebug.h>
#include <KGlobal>
#include <KConfig>
#include <KConfigGroup>
#include "kgrview.h"
#include "kgrscene.h"
#include "kgrglobals.h"
#include "kgrrenderer.h"
KGrScene::KGrScene (QObject * parent)
KGrScene::KGrScene (KGrView * view)
:
QGraphicsScene (parent),
QGraphicsScene (view),
// Allow FIELDWIDTH * FIELDHEIGHT tiles for the KGoldruner level-layouts,
// plus 2 more tile widths all around for text areas, frame and spillover
// for mouse actions (to avoid accidental clicks affecting the desktop).
m_background (0),
m_scoreText (0),
m_livesText (0),
m_scoreDisplay (0),
m_livesDisplay (0),
m_tilesWide (FIELDWIDTH + 2 * 2),
m_tilesHigh (FIELDHEIGHT + 2 * 2),
m_tileSize (10),
m_themeChanged (true)
m_view (view),
m_background (0),
m_scoreText (0),
m_livesText (0),
m_scoreDisplay (0),
m_livesDisplay (0),
m_tilesWide (FIELDWIDTH + 2 * 2),
m_tilesHigh (FIELDHEIGHT + 2 * 2),
m_tileSize (10),
m_themeChanged (true)
{
m_tiles.fill (0, m_tilesWide * m_tilesHigh);
m_tiles.fill (0, m_tilesWide * m_tilesHigh);
m_tileTypes.fill (FREE, m_tilesWide * m_tilesHigh);
m_renderer = new KGrRenderer (this);
}
......@@ -55,7 +58,7 @@ void KGrScene::redrawScene ()
{
if (m_sizeChanged) {
// Calculate what size of tile will fit in the view.
QSize size = views().at(0)->size();
QSize size = m_view->size();
int tileSize = qMin (size.width() / m_tilesWide,
size.height() / m_tilesHigh);
......@@ -88,6 +91,15 @@ void KGrScene::redrawScene ()
// appearing if rendering and painting is momentarily a bit slow.
setBackgroundBrush (m_renderer->borderColor());
drawBorder();
// Redraw the all the tiles, except for borders and tiles of type FREE.
for (int i = 1; i <= FIELDWIDTH; i++) {
for (int j = 1; j <= FIELDHEIGHT; j++) {
int index = i * m_tilesHigh + j;
paintCell (i, j, m_tileTypes[index]);
}
}
m_themeChanged = false;
}
}
......@@ -168,6 +180,7 @@ void KGrScene::paintCell (const int i, const int j, const char type)
int index = i * m_tilesHigh + j;
KGameRenderedItem * t = m_renderer->getTileItem (type, m_tiles.at(index));
m_tiles[index] = t;
m_tileTypes[index] = type;
if (t) {
setTileSize (t, m_tileSize);
......
......@@ -54,6 +54,7 @@
#include <KgTheme>
#include <KGameRenderedItem>
class KGrView;
class KGrRenderer;
class KGameRenderer;
......@@ -61,7 +62,7 @@ class KGrScene : public QGraphicsScene
{
Q_OBJECT
public:
KGrScene (QObject * parent);
KGrScene (KGrView * view);
~KGrScene ();
/*
......@@ -126,6 +127,7 @@ private:
*/
void setTileSize (KGameRenderedItem * tile, const int tileSize);
KGrView * m_view;
KGrRenderer * m_renderer;
KGameRenderedItem * m_background;
......@@ -145,6 +147,8 @@ private:
// The visible elements of the scenario (tiles and borders), excluding the
// background picture.
QVector <KGameRenderedItem *> m_tiles;
// The type of each tile stored in m_tiles.
QByteArray m_tileTypes;
};
#endif // KGRSCENE_H
......@@ -17,6 +17,7 @@
#include "kgrview.h"
#include "kgrscene.h"
#include "kgrglobals.h"
#include "kgrrenderer.h"
#include <QDebug>
......@@ -24,19 +25,35 @@
KGrView::KGrView (QWidget * parent)
:
QGraphicsView (parent),
m_scene (new KGrScene (parent))
m_mouse (new QCursor ()),
m_scene (new KGrScene (this))
{
setScene (m_scene);
}
KGrView::~KGrView ()
{
delete m_mouse;
}
void KGrView::keyReleaseEvent (QKeyEvent * event)
void KGrView::getMousePos (int & i, int & j)
{
if (event->key() == Qt::Key_T)
m_scene->renderer()->selectTheme();
QPointF pos = mapToScene (mapFromGlobal (m_mouse->pos()));
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);
}
void KGrView::resizeEvent (QResizeEvent *)
......
......@@ -18,6 +18,7 @@
#ifndef KGRVIEW_H
#define KGRVIEW_H
#include <QCursor>
#include <QResizeEvent>
#include <QGraphicsView>
......@@ -25,17 +26,31 @@ class KGrScene;
class KGrView : public QGraphicsView
{
Q_OBJECT
public:
KGrView (QWidget * parent);
~KGrView ();
KGrScene * gameScene () const { return m_scene; }
/*
* Get the cursor's position in this widget coordinates.
*/
QPointF mousePos ();
/*
* Get a pointer to the game scene.
*/
KGrScene * gameScene () const { return m_scene; }
public slots:
void getMousePos (int & i, int & j);
void setMousePos (const int i, const int j);
protected:
virtual void resizeEvent (QResizeEvent *);
virtual void keyReleaseEvent (QKeyEvent * event);
virtual void resizeEvent (QResizeEvent *);
private:
KGrScene * m_scene;
QCursor * m_mouse;
KGrScene * m_scene;
};
#endif // KGRVIEW_H
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