コード例 #1
0
        public bool Import()
        {
            if (CurrentTexture == null)
            {
                return(false);
            }

            if (opfJsonDialog.ShowDialog() == true)
            {
                JObject data = JObject.Parse(File.ReadAllText(opfJsonDialog.FileName));
                _keyColor.A = 255;
                _keyColor.R = data["KeyColor"]["Red"].ToObject <byte>();
                _keyColor.G = data["KeyColor"]["Green"].ToObject <byte>();
                _keyColor.B = data["KeyColor"]["Blue"].ToObject <byte>();

                List <int[]> sprites = data["Sprites"].ToObject <List <int[]> >();
                int          index   = SpriteCollection.Children.Count;
                foreach (int[] sprite in sprites)
                {
                    SpriteCut cut = new SpriteCut();
                    cut.X      = sprite[0];
                    cut.Y      = sprite[1];
                    cut.Width  = sprite[2];
                    cut.Height = sprite[3];
                    cut.Index  = index;
                    index++;
                    spriteCollection.Children.Add(cut);
                }

                ToolBarViewModel.GetInstance().ChangeColorPickerColor();
            }

            return(false);
        }
コード例 #2
0
        protected void UpdateToScale()
        {
            double offSetX = imageViewPort.VerticalOffset / imageDisplay.Width;
            double offSetY = imageViewPort.HorizontalOffset / imageDisplay.Height;

            imageDisplay.Width  = (int)(originalWidth * scale + 0.5);
            imageDisplay.Height = (int)(originalHeight * scale + 0.5);

            if (taskRunning)
            {
                stopTask = true;
            }

            int count       = spriteCollection.Children.Count;
            int updateNumer = (int)(50);
            int runtime     = 0;

            if (updateNumer != 0)
            {
                runtime = count / updateNumer;
            }
            int updateAfter = count - (runtime * updateNumer);

            Task.Run(() =>
            {
                taskRunning = true;
                int index   = 0;
                while (index < runtime)
                {
                    spriteCollection.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
                    {
                        for (int i = 0; i < updateNumer; i++)
                        {
                            SpriteCut sprite = (SpriteCut)spriteCollection.Children[updateNumer * index + i];
                            sprite.UpdateToScale();
                        }
                    }));
                    index++;

                    if (stopTask)
                    {
                        return;
                    }
                }

                spriteCollection.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
                {
                    for (int i = 0; i < updateAfter; i++)
                    {
                        SpriteCut sprite = (SpriteCut)spriteCollection.Children[updateNumer * index + i];
                        sprite.UpdateToScale();
                    }
                }));
                taskRunning = false;
            });

            stopTask = false;
            imageViewPort.ScrollToVerticalOffset((int)(imageDisplay.Width * offSetX + 0.5));
            imageViewPort.ScrollToHorizontalOffset((int)(imageDisplay.Height * offSetY + 0.5));
        }
コード例 #3
0
        public override void StartJob(MouseButtonEventArgs e)
        {
            base.StartJob(e);

            int index = editor.GetMouseOverIndex(e.GetPosition(editor.SpriteCollection));

            if (index >= 0)
            {
                if (index != editingIndex)
                {
                    if (editingSprite != null)
                    {
                        editingSprite.Unselect();
                    }

                    editingIndex  = index;
                    editingSprite = (SpriteCut)editor.SpriteCollection.Children[editingIndex];
                    editingSprite.Select();

                    toolBar.EditButton.DataContext = editingSprite;
                }

                oldPoint   = e.GetPosition(editor.SpriteCollection);
                oldPoint.X = (int)(oldPoint.X / editor.Scale);
                oldPoint.Y = (int)(oldPoint.Y / editor.Scale);
            }
            else
            {
                isStarted = false;
            }
        }
コード例 #4
0
 public override void Unselect()
 {
     base.Unselect();
     if (editingSprite != null)
     {
         editingSprite.Unselect();
     }
     editingSprite = null;
     editingIndex  = -1;
     toolBar.EditButton.DataContext = null;
 }
コード例 #5
0
        public override void EndJob(MouseButtonEventArgs e)
        {
            if (!isStarted)
            {
                return;
            }

            base.EndJob(e);
            if (currentSprite.Width == 0 || currentSprite.Height == 0)
            {
                editor.SpriteCollection.Children.Remove(currentSprite);
            }
            currentSprite = null;
        }
コード例 #6
0
        public override void StartJob(MouseButtonEventArgs e)
        {
            base.StartJob(e);

            origin = e.GetPosition(editor.ImageDisplay);
            SpriteCut newSprite = new SpriteCut();

            editor.SpriteCollection.Children.Add(newSprite);
            newSprite.Index    = editor.SpriteCollection.Children.IndexOf(newSprite);
            currentSprite      = newSprite;
            newSprite.Width    = 0;
            newSprite.Height   = 0;
            origin.X           = origin.X / editor.Scale;
            origin.Y           = origin.Y / editor.Scale;
            newSprite.Position = origin;
        }
コード例 #7
0
        public void AutoSliceSprite()
        {
            if (currentTexture == null)
            {
                return;
            }

            AutoSliceWindow wnd = new AutoSliceWindow();

            wnd.Owner = MainWindow;
            if (wnd.ShowDialog() == true)
            {
                int spriteWidth  = int.Parse(wnd.widthText.Text);
                int spriteHeight = int.Parse(wnd.heightText.Text);

                int offSetLeft   = int.Parse(wnd.leftOffSet.Text);
                int offSetRight  = int.Parse(wnd.rightOffSet.Text);
                int offSetTop    = int.Parse(wnd.topOffSet.Text);
                int offSetBottom = int.Parse(wnd.bottomOffSet.Text);

                int gridWidth  = offSetTop + offSetBottom + spriteHeight;
                int gridHeight = offSetLeft + offSetRight + spriteWidth;
                if (gridWidth <= 0 || gridHeight <= 0)
                {
                    MessageBox.Show("Invalid input number");
                    return;
                }

                int cutRow = (int)(originalHeight / gridHeight);
                int cutCol = (int)(originalWidth / gridWidth);

                // If there is a single avaliable sprite it will clear all other sprite
                if (cutRow == 0 || cutCol == 0)
                {
                    return;
                }

                ProcessingWnd processingWnd = new ProcessingWnd();
                processingWnd.Text = "Slicing up the image :D";

                Task.Run(() =>
                {
                    processingWnd.Dispatcher.Invoke(new Action(() =>
                    {
                        processingWnd.Show();
                        MainWindow.IsEnabled = false;
                    }));

                    int index = 0;
                    spriteCollection.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
                    {
                        SpriteCollection.Children.Clear();
                    }));
                    for (int row = 0; row < cutRow; row++)
                    {
                        int offSetRow = gridHeight * row + offSetTop + offSetBottom * row;
                        spriteCollection.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
                        {
                            for (int col = 0; col < cutCol; col++)
                            {
                                SpriteCut sprite = new SpriteCut();
                                sprite.Position  = new Point(gridWidth * col + offSetLeft + offSetRight * col, offSetRow);
                                sprite.Width     = spriteWidth;
                                sprite.Height    = spriteHeight;
                                sprite.Index     = index;
                                spriteCollection.Children.Add(sprite);
                                index++;
                            }
                        }));
                    }

                    processingWnd.Dispatcher.Invoke(new Action(() =>
                    {
                        processingWnd.Close();
                        MainWindow.IsEnabled = true;
                    }));
                });
            }
        }