예제 #1
0
        public GraphicEditorVM()
        {
            PixelCommand = new RelayCommand <Pixel>((p) =>
            {
                if (SelectedColor.A != 0)
                {
                    var old = SelectedGraphicPixelData;
                    old[p.Y][p.X].PixelColor = SelectedColor;
                    SelectedGraphicPixelData = null;
                    SelectedGraphicPixelData = old;
                }
            });

            NewCommand = new RelayCommand(() =>
            {
                CurrentGraphic = new Graphic();
                isNewGraphic   = true;
            });

            SaveCommand = new RelayCommand(() =>
            {
                var colors   = currentGraphic.ValidColors;
                var backDict = new Dictionary <Color, char>();
                foreach (var kv in colors)
                {
                    backDict.Add(kv.Value, kv.Key);
                }

                var chars = new List <char>();
                foreach (var row in selectedGraphicPixelData)
                {
                    foreach (var pix in row)
                    {
                        chars.Add(backDict[pix.PixelColor]);
                    }
                }
                currentGraphic.Data = string.Concat(chars);

                var suc = currentGraphic.Save();
                if (!suc)
                {
                    MessageBox.Show(string.Format("Error saving file {0}.", currentGraphic.FilePath), "Error");
                    return;
                }

                if (isNewGraphic)
                {
                    currentSign.AddGraphic(currentGraphic);
                }

                isNewGraphic = false;
                RaisePropertyChanged("");
                MainWindow.refreshGraphics();
            });

            ResetCommand = new RelayCommand(() =>
            {
            });

            DeleteCommand = new RelayCommand(() =>
            {
                if (!isNewGraphic)
                {
                    var ans = MessageBox.Show(string.Format("Are you sure you want to delete {0}?", currentGraphic.FileName), "Confirm", MessageBoxButton.YesNo);
                    if (ans == MessageBoxResult.No)
                    {
                        return;
                    }

                    var suc = currentGraphic.Delete();
                    if (!suc)
                    {
                        MessageBox.Show(string.Format("Error deleting file {0}.", currentGraphic.FilePath), "Error");
                        return;
                    }
                }

                currentSign.RemoveGraphic(CurrentGraphic);
                NewCommand.Execute(null);
                RaisePropertyChanged("");
                MainWindow.refreshGraphics();
            });
        }