Exemplo n.º 1
0
 public void Merge(SpriteFrame other)
 {
     this.Pixels.AddRange(other.Pixels);
     if (this.Rect.IsEmpty)
     {
         this.Rect = other.Rect;
     }
     else if (!other.Rect.IsEmpty)
     {
         this.Rect = Rectangle.Union(this.Rect, other.Rect);
     }
 }
Exemplo n.º 2
0
        private void LoadFromImage(Bitmap bitmap)
        {
            var frameList     = new List <SpriteFrame>();
            var pixelMappings = new Dictionary <Point, SpriteFrame>();

            //iterate through image and create frames
            for (int y = 0; y < bitmap.Height; ++y)
            {
                for (int x = 0; x < bitmap.Width; ++x)
                {
                    var currPixel = new Point(x, y);
                    if (bitmap.GetPixel(x, y).A < 80)
                    {
                        //if this pixel is transparent, it's not part of a region, so let's do the next one
                        continue;
                    }

                    var neighbours = new List <Point>(4);

                    neighbours.Add(new Point(x - 1, y - 1));
                    neighbours.Add(new Point(x, y - 1));
                    neighbours.Add(new Point(x + 1, y - 1));
                    neighbours.Add(new Point(x - 1, y));

                    foreach (var pixel in neighbours)
                    {
                        if (!pixelMappings.ContainsKey(pixel))
                        {
                            // This neighbour pixel is transparent, so we'll ignore it
                            continue;
                        }

                        if (pixelMappings.ContainsKey(currPixel))
                        {
                            var currentFrame = pixelMappings[currPixel];
                            if (pixelMappings[pixel] != pixelMappings[currPixel])
                            {
                                //if the pixel above and the pixel to the left are distinct regions, the current pixel means they're the same region
                                var neighbourFrame = pixelMappings[pixel];

                                //merge the regions together, removing one altogether and relabeling all the other entries in that list
                                currentFrame.Merge(neighbourFrame);
                                foreach (var pixelToMerge in neighbourFrame.Pixels)
                                {
                                    pixelMappings[pixelToMerge] = currentFrame;
                                }

                                //remove the old frame entry
                                frameList.Remove(neighbourFrame);
                            }
                        }
                        else
                        {
                            //if this neighbour pixel is part of a region, the current pixel also is
                            pixelMappings[currPixel] = pixelMappings[pixel];
                        }
                    }

                    if (!pixelMappings.ContainsKey(currPixel))
                    {
                        //if the previous pixels aren't in regions, then this pixel is part of a new region we need to create
                        var newFrame = new SpriteFrame();
                        pixelMappings[currPixel] = newFrame;
                        frameList.Add(newFrame);
                    }

                    if (pixelMappings.ContainsKey(currPixel))
                    {
                        pixelMappings[currPixel].InsertPixel(currPixel);
                    }
                }
            }

            this.frames = frameList.Where(frame => frame != null).OrderBy(frame => frame.Rect.Left / 10).OrderBy(frame => frame.Rect.Top / 10).ToList();
        }
Exemplo n.º 3
0
        private void LoadFromImage(Bitmap bitmap)
        {
            var frameList = new List<SpriteFrame>();
            var pixelMappings = new Dictionary<Point, SpriteFrame>();

            //iterate through image and create frames
            for (int y = 0; y < bitmap.Height; ++y)
            {
                for (int x = 0; x < bitmap.Width; ++x)
                {
                    var currPixel = new Point(x, y);
                    if (bitmap.GetPixel(x, y).A < 80)
                    {
                        //if this pixel is transparent, it's not part of a region, so let's do the next one
                        continue;
                    }

                    var neighbours = new List<Point>(4);

                    neighbours.Add(new Point(x - 1, y - 1));
                    neighbours.Add(new Point(x, y - 1));
                    neighbours.Add(new Point(x + 1, y - 1));
                    neighbours.Add(new Point(x - 1, y));

                    foreach (var pixel in neighbours)
                    {
                        if (!pixelMappings.ContainsKey(pixel))
                        {
                            // This neighbour pixel is transparent, so we'll ignore it
                            continue;
                        }

                        if (pixelMappings.ContainsKey(currPixel))
                        {
                            var currentFrame = pixelMappings[currPixel];
                            if (pixelMappings[pixel] != pixelMappings[currPixel])
                            {
                                //if the pixel above and the pixel to the left are distinct regions, the current pixel means they're the same region
                                var neighbourFrame = pixelMappings[pixel];

                                //merge the regions together, removing one altogether and relabeling all the other entries in that list
                                currentFrame.Merge(neighbourFrame);
                                foreach (var pixelToMerge in neighbourFrame.Pixels)
                                {
                                    pixelMappings[pixelToMerge] = currentFrame;
                                }

                                //remove the old frame entry
                                frameList.Remove(neighbourFrame);
                            }
                        }
                        else
                        {
                            //if this neighbour pixel is part of a region, the current pixel also is
                            pixelMappings[currPixel] = pixelMappings[pixel];
                        }
                    }

                    if (!pixelMappings.ContainsKey(currPixel))
                    {
                        //if the previous pixels aren't in regions, then this pixel is part of a new region we need to create
                        var newFrame = new SpriteFrame();
                        pixelMappings[currPixel] = newFrame;
                        frameList.Add(newFrame);
                    }

                    if (pixelMappings.ContainsKey(currPixel))
                    {
                        pixelMappings[currPixel].InsertPixel(currPixel);
                    }
                }
            }

            this.frames = frameList.Where(frame => frame != null).OrderBy(frame => frame.Rect.Left / 10).OrderBy(frame => frame.Rect.Top / 10).ToList();
        }
Exemplo n.º 4
0
        private void ChangeFrame()
        {
            if (this.frameListBox.SelectedIndex < 0)
            {
                this.currentFrame = null;
            }
            else
            {
                this.currentFrame = this.frameFinder.Frames[this.frameListBox.SelectedIndex];

                this.frameImageBox.Width = currentFrame.Rect.Width;
                this.frameImageBox.Height = currentFrame.Rect.Height;

                this.frameNameText.Text = this.currentFrame.Name;
            }

            Refresh();
        }