Exemplo n.º 1
0
        public void ExportSpriteMapXML()
        {
            if (CurrentShownSpriteSheet == null)
            {
                Messager.ShowMessage(Messager.Mode.Exception,
                                     "No spritesheet loaded! Please load a sprite sheet first by clicking the button 'Import Spritesheet' on topbar");
                return;
            }


            var browser = MainWindow.ObjectsBrowser;


            if (browser.SelectedNode.Level == 0)
            {
                ExportComplete(browser.SelectedNode.Tag.ToString());
            }
            else
            {
                ExportSingle(browser.SelectedNode.Tag.ToString());
            }
        }
Exemplo n.º 2
0
        public void AddFramesToAnimation(List <Frame> frames)
        {
            if (Guard.CheckNull(_currentShownSpriteSheet))
            {
                Messager.ShowMessage(Messager.Mode.Exception,
                                     "No spritesheet loaded! Please load a sprite sheet first by clicking the button Import Spritesheet on topbar");
                return;
            }


            if (Guard.CheckNull(CurrentShownAnimation))
            {
                Messager.ShowMessage(Messager.Mode.Exception,
                                     "There's no animations created! Please create one by clicking on 'New Animation'");
                return;
            }

            var animConfig = GetDisplay <AnimConfigDisplay>("AnimConfig");


            animConfig.AddFramesToAnimationStrip(frames);

            _currentShownSpriteSheet.UnselectAllFrames();
        }
Exemplo n.º 3
0
        public void ExportAnimationAsGif()
        {
            var error = false;

            if (CurrentShownSpriteSheet == null)
            {
                Messager.ShowMessage(Messager.Mode.Exception,
                                     "No spritesheet loaded! Please load a sprite sheet first by clicking the button Import Spritesheet on topbar");
                return;
            }

            if (CurrentShownAnimation != null)
            {
                if (CurrentShownAnimation.Frames.Count == 0)
                {
                    Messager.ShowMessage(Messager.Mode.Exception, "No frames to export. The animation is empty!");
                    return;
                }

                var path = ShowCompleteSaveToDialog("Choose location to save GIF", @"Animated GIF(.gif)|*.gif");

                if (path == null)
                {
                    return;
                }

                if (path.Length == 0)
                {
                    Messager.ShowMessage(Messager.Mode.Exception,
                                         string.Format("Nothing exported. The path is invalid or empty!"));
                    return;
                }

                try
                {
                    Size  frameSize = Size.Empty;
                    Point offSetMin = Point.Empty;
                    Point offSetMax = Point.Empty;

                    foreach (var frame in CurrentShownAnimation.Frames)
                    {
                        if (frame.SpriteFrame.Width > frameSize.Width)
                        {
                            frameSize.Width = frame.SpriteFrame.Width;
                        }
                        if (frame.SpriteFrame.Height > frameSize.Height)
                        {
                            frameSize.Height = frame.SpriteFrame.Height;
                        }
                    }

                    foreach (var frame in CurrentShownAnimation.Frames)
                    {
                        if (frame.OffSetX < offSetMin.X)
                        {
                            offSetMin.X = frame.OffSetX;
                        }
                        if (frame.OffSetY < offSetMin.Y)
                        {
                            offSetMin.Y = frame.OffSetY;
                        }

                        if (frame.OffSetX > offSetMax.X)
                        {
                            offSetMax.X = frame.OffSetX;
                        }
                        if (frame.OffSetY > offSetMax.Y)
                        {
                            offSetMax.Y = frame.OffSetY;
                        }
                    }

                    frameSize.Width += Math.Abs(offSetMin.X) + offSetMax.X;

                    frameSize.Height += Math.Abs(offSetMin.Y) + offSetMax.Y;

                    frameSize.Width = frameSize.Width > frameSize.Height ? frameSize.Width : frameSize.Height;

                    frameSize.Height = frameSize.Width;

                    int repeatCount = CurrentShownAnimation.Loop ? 0 : 1;

                    ColourTableStrategy strategy = ColourTableStrategy.UseGlobal;


                    int quality = 10;

                    AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder
                    {
                        LogicalScreenSize   = frameSize,
                        RepeatCount         = repeatCount,
                        ColourTableStrategy = strategy,
                        SamplingFactor      = quality,
                    };

                    var texture = CurrentShownSpriteSheet.Texture;

                    animatedGifEncoder.Transparent = Color.Black;
                    foreach (var frame in CurrentShownAnimation.Frames)
                    {
                        Bitmap frameBitMap = texture.Surface.ToBitmap(frame.SpriteFrame.Region);

                        GifFrame gifFrame = new GifFrame(frameBitMap);

                        gifFrame.Delay = 100 * ((int)(frame.FrameDuration));

                        if (gifFrame.Delay < 1)
                        {
                            gifFrame.Delay = 1;
                        }

                        gifFrame.Position =
                            new Point((int)((frameSize.Width / 2f - frame.SpriteFrame.Width / 2f) + frame.OffSetX),
                                      (int)((frameSize.Height / 2f - frame.SpriteFrame.Height / 2f) + frame.OffSetY));


                        animatedGifEncoder.AddFrame(gifFrame);
                    }


                    animatedGifEncoder.WriteToFile(path);

                    animatedGifEncoder.Dispose();
                }
                catch (Exception ex)
                {
                    error = true;
                    Messager.ShowMessage(Messager.Mode.Exception,
                                         string.Format("Error on exporting GIF: {0}", ex.Message));
                    Vortex.Debugging.Log.Error(ex, "Error on exporting GIF");
                }
                finally
                {
                    if (!error)
                    {
                        Messager.ShowMessage(Messager.Mode.Message, "GIF created successfully!");
                    }
                }
            }
            else
            {
                Messager.ShowMessage(Messager.Mode.Exception, "No animations to export!");
            }
        }