1 /*
2  * box2dgearjoint.cpp
3  * Copyright (c) 2011 Joonas Erkinheimo <joonas.erkinheimo@nokia.com>
4  *
5  * This file is part of the Box2D QML plugin.
6  *
7  * This software is provided 'as-is', without any express or implied warranty.
8  * In no event will the authors be held liable for any damages arising from
9  * the use of this software.
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute it
13  * freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must not
16  *    claim that you wrote the original software. If you use this software in
17  *    a product, an acknowledgment in the product documentation would be
18  *    appreciated but is not required.
19  *
20  * 2. Altered source versions must be plainly marked as such, and must not be
21  *    misrepresented as being the original software.
22  *
23  * 3. This notice may not be removed or altered from any source distribution.
24  */
25 
26 #include "box2dgearjoint.h"
27 #include "box2dworld.h"
28 #include "box2dbody.h"
29 
Box2DGearJoint(QObject * parent)30 Box2DGearJoint::Box2DGearJoint(QObject *parent)
31     : Box2DJoint(GearJoint, parent)
32     , m_joint1(0)
33     , m_joint2(0)
34     , m_ratio(1.0f)
35 {
36 }
37 
setRatio(float ratio)38 void Box2DGearJoint::setRatio(float ratio)
39 {
40     if (!b2IsValid(ratio)) {
41         qWarning() << "GearJoint: Invalid ratio:" << ratio;
42         return;
43     }
44     if (m_ratio == ratio)
45         return;
46 
47     m_ratio = ratio;
48     if (gearJoint())
49         gearJoint()->SetRatio(ratio);
50     emit ratioChanged();
51 }
52 
validJoint(Box2DJoint * joint)53 static bool validJoint(Box2DJoint *joint)
54 {
55     if (!joint)
56         return true;
57 
58     const Box2DJoint::JointType type = joint->jointType();
59     return type == Box2DJoint::RevoluteJoint ||
60             type == Box2DJoint::PrismaticJoint;
61 }
62 
setJoint1(Box2DJoint * joint1)63 void Box2DGearJoint::setJoint1(Box2DJoint *joint1)
64 {
65     if (m_joint1 == joint1)
66         return;
67 
68     if (!validJoint(joint1)) {
69         qWarning() << "GearJoint.joint1: Invalid joint type";
70         joint1 = 0;
71     }
72 
73     m_joint1 = joint1;
74 
75     if (!joint1 || joint1->joint())
76         initialize();
77     else
78         connect(joint1, SIGNAL(created()), this, SLOT(joint1Created()));
79 
80     emit joint1Changed();
81 }
82 
setJoint2(Box2DJoint * joint2)83 void Box2DGearJoint::setJoint2(Box2DJoint *joint2)
84 {
85     if (m_joint2 == joint2)
86         return;
87 
88     if (!validJoint(joint2)) {
89         qWarning() << "GearJoint.joint2: Invalid joint type";
90         joint2 = 0;
91     }
92 
93     m_joint2 = joint2;
94 
95     if (!joint2 || joint2->joint())
96         initialize();
97     else
98         connect(joint2, SIGNAL(created()), this, SLOT(joint2Created()));
99 
100     emit joint2Changed();
101 }
102 
createJoint()103 b2Joint *Box2DGearJoint::createJoint()
104 {
105     if (!m_joint1 || !m_joint2)
106         return 0;
107     if (!m_joint1->joint() || !m_joint2->joint())
108         return 0;
109 
110     b2GearJointDef jointDef;
111     initializeJointDef(jointDef);
112 
113     jointDef.joint1 = m_joint1->joint();
114     jointDef.joint2 = m_joint2->joint();
115     jointDef.ratio = m_ratio;
116 
117     return world()->world().CreateJoint(&jointDef);
118 }
119 
joint1Created()120 void Box2DGearJoint::joint1Created()
121 {
122     disconnect(m_joint1, SIGNAL(created()), this, SLOT(joint1Created()));
123     initialize();
124 }
125 
joint2Created()126 void Box2DGearJoint::joint2Created()
127 {
128     disconnect(m_joint2, SIGNAL(created()), this, SLOT(joint2Created()));
129     initialize();
130 }
131