From 9d4aff8da724df1879a446c6d1495bf0b0581168 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Sat, 25 May 2013 17:41:52 +0200 Subject: [PATCH] Stripped folder paths from atlas region names. Formatting. --- spine-tk2d/Code/tk2dSpineAttachmentLoader.cs | 54 +++++++++---------- spine-tk2d/Code/tk2dSpineSkeleton.cs | 42 ++++----------- spine-tk2d/Code/tk2dSpineSkeletonDataAsset.cs | 25 +++------ 3 files changed, 42 insertions(+), 79 deletions(-) diff --git a/spine-tk2d/Code/tk2dSpineAttachmentLoader.cs b/spine-tk2d/Code/tk2dSpineAttachmentLoader.cs index f5b6af31a..70cb21b8d 100644 --- a/spine-tk2d/Code/tk2dSpineAttachmentLoader.cs +++ b/spine-tk2d/Code/tk2dSpineAttachmentLoader.cs @@ -4,52 +4,49 @@ using Spine; // TODO: handle TPackerCW flip mode (probably not swap uv horizontaly) -/* - */ public class tk2dSpineAttachmentLoader : AttachmentLoader { - - /* - */ private tk2dSpriteCollectionData sprites; - - /* - */ - public tk2dSpineAttachmentLoader(tk2dSpriteCollectionData s) { - if(s == null) throw new ArgumentNullException("sprites cannot be null."); - sprites = s; + + public tk2dSpineAttachmentLoader(tk2dSpriteCollectionData sprites) { + if (sprites == null) throw new ArgumentNullException("sprites cannot be null."); + this.sprites = sprites; } - public Attachment NewAttachment(Skin skin,AttachmentType type,String name) { - if(type != AttachmentType.region) throw new Exception("Unknown attachment type: " + type); - + public Attachment NewAttachment(Skin skin, AttachmentType type, String name) { + if (type != AttachmentType.region) throw new Exception("Unknown attachment type: " + type); + + // Strip folder names. + int index = name.LastIndexOfAny(new char[] {'/', '\\'}); + if (index != -1) name = name.Substring(index + 1); + tk2dSpriteDefinition attachmentParameters = null; - for(int i = 0; i < sprites.inst.spriteDefinitions.Length; ++i) { + for (int i = 0; i < sprites.inst.spriteDefinitions.Length; ++i) { tk2dSpriteDefinition def = sprites.inst.spriteDefinitions[i]; - if(def.name == name) { + if (def.name == name) { attachmentParameters = def; break; } } - if(attachmentParameters == null ) throw new Exception("Sprite not found in atlas: " + name + " (" + type + ")"); - if(attachmentParameters.complexGeometry) throw new NotImplementedException("Complex geometry is not supported: " + name + " (" + type + ")"); - if(attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW) throw new NotImplementedException("Only 2d toolkit atlases are supported: " + name + " (" + type + ")"); - Texture tex = attachmentParameters.material.mainTexture; - + if (attachmentParameters == null) throw new Exception("Sprite not found in atlas: " + name + " (" + type + ")"); + if (attachmentParameters.complexGeometry) throw new NotImplementedException("Complex geometry is not supported: " + name + " (" + type + ")"); + if (attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW) throw new NotImplementedException("Only 2D Toolkit atlases are supported: " + name + " (" + type + ")"); + Vector2 minTexCoords = Vector2.one; Vector2 maxTexCoords = Vector2.zero; - for(int i = 0; i < attachmentParameters.uvs.Length; ++i) { + for (int i = 0; i < attachmentParameters.uvs.Length; ++i) { Vector2 uv = attachmentParameters.uvs[i]; - minTexCoords = Vector2.Min(minTexCoords,uv); - maxTexCoords = Vector2.Max(maxTexCoords,uv); + minTexCoords = Vector2.Min(minTexCoords, uv); + maxTexCoords = Vector2.Max(maxTexCoords, uv); } - int width = (int)(Mathf.Abs(maxTexCoords.x - minTexCoords.x) * tex.width); - int height = (int)(Mathf.Abs(maxTexCoords.y - minTexCoords.y) * tex.height); + Texture texture = attachmentParameters.material.mainTexture; + int width = (int)(Mathf.Abs(maxTexCoords.x - minTexCoords.x) * texture.width); + int height = (int)(Mathf.Abs(maxTexCoords.y - minTexCoords.y) * texture.height); bool rotated = (attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.Tk2d); - if(rotated) { + if (rotated) { float temp = minTexCoords.x; minTexCoords.x = maxTexCoords.x; maxTexCoords.x = temp; @@ -64,7 +61,8 @@ public class tk2dSpineAttachmentLoader : AttachmentLoader { minTexCoords.y, rotated ); - + + // TODO - Set attachment.RegionOffsetX/Y. What units does attachmentParameters.untrimmedBoundsData use?! attachment.RegionWidth = width; attachment.RegionHeight = height; attachment.RegionOriginalWidth = width; diff --git a/spine-tk2d/Code/tk2dSpineSkeleton.cs b/spine-tk2d/Code/tk2dSpineSkeleton.cs index e6ec81750..df593c684 100644 --- a/spine-tk2d/Code/tk2dSpineSkeleton.cs +++ b/spine-tk2d/Code/tk2dSpineSkeleton.cs @@ -5,15 +5,10 @@ using Spine; // TODO: split skeleton and animation components // TODO: add events in animation component -/* - */ [ExecuteInEditMode] [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionForceBuild { - - /* - */ public tk2dSpineSkeletonDataAsset skeletonDataAsset; public Skeleton skeleton; @@ -22,8 +17,6 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor public float animationSpeed = 1; public Spine.AnimationState state; - /* - */ private Mesh mesh; private Vector3[] vertices; private Color[] colors; @@ -32,18 +25,14 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor private int cachedQuadCount; private float[] vertexPositions; - /* - */ void Awake() { vertexPositions = new float[8]; } - /* - */ - void Update() { + void Update () { SkeletonData skeletonData = (skeletonDataAsset != null) ? skeletonDataAsset.GetSkeletonData() : null; - if(skeletonData == null) { + if (skeletonData == null) { Clear(); return; } @@ -56,8 +45,6 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor UpdateMesh(); } - /* - */ private void Clear() { GetComponent().mesh = null; DestroyImmediate(mesh); @@ -69,8 +56,6 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor state = null; } - /* - */ private void Initialize() { mesh = new Mesh(); GetComponent().mesh = mesh; @@ -81,18 +66,16 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData()); } - /* - */ private void UpdateMesh() { int quadIndex = 0; int drawCount = skeleton.DrawOrder.Count; Color currentColor = new Color(); - for(int i = 0; i < drawCount; i++) { + for (int i = 0; i < drawCount; i++) { Slot slot = skeleton.DrawOrder[i]; Attachment attachment = slot.Attachment; - if(attachment is RegionAttachment) { + if (attachment is RegionAttachment) { RegionAttachment regionAttachment = attachment as RegionAttachment; regionAttachment.ComputeVertices(slot.Bone,vertexPositions); @@ -143,8 +126,8 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor if (skeletonDataAsset.normalGenerationMode == tk2dSpriteCollection.NormalGenerationMode.NormalsAndTangents) { Vector4[] tangents = new Vector4[mesh.normals.Length]; - for (int t = 0; t < tangents.Length; ++t) { - tangents[t] = new Vector4(1, 0, 0, 1); + for (int i = 0; i < tangents.Length; i++) { + tangents[i] = new Vector4(1, 0, 0, 1); } mesh.tangents = tangents; } @@ -153,18 +136,16 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor renderer.sharedMaterial = skeletonDataAsset.spritesData.inst.materials[0]; } - /* - */ private void UpdateCache() { int quadCount = 0; int drawCount = skeleton.DrawOrder.Count; - for(int i = 0; i < drawCount; i++) { + for (int i = 0; i < drawCount; i++) { Attachment attachment = skeleton.DrawOrder[i].Attachment; - if(attachment is RegionAttachment) quadCount++; + if (attachment is RegionAttachment) quadCount++; } - if(quadCount == cachedQuadCount) return; + if (quadCount == cachedQuadCount) return; cachedQuadCount = quadCount; vertices = new Vector3[quadCount * 4]; @@ -173,17 +154,12 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor triangles = new int[quadCount * 6]; } - /* - */ private void UpdateSkeleton() { skeleton.Update(Time.deltaTime * animationSpeed); skeleton.UpdateWorldTransform(); } - /* - */ private void UpdateAnimation() { - // Check if we need to stop current animation if(state.Animation != null && animationName == null) { state.ClearAnimation(); diff --git a/spine-tk2d/Code/tk2dSpineSkeletonDataAsset.cs b/spine-tk2d/Code/tk2dSpineSkeletonDataAsset.cs index b5b5f3ab7..eafa1e616 100644 --- a/spine-tk2d/Code/tk2dSpineSkeletonDataAsset.cs +++ b/spine-tk2d/Code/tk2dSpineSkeletonDataAsset.cs @@ -3,12 +3,7 @@ using System.IO; using UnityEngine; using Spine; -/* - */ public class tk2dSpineSkeletonDataAsset : ScriptableObject { - - /* - */ public tk2dSpriteCollectionData spritesData; public tk2dSpriteCollection.NormalGenerationMode normalGenerationMode = tk2dSpriteCollection.NormalGenerationMode.None; @@ -20,36 +15,30 @@ public class tk2dSpineSkeletonDataAsset : ScriptableObject { public string[] toAnimation; public float[] duration; - /* - */ private SkeletonData skeletonData; private AnimationStateData stateData; - /* - */ public SkeletonData GetSkeletonData() { - if(skeletonData != null) return skeletonData; + if (skeletonData != null) return skeletonData; MakeSkeletonAndAnimationData(); return skeletonData; } public AnimationStateData GetAnimationStateData () { - if(stateData != null) return stateData; + if (stateData != null) return stateData; MakeSkeletonAndAnimationData(); return stateData; } - /* - */ private void MakeSkeletonAndAnimationData() { - if(spritesData == null) { + if (spritesData == null) { Debug.LogWarning("Sprite collection not set for skeleton data asset: " + name,this); return; } - if(skeletonJSON == null) { + if (skeletonJSON == null) { Debug.LogWarning("Skeleton JSON file not set for skeleton data asset: " + name,this); return; } @@ -65,9 +54,9 @@ public class tk2dSpineSkeletonDataAsset : ScriptableObject { } stateData = new AnimationStateData(skeletonData); - for(int i = 0, n = fromAnimation.Length; i < n; i++) { - if(fromAnimation[i].Length == 0 || toAnimation[i].Length == 0) continue; - stateData.SetMix(fromAnimation[i],toAnimation[i],duration[i]); + for (int i = 0, n = fromAnimation.Length; i < n; i++) { + if (fromAnimation[i].Length == 0 || toAnimation[i].Length == 0) continue; + stateData.SetMix(fromAnimation[i], toAnimation[i], duration[i]); } }