public ImageFrameExport(ImageFrame frame)
 {
     CenterPoint = new Vector2(frame.MappedCenterPointX, frame.MappedCenterPointY);
     PolygonGroups = new List<PolygonGroupExport>(frame.Children.Count);
     foreach (PolygonGroup polyGroup in frame.Children)
     {
         PolygonGroups.Add(new PolygonGroupExport(polyGroup));
     }
     Duration = frame.Duration;
     IsOpen = frame.IsOpen;
 }
        private static void SetNaturalCenter(ImageFrame frame)
        {
            var left = frame.TrimRectangle.Left;
            var width = frame.TrimRectangle.Width;
            var height = frame.TrimRectangle.Height;
            var top = frame.TrimRectangle.Top;

            frame.CenterPointX = left + (int)(width * 0.5f);
            frame.CenterPointY = top + (int)(height * 0.5f);
        }
Exemple #3
0
        public ImageData(string filename)
            : this()
        {
            string extension;
            using (var stream = File.Open(filename, FileMode.Open))
            {
                using (var image = Image.FromStream(stream))
                {
                    extension = Path.GetExtension(filename);
                    var thumbnail = image.GetThumbnailImage(38, 38, null, new IntPtr());

                    if (extension != null && extension.Equals(".png"))
                    {
                        using (var ms = new MemoryStream())
                        {
                            image.Save(ms, ImageFormat.Png);

                            var frame = new ImageFrame(ms.ToArray(), image.Width, image.Height)
                            {
                                ImagePath = filename,
                                CenterPointX = image.Width / 2,
                                CenterPointY = image.Height / 2,
                                Thumbnail = ImageHelper.ImageToByteArray(thumbnail),
                                Name = "Frame 1",
                            };

                            frame.Initialize();
                            AddChild(frame);
                        }
                    }
                    else
                    {
                        extension = Path.GetExtension(filename);
                        if (extension != null && extension.Equals(".gif"))
                        {
                            var dimension = new FrameDimension(image.FrameDimensionsList[0]);

                            // Number of frames
                            var frameCount = image.GetFrameCount(dimension);
                            // Return an Image at a certain index

                            for (var i = 0; i < frameCount; i++)
                            {
                                image.SelectActiveFrame(dimension, i);

                                using (var ms = new MemoryStream())
                                {
                                    // Replace indexed transparency (for use with spritesheets with indexed color)
                                    var img = BitmapTools.ReplaceIndexTransparency((Bitmap)image);
                                    img.Save(ms, ImageFormat.Png);

                                    var child = new ImageFrame(ms.ToArray(), image.Width, image.Height);
                                    child.Initialize();
                                    AddChild(child);

                                    var frame = Children[i] as ImageFrame;
                                    if (frame == null) continue;

                                    frame.CenterPointX = image.Width / 2;
                                    frame.CenterPointY = image.Height / 2;
                                    frame.Thumbnail = ImageHelper.ImageToByteArray(thumbnail);
                                    frame.Name = "Frame " + (i + 1);
                                    var item = image.GetPropertyItem(0x5100); // FrameDelay in libgdiplus
                                    // Time is in 1/100th of a second, in miliseconds
                                    var time = (item.Value[0] + item.Value[1] * 256) * 10;
                                    frame.Duration = time;
                                }
                            }
                        }
                    }
                }
            }
            FilePath = filename;
            FileLastModified = File.GetLastAccessTimeUtc(filename).ToUniversalTime();
            Name = Path.GetFileNameWithoutExtension(filename);
            Extension = extension;
        }
        private static void AddPlatformBoxStub(ImageFrame frame)
        {
            var platformGroup = new PolygonGroup { Name = "Platform", Parent = frame };
            platformGroup.Initialize();

            frame.AddChild(platformGroup);
            var attack = new Polygon { Name = "Polygon 1", Parent = platformGroup };
            platformGroup.AddChild(attack);
        }
        public static bool EnsureDefaults(ImageFrame frame, bool rebuildAll)
        {
            if (frame.TrimRectangle == Rectangle.Empty)
            {
                // There is no significant space in the cell, i.e., it's blank.
                //   We don't bother drawing any boxes for it because these would cause errors,
                //   though this can make sense for situations like "blinking"
                return true;
            }

            bool addAttack = true,
                 addClipping = true,
                 addPlatform = true,
                 addLanding = true,
                 addFoot = true,
                 addDepth = true,
                 addBody = true
                 ;

            var toRemove = new List<INode>();
            foreach (var child in frame.Children)
            {
                if (!(child is PolygonGroup)) continue;
                if (child.Name == "Attack")
                {
                    if (rebuildAll)
                    {
                        toRemove.Add(child);
                    }
                    else
                    {
                        addAttack = false;
                    }
                }
                if (child.Name == "Clipping")
                {
                    if (rebuildAll)
                    {
                        toRemove.Add(child);
                    }
                    else
                    {
                        addClipping = false;
                    }
                }
                if (child.Name == "Platform")
                {
                    if (rebuildAll)
                    {
                        toRemove.Add(child);
                    }
                    else
                    {
                        addPlatform = false;
                    }
                }
                if (child.Name == "Landing")
                {
                    if (rebuildAll)
                    {
                        toRemove.Add(child);
                    }
                    else
                    {
                        addLanding = false;
                    }
                }
                if (child.Name == "Foot")
                {
                    if (rebuildAll)
                    {
                        toRemove.Add(child);
                    }
                    else
                    {
                        addFoot = false;
                    }
                }
                if (child.Name == "Depth")
                {
                    if (rebuildAll)
                    {
                        toRemove.Add(child);
                    }
                    else
                    {
                        addDepth = false;
                    }
                }
                if (child.Name == "Body")
                {
                    if (rebuildAll)
                    {
                        toRemove.Add(child);
                    }
                    else
                    {
                        addBody = false;
                    }
                }
            }

            foreach (var child in toRemove)
            {
                frame.Children.Remove(child);
            }

            if(addAttack) AddAttackBoxStub(frame);
            if(addClipping) AddClippingBoxStub(frame);
            if(addPlatform) AddPlatformBoxStub(frame);
            if(addLanding) AddLandingBoxStub(frame);
            if(addFoot) AddDefaultFootBox(frame);
            if(addDepth) AddDefaultDepthBox(frame);
            if(addBody && !frame.FailsAutoTrace) AddBodyTrace(frame);

            return addAttack || addClipping || addPlatform || addFoot || addDepth || addBody;
        }
        private static void AddLandingBoxStub(ImageFrame frame)
        {
            var landingGroup = new PolygonGroup { Name = "Landing", Parent = frame };
            landingGroup.Initialize();

            frame.AddChild(landingGroup);
            var attack = new Polygon { Name = "Polygon 1", Parent = landingGroup};
            landingGroup.AddChild(attack);
        }
        private static void AddDefaultFootBox(ImageFrame frame)
        {
            var footGroup = new PolygonGroup { Name = "Foot", Parent = frame };
            footGroup.Initialize();

            frame.AddChild(footGroup);
            var foot = new Polygon { Name = "Foot", Parent = footGroup };

            var bottom = frame.TrimRectangle.Bottom - 1;
            var left = frame.TrimRectangle.Left;
            var right = frame.TrimRectangle.Right;
            var width = frame.TrimRectangle.Width;

            var tl = new PolyPoint(left + (int)(width * 0.25f), bottom - 2) { Parent = foot };
            var tr = new PolyPoint(right - (int)(width * 0.25f), bottom - 2) { Parent = foot };
            var br = new PolyPoint(right - (int)(width * 0.25f), bottom) { Parent = foot };
            var bl = new PolyPoint(left + (int)(width * 0.25f), bottom) { Parent = foot };

            foot.AddChild(tl);
            foot.AddChild(tr);
            foot.AddChild(br);
            foot.AddChild(bl);

            footGroup.AddChild(foot);
        }
        private static void AddDefaultDepthBox(ImageFrame frame)
        {
            var depthGroup = new PolygonGroup { Name = "Depth", Parent = frame };
            depthGroup.Initialize();

            frame.AddChild(depthGroup);
            var depth = new Polygon { Name = "Depth", Parent = depthGroup };

            var bottom = frame.TrimRectangle.Bottom - 1;
            var left = frame.TrimRectangle.Left;
            var right = frame.TrimRectangle.Right;
            var width = frame.TrimRectangle.Width;

            var defaultDepthPercentage = (int)(frame.TrimRectangle.Height * 0.125f);
            const float defaultWidthBorder = 0.9f; // 10% on each side = 80%

            var tl = new PolyPoint(left + (int)(width * defaultWidthBorder), bottom - defaultDepthPercentage) { Parent = depth };
            var tr = new PolyPoint(right - (int)(width * defaultWidthBorder), bottom - defaultDepthPercentage) { Parent = depth };
            var br = new PolyPoint(right - (int)(width * defaultWidthBorder), bottom) { Parent = depth };
            var bl = new PolyPoint(left + (int)(width * defaultWidthBorder), bottom) { Parent = depth };
            depth.AddChild(tl);
            depth.AddChild(tr);
            depth.AddChild(br);
            depth.AddChild(bl);

            depthGroup.AddChild(depth);
        }
        private static void AddBodyTrace(ImageFrame frame)
        {
            using (var ms = new MemoryStream(frame.Data))
            {
                var imageBitmap = Image.FromStream(ms);
                var errorBuilder = new StringBuilder();

                var bodyGroup = new PolygonGroup { Name = "Body", Parent = frame };
                bodyGroup.Initialize();
                frame.AddChild(bodyGroup);

                var shape = TraceService.CreateSimpleShape(imageBitmap, 200, errorBuilder);
                if (shape == null)
                {
                    frame.FailsAutoTrace = true;
                    return;
                }

                var count = 1;
                foreach (var polygon in shape.Vertices)
                {
                    var poly = new Polygon { Name = "Polygon " + count, Parent = bodyGroup };
                    foreach (var point in polygon)
                    {
                        var x = (int)ConvertUnits.ToDisplayUnits(point.X);
                        var y = (int)ConvertUnits.ToDisplayUnits(point.Y);

                        x += (int)(frame.Width * 0.5f);
                        y += (int)(frame.Height * 0.5f);
                        poly.AddChild(new PolyPoint(x, y) { Parent = poly });
                    }

                    bodyGroup.AddChild(poly);
                    count++;
                }
            }
        }
        private static void AddAttackBoxStub(ImageFrame frame)
        {
            var attackGroup = new PolygonGroup { Name = "Attack", Parent = frame};
            attackGroup.Initialize();

            frame.AddChild(attackGroup);
            var attack = new Polygon { Name = "Polygon 1", Parent = attackGroup };
            attackGroup.AddChild(attack);
        }