void Subscribe(Spine.AnimationState state, Spine.EventData eventDataReference, bool unsubscribe) { #if PREUNITY_5_3 Debug.LogWarning("Unity 5.3 or later is required for Spine Unity custom yield instructions to function correctly."); #endif if (state == null) { Debug.LogWarning("AnimationState argument was null. Coroutine will continue immediately."); m_WasFired = true; return; } else if (eventDataReference == null) { Debug.LogWarning("eventDataReference argument was null. Coroutine will continue immediately."); m_WasFired = true; return; } m_AnimationState = state; m_TargetEvent = eventDataReference; state.Event += HandleAnimationStateEvent; m_unsubscribeAfterFiring = unsubscribe; }
private void HandleAnimationStateEvent(Spine.TrackEntry entry, Spine.Event e) { eventData_hit = skeletonAnim.Skeleton.Data.FindEvent("hit"); eventData_swing = skeletonAnim.Skeleton.Data.FindEvent("swing"); eventData_run = skeletonAnim.Skeleton.Data.FindEvent("run"); eventData_rebirth = skeletonAnim.Skeleton.Data.FindEvent("rebirth"); if (e.Data == eventData_hit) { ObjectPoolManager.instance.PopFromPool_HitSound(); if (DungeonManager.instance.animationNotActive.Equals(false)) { AttackNext(); } //else // Idle(); } else if (e.Data == eventData_swing) { ObjectPoolManager.instance.PopFromPool_SwingSound(); } else if (e.Data == eventData_run) { ObjectPoolManager.instance.PopFromPool_RunSound(); } else if (e.Data == eventData_rebirth) { RebirthManager.instance.rebirthEffect.SetActive(true); SoundManager.instance.UISoundPlay(0); } }
public override void OnEnter() { GameObject go = Fsm.GetOwnerDefaultTarget(spineGameObject); if (go != null) { var component = go.GetComponent <IAnimationStateComponent>(); if (component != null) { state = component.AnimationState; if (state != null) { var skeletonComponent = component as ISkeletonComponent; var skeletonData = skeletonComponent.Skeleton.Data; eventData = skeletonData.FindEvent(eventName); if (eventData != null) { state.Event -= HandleEvent; state.Event += HandleEvent; } } else { Debug.LogWarning("Not valid animation skeleton data!"); } } } }
private void HandleAnimationStateEvent(Spine.TrackEntry entry, Spine.Event e) { eventData_open = boxSkeletonAnimation_result.Skeleton.Data.FindEvent("open"); if (e.Data == eventData_open) { SoundManager.instance.UISoundPlay(2); } }
public WaitForSpineEvent NowWaitFor(Spine.AnimationState state, Spine.EventData eventDataReference, bool unsubscribeAfterFiring = true) { ((IEnumerator)this).Reset(); Clear(state); Subscribe(state, eventDataReference, unsubscribeAfterFiring); return(this); }
void Subscribe (Spine.AnimationState state, Spine.EventData eventDataReference, bool unsubscribe) { if (state == null || eventDataReference == null) { m_WasFired = true; } else { m_AnimationState = state; m_TargetEvent = eventDataReference; state.Event += HandleAnimationStateEvent; m_unsubscribeAfterFiring = unsubscribe; } }
public void Initialize() { if (this.skeletonDataAsset != null) { this.eventData = this.skeletonDataAsset.GetSkeletonData(true).FindEvent(this.eventName); if (this.eventData == null) { object[] args = new object[] { this.eventName, this.skeletonDataAsset.name }; Debug.LogWarningFormat("Event Data '{0}' not found in SkeletonData : {1}.", args); } } }
void Subscribe(Spine.AnimationState state, Spine.EventData eventDataReference, bool unsubscribe) { if (state == null || eventDataReference == null) { m_WasFired = true; } else { m_AnimationState = state; m_TargetEvent = eventDataReference; state.Event += HandleAnimationStateEvent; m_unsubscribeAfterFiring = unsubscribe; } }
static int FindEvent(IntPtr L) { try { ToLua.CheckArgsCount(L, 2); Spine.SkeletonData obj = (Spine.SkeletonData)ToLua.CheckObject <Spine.SkeletonData>(L, 1); string arg0 = ToLua.CheckString(L, 2); Spine.EventData o = obj.FindEvent(arg0); ToLua.PushObject(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int get_Data(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); Spine.Event obj = (Spine.Event)o; Spine.EventData ret = obj.Data; ToLua.PushObject(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o, "attempt to index Data on a nil value")); } }
void Subscribe(Spine.AnimationState state, Spine.EventData eventDataReference, bool unsubscribe) { if (state == null) { Debug.LogWarning("AnimationState argument was null. Coroutine will continue immediately."); m_WasFired = true; return; } else if (eventDataReference == null) { Debug.LogWarning("eventDataReference argument was null. Coroutine will continue immediately."); m_WasFired = true; return; } m_AnimationState = state; m_TargetEvent = eventDataReference; state.Event += HandleAnimationStateEvent; m_unsubscribeAfterFiring = unsubscribe; }
void Subscribe (Spine.AnimationState state, Spine.EventData eventDataReference, bool unsubscribe) { #if PREUNITY_5_3 Debug.LogWarning("Unity 5.3 or later is required for Spine Unity custom yield instructions to function correctly."); #endif if (state == null) { Debug.LogWarning("AnimationState argument was null. Coroutine will continue immediately."); m_WasFired = true; return; } else if (eventDataReference == null) { Debug.LogWarning("eventDataReference argument was null. Coroutine will continue immediately."); m_WasFired = true; return; } m_AnimationState = state; m_TargetEvent = eventDataReference; state.Event += HandleAnimationStateEvent; m_unsubscribeAfterFiring = unsubscribe; }
static int _CreateSpine_Event(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 2) { float arg0 = (float)LuaDLL.luaL_checknumber(L, 1); Spine.EventData arg1 = (Spine.EventData)ToLua.CheckObject <Spine.EventData>(L, 2); Spine.Event obj = new Spine.Event(arg0, arg1); ToLua.PushObject(L, obj); return(1); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to ctor method: Spine.Event.New")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public SkeletonData ReadSkeletonData (TextReader reader) { if (reader == null) throw new ArgumentNullException("reader", "reader cannot be null."); var scale = this.Scale; var skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary<String, Object>; if (root == null) throw new Exception("Invalid JSON."); // Skeleton. if (root.ContainsKey("skeleton")) { var skeletonMap = (Dictionary<String, Object>)root["skeleton"]; skeletonData.hash = (String)skeletonMap["hash"]; skeletonData.version = (String)skeletonMap["spine"]; skeletonData.width = GetFloat(skeletonMap, "width", 0); skeletonData.height = GetFloat(skeletonMap, "height", 0); skeletonData.fps = GetFloat(skeletonMap, "fps", 0); skeletonData.imagesPath = GetString(skeletonMap, "images", null); } // Bones. foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) throw new Exception("Parent bone not found: " + boneMap["parent"]); } var data = new BoneData(skeletonData.Bones.Count, (String)boneMap["name"], parent); data.length = GetFloat(boneMap, "length", 0) * scale; data.x = GetFloat(boneMap, "x", 0) * scale; data.y = GetFloat(boneMap, "y", 0) * scale; data.rotation = GetFloat(boneMap, "rotation", 0); data.scaleX = GetFloat(boneMap, "scaleX", 1); data.scaleY = GetFloat(boneMap, "scaleY", 1); data.shearX = GetFloat(boneMap, "shearX", 0); data.shearY = GetFloat(boneMap, "shearY", 0); string tm = GetString(boneMap, "transform", TransformMode.Normal.ToString()); data.transformMode = (TransformMode)Enum.Parse(typeof(TransformMode), tm, true); skeletonData.bones.Add(data); } // Slots. if (root.ContainsKey("slots")) { foreach (Dictionary<String, Object> slotMap in (List<Object>)root["slots"]) { var slotName = (String)slotMap["name"]; var boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) throw new Exception("Slot bone not found: " + boneName); var data = new SlotData(skeletonData.Slots.Count, slotName, boneData); if (slotMap.ContainsKey("color")) { var color = (String)slotMap["color"]; data.r = ToColor(color, 0); data.g = ToColor(color, 1); data.b = ToColor(color, 2); data.a = ToColor(color, 3); } data.attachmentName = GetString(slotMap, "attachment", null); if (slotMap.ContainsKey("blend")) data.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (String)slotMap["blend"], false); else data.blendMode = BlendMode.normal; skeletonData.slots.Add(data); } } // IK constraints. if (root.ContainsKey("ik")) { foreach (Dictionary<String, Object> constraintMap in (List<Object>)root["ik"]) { IkConstraintData data = new IkConstraintData((String)constraintMap["name"]); data.order = GetInt(constraintMap, "order", 0); foreach (String boneName in (List<Object>)constraintMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) throw new Exception("IK constraint bone not found: " + boneName); data.bones.Add(bone); } String targetName = (String)constraintMap["target"]; data.target = skeletonData.FindBone(targetName); if (data.target == null) throw new Exception("Target bone not found: " + targetName); data.bendDirection = GetBoolean(constraintMap, "bendPositive", true) ? 1 : -1; data.mix = GetFloat(constraintMap, "mix", 1); skeletonData.ikConstraints.Add(data); } } // Transform constraints. if (root.ContainsKey("transform")) { foreach (Dictionary<String, Object> constraintMap in (List<Object>)root["transform"]) { TransformConstraintData data = new TransformConstraintData((String)constraintMap["name"]); data.order = GetInt(constraintMap, "order", 0); foreach (String boneName in (List<Object>)constraintMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) throw new Exception("Transform constraint bone not found: " + boneName); data.bones.Add(bone); } String targetName = (String)constraintMap["target"]; data.target = skeletonData.FindBone(targetName); if (data.target == null) throw new Exception("Target bone not found: " + targetName); data.offsetRotation = GetFloat(constraintMap, "rotation", 0); data.offsetX = GetFloat(constraintMap, "x", 0) * scale; data.offsetY = GetFloat(constraintMap, "y", 0) * scale; data.offsetScaleX = GetFloat(constraintMap, "scaleX", 0); data.offsetScaleY = GetFloat(constraintMap, "scaleY", 0); data.offsetShearY = GetFloat(constraintMap, "shearY", 0); data.rotateMix = GetFloat(constraintMap, "rotateMix", 1); data.translateMix = GetFloat(constraintMap, "translateMix", 1); data.scaleMix = GetFloat(constraintMap, "scaleMix", 1); data.shearMix = GetFloat(constraintMap, "shearMix", 1); skeletonData.transformConstraints.Add(data); } } // Path constraints. if(root.ContainsKey("path")) { foreach (Dictionary<String, Object> constraintMap in (List<Object>)root["path"]) { PathConstraintData data = new PathConstraintData((String)constraintMap["name"]); data.order = GetInt(constraintMap, "order", 0); foreach (String boneName in (List<Object>)constraintMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) throw new Exception("Path bone not found: " + boneName); data.bones.Add(bone); } String targetName = (String)constraintMap["target"]; data.target = skeletonData.FindSlot(targetName); if (data.target == null) throw new Exception("Target slot not found: " + targetName); data.positionMode = (PositionMode)Enum.Parse(typeof(PositionMode), GetString(constraintMap, "positionMode", "percent"), true); data.spacingMode = (SpacingMode)Enum.Parse(typeof(SpacingMode), GetString(constraintMap, "spacingMode", "length"), true); data.rotateMode = (RotateMode)Enum.Parse(typeof(RotateMode), GetString(constraintMap, "rotateMode", "tangent"), true); data.offsetRotation = GetFloat(constraintMap, "rotation", 0); data.position = GetFloat(constraintMap, "position", 0); if (data.positionMode == PositionMode.Fixed) data.position *= scale; data.spacing = GetFloat(constraintMap, "spacing", 0); if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale; data.rotateMix = GetFloat(constraintMap, "rotateMix", 1); data.translateMix = GetFloat(constraintMap, "translateMix", 1); skeletonData.pathConstraints.Add(data); } } // Skins. if (root.ContainsKey("skins")) { foreach (KeyValuePair<String, Object> skinMap in (Dictionary<String, Object>)root["skins"]) { var skin = new Skin(skinMap.Key); foreach (KeyValuePair<String, Object> slotEntry in (Dictionary<String, Object>)skinMap.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair<String, Object> entry in ((Dictionary<String, Object>)slotEntry.Value)) { try { Attachment attachment = ReadAttachment((Dictionary<String, Object>)entry.Value, skin, slotIndex, entry.Key); if (attachment != null) skin.AddAttachment(slotIndex, entry.Key, attachment); } catch (Exception e) { throw new Exception("Error reading attachment: " + entry.Key + ", skin: " + skin, e); } } } skeletonData.skins.Add(skin); if (skin.name == "default") skeletonData.defaultSkin = skin; } } // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) throw new Exception("Slot not found: " + linkedMesh.skin); Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent); linkedMesh.mesh.ParentMesh = (MeshAttachment)parent; linkedMesh.mesh.UpdateUVs(); } linkedMeshes.Clear(); // Events. if (root.ContainsKey("events")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["events"]) { var entryMap = (Dictionary<String, Object>)entry.Value; var data = new EventData(entry.Key); data.Int = GetInt(entryMap, "int", 0); data.Float = GetFloat(entryMap, "float", 0); data.String = GetString(entryMap, "string", string.Empty); skeletonData.events.Add(data); } } // Animations. if (root.ContainsKey("animations")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["animations"]) { try { ReadAnimation((Dictionary<String, Object>)entry.Value, entry.Key, skeletonData); } catch (Exception e) { throw new Exception("Error reading animation: " + entry.Key, e); } } } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return skeletonData; }
public SkeletonData ReadSkeletonData (TextReader reader) { if (reader == null) throw new ArgumentNullException("reader cannot be null."); SkeletonData skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary<String, Object>; if (root == null) throw new Exception("Invalid JSON."); // Bones. foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) throw new Exception("Parent bone not found: " + boneMap["parent"]); } BoneData boneData = new BoneData((String)boneMap["name"], parent); boneData.length = GetFloat(boneMap, "length", 0) * Scale; boneData.x = GetFloat(boneMap, "x", 0) * Scale; boneData.y = GetFloat(boneMap, "y", 0) * Scale; boneData.rotation = GetFloat(boneMap, "rotation", 0); boneData.scaleX = GetFloat(boneMap, "scaleX", 1); boneData.scaleY = GetFloat(boneMap, "scaleY", 1); boneData.inheritScale = GetBoolean(boneMap, "inheritScale", true); boneData.inheritRotation = GetBoolean(boneMap, "inheritRotation", true); skeletonData.AddBone(boneData); } // Slots. if (root.ContainsKey("slots")) { foreach (Dictionary<String, Object> slotMap in (List<Object>)root["slots"]) { String slotName = (String)slotMap["name"]; String boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) throw new Exception("Slot bone not found: " + boneName); SlotData slotData = new SlotData(slotName, boneData); if (slotMap.ContainsKey("color")) { String color = (String)slotMap["color"]; slotData.r = ToColor(color, 0); slotData.g = ToColor(color, 1); slotData.b = ToColor(color, 2); slotData.a = ToColor(color, 3); } if (slotMap.ContainsKey("attachment")) slotData.attachmentName = (String)slotMap["attachment"]; if (slotMap.ContainsKey("additive")) slotData.additiveBlending = (bool)slotMap["additive"]; skeletonData.AddSlot(slotData); } } // Skins. if (root.ContainsKey("skins")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["skins"]) { Skin skin = new Skin(entry.Key); foreach (KeyValuePair<String, Object> slotEntry in (Dictionary<String, Object>)entry.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair<String, Object> attachmentEntry in ((Dictionary<String, Object>)slotEntry.Value)) { Attachment attachment = ReadAttachment(skin, attachmentEntry.Key, (Dictionary<String, Object>)attachmentEntry.Value); if (attachment != null) skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment); } } skeletonData.AddSkin(skin); if (skin.name == "default") skeletonData.defaultSkin = skin; } } // Events. if (root.ContainsKey("events")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["events"]) { var entryMap = (Dictionary<String, Object>)entry.Value; EventData eventData = new EventData(entry.Key); eventData.Int = GetInt(entryMap, "int", 0); eventData.Float = GetFloat(entryMap, "float", 0); eventData.String = GetString(entryMap, "string", null); skeletonData.AddEvent(eventData); } } // Animations. if (root.ContainsKey("animations")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["animations"]) ReadAnimation(entry.Key, (Dictionary<String, Object>)entry.Value, skeletonData); } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.animations.TrimExcess(); return skeletonData; }
public SkeletonData ReadSkeletonData (Stream input) { if (input == null) throw new ArgumentNullException("input cannot be null."); float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) skeletonData.hash = null; skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) skeletonData.version = null; skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) skeletonData.imagesPath = null; } // Bones. for (int i = 0, n = ReadInt(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = null; int parentIndex = ReadInt(input, true) - 1; if (parentIndex != -1) parent = skeletonData.bones.Items[parentIndex]; BoneData boneData = new BoneData(name, parent); boneData.x = ReadFloat(input) * scale; boneData.y = ReadFloat(input) * scale; boneData.scaleX = ReadFloat(input); boneData.scaleY = ReadFloat(input); boneData.rotation = ReadFloat(input); boneData.length = ReadFloat(input) * scale; boneData.flipX = ReadBoolean(input); boneData.flipY = ReadBoolean(input); boneData.inheritScale = ReadBoolean(input); boneData.inheritRotation = ReadBoolean(input); if (nonessential) ReadInt(input); // Skip bone color. skeletonData.bones.Add(boneData); } // IK constraints. for (int i = 0, n = ReadInt(input, true); i < n; i++) { IkConstraintData ikConstraintData = new IkConstraintData(ReadString(input)); for (int ii = 0, nn = ReadInt(input, true); ii < nn; ii++) ikConstraintData.bones.Add(skeletonData.bones.Items[ReadInt(input, true)]); ikConstraintData.target = skeletonData.bones.Items[ReadInt(input, true)]; ikConstraintData.mix = ReadFloat(input); ikConstraintData.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(ikConstraintData); } // Slots. for (int i = 0, n = ReadInt(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones.Items[ReadInt(input, true)]; SlotData slotData = new SlotData(slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadInt(input, true); skeletonData.slots.Add(slotData); } // Default skin. Skin defaultSkin = ReadSkin(input, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadInt(input, true); i < n; i++) skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential)); // Events. for (int i = 0, n = ReadInt(input, true); i < n; i++) { EventData eventData = new EventData(ReadString(input)); eventData.Int = ReadInt(input, false); eventData.Float = ReadFloat(input); eventData.String = ReadString(input); skeletonData.events.Add(eventData); } // Animations. for (int i = 0, n = ReadInt(input, true); i < n; i++) ReadAnimation(ReadString(input), input, skeletonData); skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return skeletonData; }
public Event(EventData data) { Data = data; }
public Event (float time, EventData data) { Time = time; Data = data; }
public SkeletonData ReadSkeletonData(Stream input) { if (input == null) { throw new ArgumentNullException("input"); } float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) { skeletonData.hash = null; } skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) { skeletonData.version = null; } skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.fps = ReadFloat(input); skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) { skeletonData.imagesPath = null; } } // Bones. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)]; BoneData data = new BoneData(i, name, parent); data.rotation = ReadFloat(input); data.x = ReadFloat(input) * scale; data.y = ReadFloat(input) * scale; data.scaleX = ReadFloat(input); data.scaleY = ReadFloat(input); data.shearX = ReadFloat(input); data.shearY = ReadFloat(input); data.length = ReadFloat(input) * scale; data.transformMode = TransformModeValues[ReadVarint(input, true)]; if (nonessential) { ReadInt(input); // Skip bone color. } skeletonData.bones.Add(data); } // Slots. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)]; SlotData slotData = new SlotData(i, slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; int darkColor = ReadInt(input); // 0x00rrggbb if (darkColor != -1) { slotData.hasSecondColor = true; slotData.r2 = ((darkColor & 0x00ff0000) >> 16) / 255f; slotData.g2 = ((darkColor & 0x0000ff00) >> 8) / 255f; slotData.b2 = ((darkColor & 0x000000ff)) / 255f; } slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadVarint(input, true); skeletonData.slots.Add(slotData); } // IK constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { IkConstraintData data = new IkConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } data.target = skeletonData.bones.Items[ReadVarint(input, true)]; data.mix = ReadFloat(input); data.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(data); } // Transform constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { TransformConstraintData data = new TransformConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } data.target = skeletonData.bones.Items[ReadVarint(input, true)]; data.local = ReadBoolean(input); data.relative = ReadBoolean(input); data.offsetRotation = ReadFloat(input); data.offsetX = ReadFloat(input) * scale; data.offsetY = ReadFloat(input) * scale; data.offsetScaleX = ReadFloat(input); data.offsetScaleY = ReadFloat(input); data.offsetShearY = ReadFloat(input); data.rotateMix = ReadFloat(input); data.translateMix = ReadFloat(input); data.scaleMix = ReadFloat(input); data.shearMix = ReadFloat(input); skeletonData.transformConstraints.Add(data); } // Path constraints for (int i = 0, n = ReadVarint(input, true); i < n; i++) { PathConstraintData data = new PathConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } data.target = skeletonData.slots.Items[ReadVarint(input, true)]; data.positionMode = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(ReadVarint(input, true)); data.spacingMode = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue(ReadVarint(input, true)); data.rotateMode = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue(ReadVarint(input, true)); data.offsetRotation = ReadFloat(input); data.position = ReadFloat(input); if (data.positionMode == PositionMode.Fixed) { data.position *= scale; } data.spacing = ReadFloat(input); if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) { data.spacing *= scale; } data.rotateMix = ReadFloat(input); data.translateMix = ReadFloat(input); skeletonData.pathConstraints.Add(data); } // Default skin. Skin defaultSkin = ReadSkin(input, skeletonData, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { skeletonData.skins.Add(ReadSkin(input, skeletonData, ReadString(input), nonessential)); } // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) { throw new Exception("Skin not found: " + linkedMesh.skin); } Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) { throw new Exception("Parent mesh not found: " + linkedMesh.parent); } linkedMesh.mesh.ParentMesh = (MeshAttachment)parent; linkedMesh.mesh.UpdateUVs(); } linkedMeshes.Clear(); // Events. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { EventData data = new EventData(ReadString(input)); data.Int = ReadVarint(input, false); data.Float = ReadFloat(input); data.String = ReadString(input); skeletonData.events.Add(data); } // Animations. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { ReadAnimation(ReadString(input), input, skeletonData); } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); skeletonData.pathConstraints.TrimExcess(); return(skeletonData); }
// --- Events. public void AddEvent(EventData eventData) { if (eventData == null) throw new ArgumentNullException("eventData cannot be null."); events.Add(eventData); }
public WaitForSpineEvent(SkeletonAnimation skeletonAnimation, Spine.EventData eventDataReference, bool unsubscribeAfterFiring = true) { // If skeletonAnimation is invalid, its state will be null. Subscribe handles null states just fine. Subscribe(skeletonAnimation.state, eventDataReference, unsubscribeAfterFiring); }
public WaitForSpineEvent(Spine.AnimationState state, Spine.EventData eventDataReference, bool unsubscribeAfterFiring = true) { Subscribe(state, eventDataReference, unsubscribeAfterFiring); }
public Event(float time, EventData data) { Data = data; }
public SkeletonData ReadSkeletonData (Stream input) { if (input == null) throw new ArgumentNullException("input"); float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) skeletonData.hash = null; skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) skeletonData.version = null; skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.fps = ReadFloat(input); skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) skeletonData.imagesPath = null; } // Bones. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)]; BoneData data = new BoneData(i, name, parent); data.rotation = ReadFloat(input); data.x = ReadFloat(input) * scale; data.y = ReadFloat(input) * scale; data.scaleX = ReadFloat(input); data.scaleY = ReadFloat(input); data.shearX = ReadFloat(input); data.shearY = ReadFloat(input); data.length = ReadFloat(input) * scale; data.transformMode = TransformModeValues[ReadVarint(input, true)]; if (nonessential) ReadInt(input); // Skip bone color. skeletonData.bones.Add(data); } // Slots. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)]; SlotData slotData = new SlotData(i, slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadVarint(input, true); skeletonData.slots.Add(slotData); } // IK constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { IkConstraintData data = new IkConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); data.target = skeletonData.bones.Items[ReadVarint(input, true)]; data.mix = ReadFloat(input); data.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(data); } // Transform constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { TransformConstraintData data = new TransformConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); data.target = skeletonData.bones.Items[ReadVarint(input, true)]; data.offsetRotation = ReadFloat(input); data.offsetX = ReadFloat(input) * scale; data.offsetY = ReadFloat(input) * scale; data.offsetScaleX = ReadFloat(input); data.offsetScaleY = ReadFloat(input); data.offsetShearY = ReadFloat(input); data.rotateMix = ReadFloat(input); data.translateMix = ReadFloat(input); data.scaleMix = ReadFloat(input); data.shearMix = ReadFloat(input); skeletonData.transformConstraints.Add(data); } // Path constraints for (int i = 0, n = ReadVarint(input, true); i < n; i++) { PathConstraintData data = new PathConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); data.target = skeletonData.slots.Items[ReadVarint(input, true)]; data.positionMode = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(ReadVarint(input, true)); data.spacingMode = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue(ReadVarint(input, true)); data.rotateMode = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue(ReadVarint(input, true)); data.offsetRotation = ReadFloat(input); data.position = ReadFloat(input); if (data.positionMode == PositionMode.Fixed) data.position *= scale; data.spacing = ReadFloat(input); if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale; data.rotateMix = ReadFloat(input); data.translateMix = ReadFloat(input); skeletonData.pathConstraints.Add(data); } // Default skin. Skin defaultSkin = ReadSkin(input, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadVarint(input, true); i < n; i++) skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential)); // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) throw new Exception("Skin not found: " + linkedMesh.skin); Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent); linkedMesh.mesh.ParentMesh = (MeshAttachment)parent; linkedMesh.mesh.UpdateUVs(); } linkedMeshes.Clear(); // Events. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { EventData data = new EventData(ReadString(input)); data.Int = ReadVarint(input, false); data.Float = ReadFloat(input); data.String = ReadString(input); skeletonData.events.Add(data); } // Animations. for (int i = 0, n = ReadVarint(input, true); i < n; i++) ReadAnimation(ReadString(input), input, skeletonData); skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); skeletonData.pathConstraints.TrimExcess(); return skeletonData; }
protected override void DoUpdate() { base.DoUpdate(); if (_skAnima == null || _skAniData == null || _skAnima.state == null) { return; } if (_skAnima.state.ToString() == "<none>" || _curEventList == null) { return; } SetAlpha(CurAlpha); var track = _skAnima.state.GetCurrent(0); _curFrameCount = Mathf.FloorToInt(track.TrackTime * 30f); if ((_skAnima.loop && track.TrackTime > track.Animation.Duration) || _needInit) { // 若循环播放,则播完一轮后,事件重新加入队列 track.TrackTime = 0; _curEventList.Clear(); //@la 有问题这里改一下 SpineAniController.AniEventData _curPlayAniData = new SpineAniController.AniEventData(); var e = _skAniData.GetEnumerator(); while (e.MoveNext()) { if (e.Current.Name == _curAniName) { _curPlayAniData = e.Current; } } if (_curPlayAniData.EventList != null && _curPlayAniData.EventList.Count != 0) { for (int index = 0; index < _curPlayAniData.EventList.Count; ++index) { _curEventList.Add(_curPlayAniData.EventList[index]); } } _needInit = false; if (_curFrameCount == 0) { return; } } if (_curEventList.Count == 0) { return; } /*for (int i = 0; i < _curEventList.Count; i++) * { * var curTickEvent = _curEventList[i]; * if (curTickEvent.Check(_curFrameCount)) * { * _curEventList[i] = curTickEvent; * * Spine.EventData eData = new Spine.EventData(curTickEvent.DataName); * eData.String = curTickEvent.EventString; * Spine.Event e = new Spine.Event(0f, eData); * e.String = curTickEvent.EventString; * if (_forUI && curTickEvent.DataName == "PlayAudio") * { * // ignore * } * else * Event(_curAniName, e); * } * }*/ _EventEnumerator = _curEventList.GetEnumerator(); while (_EventEnumerator.MoveNext()) { _curTickEvent = _EventEnumerator.Current; if (_curTickEvent.Check(_curFrameCount)) { if (Event != null) { Spine.EventData eData = new Spine.EventData(_curTickEvent.DataName); eData.String = _curTickEvent.EventString; Spine.Event e = new Spine.Event(0f, eData); e.String = _curTickEvent.EventString; if (_forUI && _curTickEvent.DataName == "PlayAudio") { // ignore } else { Event(_curAniName, e); } } _curEventList.Remove(_EventEnumerator.Current); _EventEnumerator = _curEventList.GetEnumerator(); } } }
public Event (float time, EventData data) { if (data == null) throw new ArgumentNullException("data", "data cannot be null."); Time = time; Data = data; }
private void ReadAnimation(String name, Stream input, SkeletonData skeletonData) { var timelines = new ExposedList <Timeline>(); float scale = Scale; float duration = 0; // Slot timelines. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { int slotIndex = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { int timelineType = input.ReadByte(); int frameCount = ReadVarint(input, true); switch (timelineType) { case SLOT_ATTACHMENT: { AttachmentTimeline timeline = new AttachmentTimeline(frameCount); timeline.slotIndex = slotIndex; for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) { timeline.SetFrame(frameIndex, ReadFloat(input), ReadString(input)); } timelines.Add(timeline); duration = Math.Max(duration, timeline.frames[frameCount - 1]); break; } case SLOT_COLOR: { ColorTimeline timeline = new ColorTimeline(frameCount); timeline.slotIndex = slotIndex; for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) { float time = ReadFloat(input); int color = ReadInt(input); float r = ((color & 0xff000000) >> 24) / 255f; float g = ((color & 0x00ff0000) >> 16) / 255f; float b = ((color & 0x0000ff00) >> 8) / 255f; float a = ((color & 0x000000ff)) / 255f; timeline.SetFrame(frameIndex, time, r, g, b, a); if (frameIndex < frameCount - 1) { ReadCurve(input, frameIndex, timeline); } } timelines.Add(timeline); duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * ColorTimeline.ENTRIES]); break; } case SLOT_TWO_COLOR: { TwoColorTimeline timeline = new TwoColorTimeline(frameCount); timeline.slotIndex = slotIndex; for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) { float time = ReadFloat(input); int color = ReadInt(input); float r = ((color & 0xff000000) >> 24) / 255f; float g = ((color & 0x00ff0000) >> 16) / 255f; float b = ((color & 0x0000ff00) >> 8) / 255f; float a = ((color & 0x000000ff)) / 255f; int color2 = ReadInt(input); // 0x00rrggbb float r2 = ((color2 & 0x00ff0000) >> 16) / 255f; float g2 = ((color2 & 0x0000ff00) >> 8) / 255f; float b2 = ((color2 & 0x000000ff)) / 255f; timeline.SetFrame(frameIndex, time, r, g, b, a, r2, g2, b2); if (frameIndex < frameCount - 1) { ReadCurve(input, frameIndex, timeline); } } timelines.Add(timeline); duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * TwoColorTimeline.ENTRIES]); break; } } } } // Bone timelines. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { int boneIndex = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { int timelineType = input.ReadByte(); int frameCount = ReadVarint(input, true); switch (timelineType) { case BONE_ROTATE: { RotateTimeline timeline = new RotateTimeline(frameCount); timeline.boneIndex = boneIndex; for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) { timeline.SetFrame(frameIndex, ReadFloat(input), ReadFloat(input)); if (frameIndex < frameCount - 1) { ReadCurve(input, frameIndex, timeline); } } timelines.Add(timeline); duration = Math.Max(duration, timeline.frames[(frameCount - 1) * RotateTimeline.ENTRIES]); break; } case BONE_TRANSLATE: case BONE_SCALE: case BONE_SHEAR: { TranslateTimeline timeline; float timelineScale = 1; if (timelineType == BONE_SCALE) { timeline = new ScaleTimeline(frameCount); } else if (timelineType == BONE_SHEAR) { timeline = new ShearTimeline(frameCount); } else { timeline = new TranslateTimeline(frameCount); timelineScale = scale; } timeline.boneIndex = boneIndex; for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) { timeline.SetFrame(frameIndex, ReadFloat(input), ReadFloat(input) * timelineScale, ReadFloat(input) * timelineScale); if (frameIndex < frameCount - 1) { ReadCurve(input, frameIndex, timeline); } } timelines.Add(timeline); duration = Math.Max(duration, timeline.frames[(frameCount - 1) * TranslateTimeline.ENTRIES]); break; } } } } // IK timelines. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { int index = ReadVarint(input, true); int frameCount = ReadVarint(input, true); IkConstraintTimeline timeline = new IkConstraintTimeline(frameCount); timeline.ikConstraintIndex = index; for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) { timeline.SetFrame(frameIndex, ReadFloat(input), ReadFloat(input), ReadSByte(input)); if (frameIndex < frameCount - 1) { ReadCurve(input, frameIndex, timeline); } } timelines.Add(timeline); duration = Math.Max(duration, timeline.frames[(frameCount - 1) * IkConstraintTimeline.ENTRIES]); } // Transform constraint timelines. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { int index = ReadVarint(input, true); int frameCount = ReadVarint(input, true); TransformConstraintTimeline timeline = new TransformConstraintTimeline(frameCount); timeline.transformConstraintIndex = index; for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) { timeline.SetFrame(frameIndex, ReadFloat(input), ReadFloat(input), ReadFloat(input), ReadFloat(input), ReadFloat(input)); if (frameIndex < frameCount - 1) { ReadCurve(input, frameIndex, timeline); } } timelines.Add(timeline); duration = Math.Max(duration, timeline.frames[(frameCount - 1) * TransformConstraintTimeline.ENTRIES]); } // Path constraint timelines. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { int index = ReadVarint(input, true); PathConstraintData data = skeletonData.pathConstraints.Items[index]; for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { int timelineType = ReadSByte(input); int frameCount = ReadVarint(input, true); switch (timelineType) { case PATH_POSITION: case PATH_SPACING: { PathConstraintPositionTimeline timeline; float timelineScale = 1; if (timelineType == PATH_SPACING) { timeline = new PathConstraintSpacingTimeline(frameCount); if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) { timelineScale = scale; } } else { timeline = new PathConstraintPositionTimeline(frameCount); if (data.positionMode == PositionMode.Fixed) { timelineScale = scale; } } timeline.pathConstraintIndex = index; for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) { timeline.SetFrame(frameIndex, ReadFloat(input), ReadFloat(input) * timelineScale); if (frameIndex < frameCount - 1) { ReadCurve(input, frameIndex, timeline); } } timelines.Add(timeline); duration = Math.Max(duration, timeline.frames[(frameCount - 1) * PathConstraintPositionTimeline.ENTRIES]); break; } case PATH_MIX: { PathConstraintMixTimeline timeline = new PathConstraintMixTimeline(frameCount); timeline.pathConstraintIndex = index; for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) { timeline.SetFrame(frameIndex, ReadFloat(input), ReadFloat(input), ReadFloat(input)); if (frameIndex < frameCount - 1) { ReadCurve(input, frameIndex, timeline); } } timelines.Add(timeline); duration = Math.Max(duration, timeline.frames[(frameCount - 1) * PathConstraintMixTimeline.ENTRIES]); break; } } } } // Deform timelines. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { Skin skin = skeletonData.skins.Items[ReadVarint(input, true)]; for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { int slotIndex = ReadVarint(input, true); for (int iii = 0, nnn = ReadVarint(input, true); iii < nnn; iii++) { VertexAttachment attachment = (VertexAttachment)skin.GetAttachment(slotIndex, ReadString(input)); bool weighted = attachment.bones != null; float[] vertices = attachment.vertices; int deformLength = weighted ? vertices.Length / 3 * 2 : vertices.Length; int frameCount = ReadVarint(input, true); DeformTimeline timeline = new DeformTimeline(frameCount); timeline.slotIndex = slotIndex; timeline.attachment = attachment; for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) { float time = ReadFloat(input); float[] deform; int end = ReadVarint(input, true); if (end == 0) { deform = weighted ? new float[deformLength] : vertices; } else { deform = new float[deformLength]; int start = ReadVarint(input, true); end += start; if (scale == 1) { for (int v = start; v < end; v++) { deform[v] = ReadFloat(input); } } else { for (int v = start; v < end; v++) { deform[v] = ReadFloat(input) * scale; } } if (!weighted) { for (int v = 0, vn = deform.Length; v < vn; v++) { deform[v] += vertices[v]; } } } timeline.SetFrame(frameIndex, time, deform); if (frameIndex < frameCount - 1) { ReadCurve(input, frameIndex, timeline); } } timelines.Add(timeline); duration = Math.Max(duration, timeline.frames[frameCount - 1]); } } } // Draw order timeline. int drawOrderCount = ReadVarint(input, true); if (drawOrderCount > 0) { DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrderCount); int slotCount = skeletonData.slots.Count; for (int i = 0; i < drawOrderCount; i++) { float time = ReadFloat(input); int offsetCount = ReadVarint(input, true); int[] drawOrder = new int[slotCount]; for (int ii = slotCount - 1; ii >= 0; ii--) { drawOrder[ii] = -1; } int[] unchanged = new int[slotCount - offsetCount]; int originalIndex = 0, unchangedIndex = 0; for (int ii = 0; ii < offsetCount; ii++) { int slotIndex = ReadVarint(input, true); // Collect unchanged items. while (originalIndex != slotIndex) { unchanged[unchangedIndex++] = originalIndex++; } // Set changed items. drawOrder[originalIndex + ReadVarint(input, true)] = originalIndex++; } // Collect remaining unchanged items. while (originalIndex < slotCount) { unchanged[unchangedIndex++] = originalIndex++; } // Fill in unchanged items. for (int ii = slotCount - 1; ii >= 0; ii--) { if (drawOrder[ii] == -1) { drawOrder[ii] = unchanged[--unchangedIndex]; } } timeline.SetFrame(i, time, drawOrder); } timelines.Add(timeline); duration = Math.Max(duration, timeline.frames[drawOrderCount - 1]); } // Event timeline. int eventCount = ReadVarint(input, true); if (eventCount > 0) { EventTimeline timeline = new EventTimeline(eventCount); for (int i = 0; i < eventCount; i++) { float time = ReadFloat(input); EventData eventData = skeletonData.events.Items[ReadVarint(input, true)]; Event e = new Event(time, eventData); e.Int = ReadVarint(input, false); e.Float = ReadFloat(input); e.String = ReadBoolean(input) ? ReadString(input) : eventData.String; timeline.SetFrame(i, e); } timelines.Add(timeline); duration = Math.Max(duration, timeline.frames[eventCount - 1]); } timelines.TrimExcess(); skeletonData.animations.Add(new Animation(name, timelines, duration)); }
public SkeletonData ReadSkeletonData (TextReader reader) { if (reader == null) throw new ArgumentNullException("reader cannot be null."); var skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary<String, Object>; if (root == null) throw new Exception("Invalid JSON."); // Skeleton. if (root.ContainsKey("skeleton")) { var skeletonMap = (Dictionary<String, Object>)root["skeleton"]; skeletonData.hash = (String)skeletonMap["hash"]; skeletonData.version = (String)skeletonMap["spine"]; skeletonData.width = GetFloat(skeletonMap, "width", 0); skeletonData.height = GetFloat(skeletonMap, "height", 0); } // Bones. foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) throw new Exception("Parent bone not found: " + boneMap["parent"]); } var boneData = new BoneData((String)boneMap["name"], parent); boneData.length = GetFloat(boneMap, "length", 0) * Scale; boneData.x = GetFloat(boneMap, "x", 0) * Scale; boneData.y = GetFloat(boneMap, "y", 0) * Scale; boneData.rotation = GetFloat(boneMap, "rotation", 0); boneData.scaleX = GetFloat(boneMap, "scaleX", 1); boneData.scaleY = GetFloat(boneMap, "scaleY", 1); boneData.inheritScale = GetBoolean(boneMap, "inheritScale", true); boneData.inheritRotation = GetBoolean(boneMap, "inheritRotation", true); skeletonData.bones.Add(boneData); } // IK constraints. if (root.ContainsKey("ik")) { foreach (Dictionary<String, Object> ikMap in (List<Object>)root["ik"]) { IkConstraintData ikConstraintData = new IkConstraintData((String)ikMap["name"]); foreach (String boneName in (List<Object>)ikMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) throw new Exception("IK bone not found: " + boneName); ikConstraintData.bones.Add(bone); } String targetName = (String)ikMap["target"]; ikConstraintData.target = skeletonData.FindBone(targetName); if (ikConstraintData.target == null) throw new Exception("Target bone not found: " + targetName); ikConstraintData.bendDirection = GetBoolean(ikMap, "bendPositive", true) ? 1 : -1; ikConstraintData.mix = GetFloat(ikMap, "mix", 1); skeletonData.ikConstraints.Add(ikConstraintData); } } // Transform constraints. if (root.ContainsKey("transform")) { foreach (Dictionary<String, Object> transformMap in (List<Object>)root["ik"]) { TransformConstraintData transformConstraintData = new TransformConstraintData((String)transformMap["name"]); String boneName = (String)transformMap["bone"]; transformConstraintData.target = skeletonData.FindBone(boneName); if (transformConstraintData.target == null) throw new Exception("Bone not found: " + boneName); String targetName = (String)transformMap["target"]; transformConstraintData.target = skeletonData.FindBone(targetName); if (transformConstraintData.target == null) throw new Exception("Target bone not found: " + targetName); transformConstraintData.translateMix = GetFloat(transformMap, "mix", 1); transformConstraintData.x = GetFloat(transformMap, "x", 0); transformConstraintData.y = GetFloat(transformMap, "y", 0); skeletonData.transformConstraints.Add(transformConstraintData); } } // Slots. if (root.ContainsKey("slots")) { foreach (Dictionary<String, Object> slotMap in (List<Object>)root["slots"]) { var slotName = (String)slotMap["name"]; var boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) throw new Exception("Slot bone not found: " + boneName); var slotData = new SlotData(slotName, boneData); if (slotMap.ContainsKey("color")) { var color = (String)slotMap["color"]; slotData.r = ToColor(color, 0); slotData.g = ToColor(color, 1); slotData.b = ToColor(color, 2); slotData.a = ToColor(color, 3); } if (slotMap.ContainsKey("attachment")) slotData.attachmentName = (String)slotMap["attachment"]; if (slotMap.ContainsKey("blend")) slotData.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (String)slotMap["blend"], false); else slotData.blendMode = BlendMode.normal; skeletonData.slots.Add(slotData); } } // Skins. if (root.ContainsKey("skins")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["skins"]) { var skin = new Skin(entry.Key); foreach (KeyValuePair<String, Object> slotEntry in (Dictionary<String, Object>)entry.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair<String, Object> attachmentEntry in ((Dictionary<String, Object>)slotEntry.Value)) { Attachment attachment = ReadAttachment(skin, attachmentEntry.Key, (Dictionary<String, Object>)attachmentEntry.Value); if (attachment != null) skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment); } } skeletonData.skins.Add(skin); if (skin.name == "default") skeletonData.defaultSkin = skin; } } // Events. if (root.ContainsKey("events")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["events"]) { var entryMap = (Dictionary<String, Object>)entry.Value; var eventData = new EventData(entry.Key); eventData.Int = GetInt(entryMap, "int", 0); eventData.Float = GetFloat(entryMap, "float", 0); eventData.String = GetString(entryMap, "string", null); skeletonData.events.Add(eventData); } } // Animations. if (root.ContainsKey("animations")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["animations"]) ReadAnimation(entry.Key, (Dictionary<String, Object>)entry.Value, skeletonData); } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return skeletonData; }
public SkeletonData ReadSkeletonData (Stream input) { if (input == null) throw new ArgumentNullException("input"); float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) skeletonData.hash = null; skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) skeletonData.version = null; skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) skeletonData.imagesPath = null; } // Bones. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)]; BoneData boneData = new BoneData(name, parent); boneData.x = ReadFloat(input) * scale; boneData.y = ReadFloat(input) * scale; boneData.scaleX = ReadFloat(input); boneData.scaleY = ReadFloat(input); boneData.rotation = ReadFloat(input); boneData.length = ReadFloat(input) * scale; boneData.inheritScale = ReadBoolean(input); boneData.inheritRotation = ReadBoolean(input); if (nonessential) ReadInt(input); // Skip bone color. skeletonData.bones.Add(boneData); } // IK constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { IkConstraintData ikConstraintData = new IkConstraintData(ReadString(input)); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) ikConstraintData.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); ikConstraintData.target = skeletonData.bones.Items[ReadVarint(input, true)]; ikConstraintData.mix = ReadFloat(input); ikConstraintData.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(ikConstraintData); } // Transform constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { TransformConstraintData transformConstraintData = new TransformConstraintData(ReadString(input)); transformConstraintData.bone = skeletonData.bones.Items[ReadVarint(input, true)]; transformConstraintData.target = skeletonData.bones.Items[ReadVarint(input, true)]; transformConstraintData.translateMix = ReadFloat(input); transformConstraintData.x = ReadFloat(input) * scale; transformConstraintData.y = ReadFloat(input) * scale; skeletonData.transformConstraints.Add(transformConstraintData); } // Slots. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)]; SlotData slotData = new SlotData(slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadVarint(input, true); skeletonData.slots.Add(slotData); } // Default skin. Skin defaultSkin = ReadSkin(input, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadVarint(input, true); i < n; i++) skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential)); // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) throw new Exception("Skin not found: " + linkedMesh.skin); Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent); if (linkedMesh.mesh is MeshAttachment) { MeshAttachment mesh = (MeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (MeshAttachment)parent; mesh.UpdateUVs(); } else { WeightedMeshAttachment mesh = (WeightedMeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (WeightedMeshAttachment)parent; mesh.UpdateUVs(); } } linkedMeshes.Clear(); // Events. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { EventData eventData = new EventData(ReadString(input)); eventData.Int = ReadVarint(input, false); eventData.Float = ReadFloat(input); eventData.String = ReadString(input); skeletonData.events.Add(eventData); } // Animations. for (int i = 0, n = ReadVarint(input, true); i < n; i++) ReadAnimation(ReadString(input), input, skeletonData); skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return skeletonData; }