MauiKit Controls

android/shadowhelper/tileset.cpp
1/*************************************************************************
2 * Copyright (C) 2014 by Hugo Pereira Da Costa <hugo.pereira@free.fr> *
3 * *
4 * This program 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 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 *************************************************************************/
19
20#include "tileset.h"
21
22#include <QPainter>
23
24//___________________________________________________________
25inline bool bits(TileSet::Tiles flags, TileSet::Tiles testFlags)
26{ return (flags & testFlags) == testFlags; }
27
28//______________________________________________________________________________________
29inline qreal devicePixelRatio( const QPixmap& pixmap )
30{
31 return pixmap.devicePixelRatio();
32}
33
34//______________________________________________________________________________________
35inline void setDevicePixelRatio( QPixmap& pixmap, qreal value )
36{
37 return pixmap.setDevicePixelRatio( value );
38}
39
40//______________________________________________________________
41void TileSet::initPixmap( PixmapList& pixmaps, const QPixmap &source, int width, int height, const QRect &rect)
42{
43 QSize size( width, height );
44 if( !( size.isValid() && rect.isValid() ) )
45 {
46 pixmaps.append( QPixmap() );
47
48 } else if( size != rect.size() ) {
49
50 const qreal dpiRatio( devicePixelRatio( source ) );
51 const QRect scaledRect( rect.topLeft()*dpiRatio, rect.size()*dpiRatio );
52 const QSize scaledSize( size*dpiRatio );
53 const QPixmap tile( source.copy(scaledRect) );
54 QPixmap pixmap( scaledSize );
55
56 pixmap.fill(Qt::transparent);
57 QPainter painter(&pixmap);
58 painter.drawTiledPixmap(0, 0, scaledSize.width(), scaledSize.height(), tile);
59 setDevicePixelRatio( pixmap, dpiRatio );
60 pixmaps.append( pixmap );
61
62 } else {
63
64 const qreal dpiRatio( devicePixelRatio( source ) );
65 const QRect scaledRect( rect.topLeft()*dpiRatio, rect.size()*dpiRatio );
66 QPixmap pixmap( source.copy( scaledRect ) );
67 setDevicePixelRatio( pixmap, dpiRatio );
68 pixmaps.append( pixmap );
69
70 }
71
72}
73
74//______________________________________________________________
76 _w1(0),
77 _h1(0),
78 _w3(0),
79 _h3(0)
80{ _pixmaps.reserve(9); }
81
82//______________________________________________________________
83TileSet::TileSet(const QPixmap &source, int w1, int h1, int w2, int h2 ):
84 _w1(w1),
85 _h1(h1),
86 _w3(0),
87 _h3(0)
88{
89 _pixmaps.reserve(9);
90 if( source.isNull() ) return;
91
92 _w3 = source.width()/devicePixelRatio( source ) - (w1 + w2);
93 _h3 = source.height()/devicePixelRatio( source ) - (h1 + h2);
94 int w = w2;
95 int h = h2;
96
97 // initialise pixmap array
98 initPixmap( _pixmaps, source, _w1, _h1, QRect(0, 0, _w1, _h1) );
99 initPixmap( _pixmaps, source, w, _h1, QRect(_w1, 0, w2, _h1) );
100 initPixmap( _pixmaps, source, _w3, _h1, QRect(_w1+w2, 0, _w3, _h1) );
101 initPixmap( _pixmaps, source, _w1, h, QRect(0, _h1, _w1, h2) );
102 initPixmap( _pixmaps, source, w, h, QRect(_w1, _h1, w2, h2) );
103 initPixmap( _pixmaps, source, _w3, h, QRect(_w1+w2, _h1, _w3, h2) );
104 initPixmap( _pixmaps, source, _w1, _h3, QRect(0, _h1+h2, _w1, _h3) );
105 initPixmap( _pixmaps, source, w, _h3, QRect(_w1, _h1+h2, w2, _h3) );
106 initPixmap( _pixmaps, source, _w3, _h3, QRect(_w1+w2, _h1+h2, _w3, _h3) );
107}
108
109//___________________________________________________________
110void TileSet::render(const QRect &constRect, QPainter *painter, Tiles tiles) const
111{
112
113 const bool oldHint( painter->testRenderHint( QPainter::SmoothPixmapTransform ) );
115
116 // check initialization
117 if( _pixmaps.size() < 9 ) return;
118
119 // copy source rect
120 QRect rect( constRect );
121
122 // get rect dimensions
123 int x0, y0, w, h;
124 rect.getRect(&x0, &y0, &w, &h);
125
126 // calculate pixmaps widths
127 int wLeft(0);
128 int wRight(0);
129 if( _w1+_w3 > 0 )
130 {
131 qreal wRatio( qreal( _w1 )/qreal( _w1 + _w3 ) );
132 wLeft = (tiles&Right) ? qMin( _w1, int(w*wRatio) ):_w1;
133 wRight = (tiles&Left) ? qMin( _w3, int(w*(1.0-wRatio)) ):_w3;
134 }
135
136 // calculate pixmap heights
137 int hTop(0);
138 int hBottom(0);
139 if( _h1+_h3 > 0 )
140 {
141 qreal hRatio( qreal( _h1 )/qreal( _h1 + _h3 ) );
142 hTop = (tiles&Bottom) ? qMin( _h1, int(h*hRatio) ):_h1;
143 hBottom = (tiles&Top) ? qMin( _h3, int(h*(1.0-hRatio)) ):_h3;
144 }
145
146 // calculate corner locations
147 w -= wLeft + wRight;
148 h -= hTop + hBottom;
149 const int x1 = x0 + wLeft;
150 const int x2 = x1 + w;
151 const int y1 = y0 + hTop;
152 const int y2 = y1 + h;
153
154 const int w2 = _pixmaps.at(7).width()/devicePixelRatio( _pixmaps.at(7) );
155 const int h2 = _pixmaps.at(5).height()/devicePixelRatio( _pixmaps.at(5) );
156
157 // corner
158 if( bits( tiles, Top|Left) ) painter->drawPixmap(x0, y0, _pixmaps.at(0), 0, 0, wLeft*devicePixelRatio( _pixmaps.at(0) ), hTop*devicePixelRatio( _pixmaps.at(0) ));
159 if( bits( tiles, Top|Right) ) painter->drawPixmap(x2, y0, _pixmaps.at(2), (_w3-wRight)*devicePixelRatio( _pixmaps.at(2) ), 0, wRight*devicePixelRatio( _pixmaps.at(2) ), hTop*devicePixelRatio( _pixmaps.at(2) ) );
160 if( bits( tiles, Bottom|Left) ) painter->drawPixmap(x0, y2, _pixmaps.at(6), 0, (_h3-hBottom)*devicePixelRatio( _pixmaps.at(6) ), wLeft*devicePixelRatio( _pixmaps.at(6) ), hBottom*devicePixelRatio( _pixmaps.at(6) ));
161 if( bits( tiles, Bottom|Right) ) painter->drawPixmap(x2, y2, _pixmaps.at(8), (_w3-wRight)*devicePixelRatio( _pixmaps.at(8) ), (_h3-hBottom)*devicePixelRatio( _pixmaps.at(8) ), wRight*devicePixelRatio( _pixmaps.at(8) ), hBottom*devicePixelRatio( _pixmaps.at(8) ) );
162
163 // top and bottom
164 if( w > 0 )
165 {
166 if( tiles&Top ) painter->drawPixmap(x1, y0, w, hTop, _pixmaps.at(1), 0, 0, w2*devicePixelRatio( _pixmaps.at(1) ), hTop*devicePixelRatio( _pixmaps.at(1) ) );
167 if( tiles&Bottom ) painter->drawPixmap(x1, y2, w, hBottom, _pixmaps.at(7), 0, (_h3-hBottom)*devicePixelRatio( _pixmaps.at(7) ), w2*devicePixelRatio( _pixmaps.at(7) ), hBottom*devicePixelRatio( _pixmaps.at(7) ) );
168 }
169
170 // left and right
171 if( h > 0 )
172 {
173 if( tiles&Left ) painter->drawPixmap(x0, y1, wLeft, h, _pixmaps.at(3), 0, 0, wLeft*devicePixelRatio( _pixmaps.at(3) ), h2*devicePixelRatio( _pixmaps.at(3) ) );
174 if( tiles&Right ) painter->drawPixmap(x2, y1, wRight, h, _pixmaps.at(5), (_w3-wRight)*devicePixelRatio( _pixmaps.at(5) ), 0, wRight*devicePixelRatio( _pixmaps.at(5) ), h2*devicePixelRatio( _pixmaps.at(5) ) );
175 }
176
177 // center
178 if( (tiles&Center) && h > 0 && w > 0 ) painter->drawPixmap(x1, y1, w, h, _pixmaps.at(4));
179
180 // restore
182
183}
TileSet(const QPixmap &, int w1, int h1, int w2, int h2)
Create a TileSet from a pixmap.
void render(const QRect &, QPainter *, Tiles=Ring) const
Fills the specified rect with tiled chunks.
SmoothPixmapTransform
void drawPixmap(const QPoint &point, const QPixmap &pixmap)
void setRenderHint(RenderHint hint, bool on)
bool testRenderHint(RenderHint hint) const const
QPixmap copy(const QRect &rectangle) const const
qreal devicePixelRatio() const const
void fill(const QColor &color)
int height() const const
bool isNull() const const
void setDevicePixelRatio(qreal scaleFactor)
int width() const const
void getRect(int *x, int *y, int *width, int *height) const const
bool isValid() const const
QSize size() const const
QPoint topLeft() const const
bool isValid() const const
transparent
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:56:16 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.