/*************************************************************************** Description : KPuzzle - A KDE Jigsaw Puzzle Game Version : 0.2 Copyright : (C) 2000-2001 by Michael Wand EMail : mwand@gmx.de ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #ifndef KGAMEWIDGET_H #define KGAMEWIDGET_H #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include class KPuzzleView; class KPuzzleGame; /* This widget class represents the playing area, the area of the screen in * which the pieces are moved and put together. */ class KGameWidget : public QWidget { Q_OBJECT public: /* Constructor. Parent should be the (single) KPuzzleView object. */ KGameWidget(QWidget *parent); /* Set the pointer to the KPuzzleGame object, which contains * all information on the current game. */ void setGame(KPuzzleGame* game) { _game = game; } /* Destructor. */ virtual ~KGameWidget(); /* Initialize some things that cannot be done in the constructor. * Currently, this only concerns the size of the picture which is * the base of the current game. */ void initialize(QSize picSize); /* Data */ public: /* Upper left corner of the visible part of the picture */ QPoint& scrollPos() { return _scrollPos; } /* Which of the "moving keys" is pressed? */ char moveKeyPressed() { return _moveKeyPressed; } protected: /* Basic piece size */ QSize pieceSize(); /* Size of the displayed piece */ QSize pieceSizeDisp(); /* Center of the piece currently moved, -1/-1 if there is none */ QPoint piecePos() { return _piecePos; } /* Rectangle of the piece currently moved. The returned QRect * will have -1/-1 as top-left corner if there is no piece * currently moved. */ QRect pieceRect(); /* Top-left ocrner of the piece currently moved, -1/-1 if there is none */ QPoint pieceTopLeft(); /* Upper left corner of the visible part of the picture */ QPoint _scrollPos; /* If the right mouse key is pressed: Position of the mouse when * the last mouseMoveEvent (or mousePressEvent) was proceeded. Used * for moving the visible area with the right mouse button. */ QPoint _movePos; /* Pointer to the KPuzzleGame object, which contains all information * on the current game. */ KPuzzleGame* _game; /* XXX */ QPoint _piecePos; /* Timer for moving the visible area when a "moving key" is pressed */ QTimer* _moveTimer; /* Denotes the (single) moving key currently pressed. 0 for none, * 1..4 for left, top, right, bottom. */ char _moveKeyPressed; /* True if the right mouse key is pressed (in this case, this widget * behaves differently) */ bool _rightPressed; /* XXX */ QPixmap* _backBuffer; /* Implementation */ protected: /* Inherited widget event reaction functions. */ virtual void paintEvent(QPaintEvent* event); virtual void mouseMoveEvent(QMouseEvent* event); virtual void mousePressEvent(QMouseEvent* event); virtual void mouseReleaseEvent(QMouseEvent* event); virtual void leaveEvent(QEvent*); /* Returns the logical position of the piece at position p * (that means, if there are m * n pieces in this game, the * return value of this function ranges from (0,0) to (m-1,n-1). */ QPoint calculatePiecePos(QPoint p); /* Conversion functions for logical/device coordinates (where * device coordinates are dependent from the scrollPos()). */ QPoint LPtoDP(QPoint pt) { return pt - scrollPos(); } QPoint DPtoLP(QPoint pt) { return pt + scrollPos(); } QRect LPtoDP(QRect r) { return QRect(LPtoDP(r.topLeft()),LPtoDP(r.bottomRight())); } QRect DPtoLP(QRect r) { return QRect(DPtoLP(r.topLeft()),DPtoLP(r.bottomRight())); } public slots: /* Slots activated when moving keys are pressed or released */ void slotLeftPress(); void slotLeftRelease(); void slotRightPress(); void slotRightRelease(); void slotUpPress(); void slotUpRelease(); void slotDownPress(); void slotDownRelease(); /* The timer slot which is in use when a moving key is pressed */ void slotMoveTimer(); /* This slot is called to perform a movement of the visible area of the * game area. It is always connected to sigShowAt. */ void slotShowAt(QPoint pos); signals: /* This signal is emitted when the visible area of the game area should * be moved. The actual moving is performed by the connected slotShowAt(QPoint) */ void sigShowAt(QPoint pos); }; #endif /* KGAMEWIDGET_H */