~neon/kolf/master

« back to all changes in this revision

Viewing changes to shape.h

  • Committer: Stefan Majewsky
  • Date: 2010-10-31 23:23:16 UTC
  • Revision ID: git-v1:45f6fdbcc5733b0daa25631ea13b5b2f2d86f20e
Create a Box2D world, and attach Box2D bodies to all CanvasItems.

This is unfortunately a very large patch, because it touches everything
in the CanvasItem hierarchy, the ItemFactory, the KolfGame and the
shapes at once. All these objects are closely related through Box2D's
object hierarchy (world -> bodies -> fixtures -> shapes), so these
changes can hardly be split up further.

svn path=/trunk/KDE/kdegames/kolf/; revision=1191711

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#define KOLF_SHAPE_H
21
21
 
22
22
#include <QPainterPath>
 
23
class b2Body;
 
24
class b2Fixture;
 
25
class b2FixtureDef;
 
26
class b2Shape;
 
27
 
23
28
class CanvasItem;
24
29
 
25
30
namespace Kolf
32
37
        class Shape
33
38
        {
34
39
                Q_DISABLE_COPY(Shape)
 
40
                private:
 
41
                        enum TraitBits
 
42
                        {
 
43
                                CollisionDetectionFlag = 1 << 0,
 
44
                                PhysicalSimulationFlag = 1 << 1
 
45
                        };
35
46
                public:
 
47
                        ///Defines how a shape behaves.
 
48
                        enum Trait
 
49
                        {
 
50
                                ///This shape is not represented in the physics engine in any way. It only provides an activation and interaction area for the editor interface.
 
51
                                VirtualShape = 0,
 
52
                                ///During each step of the physical simulation, this shape is checked for intersections with other shapes, and the registered Kolf::ContactCallback methods are called as appropriate.
 
53
                                ParticipatesInCollisionDetection = CollisionDetectionFlag,
 
54
                                ///The shape behaves like physical matter, i.e. it allows the body to move and interact with other bodies through collisions.
 
55
                                ParticipatesInPhysicalSimulation = CollisionDetectionFlag | PhysicalSimulationFlag
 
56
                        };
 
57
                        Q_DECLARE_FLAGS(Traits, Trait)
36
58
                        ///@warning Any subclass constructor *must* call update() before it exits.
37
59
                        Shape();
38
60
                        virtual ~Shape();
39
61
 
 
62
                        ///Returns how this shape behaves.
 
63
                        Kolf::Shape::Traits traits() const;
 
64
                        ///Configures the behavior of this shape.
 
65
                        void setTraits(Kolf::Shape::Traits traits);
 
66
 
40
67
                        ///Returns the two-dimensional activation outline, i.e. the area of this geometry (plus some defined padding). The editor overlay is supposed to activate the editor interface of the object associated with this geometry, if a mouse click occurs inside the activation outline.
41
68
                        ///@see ActivationOutlinePadding
42
69
                        QPainterPath activationOutline() const;
50
77
 
51
78
                        ///Call this method when the parameters of this geometry change (usually in setter methods of the subclass).
52
79
                        void update();
 
80
                        ///Reimplement this method to provide a new b2Shape instance based on the current configuration of the shape.
 
81
                        ///@warning Stuff will break if you return a null pointer.
 
82
                        virtual b2Shape* createShape() = 0;
53
83
                        ///Reimplement this method to create the outlines of this geometry and pass them to the caller via the arguments. You will not have to call this function in subclass implementations, it's invoked by Kolf::Geometry::update.
54
84
                        virtual void createOutlines(QPainterPath& activationOutline, QPainterPath& interactionOutline) = 0;
55
85
                        ///Use this padding as distance between the exact InteractionOutline and the fuzzy ActivationOutline.
56
86
                        static const qreal ActivationOutlinePadding;
57
87
                private:
 
88
                        ///A submethod of update().
 
89
                        void updateFixture(b2Shape* newShape);
 
90
                private:
 
91
                        Kolf::Shape::Traits m_traits;
58
92
                        CanvasItem* m_citem;
 
93
                        b2Body* m_body;
 
94
                        b2FixtureDef* m_fixtureDef;
 
95
                        b2Fixture* m_fixture;
 
96
                        b2Shape* m_shape;
59
97
                        QPainterPath m_activationOutline, m_interactionOutline;
60
98
        };
61
99
 
67
105
                        QRectF rect() const;
68
106
                        void setRect(const QRectF& rect);
69
107
                protected:
 
108
                        virtual b2Shape* createShape();
70
109
                        virtual void createOutlines(QPainterPath& activationOutline, QPainterPath& interactionOutline);
71
110
                private:
72
111
                        QRectF m_rect;
80
119
                        QRectF rect() const;
81
120
                        void setRect(const QRectF& rect);
82
121
                protected:
 
122
                        virtual b2Shape* createShape();
83
123
                        virtual void createOutlines(QPainterPath& activationOutline, QPainterPath& interactionOutline);
84
124
                private:
85
125
                        QRectF m_rect;
93
133
                        QLineF line() const;
94
134
                        void setLine(const QLineF& line);
95
135
                protected:
 
136
                        virtual b2Shape* createShape();
96
137
                        virtual void createOutlines(QPainterPath& activationOutline, QPainterPath& interactionOutline);
97
138
                private:
98
139
                        QLineF m_line;
99
140
        };
100
141
}
101
142
 
 
143
Q_DECLARE_OPERATORS_FOR_FLAGS(Kolf::Shape::Traits)
 
144
 
102
145
#endif // KOLF_SHAPE_H