Skip to content

Commit 19d3d1b

Browse files
committed
[ios] Closes #2666, expose SkeletonBounds via spine-cpp-lite/Swift
1 parent 9207cd2 commit 19d3d1b

File tree

4 files changed

+255
-3
lines changed

4 files changed

+255
-3
lines changed

spine-cpp/spine-cpp-lite/spine-cpp-lite.cpp

+109
Original file line numberDiff line numberDiff line change
@@ -4681,3 +4681,112 @@ void spine_texture_region_set_original_height(spine_texture_region textureRegion
46814681
TextureRegion *_region = (TextureRegion *) textureRegion;
46824682
_region->originalHeight = originalHeight;
46834683
}
4684+
4685+
spine_skeleton_bounds spine_skeleton_bounds_create() {
4686+
return (spine_skeleton_bounds) new (__FILE__, __LINE__) SkeletonBounds();
4687+
}
4688+
4689+
void spine_skeleton_bounds_dispose(spine_skeleton_bounds bounds) {
4690+
if (bounds == nullptr) return;
4691+
SkeletonBounds *_bounds = (SkeletonBounds *) bounds;
4692+
delete _bounds;
4693+
}
4694+
4695+
void spine_skeleton_bounds_update(spine_skeleton_bounds bounds, spine_skeleton skeleton, spine_bool updateAabb) {
4696+
if (bounds == nullptr) return;
4697+
if (skeleton == nullptr) return;
4698+
4699+
SkeletonBounds *_bounds = (SkeletonBounds *) bounds;
4700+
Skeleton *_skeleton = (Skeleton *) skeleton;
4701+
_bounds->update(*_skeleton, updateAabb != 0);
4702+
}
4703+
4704+
spine_bool spine_skeleton_bounds_aabb_contains_point(spine_skeleton_bounds bounds, float x, float y) {
4705+
if (bounds == nullptr) return false;
4706+
return ((SkeletonBounds *) bounds)->aabbcontainsPoint(x, y);
4707+
}
4708+
4709+
spine_bool spine_skeleton_bounds_aabb_intersects_segment(spine_skeleton_bounds bounds, float x1, float y1, float x2, float y2) {
4710+
if (bounds == nullptr) return false;
4711+
return ((SkeletonBounds *) bounds)->aabbintersectsSegment(x1, y1, x2, y2);
4712+
}
4713+
4714+
spine_bool spine_skeleton_bounds_aabb_intersects_skeleton(spine_skeleton_bounds bounds, spine_skeleton_bounds otherBounds) {
4715+
if (bounds == nullptr) return false;
4716+
if (otherBounds == nullptr) return false;
4717+
return ((SkeletonBounds *) bounds)->aabbIntersectsSkeleton(*((SkeletonBounds *) bounds));
4718+
}
4719+
4720+
spine_bool spine_skeleton_bounds_contains_point(spine_skeleton_bounds bounds, spine_polygon polygon, float x, float y) {
4721+
if (bounds == nullptr) return false;
4722+
if (polygon == nullptr) return false;
4723+
return ((SkeletonBounds *) bounds)->containsPoint((Polygon *) polygon, x, y);
4724+
}
4725+
4726+
spine_bounding_box_attachment spine_skeleton_bounds_contains_point_attachment(spine_skeleton_bounds bounds, float x, float y) {
4727+
if (bounds == nullptr) return nullptr;
4728+
return (spine_bounding_box_attachment) ((SkeletonBounds *) bounds)->containsPoint(x, y);
4729+
}
4730+
4731+
spine_bounding_box_attachment spine_skeleton_bounds_intersects_segment_attachment(spine_skeleton_bounds bounds, float x1, float y1, float x2, float y2) {
4732+
if (bounds == nullptr) return nullptr;
4733+
return (spine_bounding_box_attachment) ((SkeletonBounds *) bounds)->intersectsSegment(x1, y1, x2, y2);
4734+
}
4735+
4736+
spine_bool spine_skeleton_bounds_intersects_segment(spine_skeleton_bounds bounds, spine_polygon polygon, float x1, float y1, float x2, float y2) {
4737+
if (bounds == nullptr) return false;
4738+
if (polygon == nullptr) return false;
4739+
return ((SkeletonBounds *) bounds)->intersectsSegment((Polygon *) polygon, x1, y1, x2, y2);
4740+
}
4741+
4742+
spine_polygon spine_skeleton_bounds_get_polygon(spine_skeleton_bounds bounds, spine_bounding_box_attachment attachment) {
4743+
if (bounds == nullptr) return nullptr;
4744+
if (attachment == nullptr) return nullptr;
4745+
return (spine_polygon) ((SkeletonBounds *) bounds)->getPolygon((BoundingBoxAttachment *) attachment);
4746+
}
4747+
4748+
spine_bounding_box_attachment spine_skeleton_bounds_get_bounding_box(spine_skeleton_bounds bounds, spine_polygon polygon) {
4749+
if (bounds == nullptr) return nullptr;
4750+
if (polygon == nullptr) return nullptr;
4751+
return (spine_bounding_box_attachment) ((SkeletonBounds *) bounds)->getBoundingBox((Polygon *) polygon);
4752+
}
4753+
4754+
int32_t spine_skeleton_bounds_get_num_polygons(spine_skeleton_bounds bounds) {
4755+
if (bounds == nullptr) return 0;
4756+
return (int32_t) ((SkeletonBounds *) bounds)->getPolygons().size();
4757+
}
4758+
4759+
spine_polygon *spine_skeleton_bounds_get_polygons(spine_skeleton_bounds bounds) {
4760+
if (bounds == nullptr) return nullptr;
4761+
return (spine_polygon *) ((SkeletonBounds *) bounds)->getPolygons().buffer();
4762+
}
4763+
4764+
int32_t spine_skeleton_bounds_get_num_bounding_boxes(spine_skeleton_bounds bounds) {
4765+
if (bounds == nullptr) return 0;
4766+
return (int32_t) ((SkeletonBounds *) bounds)->getBoundingBoxes().size();
4767+
}
4768+
4769+
spine_bounding_box_attachment *spine_skeleton_bounds_get_bounding_boxes(spine_skeleton_bounds bounds) {
4770+
if (bounds == nullptr) return nullptr;
4771+
return (spine_bounding_box_attachment *) ((SkeletonBounds *) bounds)->getBoundingBoxes().buffer();
4772+
}
4773+
4774+
float spine_skeleton_bounds_get_width(spine_skeleton_bounds bounds) {
4775+
if (bounds == nullptr) return 0;
4776+
return ((SkeletonBounds *) bounds)->getWidth();
4777+
}
4778+
4779+
float spine_skeleton_bounds_get_height(spine_skeleton_bounds bounds) {
4780+
if (bounds == nullptr) return 0;
4781+
return ((SkeletonBounds *) bounds)->getHeight();
4782+
}
4783+
4784+
int32_t spine_polygon_get_num_vertices(spine_polygon polygon) {
4785+
if (polygon == nullptr) return 0;
4786+
return ((Polygon *) polygon)->_vertices.size();
4787+
}
4788+
4789+
float *spine_polygon_get_vertices(spine_polygon polygon) {
4790+
if (polygon == nullptr) return 0;
4791+
return ((Polygon *) polygon)->_vertices.buffer();
4792+
}

