/*************************************************************************** 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 PIECE_H #define PIECE_H #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include /* Bitmap cache for the piece masks, which are used to * give a piece its jigsaaw-puzzle-like shape. */ extern QCache* maskCache; class KPuzzleGame; /* This class represents a single jigsaw puzzle piece. Note that when a piece * has been placed at its correct position, the flag _hasBeenSet is set * to true, and the player will never again see the piece, but the piece structure * is *not* removed from the piece list. */ class CPiece : public QObject { Q_OBJECT public: /* Constructor. Reads piece data from a stream. Used when a game is * loaded from a file. */ CPiece(KPuzzleGame* owner,QDataStream* f); /* Constructor. Makes a piece at a certain position. Used when * a new game is created. */ CPiece(KPuzzleGame* owner,QPoint pos); /* Destructor. */ virtual ~CPiece(); /* Data */ public: /* Position of this piece. Never changes. */ QPoint pos() const { return _pos; } /* The piece time in seconds, as shown in the time widget. * Only valid if gameType \in PIECE_TIME_GAME, UNBEARABLE_GAME. */ int time() const; /* The turn property saves the way a piece is turned (as * a number from 0 to 3). This reference may be used to * *change* the _turn property. */ int& turn() { return _turn; } /* Read the _turn property. */ int turn() const { return _turn; } /* The basic size of a piece (which does *not* take * into account the additional space needed for the * jigsaw puzzle-like shape of a piece). */ QSize pieceSize() const; /* The number of pieces (horizontally / vertically). */ QSize piecesCount() const; /* The number which has to be added to the piece size *in * every direction* to get the real size of a piece pixmap. * This is positive if and only if we are playing with * _useMask == true. */ int displace() const; /* _hasBeenSet is true if this piece has already been placed * at its correct position. */ bool hasBeenSet() const { return _hasBeenSet; } /* Return true if this piece should be shown to the player, i.e. * _hasBeenSet is false *and* there is time left for this piece * *and* the faults count for this piece is not too high (according * to the game type). */ bool mayKeepPiece() const; /* True if this is the current piece. Only important for loading. */ bool isCurrent() const { return _isCurrent; } protected: /* The KPuzzleGame object which "owns" this piece. */ KPuzzleGame* _owner; /* Position of this piece. Never changes. */ QPoint _pos; /* The turn property saves the way a piece is turned (as * a number from 0 to 3). */ int _turn; /* How much time has this piece been shown. */ int _time; /* How many times has the player tried to place this piece? */ int _faults; /* _hasBeenSet is true if this piece has already been placed * at its correct position. */ bool _hasBeenSet; /* True if this is the current piece. Only important for loading. */ bool _isCurrent; /* The number of the timer of this piece. Only used in modes PIECE_TIME_GAME * and UNBEARABLE_GAME. */ int _timerNr; /* Implementation */ public: /* Called when this piece is to be shown. Returns true if * this piece should be shown; in this case, makes the piece ready * to be shown (i.e. starts the timer). */ bool showPiece(); /* Hide this piece, destroy connections. */ void hidePiece(); /* Try to place this piece at position p. If p != _pos (with a certain * tolerance), return false, if p == _pos, set _hasBeenSet to true. */ bool setPiece(QPoint p); /* Copy the pixmap showing this piece (in its jigsaw-puzzle-like shape) * into p. */ void getPixmap(QPixmap* p) const; /* Copy the pixmap showing this piece (in its jigsaw-puzzle-like shape) * into p. Take into account the _turn of this piece. */ void getTurnedPixmap(QPixmap* p) const; /* Stop the timer of this piece. */ void stopTimer(); /* Start the timer of this piece. */ void startTimer(); /* Save the data of this piece to f. */ void save(QDataStream* f) const; protected: /* Retrieve the mask of this piece, which is responsible for the jigsaw-like * shape of this piece. */ QBitmap* getPieceMask() const; /* Load the data of this piece. Only called by the constructor. */ void load(QDataStream* f); /* Called on each timer tick. */ void timerEvent(QTimerEvent*); signals: /* Emitted on each timer tick, if the game type is * PIECE_FAULTS_GAME or UNBEARABLE_GAME. The parameter * is the time the player *has got* to place the piece. */ void sigTimerTick(int); }; #endif /* PIECE_H */