box2djoint.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Box2D QML plugin
  3. * Copyright (C) 2010 Nokia Corporation
  4. *
  5. * This file is part of the Box2D QML plugin.
  6. *
  7. * This library is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  15. * License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "box2djoint.h"
  21. #include "box2dbody.h"
  22. #include <QDebug>
  23. Box2DJoint::Box2DJoint(QDeclarativeItem *parent) :
  24. QDeclarativeItem(parent),
  25. mInitializePending(false),
  26. mWorld(0),
  27. mCollideConnected(false),
  28. mBodyA(0),
  29. mBodyB(0),
  30. mReleased(false)
  31. {
  32. }
  33. bool Box2DJoint::collideConnected() const
  34. {
  35. return mCollideConnected;
  36. }
  37. void Box2DJoint::setCollideConnected(bool collideConnected)
  38. {
  39. if (mCollideConnected == collideConnected)
  40. return;
  41. mCollideConnected = collideConnected;
  42. emit collideConnectedChanged();
  43. }
  44. Box2DBody* Box2DJoint::bodyA() const
  45. {
  46. return mBodyA;
  47. }
  48. void Box2DJoint::setBodyA(Box2DBody* bodyA)
  49. {
  50. //if (mBodyA != NULL)
  51. // return;
  52. if (bodyA->body() != NULL) {
  53. mBodyA = bodyA;
  54. emit bodyAChanged();
  55. initialize(mWorld);
  56. }
  57. else
  58. connect(bodyA, SIGNAL(bodyCreated()), this, SLOT(bodyACreated()));
  59. }
  60. Box2DBody* Box2DJoint::bodyB() const
  61. {
  62. return mBodyB;
  63. }
  64. void Box2DJoint::setBodyB(Box2DBody* bodyB)
  65. {
  66. if (mBodyB != NULL)
  67. return;
  68. if (bodyB->body() != NULL) {
  69. mBodyB = bodyB;
  70. emit bodyBChanged();
  71. initialize(mWorld);
  72. }
  73. else
  74. connect(bodyB, SIGNAL(bodyCreated()), this, SLOT(bodyBCreated()));
  75. }
  76. void Box2DJoint::initialize(b2World *world)
  77. {
  78. if (world != NULL)
  79. mWorld = world;
  80. if (!isComponentComplete() || mBodyA == NULL || mBodyB == NULL || mWorld == NULL) {
  81. // When components are created dynamically, they get their parent
  82. // assigned before they have been completely initialized. In that case
  83. // we need to delay initialization.
  84. mInitializePending = true;
  85. return;
  86. }
  87. createJoint();
  88. }
  89. void Box2DJoint::componentComplete()
  90. {
  91. QDeclarativeItem::componentComplete();
  92. if (mInitializePending)
  93. initialize(mWorld);
  94. }
  95. b2World* Box2DJoint::world() const
  96. {
  97. return mWorld;
  98. }
  99. void Box2DJoint::bodyACreated()
  100. {
  101. mBodyA = static_cast<Box2DBody*>(sender());
  102. emit bodyAChanged();
  103. initialize(mWorld);
  104. }
  105. void Box2DJoint::bodyBCreated()
  106. {
  107. mBodyB = static_cast<Box2DBody*>(sender());
  108. emit bodyBChanged();
  109. initialize(mWorld);
  110. }