2 Copyright 2007-2008 Fela Winkelmolen <fela.kde@gmail.com>
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <QSvgRenderer>
27 KGameCanvasWidget
*Item::canvas
= 0;
28 qreal
Item::m_scale
= 1.0;
29 int Item::m_borderLeft
= 0;
30 int Item::m_borderTop
= 0;
33 void Item::setCanvas(KGameCanvasWidget
*c
)
39 :KGameCanvasPixmap(canvas
), width(0), height(0)
42 kError() << "Item::Item(): a scene must be set "
43 "before calling the constructor!!" << endl
;
47 connect(canvas
, SIGNAL(spritesReloaded()), SLOT(loadSprite()));
52 void Item::updateScale()
54 qreal scaleX
= static_cast<double>(canvas
->width())
55 / (BRICK_WIDTH
* WIDTH
);
56 qreal scaleY
= static_cast<double>(canvas
->height())
57 / ((BRICK_HEIGHT
+ 2) * HEIGHT
);
58 m_scale
= qMin(scaleX
, scaleY
);
59 //kDebug() << "m_scale: " << m_scale;
60 // assure the m_scaled height of bricks is an int
61 // making the m_scale a little smaller if needed
62 m_scale
= static_cast<qreal
>(std::floor(m_scale
*BRICK_HEIGHT
))/BRICK_HEIGHT
;
63 if (m_scale
<= 0) m_scale
= 1.0 / BRICK_HEIGHT
;
64 //kDebug() << "m_scale: " << m_scale;
65 m_borderLeft
= qRound( (static_cast<qreal
>(canvas
->width())
66 - m_scale
* (BRICK_WIDTH
* WIDTH
)) / 2 );
67 m_borderTop
= qRound( (static_cast<qreal
>(canvas
->height())
68 - m_scale
* (BRICK_HEIGHT
* (HEIGHT
-2))) / 2 );
71 void Item::loadSprite()
75 //TODO: should not be needed, or at least I should have a closer look at it
76 if (qRound(m_scale
*width
) == 0 || qRound(m_scale
*height
) == 0) return;
78 QSize
size(qRound(m_scale
*width
), qRound(m_scale
*height
));
79 setPixmap(Renderer::self()->renderedSvgElement(elementId
, size
));
81 repaint(); //TODO: needed??????
86 // shouldn't already be called, really... but somehow without this the whole
87 // scene is repainted when an Item is deleted...
88 // (TODO: maybe because the destructor should be virtual?)
91 void Item::setType(const QString
&type
)
97 void Item::setRect(const QRectF
&/*newRect*/)
99 kDebug() << "setRect() does nothing, change it!\n";
100 /*QRectF oldRect = boundingRect();
101 m_scale((newRect.width()-2) / oldRect.width(),
102 (newRect.height()-2) / oldRect.height());
103 setPos(newRect.x() + 1, newRect.y() + 1);*/
106 QRect
Item::getRect() const
108 return QRect(static_cast<int>(m_position
.x()),
109 static_cast<int>(m_position
.y()),
113 void Item::moveTo(qreal x
, qreal y
)
115 m_position
= QPointF(x
, y
);
118 void Item::moveTo(const QPointF
&point
) {
119 moveTo(point
.x(), point
.y());
122 void Item::moveBy(qreal dx
, qreal dy
)
124 m_position
+= QPointF(dx
, dy
);
129 KGameCanvasPixmap::moveTo(
130 static_cast<int>(m_scale
* m_position
.x()) + m_borderLeft
,
131 static_cast<int>(m_scale
* m_position
.y()) + m_borderTop
);