Beispiel #1
0
        public override void Read(AssetReader reader)
        {
            base.Read(reader);

            Rect.Read(reader);
            Offset.Read(reader);
            if (IsReadBorder(reader.Version))
            {
                Border.Read(reader);
            }
            PixelsToUnits = reader.ReadSingle();
            if (IsReadPivot(reader.Version))
            {
                Pivot.Read(reader);
            }
            Extrude = reader.ReadUInt32();
            if (IsReadPolygon(reader.Version))
            {
                IsPolygon = reader.ReadBoolean();
                reader.AlignStream(AlignType.Align4);
            }

            if (IsReadRendererData(reader.Version))
            {
                RenderDataKey = reader.ReadTupleTLong <EngineGUID>();
                m_atlasTags   = reader.ReadStringArray();
                SpriteAtlas.Read(reader);
            }
            RD.Read(reader);
            reader.AlignStream(AlignType.Align4);

            if (IsReadPhysicsShape(reader.Version))
            {
                int count = reader.ReadInt32();
                m_physicsShape = new Vector2f[count][];
                for (int i = 0; i < count; i++)
                {
                    m_physicsShape[i] = reader.ReadArray <Vector2f>();
                }
            }

            if (IsReadBones(reader.Version))
            {
                m_bones = reader.ReadArray <SpriteBone>();
            }
        }
Beispiel #2
0
        public Vector2f[][] GenerateOutline(SpriteAtlas atlas, Rectf rect, Vector2f pivot)
        {
            Vector2f[][] outlines    = RD.GenerateOutline(File.Version);
            float        pivotShiftX = rect.Width * pivot.X - rect.Width * 0.5f;
            float        pivotShiftY = rect.Height * pivot.Y - rect.Height * 0.5f;
            Vector2f     pivotShift  = new Vector2f(pivotShiftX, pivotShiftY);

            foreach (Vector2f[] outline in outlines)
            {
                for (int i = 0; i < outline.Length; i++)
                {
                    Vector2f point = outline[i] * PixelsToUnits;
                    outline[i] = point + pivotShift;
                }
            }
            return(FixRotation(atlas, outlines));
        }
Beispiel #3
0
 public Vector2f[][] GeneratePhysicsShape(SpriteAtlas atlas, Rectf rect, Vector2f pivot)
 {
     if (PhysicsShape.Length > 0)
     {
         Vector2f[][] shape       = new Vector2f[PhysicsShape.Length][];
         float        pivotShiftX = rect.Width * pivot.X - rect.Width * 0.5f;
         float        pivotShiftY = rect.Height * pivot.Y - rect.Height * 0.5f;
         Vector2f     pivotShift  = new Vector2f(pivotShiftX, pivotShiftY);
         for (int i = 0; i < PhysicsShape.Length; i++)
         {
             shape[i] = new Vector2f[PhysicsShape[i].Length];
             for (int j = 0; j < PhysicsShape[i].Length; j++)
             {
                 Vector2f point = PhysicsShape[i][j] * PixelsToUnits;
                 shape[i][j] = point + pivotShift;
             }
         }
         return(FixRotation(atlas, shape));
     }
     return(Array.Empty <Vector2f[]>());
 }
Beispiel #4
0
        public void GetExportPosition(out Rectf rect, out Vector2f pivot, out Vector4f border)
        {
            SpriteAtlas atlas = null;

            if (IsReadRendererData(File.Version))
            {
                atlas = SpriteAtlas.FindAsset(File);
            }
            Vector2f rectOffset;

            if (atlas == null)
            {
                Vector2f textureOffset = RD.TextureRect.Position;
                Vector2f textureSize   = RD.TextureRect.Size;
                rectOffset = RD.TextureRectOffset;                 // should be equal to RD.TextureRect.Position - Rect.Position
                rect       = new Rectf(textureOffset, textureSize);
            }
            else
            {
                SpriteAtlasData atlasData     = atlas.RenderDataMap[RenderDataKey];
                Vector2f        textureOffset = atlasData.TextureRect.Position;
                Vector2f        textureSize   = atlasData.TextureRect.Size;
                rectOffset = atlasData.TextureRectOffset;
                rect       = new Rectf(textureOffset, textureSize);
            }

            Vector2f sizeDif           = Rect.Size - rect.Size;
            Vector2f pivotShiftSize    = new Vector2f(Pivot.X * sizeDif.X, Pivot.Y * sizeDif.Y);
            Vector2f relPivotShiftPos  = new Vector2f(rectOffset.X / rect.Size.X, rectOffset.Y / rect.Size.Y);
            Vector2f relPivotShiftSize = new Vector2f(pivotShiftSize.X / rect.Size.X, pivotShiftSize.Y / rect.Size.Y);

            pivot = Pivot - relPivotShiftPos + relPivotShiftSize;

            float borderL = Border.X == 0.0f ? 0.0f : Border.X - rectOffset.X;
            float borderB = Border.Y == 0.0f ? 0.0f : Border.Y - rectOffset.Y;
            float borderR = Border.Z == 0.0f ? 0.0f : Border.Z + rectOffset.X - sizeDif.X;
            float borderT = Border.W == 0.0f ? 0.0f : Border.W + rectOffset.Y - sizeDif.Y;

            border = new Vector4f(borderL, borderB, borderR, borderT);
        }
