File indexing completed on 2024-04-21 14:43:25

0001 /*
0002  * box2djoint.h
0003  * Copyright (c) 2011 Joonas Erkinheimo <joonas.erkinheimo@nokia.com>
0004  * Copyright (c) 2011 Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
0005  *
0006  * This file is part of the Box2D QML plugin.
0007  *
0008  * This software is provided 'as-is', without any express or implied warranty.
0009  * In no event will the authors be held liable for any damages arising from
0010  * the use of this software.
0011  *
0012  * Permission is granted to anyone to use this software for any purpose,
0013  * including commercial applications, and to alter it and redistribute it
0014  * freely, subject to the following restrictions:
0015  *
0016  * 1. The origin of this software must not be misrepresented; you must not
0017  *    claim that you wrote the original software. If you use this software in
0018  *    a product, an acknowledgment in the product documentation would be
0019  *    appreciated but is not required.
0020  *
0021  * 2. Altered source versions must be plainly marked as such, and must not be
0022  *    misrepresented as being the original software.
0023  *
0024  * 3. This notice may not be removed or altered from any source distribution.
0025  */
0026 
0027 #ifndef BOX2DJOINT_H
0028 #define BOX2DJOINT_H
0029 
0030 #include <QObject>
0031 #include <QPointF>
0032 #include <QQmlParserStatus>
0033 
0034 #include <Box2D.h>
0035 
0036 class b2World;
0037 class Box2DBody;
0038 class Box2DWorld;
0039 
0040 class Box2DJoint : public QObject, public QQmlParserStatus
0041 {
0042     Q_OBJECT
0043     Q_INTERFACES(QQmlParserStatus)
0044 
0045     Q_ENUMS(JointType)
0046     Q_PROPERTY(JointType jointType READ jointType CONSTANT)
0047     Q_PROPERTY(bool collideConnected READ collideConnected WRITE setCollideConnected NOTIFY collideConnectedChanged)
0048     Q_PROPERTY(Box2DBody *bodyA READ bodyA WRITE setBodyA NOTIFY bodyAChanged)
0049     Q_PROPERTY(Box2DBody *bodyB READ bodyB WRITE setBodyB NOTIFY bodyBChanged)
0050 
0051 public:
0052     enum JointType { // Matches b2JointType
0053         UnknownJoint,
0054         RevoluteJoint,
0055         PrismaticJoint,
0056         DistanceJoint,
0057         PulleyJoint,
0058         MouseJoint,
0059         GearJoint,
0060         WheelJoint,
0061         WeldJoint,
0062         FrictionJoint,
0063         RopeJoint,
0064         MotorJoint
0065     };
0066 
0067     Box2DJoint(JointType jointType, QObject *parent = 0);
0068     ~Box2DJoint();
0069 
0070     JointType jointType() const;
0071 
0072     bool collideConnected() const;
0073     void setCollideConnected(bool collideConnected);
0074 
0075     Box2DBody *bodyA() const;
0076     void setBodyA(Box2DBody *bodyA);
0077 
0078     Box2DBody *bodyB() const;
0079     void setBodyB(Box2DBody *bodyB);
0080 
0081     void initialize();
0082 
0083     void nullifyJoint();
0084     Box2DWorld *world() const;
0085     b2Joint *joint() const;
0086 
0087     // QQmlParserStatus interface
0088     void classBegin() {}
0089     void componentComplete();
0090 
0091 protected:
0092     virtual b2Joint *createJoint() = 0;
0093     void initializeJointDef(b2JointDef &def);
0094 
0095 private slots:
0096     void bodyACreated();
0097     void bodyBCreated();
0098 
0099 signals:
0100     void collideConnectedChanged();
0101     void bodyAChanged();
0102     void bodyBChanged();
0103     void created();
0104 
0105 private:
0106     JointType mJointType;
0107     bool mCollideConnected;
0108     bool mComponentComplete;
0109     bool mInitializePending;
0110     Box2DBody *mBodyA;
0111     Box2DBody *mBodyB;
0112     Box2DWorld *mWorld;
0113     b2Joint *mJoint;
0114 };
0115 
0116 inline Box2DJoint::JointType Box2DJoint::jointType() const
0117 {
0118     return mJointType;
0119 }
0120 
0121 inline bool Box2DJoint::collideConnected() const
0122 {
0123     return mCollideConnected;
0124 }
0125 
0126 inline Box2DBody *Box2DJoint::bodyA() const
0127 {
0128     return mBodyA;
0129 }
0130 
0131 inline Box2DBody *Box2DJoint::bodyB() const
0132 {
0133     return mBodyB;
0134 }
0135 
0136 inline void Box2DJoint::nullifyJoint()
0137 {
0138     mJoint = 0;
0139 }
0140 
0141 inline Box2DWorld *Box2DJoint::world() const
0142 {
0143     return mWorld;
0144 }
0145 
0146 inline b2Joint *Box2DJoint::joint() const
0147 {
0148     return mJoint;
0149 }
0150 
0151 
0152 /**
0153  * Convenience function to get the Box2DJoint wrapping a b2Joint.
0154  */
0155 inline Box2DJoint *toBox2DJoint(b2Joint *joint)
0156 {
0157     return static_cast<Box2DJoint*>(joint->GetUserData());
0158 }
0159 
0160 
0161 #endif // BOX2DJOINT_H