File indexing completed on 2024-12-22 04:31:08

0001 #ifndef TILESET_H
0002 #define TILESET_H
0003 
0004 /*************************************************************************
0005  * Copyright (C) 2014 by Hugo Pereira Da Costa <hugo.pereira@free.fr>    *
0006  *                                                                       *
0007  * This program is free software; you can redistribute it and/or modify  *
0008  * it under the terms of the GNU General Public License as published by  *
0009  * the Free Software Foundation; either version 2 of the License, or     *
0010  * (at your option) any later version.                                   *
0011  *                                                                       *
0012  * This program is distributed in the hope that it will be useful,       *
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0015  * GNU General Public License for more details.                          *
0016  *                                                                       *
0017  * You should have received a copy of the GNU General Public License     *
0018  * along with this program; if not, write to the                         *
0019  * Free Software Foundation, Inc.,                                       *
0020  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0021  *************************************************************************/
0022 
0023 
0024 #include <QPixmap>
0025 #include <QRect>
0026 #include <QVector>
0027 
0028 //* handles proper scaling of pixmap to match widget rect.
0029 /**
0030 tilesets are collections of stretchable pixmaps corresponding to a given widget corners, sides, and center.
0031 corner pixmaps are never stretched. center pixmaps are
0032 */
0033 class TileSet
0034 {
0035     public:
0036     /**
0037     Create a TileSet from a pixmap. The size of the bottom/right chunks is
0038     whatever is left over from the other chunks, whose size is specified
0039     in the required parameters.
0040 
0041     @param w1 width of the left chunks
0042     @param h1 height of the top chunks
0043     @param w2 width of the not-left-or-right chunks
0044     @param h2 height of the not-top-or-bottom chunks
0045     */
0046     TileSet(const QPixmap&, int w1, int h1, int w2, int h2 );
0047 
0048     //* empty constructor
0049     TileSet();
0050 
0051     //* destructor
0052     virtual ~TileSet()
0053     {}
0054 
0055     /**
0056     Flags specifying what sides to draw in ::render. Corners are drawn when
0057     the sides forming that corner are drawn, e.g. Top|Left draws the
0058     top-center, center-left, and top-left chunks. The center-center chunk is
0059     only drawn when Center is requested.
0060     */
0061     enum Tile {
0062         Top = 0x1,
0063         Left = 0x2,
0064         Bottom = 0x4,
0065         Right = 0x8,
0066         Center = 0x10,
0067         TopLeft = Top|Left,
0068         TopRight = Top|Right,
0069         BottomLeft = Bottom|Left,
0070         BottomRight = Bottom|Right,
0071         Ring = Top|Left|Bottom|Right,
0072         Horizontal = Left|Right|Center,
0073         Vertical = Top|Bottom|Center,
0074         Full = Ring|Center
0075     };
0076     Q_DECLARE_FLAGS(Tiles, Tile)
0077 
0078     /**
0079     Fills the specified rect with tiled chunks. Corners are never tiled,
0080     edges are tiled in one direction, and the center chunk is tiled in both
0081     directions. Partial tiles are used as needed so that the entire rect is
0082     perfectly filled. Filling is performed as if all chunks are being drawn.
0083     */
0084     void render(const QRect&, QPainter*, Tiles = Ring) const;
0085 
0086     //* return size associated to this tileset
0087     QSize size() const
0088     { return QSize( _w1 + _w3, _h1 + _h3 ); }
0089 
0090     //* is valid
0091     bool isValid() const
0092     { return _pixmaps.size() == 9; }
0093 
0094     //* returns pixmap for given index
0095     QPixmap pixmap( int index ) const
0096     { return _pixmaps[index]; }
0097 
0098     protected:
0099 
0100     //* shortcut to pixmap list
0101     using PixmapList = QVector<QPixmap>;
0102 
0103     //* initialize pixmap
0104     void initPixmap( PixmapList&, const QPixmap&, int w, int h, const QRect& );
0105 
0106     private:
0107 
0108     //* pixmap arry
0109     PixmapList _pixmaps;
0110 
0111     // dimensions
0112     int _w1;
0113     int _h1;
0114     int _w3;
0115     int _h3;
0116 
0117 };
0118 
0119 Q_DECLARE_OPERATORS_FOR_FLAGS(TileSet::Tiles)
0120 
0121 #endif //TILESET_H