File indexing completed on 2021-12-21 12:50:22

0001 /*
0002  *  ksokoban - a Sokoban game by KDE
0003  *  Copyright (C) 1998  Anders Widell  <d95-awi@nada.kth.se>
0004  *
0005  *  This program is free software; you can redistribute it and/or modify
0006  *  it under the terms of the GNU General Public License as published by
0007  *  the Free Software Foundation; either version 2 of the License, or
0008  *  (at your option) any later version.
0009  *
0010  *  This program is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  *  GNU General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU General Public License
0016  *  along with this program; if not, write to the Free Software
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018  */
0019 
0020 #ifndef PLAYFIELD_H
0021 #define PLAYFIELD_H
0022 
0023 #include <QWidget>
0024 #include <QString>
0025 #include <QFont>
0026 #include <QFontMetrics>
0027 #include <QPixmap>
0028 #include <QImage>
0029 #include <QBrush>
0030 
0031 #include "ImageData.h"
0032 #include "LevelMap.h"
0033 class MapDelta;
0034 class MoveSequence;
0035 class Move;
0036 #include "PathFinder.h"
0037 
0038 class History;
0039 class Bookmark;
0040 class LevelCollection;
0041 class QPainter;
0042 class QCursor;
0043 
0044 class PlayField : public QWidget {
0045   Q_OBJECT
0046 public:
0047   PlayField(QWidget *parent);
0048   ~PlayField () override;
0049 
0050   bool canMoveNow();
0051   int animDelay() { return animDelay_; }
0052 
0053   void setSize(int w, int h);
0054   void level(int _l) { levelMap_->level(_l); }
0055   LevelCollection  *collection() const { return levelMap_->collection(); }
0056   void setBookmark(Bookmark *bm);
0057   void goToBookmark(Bookmark *bm);
0058 
0059   int level() const;
0060   const QString &collectionName();
0061   int totalMoves() const;
0062   int totalPushes() const;
0063 
0064   void updateCollectionXpm();
0065   void updateTextXpm();
0066   void updateLevelXpm();
0067   void updateStepsXpm();
0068   void updatePushesXpm();
0069 
0070 public slots:
0071   void nextLevel();
0072   void previousLevel();
0073   void undo();
0074   void redo();
0075   void restartLevel();
0076   void changeCollection(LevelCollection *collection);
0077   void changeAnim(int num);
0078 
0079 protected:
0080   ImageData *imageData_;
0081   LevelMap  *levelMap_;
0082   History   *history_;
0083   int        lastLevel_;
0084   MoveSequence  *moveSequence_;
0085   MapDelta  *mapDelta_;
0086   bool       moveInProgress_;
0087   bool       dragInProgress_;
0088   PathFinder pathFinder_;
0089   int        animDelay_;
0090   const QCursor* cursor_;
0091 
0092   void levelChange ();
0093   void paintSquare (int x, int y, QPainter &paint);
0094   void paintDelta ();
0095   void paintEvent (QPaintEvent *e) override;
0096   void paintPainterClip(QPainter& paint, int x, int y, int w, int h);
0097   void paintPainter(QPainter& paint, const QRect& rect);
0098   void resizeEvent (QResizeEvent *e) override;
0099   void mouseMoveEvent(QMouseEvent* e) override;
0100   void keyPressEvent (QKeyEvent *) override;
0101   void focusInEvent (QFocusEvent *) override;
0102   void focusOutEvent (QFocusEvent *) override;
0103   void mousePressEvent (QMouseEvent *) override;
0104   void mouseReleaseEvent(QMouseEvent*) override;
0105   void leaveEvent(QEvent*) override;
0106   void wheelEvent (QWheelEvent *) override;
0107   void step (int _x, int _y);
0108   void push (int _x, int _y);
0109   void timerEvent (QTimerEvent *) override;
0110   void stopDrag();
0111   void dragObject(int xpixel, int ypixel);
0112   void highlight();
0113   void changeCursor(const QCursor* c);
0114 
0115 private:
0116   int size_, xOffs_, yOffs_;
0117   int highlightX_, highlightY_;
0118   int dragX_, dragY_;
0119   int lastMouseXPos_, lastMouseYPos_;
0120   int mousePosX_, mousePosY_;
0121   int wheelDelta_;
0122   int debug_counter;
0123   
0124   QList<int> timers;
0125   void killTimers();
0126   QCursor sizeAllCursor;
0127   QCursor crossCursor; 
0128   int x2pixel (int x) const { return size_*x+xOffs_; }
0129   int y2pixel (int y) const { return size_*y+yOffs_; }
0130 
0131   int pixel2x (int x) const { return (x-xOffs_)/size_; }
0132   int pixel2y (int y) const { return (y-yOffs_)/size_; }
0133 
0134   void startMoving (Move *m);
0135   void startMoving (MoveSequence *ms);
0136   void stopMoving ();
0137 
0138   QRect pnumRect_, ptxtRect_, snumRect_, stxtRect_, lnumRect_, ltxtRect_;
0139   QRect collRect_;
0140 
0141   const QString levelText_, stepsText_, pushesText_;
0142   QPixmap *pnumXpm_, *ptxtXpm_, *snumXpm_, *stxtXpm_, *lnumXpm_, *ltxtXpm_;
0143   QPixmap *collXpm_;
0144   QPixmap dragXpm_;
0145   QImage  dragImage_;
0146   QFont         statusFont_;
0147   QFontMetrics  statusMetrics_;
0148   QBrush  background_;
0149   QBrush  floor_;
0150 
0151 };
0152 
0153 #endif  /* PLAYFIELD_H */