spine-cpp/spine-cpp-lite/spine-cpp-lite.h

+25-1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ SPINE_OPAQUE_TYPE(spine_vector)
104104
SPINE_OPAQUE_TYPE(spine_skeleton_drawable)
105105
SPINE_OPAQUE_TYPE(spine_skin_entry)
106106
SPINE_OPAQUE_TYPE(spine_skin_entries)
107+
SPINE_OPAQUE_TYPE(spine_skeleton_bounds)
108+
SPINE_OPAQUE_TYPE(spine_polygon)
107109

108110
// @end: opaque_types
109111

@@ -263,7 +265,7 @@ SPINE_CPP_LITE_EXPORT void spine_skeleton_data_set_width(spine_skeleton_data dat
263265
SPINE_CPP_LITE_EXPORT float spine_skeleton_data_get_height(spine_skeleton_data data);
264266
SPINE_CPP_LITE_EXPORT void spine_skeleton_data_set_height(spine_skeleton_data data, float height);
265267
SPINE_CPP_LITE_EXPORT const utf8 *spine_skeleton_data_get_version(spine_skeleton_data data);
266-
// OMITTED setVersion()
268+
// OMITTED setVersion()
267269
// @ignore
268270
SPINE_CPP_LITE_EXPORT const utf8 *spine_skeleton_data_get_hash(spine_skeleton_data data);
269271
// OMITTED setHash()
@@ -1047,6 +1049,28 @@ SPINE_CPP_LITE_EXPORT void spine_texture_region_set_original_width(spine_texture
10471049
SPINE_CPP_LITE_EXPORT int32_t spine_texture_region_get_original_height(spine_texture_region textureRegion);
10481050
SPINE_CPP_LITE_EXPORT void spine_texture_region_set_original_height(spine_texture_region textureRegion, int32_t originalHeight);
10491051

1052+
SPINE_CPP_LITE_EXPORT spine_skeleton_bounds spine_skeleton_bounds_create();
1053+
SPINE_CPP_LITE_EXPORT void spine_skeleton_bounds_dispose(spine_skeleton_bounds bounds);
1054+
SPINE_CPP_LITE_EXPORT void spine_skeleton_bounds_update(spine_skeleton_bounds bounds, spine_skeleton skeleton, spine_bool updateAabb);
1055+
SPINE_CPP_LITE_EXPORT spine_bool spine_skeleton_bounds_aabb_contains_point(spine_skeleton_bounds bounds, float x, float y);
1056+
SPINE_CPP_LITE_EXPORT spine_bool spine_skeleton_bounds_aabb_intersects_segment(spine_skeleton_bounds bounds, float x1, float y1, float x2, float y2);
1057+
SPINE_CPP_LITE_EXPORT spine_bool spine_skeleton_bounds_aabb_intersects_skeleton(spine_skeleton_bounds bounds, spine_skeleton_bounds otherBounds);
1058+
SPINE_CPP_LITE_EXPORT spine_bool spine_skeleton_bounds_contains_point(spine_skeleton_bounds bounds, spine_polygon polygon, float x, float y);
1059+
SPINE_CPP_LITE_EXPORT spine_bounding_box_attachment spine_skeleton_bounds_contains_point_attachment(spine_skeleton_bounds bounds, float x, float y);
1060+
SPINE_CPP_LITE_EXPORT spine_bounding_box_attachment spine_skeleton_bounds_intersects_segment_attachment(spine_skeleton_bounds bounds, float x1, float y1, float x2, float y2);
1061+
SPINE_CPP_LITE_EXPORT spine_bool spine_skeleton_bounds_intersects_segment(spine_skeleton_bounds bounds, spine_polygon polygon, float x1, float y1, float x2, float y2);
1062+
SPINE_CPP_LITE_EXPORT spine_polygon spine_skeleton_bounds_get_polygon(spine_skeleton_bounds bounds, spine_bounding_box_attachment attachment);
1063+
SPINE_CPP_LITE_EXPORT spine_bounding_box_attachment spine_skeleton_bounds_get_bounding_box(spine_skeleton_bounds bounds, spine_polygon polygon);
1064+
SPINE_CPP_LITE_EXPORT int32_t spine_skeleton_bounds_get_num_polygons(spine_skeleton_bounds bounds);
1065+
SPINE_CPP_LITE_EXPORT spine_polygon *spine_skeleton_bounds_get_polygons(spine_skeleton_bounds bounds);
1066+
SPINE_CPP_LITE_EXPORT int32_t spine_skeleton_bounds_get_num_bounding_boxes(spine_skeleton_bounds bounds);
1067+
SPINE_CPP_LITE_EXPORT spine_bounding_box_attachment *spine_skeleton_bounds_get_bounding_boxes(spine_skeleton_bounds bounds);
1068+
SPINE_CPP_LITE_EXPORT float spine_skeleton_bounds_get_width(spine_skeleton_bounds bounds);
1069+
SPINE_CPP_LITE_EXPORT float spine_skeleton_bounds_get_height(spine_skeleton_bounds bounds);
1070+
1071+
SPINE_CPP_LITE_EXPORT int32_t spine_polygon_get_num_vertices(spine_polygon polygon);
1072+
SPINE_CPP_LITE_EXPORT float *spine_polygon_get_vertices(spine_polygon polygon);
1073+
10501074
// @end: function_declarations
10511075

10521076
#endif

spine-glfw/example/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ int main() {
3939
// Load the atlas and the skeleton data
4040
GlTextureLoader textureLoader;
4141
Atlas *atlas = new Atlas("data/spineboy-pma.atlas", &textureLoader);
42-
SkeletonJson json(atlas);
43-
SkeletonData *skeletonData = json.readSkeletonDataFile("data/spineboy-pro.skel");
42+
SkeletonBinary binary(atlas);
43+
SkeletonData *skeletonData = binary.readSkeletonDataFile("data/spineboy-pro.skel");
4444

4545
// Create a skeleton from the data, set the skeleton's position to the bottom center of
4646
// the screen and scale it to make it smaller.

spine-ios/Sources/Spine/Spine.Generated.swift

+119
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,104 @@ public final class AnimationState: NSObject {
17211721

17221722
}
17231723

1724+
@objc(SpineSkeletonBounds)
1725+
@objcMembers
1726+
public final class SkeletonBounds: NSObject {
1727+
1728+
internal let wrappee: spine_skeleton_bounds
1729+
internal var disposed = false
1730+
1731+
internal init(_ wrappee: spine_skeleton_bounds) {
1732+
self.wrappee = wrappee
1733+
super.init()
1734+
}
1735+
1736+
public var polygons: [Polygon] {
1737+
let num = Int(spine_skeleton_bounds_get_num_polygons(wrappee))
1738+
let ptr = spine_skeleton_bounds_get_polygons(wrappee)
1739+
return (0..<num).compactMap {
1740+
ptr?[$0].flatMap { .init($0) }
1741+
}
1742+
}
1743+
1744+
public var boundingBoxes: [BoundingBoxAttachment] {
1745+
let num = Int(spine_skeleton_bounds_get_num_bounding_boxes(wrappee))
1746+
let ptr = spine_skeleton_bounds_get_bounding_boxes(wrappee)
1747+
return (0..<num).compactMap {
1748+
ptr?[$0].flatMap { .init($0) }
1749+
}
1750+
}
1751+
1752+
public var width: Float {
1753+
return spine_skeleton_bounds_get_width(wrappee)
1754+
}
1755+
1756+
public var height: Float {
1757+
return spine_skeleton_bounds_get_height(wrappee)
1758+
}
1759+
1760+
@discardableResult
1761+
public func create() -> SkeletonBounds {
1762+
return .init(spine_skeleton_bounds_create())
1763+
}
1764+
1765+
public func dispose() {
1766+
if disposed { return }
1767+
disposed = true
1768+
spine_skeleton_bounds_dispose(wrappee)
1769+
}
1770+
1771+
public func update(skeleton: Skeleton, updateAabb: Bool) {
1772+
spine_skeleton_bounds_update(wrappee, skeleton.wrappee, updateAabb ? -1 : 0)
1773+
}
1774+
1775+
@discardableResult
1776+
public func aabbContainsPoint(x: Float, y: Float) -> Bool {
1777+
return spine_skeleton_bounds_aabb_contains_point(wrappee, x, y) != 0
1778+
}
1779+
1780+
@discardableResult
1781+
public func aabbIntersectsSegment(x1: Float, y1: Float, x2: Float, y2: Float) -> Bool {
1782+
return spine_skeleton_bounds_aabb_intersects_segment(wrappee, x1, y1, x2, y2) != 0
1783+
}
1784+
1785+
@discardableResult
1786+
public func aabbIntersectsSkeleton(otherBounds: SkeletonBounds) -> Bool {
1787+
return spine_skeleton_bounds_aabb_intersects_skeleton(wrappee, otherBounds.wrappee) != 0
1788+
}
1789+
1790+
@discardableResult
1791+
public func containsPoint(polygon: Polygon, x: Float, y: Float) -> Bool {
1792+
return spine_skeleton_bounds_contains_point(wrappee, polygon.wrappee, x, y) != 0
1793+
}
1794+
1795+
@discardableResult
1796+
public func containsPointAttachment(x: Float, y: Float) -> BoundingBoxAttachment {
1797+
return .init(spine_skeleton_bounds_contains_point_attachment(wrappee, x, y))
1798+
}
1799+
1800+
@discardableResult
1801+
public func intersectsSegmentAttachment(x1: Float, y1: Float, x2: Float, y2: Float) -> BoundingBoxAttachment {
1802+
return .init(spine_skeleton_bounds_intersects_segment_attachment(wrappee, x1, y1, x2, y2))
1803+
}
1804+
1805+
@discardableResult
1806+
public func intersectsSegment(polygon: Polygon, x1: Float, y1: Float, x2: Float, y2: Float) -> Bool {
1807+
return spine_skeleton_bounds_intersects_segment(wrappee, polygon.wrappee, x1, y1, x2, y2) != 0
1808+
}
1809+
1810+
@discardableResult
1811+
public func getPolygon(attachment: BoundingBoxAttachment) -> Polygon {
1812+
return .init(spine_skeleton_bounds_get_polygon(wrappee, attachment.wrappee))
1813+
}
1814+
1815+
@discardableResult
1816+
public func getBoundingBox(polygon: Polygon) -> BoundingBoxAttachment {
1817+
return .init(spine_skeleton_bounds_get_bounding_box(wrappee, polygon.wrappee))
1818+
}
1819+
1820+
}
1821+
17241822
@objc(SpineTextureRegion)
17251823
@objcMembers
17261824
public final class TextureRegion: NSObject {
@@ -3090,6 +3188,27 @@ public final class Sequence: NSObject {
30903188

30913189
}
30923190

3191+
@objc(SpinePolygon)
3192+
@objcMembers
3193+
public final class Polygon: NSObject {
3194+
3195+
internal let wrappee: spine_polygon
3196+
3197+
internal init(_ wrappee: spine_polygon) {
3198+
self.wrappee = wrappee
3199+
super.init()
3200+
}
3201+
3202+
public var vertices: [Float?] {
3203+
let num = Int(spine_polygon_get_num_vertices(wrappee))
3204+
let ptr = spine_polygon_get_vertices(wrappee)
3205+
return (0..<num).compactMap {
3206+
ptr?[$0]
3207+
}
3208+
}
3209+
3210+
}
3211+
30933212
@objc(SpineBounds)
30943213
@objcMembers
30953214
public final class Bounds: NSObject {

0 commit comments

Comments
 (0)