SkeletonBounds.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated May 1, 2019. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2019, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
  19. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. * NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
  24. * INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifndef Spine_SkeletonBounds_h
  30. #define Spine_SkeletonBounds_h
  31. #include <spine/Vector.h>
  32. #include <spine/SpineObject.h>
  33. namespace spine {
  34. class Skeleton;
  35. class BoundingBoxAttachment;
  36. class Polygon;
  37. /// Collects each BoundingBoxAttachment that is visible and computes the world vertices for its polygon.
  38. /// The polygon vertices are provided along with convenience methods for doing hit detection.
  39. class SP_API SkeletonBounds : public SpineObject {
  40. public:
  41. SkeletonBounds();
  42. /// Clears any previous polygons, finds all visible bounding box attachments,
  43. /// and computes the world vertices for each bounding box's polygon.
  44. /// @param skeleton The skeleton.
  45. /// @param updateAabb
  46. /// If true, the axis aligned bounding box containing all the polygons is computed.
  47. /// If false, the SkeletonBounds AABB methods will always return true.
  48. ///
  49. void update(Skeleton& skeleton, bool updateAabb);
  50. /// Returns true if the axis aligned bounding box contains the point.
  51. bool aabbcontainsPoint(float x, float y);
  52. /// Returns true if the axis aligned bounding box intersects the line segment.
  53. bool aabbintersectsSegment(float x1, float y1, float x2, float y2);
  54. /// Returns true if the axis aligned bounding box intersects the axis aligned bounding box of the specified bounds.
  55. bool aabbIntersectsSkeleton(SkeletonBounds bounds);
  56. /// Returns true if the polygon contains the point.
  57. bool containsPoint(Polygon* polygon, float x, float y);
  58. /// Returns the first bounding box attachment that contains the point, or NULL. When doing many checks, it is usually more
  59. /// efficient to only call this method if {@link #aabbcontainsPoint(float, float)} returns true.
  60. BoundingBoxAttachment* containsPoint(float x, float y);
  61. /// Returns the first bounding box attachment that contains the line segment, or NULL. When doing many checks, it is usually
  62. /// more efficient to only call this method if {@link #aabbintersectsSegment(float, float, float, float)} returns true.
  63. BoundingBoxAttachment* intersectsSegment(float x1, float y1, float x2, float y2);
  64. /// Returns true if the polygon contains the line segment.
  65. bool intersectsSegment(Polygon* polygon, float x1, float y1, float x2, float y2);
  66. Polygon* getPolygon(BoundingBoxAttachment* attachment);
  67. float getWidth();
  68. float getHeight();
  69. private:
  70. Vector<Polygon*> _polygonPool;
  71. Vector<BoundingBoxAttachment*> _boundingBoxes;
  72. Vector<Polygon*> _polygons;
  73. float _minX, _minY, _maxX, _maxY;
  74. void aabbCompute();
  75. };
  76. class Polygon : public SpineObject {
  77. public:
  78. Vector<float> _vertices;
  79. int _count;
  80. Polygon() : _count(0) {
  81. _vertices.ensureCapacity(16);
  82. }
  83. };
  84. }
  85. #endif /* Spine_SkeletonBounds_h */