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

0001 /*
0002  * box2dmousejoint.cpp
0003  * Copyright (c) 2011 Joonas Erkinheimo <joonas.erkinheimo@nokia.com>
0004  *
0005  * This file is part of the Box2D QML plugin.
0006  *
0007  * This software is provided 'as-is', without any express or implied warranty.
0008  * In no event will the authors be held liable for any damages arising from
0009  * the use of this software.
0010  *
0011  * Permission is granted to anyone to use this software for any purpose,
0012  * including commercial applications, and to alter it and redistribute it
0013  * freely, subject to the following restrictions:
0014  *
0015  * 1. The origin of this software must not be misrepresented; you must not
0016  *    claim that you wrote the original software. If you use this software in
0017  *    a product, an acknowledgment in the product documentation would be
0018  *    appreciated but is not required.
0019  *
0020  * 2. Altered source versions must be plainly marked as such, and must not be
0021  *    misrepresented as being the original software.
0022  *
0023  * 3. This notice may not be removed or altered from any source distribution.
0024  */
0025 
0026 #include "box2dgearjoint.h"
0027 #include "box2dworld.h"
0028 #include "box2dbody.h"
0029 
0030 Box2DGearJoint::Box2DGearJoint(QObject *parent)
0031     : Box2DJoint(GearJoint, parent)
0032     , m_joint1(0)
0033     , m_joint2(0)
0034     , m_ratio(1.0f)
0035 {
0036 }
0037 
0038 void Box2DGearJoint::setRatio(float ratio)
0039 {
0040     if (!b2IsValid(ratio)) {
0041         qWarning() << "GearJoint: Invalid ratio:" << ratio;
0042         return;
0043     }
0044     if (m_ratio == ratio)
0045         return;
0046 
0047     m_ratio = ratio;
0048     if (gearJoint())
0049         gearJoint()->SetRatio(ratio);
0050     emit ratioChanged();
0051 }
0052 
0053 static bool validJoint(Box2DJoint *joint)
0054 {
0055     if (!joint)
0056         return true;
0057 
0058     const Box2DJoint::JointType type = joint->jointType();
0059     return type == Box2DJoint::RevoluteJoint ||
0060             type == Box2DJoint::PrismaticJoint;
0061 }
0062 
0063 void Box2DGearJoint::setJoint1(Box2DJoint *joint1)
0064 {
0065     if (m_joint1 == joint1)
0066         return;
0067 
0068     if (!validJoint(joint1)) {
0069         qWarning() << "GearJoint.joint1: Invalid joint type";
0070         joint1 = 0;
0071     }
0072 
0073     m_joint1 = joint1;
0074 
0075     if (!joint1 || joint1->joint())
0076         initialize();
0077     else
0078         connect(joint1, SIGNAL(created()), this, SLOT(joint1Created()));
0079 
0080     emit joint1Changed();
0081 }
0082 
0083 void Box2DGearJoint::setJoint2(Box2DJoint *joint2)
0084 {
0085     if (m_joint2 == joint2)
0086         return;
0087 
0088     if (!validJoint(joint2)) {
0089         qWarning() << "GearJoint.joint2: Invalid joint type";
0090         joint2 = 0;
0091     }
0092 
0093     m_joint2 = joint2;
0094 
0095     if (!joint2 || joint2->joint())
0096         initialize();
0097     else
0098         connect(joint2, SIGNAL(created()), this, SLOT(joint2Created()));
0099 
0100     emit joint2Changed();
0101 }
0102 
0103 b2Joint *Box2DGearJoint::createJoint()
0104 {
0105     if (!m_joint1 || !m_joint2)
0106         return 0;
0107     if (!m_joint1->joint() || !m_joint2->joint())
0108         return 0;
0109 
0110     b2GearJointDef jointDef;
0111     initializeJointDef(jointDef);
0112 
0113     jointDef.joint1 = m_joint1->joint();
0114     jointDef.joint2 = m_joint2->joint();
0115     jointDef.ratio = m_ratio;
0116 
0117     return world()->world().CreateJoint(&jointDef);
0118 }
0119 
0120 void Box2DGearJoint::joint1Created()
0121 {
0122     disconnect(m_joint1, SIGNAL(created()), this, SLOT(joint1Created()));
0123     initialize();
0124 }
0125 
0126 void Box2DGearJoint::joint2Created()
0127 {
0128     disconnect(m_joint2, SIGNAL(created()), this, SLOT(joint2Created()));
0129     initialize();
0130 }