Browse Source

Merge pull request #84 from PixiEditor/dropdown-settings-hotfix

Fixed dropdown settings not changing
Krzysztof Krysiński 4 years ago
parent
commit
c5fcad3eee

+ 46 - 46
PixiEditor/Models/Tools/ToolSettings/Settings/DropdownSetting.cs

@@ -1,49 +1,49 @@
-using System.Windows;
-using System.Windows.Controls;
-using System.Windows.Controls.Primitives;
-using System.Windows.Data;
-
-namespace PixiEditor.Models.Tools.ToolSettings.Settings
-{
-    public class DropdownSetting : Setting<ComboBoxItem>
-    {
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Controls.Primitives;
+using System.Windows.Data;
+
+namespace PixiEditor.Models.Tools.ToolSettings.Settings
+{
+    public class DropdownSetting : Setting<object>
+    {
         public DropdownSetting(string name, string[] values, string label)
-            : base(name)
-        {
-            Values = values;
-            SettingControl = GenerateDropdown();
-            Value = (ComboBoxItem)((ComboBox)SettingControl).Items[0];
-            Label = label;
-        }
-
-        public string[] Values { get; set; }
+            : base(name)
+        {
+            Values = values;
+            SettingControl = GenerateDropdown();
+            Value = ((ComboBox)SettingControl).Items[0];
+            Label = label;
+        }
+
+        public string[] Values { get; set; }
+
+        private ComboBox GenerateDropdown()
+        {
+            ComboBox combobox = new ComboBox
+            {
+                VerticalAlignment = VerticalAlignment.Center
+            };
+            GenerateItems(combobox);
+
+            Binding binding = new Binding("Value")
+            {
+                Mode = BindingMode.TwoWay
+            };
+            combobox.SetBinding(Selector.SelectedValueProperty, binding);
+            return combobox;
+        }
 
-        private ComboBox GenerateDropdown()
-        {
-            ComboBox combobox = new ComboBox
-            {
-                VerticalAlignment = VerticalAlignment.Center
-            };
-            GenerateItems(combobox);
-
-            Binding binding = new Binding("Value")
-            {
-                Mode = BindingMode.TwoWay
-            };
-            combobox.SetBinding(Selector.SelectedValueProperty, binding);
-            return combobox;
-        }
-
-        private void GenerateItems(ComboBox comboBox)
-        {
-            for (int i = 0; i < Values.Length; i++)
-            {
-                ComboBoxItem item = new ComboBoxItem
-                {
-                    Content = Values[i]
-                };
-                comboBox.Items.Add(item);
-            }
-        }
-    }
+        private void GenerateItems(ComboBox comboBox)
+        {
+            for (int i = 0; i < Values.Length; i++)
+            {
+                ComboBoxItem item = new ComboBoxItem
+                {
+                    Content = Values[i]
+                };
+                comboBox.Items.Add(item);
+            }
+        }
+    }
 }

+ 70 - 70
PixiEditor/Models/Tools/Tools/BrightnessTool.cs

@@ -1,47 +1,47 @@
-using System;
-using System.Collections.Generic;
-using System.Windows.Controls;
-using System.Windows.Input;
-using System.Windows.Media;
-using PixiEditor.Models.Colors;
-using PixiEditor.Models.DataHolders;
-using PixiEditor.Models.Enums;
-using PixiEditor.Models.Layers;
-using PixiEditor.Models.Position;
-using PixiEditor.Models.Tools.ToolSettings.Settings;
-using PixiEditor.Models.Tools.ToolSettings.Toolbars;
-
-namespace PixiEditor.Models.Tools.Tools
-{
-    public class BrightnessTool : BitmapOperationTool
-    {
-        private const float CorrectionFactor = 5f; // Initial correction factor
-
+using System;
+using System.Collections.Generic;
+using System.Windows.Controls;
+using System.Windows.Input;
+using System.Windows.Media;
+using PixiEditor.Models.Colors;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Enums;
+using PixiEditor.Models.Layers;
+using PixiEditor.Models.Position;
+using PixiEditor.Models.Tools.ToolSettings.Settings;
+using PixiEditor.Models.Tools.ToolSettings.Toolbars;
+
+namespace PixiEditor.Models.Tools.Tools
+{
+    public class BrightnessTool : BitmapOperationTool
+    {
+        private const float CorrectionFactor = 5f; // Initial correction factor
+
         private List<Coordinates> pixelsVisited = new List<Coordinates>();
 
-        public BrightnessTool()
-        {
-            Tooltip = "Makes pixel brighter or darker pixel (U). Hold Ctrl to make pixel darker.";
-            Toolbar = new BrightnessToolToolbar(CorrectionFactor);
-        }
-
-        public override ToolType ToolType => ToolType.Brightness;
+        public BrightnessTool()
+        {
+            Tooltip = "Makes pixel brighter or darker pixel (U). Hold Ctrl to make pixel darker.";
+            Toolbar = new BrightnessToolToolbar(CorrectionFactor);
+        }
+
+        public override ToolType ToolType => ToolType.Brightness;
+
+        public BrightnessMode Mode { get; set; } = BrightnessMode.Default;
+
+        public override void OnRecordingLeftMouseDown(MouseEventArgs e)
+        {
+            pixelsVisited.Clear();
+        }
+
+        public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)
+        {
+            int toolSize = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
+            float correctionFactor = Toolbar.GetSetting<FloatSetting>("CorrectionFactor").Value;
+            Enum.TryParse((Toolbar.GetSetting<DropdownSetting>("BrightnessMode")?.Value as ComboBoxItem)?.Content as string, out BrightnessMode mode);
+            Mode = mode;
 
-        public BrightnessMode Mode { get; set; } = BrightnessMode.Default;
-
-        public override void OnRecordingLeftMouseDown(MouseEventArgs e)
-        {
-            pixelsVisited.Clear();
-        }
-
-        public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)
-        {
-            int toolSize = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
-            float correctionFactor = Toolbar.GetSetting<FloatSetting>("CorrectionFactor").Value;
-            Enum.TryParse((Toolbar.GetSetting<DropdownSetting>("Mode")?.Value as ComboBoxItem)?.Content as string, out BrightnessMode mode);
-            Mode = mode;
-
-            LayerChange[] layersChanges = new LayerChange[1];
+            LayerChange[] layersChanges = new LayerChange[1];
             if (Keyboard.IsKeyDown(Key.LeftCtrl))
             {
                 layersChanges[0] = new LayerChange(ChangeBrightness(layer, coordinates[0], toolSize, -correctionFactor), layer);
@@ -51,41 +51,41 @@ namespace PixiEditor.Models.Tools.Tools
                 layersChanges[0] = new LayerChange(ChangeBrightness(layer, coordinates[0], toolSize, correctionFactor), layer);
             }
 
-            return layersChanges;
-        }
-
-        public BitmapPixelChanges ChangeBrightness(Layer layer, Coordinates coordinates, int toolSize, float correctionFactor)
-        {
-            DoubleCords centeredCoords = CoordinatesCalculator.CalculateThicknessCenter(coordinates, toolSize);
+            return layersChanges;
+        }
+
+        public BitmapPixelChanges ChangeBrightness(Layer layer, Coordinates coordinates, int toolSize, float correctionFactor)
+        {
+            DoubleCords centeredCoords = CoordinatesCalculator.CalculateThicknessCenter(coordinates, toolSize);
             Coordinates[] rectangleCoordinates = CoordinatesCalculator.RectangleToCoordinates(
-                centeredCoords.Coords1.X,
-                centeredCoords.Coords1.Y,
-                centeredCoords.Coords2.X,
-                centeredCoords.Coords2.Y);
-            BitmapPixelChanges changes = new BitmapPixelChanges(new Dictionary<Coordinates, Color>());
-
-            for (int i = 0; i < rectangleCoordinates.Length; i++)
-            {
-                if (Mode == BrightnessMode.Default)
-                {
+                centeredCoords.Coords1.X,
+                centeredCoords.Coords1.Y,
+                centeredCoords.Coords2.X,
+                centeredCoords.Coords2.Y);
+            BitmapPixelChanges changes = new BitmapPixelChanges(new Dictionary<Coordinates, Color>());
+
+            for (int i = 0; i < rectangleCoordinates.Length; i++)
+            {
+                if (Mode == BrightnessMode.Default)
+                {
                     if (pixelsVisited.Contains(rectangleCoordinates[i]))
                     {
                         continue;
                     }
 
-                    pixelsVisited.Add(rectangleCoordinates[i]);
-                }
-
-                Color pixel = layer.GetPixelWithOffset(rectangleCoordinates[i].X, rectangleCoordinates[i].Y);
+                    pixelsVisited.Add(rectangleCoordinates[i]);
+                }
+
+                Color pixel = layer.GetPixelWithOffset(rectangleCoordinates[i].X, rectangleCoordinates[i].Y);
                 Color newColor = ExColor.ChangeColorBrightness(
-                    Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B),
-                    correctionFactor);
+                    Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B),
+                    correctionFactor);
                 changes.ChangedPixels.Add(
-                    new Coordinates(rectangleCoordinates[i].X, rectangleCoordinates[i].Y),
-                    newColor);
-            }
-
-            return changes;
-        }
-    }
+                    new Coordinates(rectangleCoordinates[i].X, rectangleCoordinates[i].Y),
+                    newColor);
+            }
+
+            return changes;
+        }
+    }
 }

