Ejemplo n.º 1
0
        private string GetImagePath(string initialDirectory)
        {
            MainWindow.OpenFileDlg.Title            = Resources.Import_SpriteSheet_Text;
            MainWindow.OpenFileDlg.RestoreDirectory = true;
            MainWindow.OpenFileDlg.InitialDirectory = initialDirectory.Length > 0
                                                          ? initialDirectory
                                                          : Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            MainWindow.OpenFileDlg.Multiselect = false;

            MainWindow.OpenFileDlg.Filter = "Image Files|";


            foreach (var ext in ImageHelper.AllowedImageExtensions)
            {
                MainWindow.OpenFileDlg.Filter += string.Format("*.{0};", ext);
            }


            if (MainWindow.OpenFileDlg.ShowDialog() == DialogResult.OK)
            {
                if (ImageHelper.CheckImage(MainWindow.OpenFileDlg.FileName))
                {
                    return(MainWindow.OpenFileDlg.FileName);
                }
                Size maxTexturesize = RenderDisplayManager.MaxTextureSize;
                Messager.ShowMessage(Messager.Mode.Exception,
                                     string.Format(
                                         "Error loading image. Image extension must be PNG, JPG, GIF or BMP and size must be lower than {0}x{1}  pixels!",
                                         maxTexturesize.Width, maxTexturesize.Height));
            }
            return(string.Empty);
        }
Ejemplo n.º 2
0
        public SpriteSheetResourceData LoadSpriteSheetImage(string imagePath, Bitmap image)
        {
            if (!(Image.IsAlphaPixelFormat(image.PixelFormat)))
            {
                ImageHelper.AdjustImagePixelFormat(ref image);

                if (Image.IsAlphaPixelFormat(image.PixelFormat))
                {
                    Messager.ShowMessage(Messager.Mode.Message, "Image converted to alpha pixel format");
                }
            }


            var spTex = new Texture(image);

            spTex.Options.Filter = Configuration.TextureFilterMode;

            var id = string.Format("{0}", _spriteSheetsImages.Count);


            var spriteSheetResourceData = new SpriteSheetResourceData(imagePath, id, spTex);

            _spriteSheetsImages.Add(id, spriteSheetResourceData);

            return(spriteSheetResourceData);
        }
Ejemplo n.º 3
0
        public void MakeAnimation(string name, float frameRate, bool loop, bool pingPong)
        {
            if (Guard.CheckNull(_currentShownSpriteSheet))
            {
                Messager.ShowMessage(Messager.Mode.Exception,
                                     "Before creating an animation please import a sprite sheet image clicking on button 'Import Spritesheet' on topbar");
                return;
            }

            Animation animation = new Animation(name, string.Format("{0}{1}",
                                                                    _currentShownSpriteSheet.Id,
                                                                    _animations.Count),
                                                _currentShownSpriteSheet.Id)
            {
                FrameRate = frameRate, Loop = loop, PingPong = pingPong
            };

            _animations.Add(animation.Id, animation);

            ShowAnimation(animation.Id);


            var eventArgs = new AppEventArgs
            {
                Type = EventType.AddedAnimation,
                AnimationModified   = animation,
                SpriteSheetModified = _currentShownSpriteSheet
            };

            MainWindow.UpdateBrowser(eventArgs);
        }
