Exemplo n.º 1
0
        public PaletteSelector()
        {
            SetStyles();

            Grid mainGrid = new Grid();

            mainGrid.RowDefinitions.Add(new RowDefinition(0, GridUnitType.Auto));
            mainGrid.RowDefinitions.Add(new RowDefinition(0, GridUnitType.Auto));

            StackPanel headerPanel = new StackPanel()
            {
                Orientation = Avalonia.Layout.Orientation.Horizontal
            };

            mainGrid.Children.Add(headerPanel);

            headerPanel.Children.Add(new TextBlock()
            {
                Text = "Palette:", Margin = new Avalonia.Thickness(5), VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center
            });

            List <string> paletteNames = (from el in Palettes select el.Name).ToList();

            int defaultIndex = DefaultIndex;

            if (Palette.CurrentPalette != null && Palettes.Contains(Palette.CurrentPalette))
            {
                defaultIndex = Palettes.IndexOf(Palette.CurrentPalette);
            }

            PaletteSelectorBox = new ComboBox()
            {
                Items = paletteNames, SelectedIndex = defaultIndex
            };
            headerPanel.Children.Add(PaletteSelectorBox);

            Canvas deleteCanvas = GetDeleteCanvas();

            deleteCanvas.Margin = new Thickness(5, 0, 0, 0);
            headerPanel.Children.Add(deleteCanvas);
            deleteCanvas.PointerPressed += (s, e) =>
            {
                Palette palette = Palettes[PaletteSelectorBox.SelectedIndex];
                if (File.Exists(palette.FileName))
                {
                    File.Delete(palette.FileName);
                }

                Palettes.Remove(palette);

                if (Palettes.Count == 0)
                {
                    string  id         = Guid.NewGuid().ToString("N");
                    Palette newPalette = new Palette("Custom", "A custom palette", Path.Combine(PaletteDirectory, id + ".palette"));
                    newPalette.Save();
                    Palettes.Add(newPalette);
                }

                List <string> _paletteNames = (from el in Palettes select el.Name).ToList();
                PaletteSelectorBox.Items         = _paletteNames;
                PaletteSelectorBox.SelectedIndex = 0;
            };

            Canvas saveCanvas = GetSaveCanvas();

            headerPanel.Children.Add(saveCanvas);
            saveCanvas.PointerPressed += (s, e) =>
            {
                Palettes[PaletteSelectorBox.SelectedIndex].Save();
            };

            Canvas addCanvas = GetAddCanvas();

            headerPanel.Children.Add(addCanvas);
            addCanvas.PointerPressed += async(s, e) =>
            {
                await AddButtonClicked();

                addCanvas.Classes.Remove("pressed");
            };

            PaletteDescription = new TextBlock()
            {
                FontStyle = FontStyle.Italic, Foreground = new SolidColorBrush(Color.FromRgb(128, 128, 128)), VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center, Margin = new Thickness(5)
            };
            headerPanel.Children.Add(PaletteDescription);

            ScrollViewer paletteScroller = new ScrollViewer()
            {
                HorizontalScrollBarVisibility = Avalonia.Controls.Primitives.ScrollBarVisibility.Auto, VerticalScrollBarVisibility = Avalonia.Controls.Primitives.ScrollBarVisibility.Disabled, HorizontalContentAlignment = Avalonia.Layout.HorizontalAlignment.Left, Margin = new Thickness(5)
            };

            Grid.SetRow(paletteScroller, 1);
            mainGrid.Children.Add(paletteScroller);

            ScrollerContainer = new Grid()
            {
                HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Left
            };

            ScrollerContainer.Children.Add(BuildPaletteCanvas(Palettes[PaletteSelectorBox.SelectedIndex]));

            paletteScroller.Content = ScrollerContainer;
            PaletteDescription.Text = Palettes[PaletteSelectorBox.SelectedIndex].Description;

            PaletteSelectorBox.SelectionChanged += (s, e) =>
            {
                ScrollerContainer.Children.Clear();
                if (PaletteSelectorBox.SelectedIndex >= 0 && PaletteSelectorBox.SelectedIndex < Palettes.Count)
                {
                    Palette.CurrentPalette = Palettes[PaletteSelectorBox.SelectedIndex];
                    ScrollerContainer.Children.Add(BuildPaletteCanvas(Palettes[PaletteSelectorBox.SelectedIndex]));
                    PaletteDescription.Text = Palettes[PaletteSelectorBox.SelectedIndex].Description;
                }
                else
                {
                    Palette.CurrentPalette = null;
                }
            };

            if (PaletteSelectorBox.SelectedIndex >= 0 && PaletteSelectorBox.SelectedIndex < Palettes.Count)
            {
                Palette.CurrentPalette = Palettes[PaletteSelectorBox.SelectedIndex];
            }
            else
            {
                Palette.CurrentPalette = null;
            }

            this.Content = mainGrid;
        }