File indexing completed on 2024-03-24 04:05:17

0001 /*
0002     SPDX-FileCopyrightText: 2012 Christian Krippendorf <Coding@Christian-Krippendorf.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef GAMESCENE_H
0008 #define GAMESCENE_H
0009 
0010 // Qt
0011 #include <QGraphicsScene>
0012 
0013 // KMahjongg
0014 #include "kmtypes.h"
0015 
0016 constexpr int BOARD_WIDTH = 36;
0017 constexpr int BOARD_HEIGHT = 16;
0018 constexpr int BOARD_DEPTH = 5;
0019 
0020 
0021 // Forward declarations...
0022 class GameItem;
0023 class GameBackground;
0024 class GameRemovedTiles;
0025 
0026 /**
0027  * Holds and manages all GameItems.
0028  *
0029  * @author Christian Krippendorf */
0030 class GameScene : public QGraphicsScene
0031 {
0032     Q_OBJECT
0033 
0034 public:
0035     explicit GameScene(QObject * parent = nullptr);
0036     ~GameScene() override;
0037 
0038     /**
0039      * Return the GameItem on the given grid position.
0040      *
0041      * @param x The x position of the item.
0042      * @param y The y position of the item.
0043      * @param z The z position of the item.
0044      *
0045      * @return The GameItem object or null if no one was found. */
0046     GameItem * getItemOnGridPos(int x, int y, int z);
0047 
0048     /**
0049      * Return the GameItem on the given grid position.
0050      *
0051      * @param stItemPos The position struct.
0052      *
0053      * @return The GameItem object or null if no one was found. */
0054     GameItem * getItemOnGridPos(POSITION & stItemPos);
0055 
0056     /**
0057      * Test whether a item exist on the given position or not.
0058      *
0059      * @param x The x position of the item.
0060      * @param y The y position of the item.
0061      * @param z The z position of the item.
0062      *
0063      * @return True if GameItem object was found, else false. */
0064     bool isItemOnGridPos(int x, int y, int z) const;
0065 
0066     /**
0067      * Override from QGraphicsScene. */
0068     void clear();
0069 
0070     /**
0071      * Clear only the GameItem objects
0072      */
0073     void clearGameItems();
0074 
0075     /**
0076      * Override from QGraphicsScene. */
0077     void addItem(GameItem * gameItem);
0078 
0079     /**
0080      * Override from QGraphicsScene. */
0081     void removeItem(GameItem * gameItem);
0082 
0083     /**
0084      * Override from QGraphicsScene with POSITION parameter.
0085      *
0086      * @param stItemPos The item position. */
0087     void removeItem(POSITION const & stItemPos);
0088 
0089     /**
0090      * Override from QGraphicsScene. */
0091     QList<GameItem *> selectedItems() const;
0092 
0093     /**
0094      * Override from QGraphicsScene. */
0095     QList<GameItem *> items() const;
0096 
0097     /**
0098      * Override from QGraphicsScene. */
0099     void mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent) override;
0100 
0101     /**
0102      * Override from QGraphicsScene. */
0103     void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent) override;
0104 
0105     /**
0106      * Override from QGraphicsScene.
0107      * Mouse wheel rotates view. */
0108     void wheelEvent(QGraphicsSceneWheelEvent * mouseEvent) override;
0109 
0110     /**
0111      * Test if the item is selectable or not.
0112      *
0113      * @param pameItem The game item to test.
0114      * @return True if selectable else false. */
0115     bool isSelectable(const GameItem * const pameItem) const;
0116 
0117     /**
0118      * Set a background
0119      * @param gameBackground The background object
0120      */
0121     void setBackgroundItem(GameBackground * gameBackground);
0122 
0123     /**
0124      * Set a removedtiles item
0125      * @param gameRemovedTiles The removedtiles object
0126      */
0127     void setRemovedTilesItem(GameRemovedTiles * gameRemovedTiles);
0128 
0129 Q_SIGNALS:
0130     void rotateCW();
0131     void rotateCCW();
0132     void clearSelectedTile();
0133 
0134 private:
0135     /**
0136      * Initialize the m_pGameItemsArray. */
0137     void initializeGameItemsArray();
0138 
0139     /**
0140      * Adds an item to the positions array.
0141      *
0142      * @param pameItem THe game item to add to array. */
0143     void addItemToPositionArray(GameItem * const pameItem);
0144 
0145     GameItem * m_pGameItemsArray[BOARD_WIDTH][BOARD_HEIGHT][BOARD_DEPTH];
0146     GameItem * m_pFirstSelectedItem;
0147     GameItem * m_pSecondSelectedItem;
0148 
0149     GameBackground * m_gameBackground;
0150     GameRemovedTiles * m_gameRemovedTiles;
0151 };
0152 
0153 #endif // GAMESCENE_H