1 | #ifndef oxygentileset_h |
2 | #define oxygentileset_h |
3 | |
4 | /* |
5 | * Copyright 2009-2010 Hugo Pereira Da Costa <hugo.pereira@free.fr> |
6 | * Copyright 2008 Long Huynh Huu <long.upcase@googlemail.com> |
7 | * Copyright 2007 Matthew Woehlke <mw_triad@users.sourceforge.net> |
8 | * |
9 | * This library is free software; you can redistribute it and/or |
10 | * modify it under the terms of the GNU Library General Public |
11 | * License version 2 as published by the Free Software Foundation. |
12 | * |
13 | * This library is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | * Library General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Library General Public License |
19 | * along with this library; see the file COPYING.LIB. If not, write to |
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | * Boston, MA 02110-1301, USA. |
22 | */ |
23 | |
24 | #include "oxygen_export.h" |
25 | |
26 | #include <QtGui/QPixmap> |
27 | #include <QtCore/QRect> |
28 | #include <QtCore/QVector> |
29 | |
30 | //! handles proper scaling of pixmap to match widget rect. |
31 | /*! |
32 | tilesets are collections of stretchable pixmaps corresponding to a given widget corners, sides, and center. |
33 | corner pixmaps are never stretched. center pixmaps are |
34 | */ |
35 | namespace Oxygen |
36 | { |
37 | class OXYGEN_EXPORT TileSet |
38 | { |
39 | public: |
40 | /*! |
41 | Create a TileSet from a pixmap. The size of the bottom/right chunks is |
42 | whatever is left over from the other chunks, whose size is specified |
43 | in the required parameters. |
44 | |
45 | @param w1 width of the left chunks |
46 | @param h1 height of the top chunks |
47 | @param w2 width of the not-left-or-right chunks |
48 | @param h2 height of the not-top-or-bottom chunks |
49 | */ |
50 | TileSet(const QPixmap&, int w1, int h1, int w2, int h2, bool stretch = false ); |
51 | |
52 | /*! |
53 | Create a TileSet from a pixmap. The size of the top/left and bottom/right |
54 | chunks is specified, with the middle chunks created from the specified |
55 | portion of the pixmap. This allows the middle chunks to overlap the outer |
56 | chunks (or to not use all pixels). The top/left and bottom/right chunks |
57 | are carved out of the corners of the pixmap. |
58 | |
59 | @param w1 width of the left chunks |
60 | @param h1 height of the top chunks |
61 | @param w3 width of the right chunks |
62 | @param h3 height of bottom chunks |
63 | @param x2 x-coordinate of the top of the not-left-or-right chunks |
64 | @param y2 y-coordinate of the left of the not-top-or-bottom chunks |
65 | @param w2 width of the not-left-or-right chunks |
66 | @param h2 height of the not-top-or-bottom chunks |
67 | */ |
68 | TileSet(const QPixmap &pix, int w1, int h1, int w3, int h3, int x2, int y2, int w2, int h2, bool stretch = false ); |
69 | |
70 | //! empty constructor |
71 | TileSet(); |
72 | |
73 | //! destructor |
74 | virtual ~TileSet() |
75 | {} |
76 | |
77 | /*! |
78 | Flags specifying what sides to draw in ::render. Corners are drawn when |
79 | the sides forming that corner are drawn, e.g. Top|Left draws the |
80 | top-center, center-left, and top-left chunks. The center-center chunk is |
81 | only drawn when Center is requested. |
82 | */ |
83 | enum Tile { |
84 | Top = 0x1, |
85 | Left = 0x2, |
86 | Bottom = 0x4, |
87 | Right = 0x8, |
88 | Center = 0x10, |
89 | TopLeft = Top|Left, |
90 | TopRight = Top|Right, |
91 | BottomLeft = Bottom|Left, |
92 | BottomRight = Bottom|Right, |
93 | Ring = Top|Left|Bottom|Right, |
94 | Horizontal = Left|Right|Center, |
95 | Vertical = Top|Bottom|Center, |
96 | Full = Ring|Center |
97 | }; |
98 | Q_DECLARE_FLAGS(Tiles, Tile) |
99 | |
100 | /*! |
101 | Fills the specified rect with tiled chunks. Corners are never tiled, |
102 | edges are tiled in one direction, and the center chunk is tiled in both |
103 | directions. Partial tiles are used as needed so that the entire rect is |
104 | perfectly filled. Filling is performed as if all chunks are being drawn. |
105 | */ |
106 | void render(const QRect&, QPainter*, Tiles = Ring) const; |
107 | |
108 | //! return size associated to this tileset |
109 | QSize size( void ) const |
110 | { return QSize( _w1 + _w3, _h1 + _h3 ); } |
111 | |
112 | //! is valid |
113 | bool isValid( void ) const |
114 | { return _pixmaps.size() == 9; } |
115 | |
116 | //! save all pixmaps |
117 | /*! pixmap names will be \p basename-position.suffix. Other arguments are the same as for QPixmap::save */ |
118 | void save( const QString& basename, const QString& suffix = "png" , const char* format = 0, int quality = -1 ) const; |
119 | |
120 | //! side extend |
121 | /*! |
122 | it is used to (pre) tile the side pixmaps, in order to make further tiling faster when rendering, at the cost of |
123 | using more memory for the cache. Changes to this member only affects tilesets that are created afterwards. |
124 | */ |
125 | void setSideExtent( int value ) |
126 | { _sideExtent = value; } |
127 | |
128 | //! returns pixmap for given index |
129 | QPixmap pixmap( int index ) const |
130 | { return _pixmaps[index]; } |
131 | |
132 | protected: |
133 | |
134 | //! shortcut to pixmap list |
135 | typedef QVector<QPixmap> PixmapList; |
136 | |
137 | //! initialize pixmap |
138 | void initPixmap( PixmapList&, const QPixmap&, int w, int h, const QRect& ); |
139 | |
140 | private: |
141 | |
142 | //! side extend |
143 | /*! |
144 | it is used to (pre) tile the side pixmaps, in order to make further tiling faster when rendering, at the cost of |
145 | using more memory for the cache. |
146 | */ |
147 | static int _sideExtent; |
148 | |
149 | //! pixmap arry |
150 | PixmapList _pixmaps; |
151 | |
152 | // stretch pixmaps |
153 | bool _stretch; |
154 | |
155 | // dimensions |
156 | int _w1; |
157 | int _h1; |
158 | int _w3; |
159 | int _h3; |
160 | |
161 | }; |
162 | |
163 | } |
164 | |
165 | Q_DECLARE_OPERATORS_FOR_FLAGS(Oxygen::TileSet::Tiles) |
166 | |
167 | #endif //TILESET_H |
168 | |