| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2015 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #ifndef TRACKTILE_HPP |
| 17 | #define TRACKTILE_HPP |
| 18 | |
| 19 | #include <QGraphicsItem> |
| 20 | #include <QMenu> |
| 21 | #include <QString> |
| 22 | |
| 23 | #include "../common/tracktilebase.hpp" |
| 24 | |
| 25 | class QGraphicsLineItem; |
| 26 | class TileAnimator; |
| 27 | |
| 28 | /*! A race track is built of TrackTiles. The TrackTile class |
| 29 | * extends TrackTileBase with features needed to render |
| 30 | * track tiles in editor's scene. Note that the track tile instances |
| 31 | * do not change. Only their types change when user sets a tile. */ |
| 32 | class TrackTile : public QGraphicsItem, public TrackTileBase |
| 33 | { |
| 34 | public: |
| 35 | /*! Constructor. |
| 36 | * \param location Location (coordinates) in the track scene. |
| 37 | * \param matrixLocation Location in the tile matrix. |
| 38 | * \param type Type of the tile. See setType(). */ |
| 39 | TrackTile(QPointF location, QPoint matrixLocation, |
| 40 | const QString & type = "clear"); |
| 41 | |
| 42 | /*! Copy constructor. |
| 43 | * NOTE!!: This is used when saving undo points so make sure all properties are copied! */ |
| 44 | TrackTile(const TrackTile & other); |
| 45 | |
| 46 | TrackTile & operator=(const TrackTile & other) = delete; |
| 47 | |
| 48 | //! Destructor |
| 49 | virtual ~TrackTile(); |
| 50 | |
| 51 | virtual QRectF boundingRect() const override; |
| 52 | |
| 53 | virtual void paint(QPainter * painter, |
| 54 | const QStyleOptionGraphicsItem * option, QWidget * widget = 0) override; |
| 55 | |
| 56 | //! Set tile active (a blue collar is drawn) |
| 57 | void setActive(bool active); |
| 58 | |
| 59 | virtual void setTileType(const QString & type) override; |
| 60 | |
| 61 | virtual void setComputerHint(ComputerHint hint) override; |
| 62 | |
| 63 | virtual void setLocation(QPointF location) override; |
| 64 | |
| 65 | /*! Get current active tile. |
| 66 | * \returns Valid pointer or nullptr if not set. */ |
| 67 | static TrackTile * activeTile(); |
| 68 | |
| 69 | //! Set the active tile |
| 70 | static void setActiveTile(TrackTile * tile); |
| 71 | |
| 72 | //! Rotate 90 degrees CW |
| 73 | bool rotate90CW(); |
| 74 | |
| 75 | //! Rotate 90 degrees CCW |
| 76 | bool rotate90CCW(); |
| 77 | |
| 78 | //! Get the current pixmap |
| 79 | QPixmap pixmap() const; |
| 80 | |
| 81 | //! Set the current pixmap |
| 82 | void setPixmap(const QPixmap & pixmap); |
| 83 | |
| 84 | //! Swap data with given tile. Used in drag'n'drop. |
| 85 | void swap(TrackTile & other); |
| 86 | |
| 87 | void setAdded(bool state); |
| 88 | |
| 89 | bool added() const; |
| 90 | |
| 91 | private: |
| 92 | //! Create the menu that is shown when right-clicking on the tile. |
| 93 | void createContextMenu(); |
| 94 | |
| 95 | //! Pointer to the active/selected tile. |
| 96 | static TrackTile * m_activeTile; |
| 97 | |
| 98 | //! Original size of the tile in pixels. |
| 99 | QSizeF m_size; |
| 100 | |
| 101 | //! True if the tile is the seleced tile. |
| 102 | bool m_active; |
| 103 | |
| 104 | //! Animator used when rotating the tile. |
| 105 | TileAnimator * m_animator; |
| 106 | |
| 107 | //! Image of the tile. |
| 108 | QPixmap m_pixmap; |
| 109 | |
| 110 | bool m_added; |
| 111 | }; |
| 112 | |
| 113 | using TrackTileS = std::shared_ptr<TrackTile>; |
| 114 | |
| 115 | #endif // TRACKTILE_HPP |