Ejemplo n.º 4
0
        public void AddFrames(List <Frame> frames)
        {
            if (_frameContainersSelected.Count > 0)
            {
                if (_frameContainersSelected.Count >= frames.Count)
                {
                    for (int i = 0; i < frames.Count; i++)
                    {
                        _frameContainersSelected[i].SetFrame(frames[i]);
                    }
                }
                else
                {
                    Messager.ShowMessage(Messager.Mode.Exception,
                                         string.Format(
                                             "You've selected {0} frames to add but only selected {1} containers!",
                                             frames.Count, _frameContainersSelected.Count));
                }
            }
            else
            {
                int indexToBeginAdding   = -1;
                int emptyContainersOnEnd = 0;

                for (int i = _frameContainers.Count - 1; i >= 0; i--)
                {
                    if (_frameContainers[i].Frame == null)
                    {
                        emptyContainersOnEnd += 1;
                        indexToBeginAdding    = i;
                    }
                    else
                    {
                        break;
                    }
                }

                int delta = frames.Count - emptyContainersOnEnd;

                if (delta > 0)
                {
                    AddContainer(delta);
                    if (indexToBeginAdding == -1)
                    {
                        indexToBeginAdding = _frameContainers.Count - delta;
                    }
                }

                for (int i = 0; i < frames.Count; i++)
                {
                    _frameContainers[indexToBeginAdding++].SetFrame(frames[i]);
                }
            }

            UnselectAll();
            RefreshAnimation();
        }
Ejemplo n.º 5
0
 public void SetFrameDuration(int frame, float duration)
 {
     if (duration <= 0)
     {
         Messager.ShowMessage(Messager.Mode.Exception, "Frame duration must be greater than zero!");
     }
     Frames[frame].FrameDuration = duration;
     _frameTime = 0;
 }
Ejemplo n.º 6
0
        public void ExportFramesImages()
        {
            try
            {
                if (CurrentShownSpriteSheet != null)
                {
                    if (CurrentShownSpriteSheet.Frames.Count > 0)
                    {
                        var path = ShowSaveToFolderDialog("Choose location to save images...");

                        if (path == null)
                        {
                            return;
                        }

                        int id = 0;

                        if (path.Length > 0)
                        {
                            foreach (var spriteSheetFrame in CurrentShownSpriteSheet.Frames)
                            {
                                Exporter.ExportSpriteSheetImage(CurrentShownSpriteSheet,
                                                                spriteSheetFrame.BoundingRect.ToRectangle(),
                                                                path + "\\frame" + id + ".png");

                                id += 1;
                            }
                            Messager.ShowMessage(Messager.Mode.Message, "Images exported sucessfully!");
                        }
                        else
                        {
                            Messager.ShowMessage(Messager.Mode.Exception,
                                                 "Nothing exported. The path is invalid or empty!");
                        }
                    }
                    else
                    {
                        Messager.ShowMessage(Messager.Mode.Exception,
                                             "The selected spritesheet has no frames defined!. Either click on 'Alpha Cut' to automatically find them or mannually select them with right mouse button.");
                    }
                }
                else
                {
                    Messager.ShowMessage(Messager.Mode.Exception,
                                         "No spritesheet loaded! Please load a sprite sheet first by clicking the button Import Spritesheet on topbar");
                }
            }
            catch (Exception ex)
            {
                Messager.ShowMessage(Messager.Mode.Exception, string.Format("Could not export images: {0}", ex.Message));
            }
        }
Ejemplo n.º 7
0
 public static bool EraseHistory()
 {
     try
     {
         if (File.Exists(historyFile))
         {
             File.Delete(historyFile);
             return(true);
         }
     }
     catch (Exception ex)
     {
         Messager.ShowMessage(Messager.Mode.Exception,
                              string.Format("Error on removing history file: {0}", ex.Message));
     }
     return(false);
 }
Ejemplo n.º 8
0
        public void ExportComplete(string spSheetId)
        {
            var spSheet = _spriteSheets[spSheetId];

            var animations = GetAnimationsFromSpriteSheet(spSheet);

            try
            {
                var path = ShowCompleteSaveToDialog("Choose location to export XML...", @"XML File(.xml)|*.xml");

                if (path == null)
                {
                    return;
                }

                if (path.Length > 0)
                {
                    var exported = Exporter.ExportSpriteMapping(spSheet, animations, path);
                    if (exported == ExportResult.ExportedAll)
                    {
                        Messager.ShowMessage(Messager.Mode.Message,
                                             "SpriteMap and all animations exported successfully!");
                    }
                    else if (exported == ExportResult.ExportedOnlySpriteMap)
                    {
                        Messager.ShowMessage(Messager.Mode.Message, "SpriteMap exported successfully!");
                    }
                    else if (exported == ExportResult.Nothing)
                    {
                        Messager.ShowMessage(Messager.Mode.Message, "Nothing exported ! No Sprites defined!");
                    }
                }
                else
                {
                    Messager.ShowMessage(Messager.Mode.Exception,
                                         string.Format("Nothing exported. The path is invalid or empty!"));
                }
            }
            catch (Exception ex)
            {
                Messager.ShowMessage(Messager.Mode.Exception,
                                     string.Format("Error on SpriteMap export: {0}", ex.Message));
                Vortex.Debugging.Log.Error(ex, "Error on SpriteMap export");
            }
        }
