1 /*
2  * mapobjectitem.h
3  * Copyright 2008, Roderic Morris <roderic@ccs.neu.edu>
4  * Copyright 2008-2011, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
5  *
6  * This file is part of Tiled.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the Free
10  * Software Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #pragma once
23 
24 #include <QCoreApplication>
25 #include <QGraphicsItem>
26 
27 namespace Tiled {
28 
29 class MapObject;
30 class Tile;
31 
32 class Handle;
33 class MapDocument;
34 class ObjectGroupItem;
35 class PointHandle;
36 class ResizeHandle;
37 
38 /**
39  * A graphics item displaying a map object.
40  */
41 class MapObjectItem : public QGraphicsItem
42 {
43     Q_DECLARE_TR_FUNCTIONS(MapObjectItem)
44 
45 public:
46     /**
47      * Constructor.
48      *
49      * @param object the object to be displayed
50      * @param parent the item of the object group this object belongs to
51      */
52     MapObjectItem(MapObject *object, MapDocument *mapDocument,
53                   QGraphicsItem *parent = nullptr);
54 
55     enum { Type = UserType + 1 };
type()56     int type() const override { return Type; }
57 
58     MapObject *mapObject() const;
59 
60     /**
61      * Should be called when the map object this item refers to was changed.
62      */
63     void syncWithMapObject();
64 
65     bool isHoverIndicator() const;
66     void setIsHoverIndicator(bool isHoverIndicator);
67 
68     // QGraphicsItem
69     QRectF boundingRect() const override;
70     QPainterPath shape() const override;
71 
72     void paint(QPainter *painter,
73                const QStyleOptionGraphicsItem *option,
74                QWidget *widget = nullptr) override;
75 
76     /**
77      * Sets a new polygon on the associated object.
78      */
79     void setPolygon(const QPolygonF &polygon);
80 
81 private:
82     void expandBoundsToCoverTileCollisionObjects(QRectF &bounds);
83     QTransform tileCollisionObjectsTransform(const Tile &tile) const;
84 
mapDocument()85     MapDocument *mapDocument() const { return mMapDocument; }
color()86     QColor color() const { return mColor; }
87 
88     MapObject *mObject;
89     MapDocument *mMapDocument;
90 
91     /** Bounding rect cached, for adapting to geometry change correctly. */
92     QRectF mBoundingRect;
93     QPolygonF mPolygon; // Copy of the polygon, so we know when it changes
94     QColor mColor;      // Cached color of the object
95     bool mIsHoveredIndicator = false;
96 };
97 
98 
mapObject()99 inline MapObject *MapObjectItem::mapObject() const
100 {
101     return mObject;
102 }
103 
isHoverIndicator()104 inline bool MapObjectItem::isHoverIndicator() const
105 {
106     return mIsHoveredIndicator;
107 }
108 
109 } // namespace Tiled
110 
111 Q_DECLARE_METATYPE(Tiled::MapObjectItem*)
112