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
|
/*
* Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Header include
#include "TileItem.h"
// Sigmod includes
#include <sigmod/Game.h>
#include <sigmod/MapTile.h>
#include <sigmod/Sprite.h>
#include <sigmod/Tile.h>
// Qt includes
#include <QtGui/QPainter>
#include <QtGui/QStyle>
#include <QtGui/QStyleOptionGraphicsItem>
using namespace Sigmod;
using namespace Sigmodr::Widgets;
TileItem::TileItem(MapTile* tile, QGraphicsScene* parent) :
MapItem(false, parent),
m_tile(tile),
m_tileIndex(-1)
{
connect(m_tile, SIGNAL(changed()), this, SLOT(tileChanged()));
setZValue(m_tile->zIndex());
QGraphicsSimpleTextItem* item = new QGraphicsSimpleTextItem(QString::number(m_tile->id()), this);
QSizeF size = item->boundingRect().size() / 2;
item->setPos(-size.width(), -size.height());
m_label = new QGraphicsSimpleTextItem(this);
tileChanged();
}
QRectF TileItem::boundingRect() const
{
return QRect(m_tile->position(), m_pixmap.size());
}
void TileItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->drawPixmap(0, 0, m_pixmap);
}
void TileItem::setZIndex(const int zIndex)
{
m_tile->setZIndex(zIndex);
}
void TileItem::setSprite(const int spriteId)
{
if (spriteId == m_tileIndex)
return;
const Sprite* sprite = m_tile->game()->spriteById(spriteId);
m_pixmap.loadFromData(sprite->sprite());
m_tileIndex = spriteId;
}
void TileItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsItem::mousePressEvent(event);
update();
}
void TileItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsItem::mouseMoveEvent(event);
m_tile->setPosition(scenePos().toPoint());
}
void TileItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsItem::mouseReleaseEvent(event);
update();
}
void TileItem::tileChanged()
{
const Tile* tile = m_tile->game()->tileById(m_tile->tile());
if (tile)
setSprite(tile->preview());
setPos(m_tile->position());
setZValue(m_tile->zIndex());
resetLabel();
update();
}
void TileItem::resetLabel()
{
const Tile* tile = m_tile->game()->tileById(m_tile->tile());
if (tile)
{
m_label->setText(tile->name());
QSizeF size = m_label->boundingRect().size() / 2 - m_pixmap.size() / 2;
m_label->setPos(-size.width(), -size.height());
}
}
|