Ejemplo n.º 9
0
        public void ExportSingle(string animId)
        {
            if (_animations[animId].Frames.Count == 0)
            {
                Messager.ShowMessage(Messager.Mode.Exception, "No frames to export. The animation is empty!");
                return;
            }

            List <Animation> animsToExport = new List <Animation>();

            var spSheetId = _animations[animId].SpriteSheetId;

            animsToExport.Add(_animations[animId]);

            try
            {
                var path = ShowCompleteSaveToDialog("Choose location to export XML...", @"XML File(.xml)|*.xml");

                if (path == null)
                {
                    return;
                }

                if (path.Length > 0)
                {
                    var exported = Exporter.ExportSpriteMapping(_spriteSheets[spSheetId], animsToExport, path);
                    if (exported == ExportResult.ExportedAll)
                    {
                        Messager.ShowMessage(Messager.Mode.Message,
                                             "SpriteMap and single animation exported successfully!");
                    }
                }
                else
                {
                    Messager.ShowMessage(Messager.Mode.Exception,
                                         string.Format("Nothing exported. The path is invalid or empty!"));
                }
            }
            catch (Exception ex)
            {
                Messager.ShowMessage(Messager.Mode.Exception,
                                     string.Format("Error on SpriteMap export: {0}", ex.Message));
                Vortex.Debugging.Log.Error(ex, "Error on SpriteMap export");
            }
        }
Ejemplo n.º 10
0
        public void ExportSpriteSheetImage()
        {
            var error = false;


            if (CurrentShownSpriteSheet != null)
            {
                try
                {
                    var path = ShowCompleteSaveToDialog("Choose location to save image...", @"PNG Image(.png)|*.png");

                    if (path == null)
                    {
                        return;
                    }

                    if (path.Length > 0)
                    {
                        Exporter.ExportSpriteSheetImage(CurrentShownSpriteSheet, path);
                    }
                    else
                    {
                        Messager.ShowMessage(Messager.Mode.Exception,
                                             string.Format("Nothing exported. The path is invalid or empty!"));
                    }
                }
                catch (Exception ex)
                {
                    error = true;
                    Messager.ShowMessage(Messager.Mode.Exception,
                                         string.Format("Could not export image: {0}", ex.Message));
                }

                if (!error)
                {
                    Messager.ShowMessage(Messager.Mode.Message, "Image exported sucessfully!");
                }
            }
            else
            {
                Messager.ShowMessage(Messager.Mode.Exception,
                                     "No spritesheet loaded! Please load a sprite sheet first by clicking the button Import Spritesheet on topbar");
            }
        }
Ejemplo n.º 11
0
        public void CutSpriteSheetByAlpha()
        {
            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 (_currentShownSpriteSheet.Frames.Count > 0)
            {
                _currentShownSpriteSheet.Frames.Clear();
            }

            var frameRects = SpriteSheetAndTextureFuncs.CutByAlpha(_currentShownSpriteSheet.Texture);

            foreach (var frameRect in frameRects)
            {
                _currentShownSpriteSheet.AddFrame(frameRect);
            }
        }
Ejemplo n.º 12
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());
            }
        }
Ejemplo n.º 13
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();
        }
Ejemplo n.º 14
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!");
            }
        }