From 916b7d01de28792513aeeb58e29d85de8bf8dfb4 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Fri, 27 Sep 2013 11:40:54 +0200 Subject: [PATCH] cocos2d ObjC style AnimationState event callback for CCSkeletonAnimation. --- spine-cocos2d-iphone/Resources/spineboy.json | 821 +++++++++++------- spine-cocos2d-iphone/example/ExampleLayer.h | 2 +- spine-cocos2d-iphone/example/ExampleLayer.m | 26 +- .../project.pbxproj | 12 + spine-cocos2d-iphone/src/spine/CCSkeleton.h | 4 +- .../src/spine/CCSkeletonAnimation.h | 46 +- .../src/spine/CCSkeletonAnimation.m | 124 ++- 7 files changed, 634 insertions(+), 401 deletions(-) diff --git a/spine-cocos2d-iphone/Resources/spineboy.json b/spine-cocos2d-iphone/Resources/spineboy.json index 57b0f6cbe..113e72c33 100644 --- a/spine-cocos2d-iphone/Resources/spineboy.json +++ b/spine-cocos2d-iphone/Resources/spineboy.json @@ -3,21 +3,21 @@ { "name": "root" }, { "name": "hip", "parent": "root", "x": 0.64, "y": 114.41 }, { "name": "left upper leg", "parent": "hip", "length": 50.39, "x": 14.45, "y": 2.81, "rotation": -89.09 }, - { "name": "left lower leg", "parent": "left upper leg", "length": 56.45, "x": 51.78, "y": 3.46, "rotation": -16.65 }, - { "name": "left foot", "parent": "left lower leg", "length": 46.5, "x": 64.02, "y": -8.67, "rotation": 102.43 }, + { "name": "pelvis", "parent": "hip", "x": 1.41, "y": -6.57 }, { "name": "right upper leg", "parent": "hip", "length": 45.76, "x": -18.27, "rotation": -101.13 }, - { "name": "right lower leg", "parent": "right upper leg", "length": 58.52, "x": 50.21, "y": 0.6, "rotation": -10.7 }, - { "name": "right foot", "parent": "right lower leg", "length": 45.45, "x": 64.88, "y": 0.04, "rotation": 110.3 }, { "name": "torso", "parent": "hip", "length": 85.82, "x": -6.42, "y": 1.97, "rotation": 94.95 }, - { "name": "neck", "parent": "torso", "length": 18.38, "x": 83.64, "y": -1.78, "rotation": 0.9 }, - { "name": "head", "parent": "neck", "length": 68.28, "x": 19.09, "y": 6.97, "rotation": -8.94 }, - { "name": "right shoulder", "parent": "torso", "length": 49.95, "x": 81.9, "y": 6.79, "rotation": 130.6 }, - { "name": "right arm", "parent": "right shoulder", "length": 36.74, "x": 49.95, "y": -0.12, "rotation": 40.12 }, - { "name": "right hand", "parent": "right arm", "length": 15.32, "x": 36.9, "y": 0.34, "rotation": 2.35 }, + { "name": "left lower leg", "parent": "left upper leg", "length": 56.45, "x": 51.78, "y": 3.46, "rotation": -16.65 }, { "name": "left shoulder", "parent": "torso", "length": 44.19, "x": 78.96, "y": -15.75, "rotation": -156.96 }, + { "name": "neck", "parent": "torso", "length": 18.38, "x": 83.64, "y": -1.78, "rotation": 0.9 }, + { "name": "right lower leg", "parent": "right upper leg", "length": 58.52, "x": 50.21, "y": 0.6, "rotation": -10.7 }, + { "name": "right shoulder", "parent": "torso", "length": 49.95, "x": 81.9, "y": 6.79, "rotation": 130.6 }, + { "name": "head", "parent": "neck", "length": 68.28, "x": 19.09, "y": 6.97, "rotation": -8.94 }, { "name": "left arm", "parent": "left shoulder", "length": 35.62, "x": 44.19, "y": -0.01, "rotation": 28.16 }, + { "name": "left foot", "parent": "left lower leg", "length": 46.5, "x": 64.02, "y": -8.67, "rotation": 102.43 }, + { "name": "right arm", "parent": "right shoulder", "length": 36.74, "x": 49.95, "y": -0.12, "rotation": 40.12 }, + { "name": "right foot", "parent": "right lower leg", "length": 45.45, "x": 64.88, "y": 0.04, "rotation": 110.3 }, { "name": "left hand", "parent": "left arm", "length": 11.52, "x": 35.62, "y": 0.07, "rotation": 2.7 }, - { "name": "pelvis", "parent": "hip", "x": 1.41, "y": -6.57 } + { "name": "right hand", "parent": "right arm", "length": 15.32, "x": 36.9, "y": 0.34, "rotation": 2.35 } ], "slots": [ { "name": "left shoulder", "bone": "left shoulder", "attachment": "left-shoulder" }, @@ -34,344 +34,281 @@ { "name": "neck", "bone": "neck", "attachment": "neck" }, { "name": "head", "bone": "head", "attachment": "head" }, { "name": "eyes", "bone": "head", "attachment": "eyes" }, - { "name": "right shoulder", "bone": "right shoulder", "attachment": "right-shoulder" }, + { "name": "right shoulder", "bone": "right shoulder", "attachment": "right-shoulder", "additive": true }, { "name": "right arm", "bone": "right arm", "attachment": "right-arm" }, - { "name": "right hand", "bone": "right hand", "attachment": "right-hand" } + { "name": "right hand", "bone": "right hand", "attachment": "right-hand" }, + { "name": "bb-head", "bone": "head", "attachment": "bb-head" } ], "skins": { "default": { - "left shoulder": { - "left-shoulder": { "x": 23.74, "y": 0.11, "rotation": 62.01, "width": 34, "height": 53 } + "bb-head": { + "bb-head": { + "type": "boundingbox", + "vertices": [ + 55.69696, + -44.60648, + 8.2226715, + -47.609646, + -11.244263, + -32.942703, + -0.05206299, + 35.835804, + 61.018433, + 43.227512, + 90.35846, + -16.054127, + 115.41275, + -32.817406, + 78.29431, + -56.05409 + ] + } + }, + "eyes": { + "eyes": { "x": 28.94, "y": -32.92, "rotation": -86.9, "width": 34, "height": 27 }, + "eyes-closed": { "x": 28.77, "y": -32.86, "rotation": -86.9, "width": 34, "height": 27 } + }, + "head": { + "head": { "x": 53.94, "y": -5.75, "rotation": -86.9, "width": 121, "height": 132 } }, "left arm": { "left-arm": { "x": 15.11, "y": -0.44, "rotation": 33.84, "width": 35, "height": 29 } }, - "left hand": { - "left-hand": { "x": 0.75, "y": 1.86, "rotation": 31.14, "width": 35, "height": 38 } - }, "left foot": { "left-foot": { "x": 24.35, "y": 8.88, "rotation": 3.32, "width": 65, "height": 30 } }, + "left hand": { + "left-hand": { "x": 0.75, "y": 1.86, "rotation": 31.14, "width": 35, "height": 38 } + }, "left lower leg": { "left-lower-leg": { "x": 24.55, "y": -1.92, "rotation": 105.75, "width": 49, "height": 64 } }, + "left shoulder": { + "left-shoulder": { "x": 23.74, "y": 0.11, "rotation": 62.01, "width": 34, "height": 53 } + }, "left upper leg": { "left-upper-leg": { "x": 26.12, "y": -1.85, "rotation": 89.09, "width": 33, "height": 67 } }, + "neck": { + "neck": { "x": 9.42, "y": -3.66, "rotation": -100.15, "width": 34, "height": 28 } + }, "pelvis": { "pelvis": { "x": -4.83, "y": 10.62, "width": 63, "height": 47 } }, + "right arm": { + "right-arm": { "x": 18.34, "y": -2.64, "rotation": 94.32, "width": 21, "height": 45 } + }, "right foot": { "right-foot": { "x": 19.02, "y": 8.47, "rotation": 1.52, "width": 67, "height": 30 } }, + "right hand": { + "right-hand": { "x": 6.82, "y": 1.25, "rotation": 91.96, "width": 32, "height": 32 } + }, "right lower leg": { "right-lower-leg": { "x": 23.28, "y": -2.59, "rotation": 111.83, "width": 51, "height": 64 } }, + "right shoulder": { + "right-shoulder": { "x": 25.86, "y": 0.03, "rotation": 134.44, "width": 52, "height": 51 } + }, "right upper leg": { "right-upper-leg": { "x": 23.03, "y": 0.25, "rotation": 101.13, "width": 44, "height": 70 } }, "torso": { "torso": { "x": 44.57, "y": -7.08, "rotation": -94.95, "width": 68, "height": 92 } - }, - "neck": { - "neck": { "x": 9.42, "y": -3.66, "rotation": -100.15, "width": 34, "height": 28 } - }, - "head": { - "head": { "x": 53.94, "y": -5.75, "rotation": -86.9, "width": 121, "height": 132 } - }, - "eyes": { - "eyes": { "x": 28.94, "y": -32.92, "rotation": -86.9, "width": 34, "height": 27 }, - "eyes-closed": { "x": 28.77, "y": -32.86, "rotation": -86.9, "width": 34, "height": 27 } - }, - "right shoulder": { - "right-shoulder": { "x": 25.86, "y": 0.03, "rotation": 134.44, "width": 52, "height": 51 } - }, - "right arm": { - "right-arm": { "x": 18.34, "y": -2.64, "rotation": 94.32, "width": 21, "height": 45 } - }, - "right hand": { - "right-hand": { "x": 6.82, "y": 1.25, "rotation": 91.96, "width": 32, "height": 32 } } } }, +"events": { + "behind": {}, + "headAttach": {}, + "headPop": {} +}, "animations": { - "walk": { + "drawOrder": { "bones": { - "left upper leg": { - "rotate": [ - { "time": 0, "angle": -26.55 }, - { "time": 0.1333, "angle": -8.78 }, - { "time": 0.2666, "angle": 9.51 }, - { "time": 0.4, "angle": 30.74 }, - { "time": 0.5333, "angle": 25.33 }, - { "time": 0.6666, "angle": 26.11 }, - { "time": 0.8, "angle": -7.7 }, - { "time": 0.9333, "angle": -21.19 }, - { "time": 1.0666, "angle": -26.55 } - ], - "translate": [ - { "time": 0, "x": -3, "y": -2.25 }, - { "time": 0.4, "x": -2.18, "y": -2.25 }, - { "time": 1.0666, "x": -3, "y": -2.25 } - ] - }, - "right upper leg": { - "rotate": [ - { "time": 0, "angle": 42.45 }, - { "time": 0.1333, "angle": 52.1 }, - { "time": 0.2666, "angle": 5.96 }, - { "time": 0.5333, "angle": -16.93 }, - { "time": 0.6666, "angle": 1.89 }, - { - "time": 0.8, - "angle": 28.06, - "curve": [ 0.462, 0.11, 1, 1 ] - }, - { - "time": 0.9333, - "angle": 58.68, - "curve": [ 0.5, 0.02, 1, 1 ] - }, - { "time": 1.0666, "angle": 42.45 } - ], - "translate": [ - { "time": 0, "x": 8.11, "y": -2.36 }, - { "time": 0.1333, "x": 10.03, "y": -2.56 }, - { "time": 0.4, "x": 2.76, "y": -2.97 }, - { "time": 0.5333, "x": 2.76, "y": -2.81 }, - { "time": 0.9333, "x": 8.67, "y": -2.54 }, - { "time": 1.0666, "x": 8.11, "y": -2.36 } - ] - }, - "left lower leg": { - "rotate": [ - { "time": 0, "angle": -10.21 }, - { "time": 0.1333, "angle": -55.64 }, - { "time": 0.2666, "angle": -68.12 }, - { "time": 0.5333, "angle": 5.11 }, - { "time": 0.6666, "angle": -28.29 }, - { "time": 0.8, "angle": 4.08 }, - { "time": 0.9333, "angle": 3.53 }, - { "time": 1.0666, "angle": -10.21 } - ] - }, - "left foot": { - "rotate": [ - { "time": 0, "angle": -3.69 }, - { "time": 0.1333, "angle": -10.42 }, - { "time": 0.2666, "angle": -17.14 }, - { "time": 0.4, "angle": -2.83 }, - { "time": 0.5333, "angle": -3.87 }, - { "time": 0.6666, "angle": 2.78 }, - { "time": 0.8, "angle": 1.68 }, - { "time": 0.9333, "angle": -8.54 }, - { "time": 1.0666, "angle": -3.69 } - ] - }, - "right shoulder": { - "rotate": [ - { - "time": 0, - "angle": 20.89, - "curve": [ 0.264, 0, 0.75, 1 ] - }, - { - "time": 0.1333, - "angle": 3.72, - "curve": [ 0.272, 0, 0.841, 1 ] - }, - { "time": 0.6666, "angle": -278.28 }, - { "time": 1.0666, "angle": 20.89 } - ], - "translate": [ - { "time": 0, "x": -7.84, "y": 7.19 }, - { "time": 0.1333, "x": -6.36, "y": 6.42 }, - { "time": 0.6666, "x": -11.07, "y": 5.25 }, - { "time": 1.0666, "x": -7.84, "y": 7.19 } - ] - }, - "right arm": { - "rotate": [ - { - "time": 0, - "angle": -4.02, - "curve": [ 0.267, 0, 0.804, 0.99 ] - }, - { - "time": 0.1333, - "angle": -13.99, - "curve": [ 0.341, 0, 1, 1 ] - }, - { - "time": 0.6666, - "angle": 36.54, - "curve": [ 0.307, 0, 0.787, 0.99 ] - }, - { "time": 1.0666, "angle": -4.02 } - ] - }, - "right hand": { - "rotate": [ - { "time": 0, "angle": 22.92 }, - { "time": 0.4, "angle": -8.97 }, - { "time": 0.6666, "angle": 0.51 }, - { "time": 1.0666, "angle": 22.92 } - ] - }, - "left shoulder": { - "rotate": [ - { "time": 0, "angle": -1.47 }, - { "time": 0.1333, "angle": 13.6 }, - { "time": 0.6666, "angle": 280.74 }, - { "time": 1.0666, "angle": -1.47 } - ], - "translate": [ - { "time": 0, "x": -1.76, "y": 0.56 }, - { "time": 0.6666, "x": -2.47, "y": 8.14 }, - { "time": 1.0666, "x": -1.76, "y": 0.56 } - ] - }, - "left hand": { - "rotate": [ - { - "time": 0, - "angle": 11.58, - "curve": [ 0.169, 0.37, 0.632, 1.55 ] - }, - { - "time": 0.1333, - "angle": 28.13, - "curve": [ 0.692, 0, 0.692, 0.99 ] - }, - { - "time": 0.6666, - "angle": -27.42, - "curve": [ 0.117, 0.41, 0.738, 1.76 ] - }, - { "time": 0.8, "angle": -36.32 }, - { "time": 1.0666, "angle": 11.58 } - ] - }, - "left arm": { - "rotate": [ - { "time": 0, "angle": -8.27 }, - { "time": 0.1333, "angle": 18.43 }, - { "time": 0.6666, "angle": 0.88 }, - { "time": 1.0666, "angle": -8.27 } - ] - }, - "torso": { - "rotate": [ - { "time": 0, "angle": -10.28 }, - { - "time": 0.1333, - "angle": -15.38, - "curve": [ 0.545, 0, 1, 1 ] - }, - { - "time": 0.4, - "angle": -9.78, - "curve": [ 0.58, 0.17, 1, 1 ] - }, - { "time": 0.6666, "angle": -15.75 }, - { "time": 0.9333, "angle": -7.06 }, - { "time": 1.0666, "angle": -10.28 } - ], - "translate": [ - { "time": 0, "x": -3.67, "y": 1.68 }, - { "time": 0.1333, "x": -3.67, "y": 0.68 }, - { "time": 0.4, "x": -3.67, "y": 1.97 }, - { "time": 0.6666, "x": -3.67, "y": -0.14 }, - { "time": 1.0666, "x": -3.67, "y": 1.68 } - ] - }, - "right foot": { - "rotate": [ - { "time": 0, "angle": -5.25 }, - { "time": 0.2666, "angle": -4.08 }, - { "time": 0.4, "angle": -6.45 }, - { "time": 0.5333, "angle": -5.39 }, - { "time": 0.8, "angle": -11.68 }, - { "time": 0.9333, "angle": 0.46 }, - { "time": 1.0666, "angle": -5.25 } - ] - }, - "right lower leg": { - "rotate": [ - { "time": 0, "angle": -3.39 }, - { "time": 0.1333, "angle": -45.53 }, - { "time": 0.2666, "angle": -2.59 }, - { "time": 0.5333, "angle": -19.53 }, - { "time": 0.6666, "angle": -64.8 }, - { - "time": 0.8, - "angle": -82.56, - "curve": [ 0.557, 0.18, 1, 1 ] - }, - { "time": 1.0666, "angle": -3.39 } - ] - }, - "hip": { - "rotate": [ - { "time": 0, "angle": 0, "curve": "stepped" }, - { "time": 1.0666, "angle": 0 } - ], - "translate": [ - { "time": 0, "x": 0, "y": 0 }, - { - "time": 0.1333, - "x": 0, - "y": -7.61, - "curve": [ 0.272, 0.86, 1, 1 ] - }, - { "time": 0.4, "x": 0, "y": 8.7 }, - { "time": 0.5333, "x": 0, "y": -0.41 }, - { - "time": 0.6666, - "x": 0, - "y": -7.05, - "curve": [ 0.235, 0.89, 1, 1 ] - }, - { "time": 0.8, "x": 0, "y": 2.92 }, - { "time": 0.9333, "x": 0, "y": 6.78 }, - { "time": 1.0666, "x": 0, "y": 0 } - ] - }, - "neck": { - "rotate": [ - { "time": 0, "angle": 3.6 }, - { "time": 0.1333, "angle": 17.49 }, - { "time": 0.2666, "angle": 6.1 }, - { "time": 0.4, "angle": 3.45 }, - { "time": 0.5333, "angle": 5.17 }, - { "time": 0.6666, "angle": 18.36 }, - { "time": 0.8, "angle": 6.09 }, - { "time": 0.9333, "angle": 2.28 }, - { "time": 1.0666, "angle": 3.6 } - ] - }, "head": { "rotate": [ + { "time": 0, "angle": 0 }, + { "time": 0.4827, "angle": -23.11 }, + { "time": 0.8965, "angle": -56.45 }, + { "time": 1.3103, "angle": 1.38 }, + { "time": 1.7931, "angle": 36.12 }, + { "time": 2.1379, "angle": 1.24 }, + { "time": 2.6206, "angle": -37.12 }, + { "time": 2.9666, "angle": 2.07 }, + { "time": 3.4666, "angle": 34.72 }, + { "time": 3.9, "angle": 359.99 } + ], + "translate": [ { "time": 0, - "angle": 3.6, - "curve": [ 0, 0, 0.704, 1.61 ] + "x": 0, + "y": 0, + "curve": [ 0.19, 0.4, 0.586, 0.75 ] }, - { "time": 0.1666, "angle": -0.2 }, - { "time": 0.2666, "angle": 6.1 }, - { "time": 0.4, "angle": 3.45 }, { - "time": 0.5333, - "angle": 5.17, - "curve": [ 0, 0, 0.704, 1.61 ] + "time": 0.2758, + "x": 57.88, + "y": -35.72, + "curve": [ 0.39, 0.54, 0.632, 0.72 ] }, - { "time": 0.7, "angle": 1.1 }, - { "time": 0.8, "angle": 6.09 }, - { "time": 0.9333, "angle": 2.28 }, - { "time": 1.0666, "angle": 3.6 } + { + "time": 0.4827, + "x": 87.26, + "y": -87.89, + "curve": [ 0.325, 0.23, 0.587, 0.36 ] + }, + { + "time": 0.6896, + "x": 28.89, + "y": -114.62, + "curve": [ 0.383, 0.23, 0.736, 0.55 ] + }, + { + "time": 0.8965, + "x": -76.58, + "y": -124.98, + "curve": [ 0.129, 0.21, 0.547, 0.64 ] + }, + { + "time": 1.1034, + "x": -154.37, + "y": -77.13, + "curve": [ 0.354, 0.48, 0.729, 0.9 ] + }, + { + "time": 1.3103, + "x": -181.02, + "y": 18.56, + "curve": [ 0.063, 0.15, 0.52, 0.62 ] + }, + { + "time": 1.5862, + "x": -150.38, + "y": 128.67, + "curve": [ 0.381, 0.54, 0.778, 1 ] + }, + { + "time": 1.7931, + "x": -112.08, + "y": 146.28, + "curve": [ 0.242, 0, 0.626, 0.45 ] + }, + { + "time": 1.931, + "x": -63.7, + "y": 111.22, + "curve": [ 0.398, 0.35, 0.786, 0.76 ] + }, + { + "time": 2.1379, + "x": -48.94, + "y": -1.55, + "curve": [ 0.188, 0.21, 0.575, 0.61 ] + }, + { + "time": 2.3448, + "x": -91.69, + "y": -91.93, + "curve": [ 0.362, 0.51, 0.766, 1 ] + }, + { + "time": 2.6206, + "x": -142.79, + "y": -126.83, + "curve": [ 0.227, 0.34, 0.593, 0.75 ] + }, + { + "time": 2.7586, + "x": -176.7, + "y": -98.32, + "curve": [ 0.26, 0.4, 0.612, 0.71 ] + }, + { + "time": 2.8965, + "x": -163.95, + "y": -24.04, + "curve": [ 0.338, 0.37, 0.676, 0.71 ] + }, + { + "time": 2.9655, + "x": -150.17, + "y": 10.71, + "curve": [ 0.387, 0.61, 0.741, 1 ] + }, + { + "time": 3.1034, + "x": -102.44, + "y": 45.92, + "curve": [ 0.31, 0.24, 0.648, 0.58 ] + }, + { + "time": 3.2413, + "x": -53.99, + "y": 70.39, + "curve": [ 0.325, 0.29, 0.663, 0.63 ] + }, + { + "time": 3.3793, + "x": 1.88, + "y": 55.54, + "curve": [ 0.387, 0.33, 0.769, 0.73 ] + }, + { + "time": 3.5862, + "x": 34.26, + "y": 36.13, + "curve": [ 0.206, 0.28, 0.596, 0.67 ] + }, + { + "time": 3.7931, + "x": 23.94, + "y": 1.01, + "curve": [ 0.373, 0.56, 0.759, 1 ] + }, + { "time": 4, "x": 0, "y": 0 } + ], + "scale": [ + { "time": 0.8275, "x": 1, "y": 1 }, + { "time": 1.3103, "x": 0.742, "y": 0.742 }, + { "time": 1.7931, "x": 1, "y": 1 }, + { "time": 2.1379, "x": 1.502, "y": 1.502 }, + { "time": 2.6206, "x": 1, "y": 1 }, + { "time": 2.9655, "x": 0.707, "y": 0.707 }, + { "time": 3.3793, "x": 1, "y": 1 } ] } - } + }, + "events": [ + { "time": 0, "name": "headPop", "string": "pop.wav" }, + { "time": 1.3103, "name": "behind" }, + { "time": 2.9655, "name": "behind" }, + { "time": 4, "name": "headAttach", "string": "attach.wav" } + ], + "draworder": [ + { + "time": 0.6206, + "offsets": [ + { "slot": "head", "offset": -12 }, + { "slot": "eyes", "offset": -12 } + ] + }, + { + "time": 1.7931, + "offsets": [ + { "slot": "head", "offset": 3 }, + { "slot": "eyes", "offset": 3 } + ] + }, + { + "time": 2.6206, + "offsets": [ + { "slot": "head", "offset": -12 }, + { "slot": "eyes", "offset": -12 } + ] + }, + { "time": 3.5862 } + ] }, "jump": { "bones": { @@ -782,6 +719,284 @@ ] } } + }, + "walk": { + "bones": { + "left upper leg": { + "rotate": [ + { "time": 0, "angle": -26.55 }, + { "time": 0.1333, "angle": -8.78 }, + { "time": 0.2666, "angle": 9.51 }, + { "time": 0.4, "angle": 30.74 }, + { "time": 0.5333, "angle": 25.33 }, + { "time": 0.6666, "angle": 26.11 }, + { "time": 0.8, "angle": -7.7 }, + { "time": 0.9333, "angle": -21.19 }, + { "time": 1.0666, "angle": -26.55 } + ], + "translate": [ + { "time": 0, "x": -3, "y": -2.25 }, + { "time": 0.4, "x": -2.18, "y": -2.25 }, + { "time": 1.0666, "x": -3, "y": -2.25 } + ] + }, + "right upper leg": { + "rotate": [ + { "time": 0, "angle": 42.45 }, + { "time": 0.1333, "angle": 52.1 }, + { "time": 0.2666, "angle": 5.96 }, + { "time": 0.5333, "angle": -16.93 }, + { "time": 0.6666, "angle": 1.89 }, + { + "time": 0.8, + "angle": 28.06, + "curve": [ 0.462, 0.11, 1, 1 ] + }, + { + "time": 0.9333, + "angle": 58.68, + "curve": [ 0.5, 0.02, 1, 1 ] + }, + { "time": 1.0666, "angle": 42.45 } + ], + "translate": [ + { "time": 0, "x": 8.11, "y": -2.36 }, + { "time": 0.1333, "x": 10.03, "y": -2.56 }, + { "time": 0.4, "x": 2.76, "y": -2.97 }, + { "time": 0.5333, "x": 2.76, "y": -2.81 }, + { "time": 0.9333, "x": 8.67, "y": -2.54 }, + { "time": 1.0666, "x": 8.11, "y": -2.36 } + ] + }, + "left lower leg": { + "rotate": [ + { "time": 0, "angle": -10.21 }, + { "time": 0.1333, "angle": -55.64 }, + { "time": 0.2666, "angle": -68.12 }, + { "time": 0.5333, "angle": 5.11 }, + { "time": 0.6666, "angle": -28.29 }, + { "time": 0.8, "angle": 4.08 }, + { "time": 0.9333, "angle": 3.53 }, + { "time": 1.0666, "angle": -10.21 } + ] + }, + "left foot": { + "rotate": [ + { "time": 0, "angle": -3.69 }, + { "time": 0.1333, "angle": -10.42 }, + { "time": 0.2666, "angle": -17.14 }, + { "time": 0.4, "angle": -2.83 }, + { "time": 0.5333, "angle": -3.87 }, + { "time": 0.6666, "angle": 2.78 }, + { "time": 0.8, "angle": 1.68 }, + { "time": 0.9333, "angle": -8.54 }, + { "time": 1.0666, "angle": -3.69 } + ] + }, + "right shoulder": { + "rotate": [ + { + "time": 0, + "angle": 20.89, + "curve": [ 0.264, 0, 0.75, 1 ] + }, + { + "time": 0.1333, + "angle": 3.72, + "curve": [ 0.272, 0, 0.841, 1 ] + }, + { "time": 0.6666, "angle": -278.28 }, + { "time": 1.0666, "angle": 20.89 } + ], + "translate": [ + { "time": 0, "x": -7.84, "y": 7.19 }, + { "time": 0.1333, "x": -6.36, "y": 6.42 }, + { "time": 0.6666, "x": -11.07, "y": 5.25 }, + { "time": 1.0666, "x": -7.84, "y": 7.19 } + ] + }, + "right arm": { + "rotate": [ + { + "time": 0, + "angle": -4.02, + "curve": [ 0.267, 0, 0.804, 0.99 ] + }, + { + "time": 0.1333, + "angle": -13.99, + "curve": [ 0.341, 0, 1, 1 ] + }, + { + "time": 0.6666, + "angle": 36.54, + "curve": [ 0.307, 0, 0.787, 0.99 ] + }, + { "time": 1.0666, "angle": -4.02 } + ] + }, + "right hand": { + "rotate": [ + { "time": 0, "angle": 22.92 }, + { "time": 0.4, "angle": -8.97 }, + { "time": 0.6666, "angle": 0.51 }, + { "time": 1.0666, "angle": 22.92 } + ] + }, + "left shoulder": { + "rotate": [ + { "time": 0, "angle": -1.47 }, + { "time": 0.1333, "angle": 13.6 }, + { "time": 0.6666, "angle": 280.74 }, + { "time": 1.0666, "angle": -1.47 } + ], + "translate": [ + { "time": 0, "x": -1.76, "y": 0.56 }, + { "time": 0.6666, "x": -2.47, "y": 8.14 }, + { "time": 1.0666, "x": -1.76, "y": 0.56 } + ] + }, + "left hand": { + "rotate": [ + { + "time": 0, + "angle": 11.58, + "curve": [ 0.169, 0.37, 0.632, 1.55 ] + }, + { + "time": 0.1333, + "angle": 28.13, + "curve": [ 0.692, 0, 0.692, 0.99 ] + }, + { + "time": 0.6666, + "angle": -27.42, + "curve": [ 0.117, 0.41, 0.738, 1.76 ] + }, + { "time": 0.8, "angle": -36.32 }, + { "time": 1.0666, "angle": 11.58 } + ] + }, + "left arm": { + "rotate": [ + { "time": 0, "angle": -8.27 }, + { "time": 0.1333, "angle": 18.43 }, + { "time": 0.6666, "angle": 0.88 }, + { "time": 1.0666, "angle": -8.27 } + ] + }, + "torso": { + "rotate": [ + { "time": 0, "angle": -10.28 }, + { + "time": 0.1333, + "angle": -15.38, + "curve": [ 0.545, 0, 1, 1 ] + }, + { + "time": 0.4, + "angle": -9.78, + "curve": [ 0.58, 0.17, 1, 1 ] + }, + { "time": 0.6666, "angle": -15.75 }, + { "time": 0.9333, "angle": -7.06 }, + { "time": 1.0666, "angle": -10.28 } + ], + "translate": [ + { "time": 0, "x": -3.67, "y": 1.68 }, + { "time": 0.1333, "x": -3.67, "y": 0.68 }, + { "time": 0.4, "x": -3.67, "y": 1.97 }, + { "time": 0.6666, "x": -3.67, "y": -0.14 }, + { "time": 1.0666, "x": -3.67, "y": 1.68 } + ] + }, + "right foot": { + "rotate": [ + { "time": 0, "angle": -5.25 }, + { "time": 0.2666, "angle": -4.08 }, + { "time": 0.4, "angle": -6.45 }, + { "time": 0.5333, "angle": -5.39 }, + { "time": 0.8, "angle": -11.68 }, + { "time": 0.9333, "angle": 0.46 }, + { "time": 1.0666, "angle": -5.25 } + ] + }, + "right lower leg": { + "rotate": [ + { "time": 0, "angle": -3.39 }, + { "time": 0.1333, "angle": -45.53 }, + { "time": 0.2666, "angle": -2.59 }, + { "time": 0.5333, "angle": -19.53 }, + { "time": 0.6666, "angle": -64.8 }, + { + "time": 0.8, + "angle": -82.56, + "curve": [ 0.557, 0.18, 1, 1 ] + }, + { "time": 1.0666, "angle": -3.39 } + ] + }, + "hip": { + "rotate": [ + { "time": 0, "angle": 0, "curve": "stepped" }, + { "time": 1.0666, "angle": 0 } + ], + "translate": [ + { "time": 0, "x": 0, "y": 0 }, + { + "time": 0.1333, + "x": 0, + "y": -7.61, + "curve": [ 0.272, 0.86, 1, 1 ] + }, + { "time": 0.4, "x": 0, "y": 8.7 }, + { "time": 0.5333, "x": 0, "y": -0.41 }, + { + "time": 0.6666, + "x": 0, + "y": -7.05, + "curve": [ 0.235, 0.89, 1, 1 ] + }, + { "time": 0.8, "x": 0, "y": 2.92 }, + { "time": 0.9333, "x": 0, "y": 6.78 }, + { "time": 1.0666, "x": 0, "y": 0 } + ] + }, + "neck": { + "rotate": [ + { "time": 0, "angle": 3.6 }, + { "time": 0.1333, "angle": 17.49 }, + { "time": 0.2666, "angle": 6.1 }, + { "time": 0.4, "angle": 3.45 }, + { "time": 0.5333, "angle": 5.17 }, + { "time": 0.6666, "angle": 18.36 }, + { "time": 0.8, "angle": 6.09 }, + { "time": 0.9333, "angle": 2.28 }, + { "time": 1.0666, "angle": 3.6 } + ] + }, + "head": { + "rotate": [ + { + "time": 0, + "angle": 3.6, + "curve": [ 0, 0, 0.704, 1.61 ] + }, + { "time": 0.1666, "angle": -0.2 }, + { "time": 0.2666, "angle": 6.1 }, + { "time": 0.4, "angle": 3.45 }, + { + "time": 0.5333, + "angle": 5.17, + "curve": [ 0, 0, 0.704, 1.61 ] + }, + { "time": 0.7, "angle": 1.1 }, + { "time": 0.8, "angle": 6.09 }, + { "time": 0.9333, "angle": 2.28 }, + { "time": 1.0666, "angle": 3.6 } + ] + } + } } } } \ No newline at end of file diff --git a/spine-cocos2d-iphone/example/ExampleLayer.h b/spine-cocos2d-iphone/example/ExampleLayer.h index 5ead22f7c..649a9e2bc 100644 --- a/spine-cocos2d-iphone/example/ExampleLayer.h +++ b/spine-cocos2d-iphone/example/ExampleLayer.h @@ -3,7 +3,7 @@ #import "cocos2d.h" #import -@interface ExampleLayer : CCLayer { +@interface ExampleLayer : CCLayer { CCSkeletonAnimation* animationNode; } diff --git a/spine-cocos2d-iphone/example/ExampleLayer.m b/spine-cocos2d-iphone/example/ExampleLayer.m index 889e4d217..997d8ba38 100644 --- a/spine-cocos2d-iphone/example/ExampleLayer.m +++ b/spine-cocos2d-iphone/example/ExampleLayer.m @@ -17,9 +17,12 @@ animationNode = [CCSkeletonAnimation skeletonWithFile:@"spineboy.json" atlasFile:@"spineboy.atlas" scale:1]; [animationNode setMixFrom:@"walk" to:@"jump" duration:0.2f]; [animationNode setMixFrom:@"jump" to:@"walk" duration:0.4f]; - [animationNode setAnimation:@"walk" loop:NO]; - [animationNode addAnimation:@"jump" loop:NO afterDelay:0]; - [animationNode addAnimation:@"walk" loop:YES afterDelay:0]; + [animationNode setDelegate:self]; + [animationNode setAnimationForTrack:0 name:@"walk" loop:NO]; + [animationNode addAnimationForTrack:0 name:@"jump" loop:NO afterDelay:0]; + [animationNode addAnimationForTrack:0 name:@"walk" loop:YES afterDelay:0]; + [animationNode addAnimationForTrack:0 name:@"jump" loop:YES afterDelay:4]; + [animationNode setAnimationForTrack:1 name:@"drawOrder" loop:YES]; animationNode.timeScale = 0.3f; animationNode.debugBones = true; @@ -34,6 +37,23 @@ return self; } +- (void) animationDidStart:(CCSkeletonAnimation*)animation track:(int)trackIndex { + CCLOG(@"%d start: %s", trackIndex, AnimationState_getCurrent(animation.state, trackIndex)->animation->name); +} + +- (void) animationWillEnd:(CCSkeletonAnimation*)animation track:(int)trackIndex { + CCLOG(@"%d end: %s", trackIndex, AnimationState_getCurrent(animation.state, trackIndex)->animation->name); +} + +- (void) animationDidTriggerEvent:(CCSkeletonAnimation*)animation track:(int)trackIndex event:(Event*)event { + CCLOG(@"%d event: %s, %s: %d, %f, %s", trackIndex, AnimationState_getCurrent(animation.state, trackIndex)->animation->name, + event->data->name, event->intValue, event->floatValue, event->stringValue); +} + +- (void) animationDidComplete:(CCSkeletonAnimation*)animation track:(int)trackIndex loopCount:(int)loopCount { + CCLOG(@"%d complete: %s, %d", trackIndex, AnimationState_getCurrent(animation.state, trackIndex)->animation->name, loopCount); +} + #if __CC_PLATFORM_MAC - (BOOL) ccMouseDown:(NSEvent*)event { CCDirector* director = [CCDirector sharedDirector]; diff --git a/spine-cocos2d-iphone/spine-cocos2d-iphone-ios.xcodeproj/project.pbxproj b/spine-cocos2d-iphone/spine-cocos2d-iphone-ios.xcodeproj/project.pbxproj index 2dc5a84bd..fd59167d9 100644 --- a/spine-cocos2d-iphone/spine-cocos2d-iphone-ios.xcodeproj/project.pbxproj +++ b/spine-cocos2d-iphone/spine-cocos2d-iphone-ios.xcodeproj/project.pbxproj @@ -9,6 +9,8 @@ /* Begin PBXBuildFile section */ 4374AF5F17F1D74000401ACE /* BoundingBoxAttachment.c in Sources */ = {isa = PBXBuildFile; fileRef = 4374AF5D17F1D74000401ACE /* BoundingBoxAttachment.c */; }; 4374AF6017F1D74000401ACE /* SkeletonBounds.c in Sources */ = {isa = PBXBuildFile; fileRef = 4374AF5E17F1D74000401ACE /* SkeletonBounds.c */; }; + 4374AF7817F5766700401ACE /* Event.c in Sources */ = {isa = PBXBuildFile; fileRef = 4374AF7617F5766700401ACE /* Event.c */; }; + 4374AF7917F5766700401ACE /* EventData.c in Sources */ = {isa = PBXBuildFile; fileRef = 4374AF7717F5766700401ACE /* EventData.c */; }; 43C3282B170B0BF6004A9460 /* ExampleLayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 43C32827170B0BF6004A9460 /* ExampleLayer.m */; }; 43C3282F170B0C19004A9460 /* spine-cocos2d-iphone.m in Sources */ = {isa = PBXBuildFile; fileRef = 43C3282D170B0C19004A9460 /* spine-cocos2d-iphone.m */; }; 43C32855170B0C6C004A9460 /* Animation.c in Sources */ = {isa = PBXBuildFile; fileRef = 43C32830170B0C6C004A9460 /* Animation.c */; }; @@ -179,6 +181,10 @@ 4374AF5E17F1D74000401ACE /* SkeletonBounds.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = SkeletonBounds.c; path = "../spine-c/src/spine/SkeletonBounds.c"; sourceTree = ""; }; 4374AF6217F1D74E00401ACE /* BoundingBoxAttachment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BoundingBoxAttachment.h; path = "../spine-c/include/spine/BoundingBoxAttachment.h"; sourceTree = ""; }; 4374AF6317F1D74E00401ACE /* SkeletonBounds.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SkeletonBounds.h; path = "../spine-c/include/spine/SkeletonBounds.h"; sourceTree = ""; }; + 4374AF7317F5765C00401ACE /* Event.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Event.h; path = "../spine-c/include/spine/Event.h"; sourceTree = ""; }; + 4374AF7417F5765C00401ACE /* EventData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = EventData.h; path = "../spine-c/include/spine/EventData.h"; sourceTree = ""; }; + 4374AF7617F5766700401ACE /* Event.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = Event.c; path = "../spine-c/src/spine/Event.c"; sourceTree = ""; }; + 4374AF7717F5766700401ACE /* EventData.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = EventData.c; path = "../spine-c/src/spine/EventData.c"; sourceTree = ""; }; 43C32826170B0BF6004A9460 /* ExampleLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ExampleLayer.h; path = example/ExampleLayer.h; sourceTree = ""; }; 43C32827170B0BF6004A9460 /* ExampleLayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ExampleLayer.m; path = example/ExampleLayer.m; sourceTree = ""; }; 43C3282D170B0C19004A9460 /* spine-cocos2d-iphone.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "spine-cocos2d-iphone.m"; path = "src/spine/spine-cocos2d-iphone.m"; sourceTree = ""; }; @@ -564,6 +570,10 @@ 43C32841170B0C6C004A9460 /* BoneData.h */, 4374AF5D17F1D74000401ACE /* BoundingBoxAttachment.c */, 4374AF6217F1D74E00401ACE /* BoundingBoxAttachment.h */, + 4374AF7617F5766700401ACE /* Event.c */, + 4374AF7317F5765C00401ACE /* Event.h */, + 4374AF7717F5766700401ACE /* EventData.c */, + 4374AF7417F5765C00401ACE /* EventData.h */, 43C32842170B0C6C004A9460 /* extension.c */, 43C32843170B0C6C004A9460 /* extension.h */, 43C32844170B0C6C004A9460 /* Json.c */, @@ -1242,6 +1252,8 @@ 43DB2A3E172C16BC003045C5 /* CCSkeletonAnimation.m in Sources */, 4374AF5F17F1D74000401ACE /* BoundingBoxAttachment.c in Sources */, 4374AF6017F1D74000401ACE /* SkeletonBounds.c in Sources */, + 4374AF7817F5766700401ACE /* Event.c in Sources */, + 4374AF7917F5766700401ACE /* EventData.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/spine-cocos2d-iphone/src/spine/CCSkeleton.h b/spine-cocos2d-iphone/src/spine/CCSkeleton.h index 8e2c841b1..4444af2a8 100644 --- a/spine-cocos2d-iphone/src/spine/CCSkeleton.h +++ b/spine-cocos2d-iphone/src/spine/CCSkeleton.h @@ -34,9 +34,7 @@ #import #import "cocos2d.h" -/** -Draws a skeleton. -*/ +/** Draws a skeleton. */ @interface CCSkeleton : CCNodeRGBA { Skeleton* _skeleton; Bone* _rootBone; diff --git a/spine-cocos2d-iphone/src/spine/CCSkeletonAnimation.h b/spine-cocos2d-iphone/src/spine/CCSkeletonAnimation.h index acadbb630..d12492a67 100644 --- a/spine-cocos2d-iphone/src/spine/CCSkeletonAnimation.h +++ b/spine-cocos2d-iphone/src/spine/CCSkeletonAnimation.h @@ -35,13 +35,24 @@ #import #import "cocos2d.h" -/** -Draws an animated skeleton, providing a simple API for applying one or more animations and queuing animations to be played later. -*/ -@interface CCSkeletonAnimation : CCSkeleton { - NSMutableArray* _states; +@class CCSkeletonAnimation; - NSMutableArray* _stateDatas; +@protocol CCSkeletonAnimationDelegate +@optional +- (void) animationDidStart:(CCSkeletonAnimation*)animation track:(int)trackIndex; +- (void) animationWillEnd:(CCSkeletonAnimation*)animation track:(int)trackIndex; +- (void) animationDidTriggerEvent:(CCSkeletonAnimation*)animation track:(int)trackIndex event:(Event*)event; +- (void) animationDidComplete:(CCSkeletonAnimation*)animation track:(int)trackIndex loopCount:(int)loopCount; +@end + +/** Draws an animated skeleton, providing an AnimationState for applying one or more animations and queuing animations to be + * played later. */ +@interface CCSkeletonAnimation : CCSkeleton { + AnimationState* _state; + bool _ownsAnimationStateData; + + id _delegate; + bool _delegateStart, _delegateEnd, _delegateEvent, _delegateComplete; } + (id) skeletonWithData:(SkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData; @@ -52,23 +63,18 @@ Draws an animated skeleton, providing a simple API for applying one or more anim - (id) initWithFile:(NSString*)skeletonDataFile atlas:(Atlas*)atlas scale:(float)scale; - (id) initWithFile:(NSString*)skeletonDataFile atlasFile:(NSString*)atlasFile scale:(float)scale; -- (void) addAnimationState; -- (void) addAnimationState:(AnimationStateData*)stateData; -- (AnimationState*) getAnimationState:(int)stateIndex; -- (void) setAnimationStateData:(AnimationStateData*)stateData forState:(int)stateIndex; - +- (void) setAnimationStateData:(AnimationStateData*)stateData; - (void) setMixFrom:(NSString*)fromAnimation to:(NSString*)toAnimation duration:(float)duration; -- (void) setMixFrom:(NSString*)fromAnimation to:(NSString*)toAnimation duration:(float)duration forState:(int)stateIndex; - -- (void) setAnimation:(NSString*)name loop:(bool)loop; -- (void) setAnimation:(NSString*)name loop:(bool)loop forState:(int)stateIndex; - -- (void) addAnimation:(NSString*)name loop:(bool)loop afterDelay:(float)delay; -- (void) addAnimation:(NSString*)name loop:(bool)loop afterDelay:(float)delay forState:(int)stateIndex; +- (void) setDelegate:(id)delegate; +- (TrackEntry*) setAnimationForTrack:(int)trackIndex name:(NSString*)name loop:(bool)loop; +- (TrackEntry*) addAnimationForTrack:(int)trackIndex name:(NSString*)name loop:(bool)loop afterDelay:(int)delay; +- (TrackEntry*) getCurrentForTrack:(int)trackIndex; - (void) clearAnimation; -- (void) clearAnimationForState:(int)stateIndex; +- (void) clearAnimationForTrack:(int)trackIndex; -@property (retain, nonatomic, readonly) NSMutableArray* states; +- (void) onAnimationStateEvent:(int)trackIndex type:(EventType)type event:(Event*)event loopCount:(int)loopCount; + +@property (nonatomic, readonly) AnimationState* state; @end diff --git a/spine-cocos2d-iphone/src/spine/CCSkeletonAnimation.m b/spine-cocos2d-iphone/src/spine/CCSkeletonAnimation.m index c1b7bad02..3c659761d 100644 --- a/spine-cocos2d-iphone/src/spine/CCSkeletonAnimation.m +++ b/spine-cocos2d-iphone/src/spine/CCSkeletonAnimation.m @@ -34,13 +34,17 @@ #import #import +static void callback (AnimationState* state, int trackIndex, EventType type, Event* event, int loopCount) { + [(CCSkeletonAnimation*)state->context onAnimationStateEvent:trackIndex type:type event:event loopCount:loopCount]; +} + @interface CCSkeletonAnimation (Private) - (void) initialize; @end @implementation CCSkeletonAnimation -@synthesize states = _states; +@synthesize state = _state; + (id) skeletonWithData:(SkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData { return [[[self alloc] initWithData:skeletonData ownsSkeletonData:ownsSkeletonData] autorelease]; @@ -55,9 +59,9 @@ } - (void) initialize { - _states = [[NSMutableArray arrayWithCapacity:2] retain]; - _stateDatas = [[NSMutableArray arrayWithCapacity:2] retain]; - [self addAnimationState]; + _state = AnimationState_create(AnimationStateData_create(_skeleton->data)); + _state->context = self; + _state->listener = callback; } - (id) initWithData:(SkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData { @@ -88,13 +92,8 @@ } - (void) dealloc { - for (NSValue* value in _stateDatas) - AnimationStateData_dispose([value pointerValue]); - [_stateDatas release]; - - for (NSValue* value in _states) - AnimationState_dispose([value pointerValue]); - [_states release]; + if (_ownsAnimationStateData) AnimationStateData_dispose(_state->data); + AnimationState_dispose(_state); [super dealloc]; } @@ -103,88 +102,71 @@ [super update:deltaTime]; deltaTime *= _timeScale; - for (NSValue* value in _states) { - AnimationState* state = [value pointerValue]; - AnimationState_update(state, deltaTime); - AnimationState_apply(state, _skeleton); - } + AnimationState_update(_state, deltaTime); + AnimationState_apply(_state, _skeleton); Skeleton_updateWorldTransform(_skeleton); } -- (void) addAnimationState { - AnimationStateData* stateData = AnimationStateData_create(_skeleton->data); - [_stateDatas addObject:[NSValue valueWithPointer:stateData]]; - [self addAnimationState:stateData]; -} - -- (void) addAnimationState:(AnimationStateData*)stateData { +- (void) setAnimationStateData:(AnimationStateData*)stateData { NSAssert(stateData, @"stateData cannot be null."); - AnimationState* state = AnimationState_create(stateData); - [_states addObject:[NSValue valueWithPointer:state]]; -} - -- (AnimationState*) getAnimationState:(int)stateIndex { - NSAssert(stateIndex >= 0 && stateIndex < (int)_states.count, @"stateIndex out of range."); - return [[_states objectAtIndex:stateIndex] pointerValue]; -} - -- (void) setAnimationStateData:(AnimationStateData*)stateData forState:(int)stateIndex { - NSAssert(stateData, @"stateData cannot be null."); - NSAssert(stateIndex >= 0 && stateIndex < (int)_states.count, @"stateIndex out of range."); - AnimationState* state = [[_states objectAtIndex:stateIndex] pointerValue]; - for (NSValue* value in _stateDatas) { - if (state->data == [value pointerValue]) { - AnimationStateData_dispose(state->data); - [_stateDatas removeObject:value]; - break; - } - } - [_states removeObject:[NSValue valueWithPointer:state]]; - AnimationState_dispose(state); + if (_ownsAnimationStateData) AnimationStateData_dispose(_state->data); + AnimationState_dispose(_state); - state = AnimationState_create(stateData); - [_states setObject:[NSValue valueWithPointer:state] atIndexedSubscript:stateIndex]; + _ownsAnimationStateData = true; + _state = AnimationState_create(stateData); + _state->context = self; + _state->listener = callback; } - (void) setMixFrom:(NSString*)fromAnimation to:(NSString*)toAnimation duration:(float)duration { - [self setMixFrom:fromAnimation to:toAnimation duration:duration forState:0]; + AnimationStateData_setMixByName(_state->data, [fromAnimation UTF8String], [toAnimation UTF8String], duration); } -- (void) setMixFrom:(NSString*)fromAnimation to:(NSString*)toAnimation duration:(float)duration forState:(int)stateIndex { - NSAssert(stateIndex >= 0 && stateIndex < (int)_states.count, @"stateIndex out of range."); - AnimationState* state = [[_states objectAtIndex:stateIndex] pointerValue]; - AnimationStateData_setMixByName(state->data, [fromAnimation UTF8String], [toAnimation UTF8String], duration); +- (void) setDelegate:(id)delegate { + _delegate = delegate; + _delegateStart = [delegate respondsToSelector:@selector(animationDidStart:track:)]; + _delegateEnd = [delegate respondsToSelector:@selector(animationWillEnd:track:)]; + _delegateEvent = [delegate respondsToSelector:@selector(animationDidTriggerEvent:track:event:)]; + _delegateComplete = [delegate respondsToSelector:@selector(animationDidComplete:track:loopCount:)]; } -- (void) setAnimation:(NSString*)name loop:(bool)loop { - [self setAnimation:name loop:loop forState:0]; +- (TrackEntry*) setAnimationForTrack:(int)trackIndex name:(NSString*)name loop:(bool)loop { + return AnimationState_setAnimationByName(_state, trackIndex, [name UTF8String], loop); } -- (void) setAnimation:(NSString*)name loop:(bool)loop forState:(int)stateIndex { - NSAssert(stateIndex >= 0 && stateIndex < (int)_states.count, @"stateIndex out of range."); - AnimationState* state = [[_states objectAtIndex:stateIndex] pointerValue]; - AnimationState_setAnimationByName(state, [name UTF8String], loop); +- (TrackEntry*) addAnimationForTrack:(int)trackIndex name:(NSString*)name loop:(bool)loop afterDelay:(int)delay { + return AnimationState_addAnimationByName(_state, trackIndex, [name UTF8String], loop, delay); } -- (void) addAnimation:(NSString*)name loop:(bool)loop afterDelay:(float)delay { - [self addAnimation:name loop:loop afterDelay:delay forState:0]; -} - -- (void) addAnimation:(NSString*)name loop:(bool)loop afterDelay:(float)delay forState:(int)stateIndex { - NSAssert(stateIndex >= 0 && stateIndex < (int)_states.count, @"stateIndex out of range."); - AnimationState* state = [[_states objectAtIndex:stateIndex] pointerValue]; - AnimationState_addAnimationByName(state, [name UTF8String], loop, delay); +- (TrackEntry*) getCurrentForTrack:(int)trackIndex { + return AnimationState_getCurrent(_state, trackIndex); } - (void) clearAnimation { - [self clearAnimationForState:0]; + AnimationState_clear(_state); } -- (void) clearAnimationForState:(int)stateIndex { - NSAssert(stateIndex >= 0 && stateIndex < (int)_states.count, @"stateIndex out of range."); - AnimationState* state = [[_states objectAtIndex:stateIndex] pointerValue]; - AnimationState_clearAnimation(state); +- (void) clearAnimationForTrack:(int)trackIndex { + AnimationState_clearTrack(_state, trackIndex); +} + +- (void) onAnimationStateEvent:(int)trackIndex type:(EventType)type event:(Event*)event loopCount:(int)loopCount { + if (!_delegate) return; + switch (type) { + case ANIMATION_START: + if (_delegateStart) [_delegate animationDidStart:self track:trackIndex]; + break; + case ANIMATION_END: + if (_delegateEnd) [_delegate animationWillEnd:self track:trackIndex]; + break; + case ANIMATION_COMPLETE: + if (_delegateComplete) [_delegate animationDidComplete:self track:trackIndex loopCount:loopCount]; + break; + case ANIMATION_EVENT: + if (_delegateEvent) [_delegate animationDidTriggerEvent:self track:trackIndex event:event]; + break; + } } @end