Beispiel #5
0
 public IReadOnlyList <IReadOnlyList <Vector2f> > GeneratePhysicsShape(SpriteAtlas atlas, Rectf rect, Vector2f pivot)
 {
     if (IsReadPhysicsShape(File.Version))
     {
         Vector2f[][] shape       = new Vector2f[PhysicsShape.Count][];
         float        pivotShiftX = rect.Width * pivot.X - rect.Width * 0.5f;
         float        pivotShiftY = rect.Height * pivot.Y - rect.Height * 0.5f;
         Vector2f     pivotShift  = new Vector2f(pivotShiftX, pivotShiftY);
         for (int i = 0; i < PhysicsShape.Count; i++)
         {
             shape[i] = new Vector2f[PhysicsShape[i].Count];
             for (int j = 0; j < PhysicsShape[i].Count; j++)
             {
                 Vector2f point = PhysicsShape[i][j] * PixelsToUnits;
                 shape[i][j] = point + pivotShift;
             }
         }
         return(FixRotation(atlas, shape));
     }
     else
     {
         return(new Vector2f[0][]);
     }
 }
Beispiel #6
0
 public SpriteMetaData GenerateSpriteMetaData(IExportContainer container, SpriteAtlas atlas)
 {
     return(SpriteConverter.GenerateSpriteMetaData(container, atlas, this));
 }
Beispiel #7
0
        private Vector2f[][] FixRotation(SpriteAtlas atlas, Vector2f[][] outlines)
        {
            bool isPacked = RD.IsPacked;
            SpritePackingRotation rotation = RD.PackingRotation;

            if (atlas != null)
            {
                SpriteAtlasData atlasData = atlas.RenderDataMap[RenderDataKey];
                isPacked = atlasData.IsPacked;
                rotation = atlasData.PackingRotation;
            }

            if (isPacked)
            {
                switch (rotation)
                {
                case SpritePackingRotation.FlipHorizontal:
                {
                    foreach (Vector2f[] outline in outlines)
                    {
                        for (int i = 0; i < outline.Length; i++)
                        {
                            Vector2f vertex = outline[i];
                            outline[i] = new Vector2f(-vertex.X, vertex.Y);
                        }
                    }
                }
                break;

                case SpritePackingRotation.FlipVertical:
                {
                    foreach (Vector2f[] outline in outlines)
                    {
                        for (int i = 0; i < outline.Length; i++)
                        {
                            Vector2f vertex = outline[i];
                            outline[i] = new Vector2f(vertex.X, -vertex.Y);
                        }
                    }
                }
                break;

                case SpritePackingRotation.Rotate90:
                {
                    foreach (Vector2f[] outline in outlines)
                    {
                        for (int i = 0; i < outline.Length; i++)
                        {
                            Vector2f vertex = outline[i];
                            outline[i] = new Vector2f(vertex.Y, vertex.X);
                        }
                    }
                }
                break;

                case SpritePackingRotation.Rotate180:
                {
                    foreach (Vector2f[] outline in outlines)
                    {
                        for (int i = 0; i < outline.Length; i++)
                        {
                            Vector2f vertex = outline[i];
                            outline[i] = new Vector2f(-vertex.X, -vertex.Y);
                        }
                    }
                }
                break;
                }
            }
            return(outlines);
        }