File indexing completed on 2024-05-12 04:06:23

0001 /*
0002     SPDX-FileCopyrightText: 2009 Stefan Majewsky <majewsky@gmx.net>
0003     SPDX-FileCopyrightText: 2014 Ian Wadham <iandw.au@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef PALAPELI_GAMEPLAY_H
0009 #define PALAPELI_GAMEPLAY_H
0010 
0011 class KConfig;
0012 
0013 class QStackedWidget;
0014 
0015 #include "../window/mainwindow.h"
0016 
0017 #include <QMap>
0018 #include <QElapsedTimer>    // IDW test.
0019 
0020 namespace Palapeli
0021 {
0022     class CollectionView;
0023     class Puzzle;
0024     class PuzzleTableWidget;
0025     class PieceHolder;
0026     class PuzzlePreview;
0027     class View;
0028     class Scene;
0029     class Piece;
0030 
0031     /**
0032      * This is the main class for Palapeli gameplay. It implements menu and
0033      * toolbar actions and provides methods such as loading and shuffling
0034      * a puzzle, starting a puzzle, saving and restoring the state of the
0035      * solution, managing piece-holders, reporting progress and showing
0036      * a victory animation.
0037      */
0038 
0039     class GamePlay : public QObject
0040     {
0041         Q_OBJECT
0042         public:
0043             explicit GamePlay(MainWindow* mainWindow = nullptr);
0044             ~GamePlay() override;
0045             void    init();
0046             void    shutdown();
0047             CollectionView* collectionView()
0048                     { return m_collectionView; };
0049             PuzzleTableWidget* puzzleTable()
0050                     { return m_puzzleTable; };
0051             static const int LargePuzzle;
0052         public Q_SLOTS:
0053             void playPuzzle(Palapeli::Puzzle* puzzle);
0054             void playPuzzleFile(const QString& path);
0055             void actionGoCollection();
0056             void actionTogglePreview();
0057             void actionCreate();
0058             void actionDelete();
0059             void actionImport();
0060             void actionExport();
0061             void createHolder();
0062             void deleteHolder();
0063             void selectAll();
0064             void rearrangePieces();
0065             void actionZoomIn();
0066             void actionZoomOut();
0067             void restartPuzzle();
0068             void configure();
0069 
0070             void positionChanged(int reduction);
0071         Q_SIGNALS:
0072             void reportProgress(int pieceCount, int originalCount);
0073             void victoryAnimationFinished();
0074         private Q_SLOTS:
0075             void loadPreview();
0076             void loadPuzzleFile();
0077             void loadNextPiece();
0078             void loadPiecePositions();
0079             void finishLoading();
0080 
0081             void playVictoryAnimation2();
0082             void playVictoryAnimation3();
0083 
0084             void updateSavedGame();
0085 
0086             void changeSelectedHolder(PieceHolder* h);
0087             void teleport(Piece* piece, const QPointF& scenePos,
0088                             View* view);
0089             void closeHolder(PieceHolder* h);
0090             void handleNewPieceSelection(View* view);
0091 
0092         private:
0093             static QString saveGamePath() { return QStringLiteral("collection/"); }
0094             static QString saveGameFileName(const QString &name) { return QStringLiteral("%1.save").arg(name); }
0095             void deletePuzzleViews();
0096             void loadPuzzle();
0097             void playVictoryAnimation();
0098             void calculatePieceAreaSize();
0099             void createHolder(const QString& name, bool sel = true);
0100             void transferPieces(const QList<Piece*> &pieces,
0101                     View* source, View* dest,
0102                     const QPointF& scenePos = QPointF());
0103             void setPalapeliMode(bool playing);
0104             QList<Piece*> getSelectedPieces(View* v);
0105 
0106             void savePuzzleSettings(KConfig* savedConfig);
0107             void restorePuzzleSettings(KConfig* savedConfig);
0108 
0109             QStackedWidget*    m_centralWidget;
0110             CollectionView*    m_collectionView;
0111             PuzzleTableWidget* m_puzzleTable;
0112             PuzzlePreview*     m_puzzlePreview;
0113             MainWindow*        m_mainWindow;
0114             Puzzle*            m_puzzle;
0115             Scene*             m_puzzleTableScene;
0116             QList<View*>       m_viewList;
0117             QSizeF             m_pieceAreaSize;
0118             QTimer*            m_savegameTimer;
0119             PieceHolder*       m_currentHolder;
0120             PieceHolder*       m_previousHolder;
0121 
0122             // Some stuff needed for loading puzzles.
0123             bool m_loadingPuzzle;
0124             bool m_restoredGame;
0125             QMap<int, Palapeli::Piece*> m_loadedPieces;
0126             int m_originalPieceCount;
0127             int m_currentPieceCount;
0128             qreal m_sizeFactor;
0129             bool m_playing;
0130             bool m_canDeletePuzzle;
0131             bool m_canExportPuzzle;
0132             QElapsedTimer t;    // IDW test.
0133     };
0134 }
0135 
0136 #endif // PALAPELI_GAMEPLAY_H