File: tileset.cpp

package info (click to toggle)
musescore 2.0.3%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 202,532 kB
  • ctags: 58,769
  • sloc: cpp: 257,595; xml: 172,226; ansic: 139,931; python: 6,565; sh: 6,383; perl: 423; makefile: 290; awk: 142; pascal: 67; sed: 3
file content (184 lines) | stat: -rw-r--r-- 6,597 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright 2009-2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
 * Copyright 2008 Long Huynh Huu <long.upcase@googlemail.com>
 * Copyright 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License version 2 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "tileset.h"

//---------------------------------------------------------
//   initPixmap
//---------------------------------------------------------

void TileSet::initPixmap( PixmapList& pixmaps, const QPixmap& pix, int w, int h, const QRect& rect) {
      QSize size( w, h );
      if (!size.isValid()) {
            pixmaps.push_back(QPixmap());
            }
      else if (size != rect.size()) {
            const QPixmap tile(pix.copy(rect));
            QPixmap pixmap(w, h);

            pixmap.fill(Qt::transparent);
            QPainter p(&pixmap);
            p.drawTiledPixmap(QRect(0, 0, w, h), tile);

            pixmaps.push_back( pixmap );
            }
      else
            pixmaps.push_back(pix.copy(rect));
      }

//---------------------------------------------------------
//   TileSet
//---------------------------------------------------------

TileSet::TileSet()
      : _w1(0), _h1(0), _w3(0), _h3(0) {
      _pixmaps.reserve(9);
      }

TileSet::TileSet(const QPixmap& pix, int w1, int h1, int w2, int h2)
      : _w1(w1), _h1(h1), _w3(0), _h3(0) {
      _pixmaps.reserve(9);
      if (pix.isNull())
            return;

      _w3 = pix.width() - (w1 + w2);
      _h3 = pix.height() - (h1 + h2);
      int w = w2;
      while (w < 32 && w2 > 0)
            w += w2;
      int h = h2;
      while (h < 32 && h2 > 0)
            h += h2;

      // initialise pixmap array
      initPixmap(_pixmaps, pix, _w1, _h1, QRect(0, 0, _w1, _h1));
      initPixmap(_pixmaps, pix, w, _h1, QRect(_w1, 0, w2, _h1));
      initPixmap(_pixmaps, pix, _w3, _h1, QRect(_w1 + w2, 0, _w3, _h1));
      initPixmap(_pixmaps, pix, _w1, h, QRect(0, _h1, _w1, h2));
      initPixmap(_pixmaps, pix, w, h, QRect(_w1, _h1, w2, h2));
      initPixmap(_pixmaps, pix, _w3, h, QRect(_w1 + w2, _h1, _w3, h2));
      initPixmap(_pixmaps, pix, _w1, _h3, QRect(0, _h1 + h2, _w1, _h3));
      initPixmap(_pixmaps, pix, w, _h3, QRect(_w1, _h1 + h2, w2, _h3));
      initPixmap(_pixmaps, pix, _w3, _h3, QRect(_w1 + w2, _h1 + h2, _w3, _h3));
      }

TileSet::TileSet(const QPixmap& pix, int w1, int h1, int w3, int h3, int x1, int y1, int w2, int h2)
      : _w1(w1), _h1(h1), _w3(w3), _h3(h3) {
      _pixmaps.reserve(9);
      if (pix.isNull())
            return;

      int x2 = pix.width() - _w3;
      int y2 = pix.height() - _h3;
      int w = w2;
      while (w < 32 && w2 > 0) w += w2;
      int h = h2;
      while (h < 32 && h2 > 0) h += h2;

      // initialise pixmap array
      initPixmap(_pixmaps, pix, _w1, _h1, QRect(0, 0, _w1, _h1));
      initPixmap(_pixmaps, pix, w, _h1, QRect(x1, 0, w2, _h1));
      initPixmap(_pixmaps, pix, _w3, _h1, QRect(x2, 0, _w3, _h1));
      initPixmap(_pixmaps, pix, _w1, h, QRect(0, y1, _w1, h2));
      initPixmap(_pixmaps, pix, w, h, QRect(x1, y1, w2, h2));
      initPixmap(_pixmaps, pix, _w3, h, QRect(x2, y1, _w3, h2));
      initPixmap(_pixmaps, pix, _w1, _h3, QRect(0, y2, _w1, _h3));
      initPixmap(_pixmaps, pix, w, _h3, QRect(x1, y2, w2, _h3));
      initPixmap(_pixmaps, pix, _w3, _h3, QRect(x2, y2, _w3, _h3));
      }

//---------------------------------------------------------
//   bits
//---------------------------------------------------------

inline bool bits(TileSet::Tiles flags, TileSet::Tiles testFlags) {
      return (flags & testFlags) == testFlags;
      }

//---------------------------------------------------------
//   render
//---------------------------------------------------------

void TileSet::render(const QRect& r, QPainter* p, Tiles t) const
      {
      // check initialization
      if (_pixmaps.size() < 9)
            return;

      int x0, y0, w, h;
      r.getRect(&x0, &y0, &w, &h);

      // calculate pixmaps widths
      int wLeft(0);
      int wRight(0);
      if (_w1 + _w3 > 0) {
            qreal wRatio(qreal(_w1) / qreal(_w1 + _w3));
            wLeft = (t & Right) ? qMin(_w1, int(w * wRatio)) : _w1;
            wRight = (t & Left) ? qMin(_w3, int(w * (1.0 - wRatio))) : _w3;
            }

      // calculate pixmap heights
      int hTop(0);
      int hBottom(0);
      if (_h1 + _h3 > 0) {
            qreal hRatio( qreal( _h1 ) / qreal( _h1 + _h3 ) );
            hTop = (t & Bottom) ? qMin( _h1, int(h * hRatio) ) : _h1;
            hBottom = (t & Top) ? qMin( _h3, int(h * (1.0 - hRatio)) ) : _h3;
            }

      // calculate corner locations
      w -= wLeft + wRight;
      h -= hTop + hBottom;
      int x1 = x0 + wLeft;
      int x2 = x1 + w;
      int y1 = y0 + hTop;
      int y2 = y1 + h;

      // corner
      if (bits(t, Top | Left))
            p->drawPixmap(x0, y0, _pixmaps.at(0), 0, 0, wLeft, hTop);
      if (bits(t, Top | Right))
            p->drawPixmap(x2, y0, _pixmaps.at(2), _w3 - wRight, 0, wRight, hTop);
      if (bits(t, Bottom | Left))
            p->drawPixmap(x0, y2, _pixmaps.at(6), 0, _h3 - hBottom, wLeft,  hBottom);
      if (bits(t, Bottom | Right))
            p->drawPixmap(x2, y2, _pixmaps.at(8), _w3 - wRight, _h3 - hBottom, wRight, hBottom);

      // top and bottom
      if (w > 0) {
            if (t & Top)
                  p->drawTiledPixmap(x1, y0, w, hTop, _pixmaps.at(1));
            if (t & Bottom)
                  p->drawTiledPixmap(x1, y2, w, hBottom, _pixmaps.at(7), 0, _h3 - hBottom);
            }

      // left and right
      if (h > 0) {
            if (t & Left)
                  p->drawTiledPixmap(x0, y1, wLeft, h, _pixmaps.at(3));
            if (t & Right)
                  p->drawTiledPixmap(x2, y1, wRight, h, _pixmaps.at(5), _w3 - wRight, 0);
            }

      // center
      if ((t & Center) && h > 0 && w > 0)
            p->drawTiledPixmap(x1, y1, w, h, _pixmaps.at(4));
      }