+ 78 - 78
PixiEditor/Models/Tools/Tools/SelectTool.cs

@@ -1,91 +1,91 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Windows.Controls;
-using System.Windows.Input;
-using PixiEditor.Models.Controllers;
-using PixiEditor.Models.DataHolders;
-using PixiEditor.Models.Enums;
-using PixiEditor.Models.Position;
-using PixiEditor.Models.Tools.ToolSettings.Settings;
-using PixiEditor.Models.Tools.ToolSettings.Toolbars;
-using PixiEditor.ViewModels;
-
-namespace PixiEditor.Models.Tools.Tools
-{
-    public class SelectTool : ReadonlyTool
-    {
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Windows.Controls;
+using System.Windows.Input;
+using PixiEditor.Models.Controllers;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Enums;
+using PixiEditor.Models.Position;
+using PixiEditor.Models.Tools.ToolSettings.Settings;
+using PixiEditor.Models.Tools.ToolSettings.Toolbars;
+using PixiEditor.ViewModels;
+
+namespace PixiEditor.Models.Tools.Tools
+{
+    public class SelectTool : ReadonlyTool
+    {
         private Selection oldSelection;
 
-        public SelectTool()
-        {
-            Tooltip = "Selects area. (M)";
-            Toolbar = new SelectToolToolbar();
+        public SelectTool()
+        {
+            Tooltip = "Selects area. (M)";
+            Toolbar = new SelectToolToolbar();
         }
 
-        public SelectionType SelectionType { get; set; } = SelectionType.Add;
-
-        public override ToolType ToolType => ToolType.Select;
-
-        public override void OnRecordingLeftMouseDown(MouseEventArgs e)
-        {
-            Enum.TryParse((Toolbar.GetSetting<DropdownSetting>("Mode")?.Value as ComboBoxItem)?.Content as string, out SelectionType selectionType);
-            SelectionType = selectionType;
-
-            oldSelection = null;
-            if (ViewModelMain.Current.ActiveSelection != null &&
+        public SelectionType SelectionType { get; set; } = SelectionType.Add;
+
+        public override ToolType ToolType => ToolType.Select;
+
+        public override void OnRecordingLeftMouseDown(MouseEventArgs e)
+        {
+            Enum.TryParse((Toolbar.GetSetting<DropdownSetting>("SelectMode")?.Value as ComboBoxItem)?.Content as string, out SelectionType selectionType);
+            SelectionType = selectionType;
+
+            oldSelection = null;
+            if (ViewModelMain.Current.ActiveSelection != null &&
                 ViewModelMain.Current.ActiveSelection.SelectedPoints != null)
             {
                 oldSelection = ViewModelMain.Current.ActiveSelection;
             }
-        }
-
-        public override void OnStoppedRecordingMouseUp(MouseEventArgs e)
-        {
-            if (ViewModelMain.Current.ActiveSelection.SelectedPoints.Count() <= 1)
-            {
-                // If we have not selected multiple points, clear the selection
-                ViewModelMain.Current.ActiveSelection.Clear();
-            }
-
-            UndoManager.AddUndoChange(new Change("ActiveSelection", oldSelection, ViewModelMain.Current.ActiveSelection, "Select pixels"));
-        }
-
-        public override void Use(Coordinates[] pixels)
-        {
-            Select(pixels);
         }
 
-        public IEnumerable<Coordinates> GetRectangleSelectionForPoints(Coordinates start, Coordinates end)
-        {
-            RectangleTool rectangleTool = new RectangleTool();
-            List<Coordinates> selection = rectangleTool.CreateRectangle(start, end, 1).ToList();
-            selection.AddRange(rectangleTool.CalculateFillForRectangle(start, end, 1));
-            return selection;
-        }
-
-        /// <summary>
-        ///     Gets coordinates of every pixel in root layer.
-        /// </summary>
-        /// <returns>Coordinates array of pixels.</returns>
-        public IEnumerable<Coordinates> GetAllSelection()
-        {
-            return GetAllSelection(ViewModelMain.Current.BitmapManager.ActiveDocument);
-        }
-
-        /// <summary>
-        ///     Gets coordinates of every pixel in chosen document.
-        /// </summary>
-        /// <returns>Coordinates array of pixels.</returns>
-        public IEnumerable<Coordinates> GetAllSelection(Document document)
-        {
-            return GetRectangleSelectionForPoints(new Coordinates(0, 0), new Coordinates(document.Width - 1, document.Height - 1));
+        public override void OnStoppedRecordingMouseUp(MouseEventArgs e)
+        {
+            if (ViewModelMain.Current.ActiveSelection.SelectedPoints.Count() <= 1)
+            {
+                // If we have not selected multiple points, clear the selection
+                ViewModelMain.Current.ActiveSelection.Clear();
+            }
+
+            UndoManager.AddUndoChange(new Change("ActiveSelection", oldSelection, ViewModelMain.Current.ActiveSelection, "Select pixels"));
+        }
+
+        public override void Use(Coordinates[] pixels)
+        {
+            Select(pixels);
+        }
+
+        public IEnumerable<Coordinates> GetRectangleSelectionForPoints(Coordinates start, Coordinates end)
+        {
+            RectangleTool rectangleTool = new RectangleTool();
+            List<Coordinates> selection = rectangleTool.CreateRectangle(start, end, 1).ToList();
+            selection.AddRange(rectangleTool.CalculateFillForRectangle(start, end, 1));
+            return selection;
+        }
+
+        /// <summary>
+        ///     Gets coordinates of every pixel in root layer.
+        /// </summary>
+        /// <returns>Coordinates array of pixels.</returns>
+        public IEnumerable<Coordinates> GetAllSelection()
+        {
+            return GetAllSelection(ViewModelMain.Current.BitmapManager.ActiveDocument);
+        }
+
+        /// <summary>
+        ///     Gets coordinates of every pixel in chosen document.
+        /// </summary>
+        /// <returns>Coordinates array of pixels.</returns>
+        public IEnumerable<Coordinates> GetAllSelection(Document document)
+        {
+            return GetRectangleSelectionForPoints(new Coordinates(0, 0), new Coordinates(document.Width - 1, document.Height - 1));
         }
 
-        private void Select(Coordinates[] pixels)
-        {
-            IEnumerable<Coordinates> selection = GetRectangleSelectionForPoints(pixels[^1], pixels[0]);
-            ViewModelMain.Current.ActiveSelection.SetSelection(selection, SelectionType);
-        }
-    }
+        private void Select(Coordinates[] pixels)
+        {
+            IEnumerable<Coordinates> selection = GetRectangleSelectionForPoints(pixels[^1], pixels[0]);
+            ViewModelMain.Current.ActiveSelection.SetSelection(selection, SelectionType);
+        }
+    }
 }