box2dfixture.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #ifndef BOX2DFIXTURE_H
  21. #define BOX2DFIXTURE_H
  22. #include <QDeclarativeItem>
  23. #include <QFlags>
  24. #include <Box2D.h>
  25. #include "box2dfixture.h"
  26. class b2Body;
  27. class b2Fixture;
  28. class b2FixtureDef;
  29. class b2Shape;
  30. class Box2DFixture : public QDeclarativeItem
  31. {
  32. Q_OBJECT
  33. Q_PROPERTY(float density READ density WRITE setDensity NOTIFY densityChanged)
  34. Q_PROPERTY(float friction READ friction WRITE setFriction NOTIFY frictionChanged)
  35. Q_PROPERTY(float restitution READ restitution WRITE setRestitution NOTIFY restitutionChanged)
  36. Q_PROPERTY(bool sensor READ isSensor WRITE setSensor NOTIFY sensorChanged)
  37. Q_PROPERTY(CategoryFlags categories READ categories WRITE setCategories NOTIFY categoriesChanged)
  38. Q_PROPERTY(CategoryFlags collidesWith READ collidesWith WRITE setCollidesWith NOTIFY collidesWithChanged)
  39. Q_PROPERTY(int groupIndex READ groupIndex WRITE setGroupIndex NOTIFY groupIndexChanged)
  40. Q_ENUMS(CategoryFlag)
  41. Q_FLAGS(CategoryFlags)
  42. public:
  43. explicit Box2DFixture(QDeclarativeItem *parent = 0);
  44. enum CategoryFlag {Category1 = 0x0001, Category2 = 0x0002, Category3 = 0x0004, Category4 = 0x0008,
  45. Category5 = 0x0010, Category6 = 0x0020, Category7 = 0x0040, Category8 = 0x0080,
  46. Category9 = 0x0100, Category10 = 0x0200, Category11 = 0x0400, Category12 = 0x0800,
  47. Category13 = 0x1000, Category14 = 0x2000, Category15 = 0x4000, Category16 = 0x8000,
  48. All = 0xFFFF, None=0x0000};
  49. Q_DECLARE_FLAGS(CategoryFlags, CategoryFlag)
  50. float density() const;
  51. void setDensity(float density);
  52. float friction() const;
  53. void setFriction(float friction);
  54. float restitution() const;
  55. void setRestitution(float restitution);
  56. bool isSensor() const;
  57. void setSensor(bool sensor);
  58. CategoryFlags categories() const;
  59. void setCategories(CategoryFlags layers);
  60. CategoryFlags collidesWith() const;
  61. void setCollidesWith(CategoryFlags layers);
  62. int groupIndex() const;
  63. void setGroupIndex(int groupIndex);
  64. void createFixture(b2Body *body);
  65. protected:
  66. b2Shape *getShape()
  67. {
  68. if (mFixture)
  69. return (mFixture->GetShape());
  70. return NULL;
  71. }
  72. virtual b2Shape *createShape() = 0;
  73. signals:
  74. void densityChanged();
  75. void frictionChanged();
  76. void restitutionChanged();
  77. void sensorChanged();
  78. void categoriesChanged();
  79. void collidesWithChanged();
  80. void groupIndexChanged();
  81. void beginContact(Box2DFixture *other);
  82. void contactChanged(Box2DFixture *other);
  83. void endContact(Box2DFixture *other);
  84. private:
  85. friend class Box2DWorld;
  86. void emitBeginContact(Box2DFixture *other);
  87. void emitContactChanged(Box2DFixture *other);
  88. void emitEndContact(Box2DFixture *other);
  89. b2FixtureDef mFixtureDef;
  90. b2Fixture *mFixture;
  91. };
  92. Q_DECLARE_OPERATORS_FOR_FLAGS(Box2DFixture::CategoryFlags)
  93. class Box2DBox : public Box2DFixture
  94. {
  95. Q_OBJECT
  96. public:
  97. explicit Box2DBox(QDeclarativeItem *parent = 0) :
  98. Box2DFixture(parent)
  99. { }
  100. protected:
  101. b2Shape *createShape();
  102. };
  103. class Box2DCircle : public Box2DFixture
  104. {
  105. Q_OBJECT
  106. Q_PROPERTY(float radius READ radius WRITE setRadius NOTIFY radiusChanged)
  107. public:
  108. explicit Box2DCircle(QDeclarativeItem *parent = 0) :
  109. Box2DFixture(parent)
  110. { }
  111. float radius() const { return mRadius; }
  112. void setRadius(float radius);
  113. signals:
  114. void radiusChanged();
  115. protected:
  116. b2Shape *createShape();
  117. private:
  118. float mRadius;
  119. };
  120. class Box2DPolygon : public Box2DFixture
  121. {
  122. Q_OBJECT
  123. Q_PROPERTY(QVariantList vertices READ vertices WRITE setVertices NOTIFY verticesChanged)
  124. public:
  125. explicit Box2DPolygon(QDeclarativeItem *parent = 0) :
  126. Box2DFixture(parent)
  127. { }
  128. QVariantList vertices() const { return mVertices; }
  129. void setVertices(const QVariantList &vertices) {
  130. if (vertices == mVertices)
  131. return;
  132. mVertices = vertices;
  133. emit verticesChanged();
  134. }
  135. signals:
  136. void verticesChanged();
  137. protected:
  138. b2Shape *createShape();
  139. private:
  140. QVariantList mVertices;
  141. };
  142. #endif // BOX2DFIXTURE_H