Browse Source

Layers are working properly

Frytek 5 years ago
parent
commit
119731edbc

+ 32 - 0
PixiEditorDotNetCore3/Helpers/Converters/BoolToVisibilityConverter.cs

@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Text;
+using System.Windows.Data;
+
+namespace PixiEditor.Helpers.Converters
+{
+    public class BoolToVisibilityConverter : IValueConverter
+    {
+        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            if(value.ToString() == "Hidden" || value.ToString() == "Collapsed")
+            {
+                return false;
+            }
+            return true;
+        }
+
+        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            if(value is bool)
+            {
+                if((bool)value == false)
+                {
+                    return "Hidden";
+                }
+            }
+            return "Visible";
+        }
+    }
+}

+ 0 - 0
PixiEditorDotNetCore3/Helpers/ToolSizeToIntConverter.cs → PixiEditorDotNetCore3/Helpers/Converters/ToolSizeToIntConverter.cs


+ 2 - 32
PixiEditorDotNetCore3/Models/Controllers/BitmapOperationsUtility.cs

@@ -19,7 +19,7 @@ namespace PixiEditor.Models.Controllers
         public MouseMovementController MouseController { get; set; }
         public MouseMovementController MouseController { get; set; }
         public Tool SelectedTool { get; set; }
         public Tool SelectedTool { get; set; }
 
 
-        private ObservableCollection<Layer> _layers;
+        private ObservableCollection<Layer> _layers = new ObservableCollection<Layer>();
 
 
         public ObservableCollection<Layer> Layers
         public ObservableCollection<Layer> Layers
         {
         {
@@ -49,7 +49,6 @@ namespace PixiEditor.Models.Controllers
 
 
         public BitmapOperationsUtility()
         public BitmapOperationsUtility()
         {
         {
-            Layers = new ObservableCollection<Layer>();
             MouseController = new MouseMovementController();
             MouseController = new MouseMovementController();
             MouseController.MousePositionChanged += Controller_MousePositionChanged;
             MouseController.MousePositionChanged += Controller_MousePositionChanged;
         }
         }
@@ -69,10 +68,9 @@ namespace PixiEditor.Models.Controllers
         public void AddNewLayer(string name, int height, int width, bool setAsActive = true)
         public void AddNewLayer(string name, int height, int width, bool setAsActive = true)
         {
         {
             Layers.Add(new Layer(name, width, height));
             Layers.Add(new Layer(name, width, height));
-            Layers.Move(Layers.Count - 1, 0);
             if (setAsActive)
             if (setAsActive)
             {
             {
-                ActiveLayerIndex = 0;
+                ActiveLayerIndex = Layers.Count - 1;
             }
             }
             LayersChanged?.Invoke(this, new LayersChangedEventArgs(0, LayerAction.Add));
             LayersChanged?.Invoke(this, new LayersChangedEventArgs(0, LayerAction.Add));
         }
         }
@@ -83,34 +81,6 @@ namespace PixiEditor.Models.Controllers
             LayersChanged?.Invoke(this, new LayersChangedEventArgs(index, LayerAction.SetActive));
             LayersChanged?.Invoke(this, new LayersChangedEventArgs(index, LayerAction.SetActive));
         }
         }
 
 
-        public WriteableBitmap GetCombinedBitmaps()
-        {
-            Layer[] visibleLayers = Layers.Where(x => x.IsVisible).ToArray();
-            visibleLayers.Reverse();
-            int width = visibleLayers[0].Width;
-            int height = visibleLayers[0].Height;
-            WriteableBitmap finalBitmap = BitmapFactory.New(width, height);
-            finalBitmap.Lock();
-            finalBitmap = WriteLayersToBitmap(finalBitmap, visibleLayers);
-            finalBitmap.Unlock();
-            return finalBitmap;
-        }
-
-        private WriteableBitmap WriteLayersToBitmap(WriteableBitmap targetBitmap, Layer[] layers)
-        {
-            for (int i = 0; i < layers.Length; i++)
-            {
-                Coordinates[] nonTransparentCords = layers[i].GetNonTransprarentPixels();
-                for (int j = 0; j < nonTransparentCords.Length; j++)
-                {
-                    int x = nonTransparentCords[j].X;
-                    int y = nonTransparentCords[j].Y;
-                    targetBitmap.SetPixel(x, y, layers[i].LayerBitmap.GetPixel(x,y));
-                }
-            }
-            return targetBitmap;
-        }
-
     }
     }
 }
 }
 
 

+ 11 - 20
PixiEditorDotNetCore3/Models/Layers/Layer.cs

@@ -1,7 +1,5 @@
-using PixiEditor.Models.Position;
-using PixiEditor.Models.Tools;
+using PixiEditor.Models.Tools;
 using System;
 using System;
-using System.Collections.Generic;
 using System.Windows.Media.Imaging;
 using System.Windows.Media.Imaging;
 
 
 namespace PixiEditor.Models.Layers
 namespace PixiEditor.Models.Layers
@@ -10,7 +8,16 @@ namespace PixiEditor.Models.Layers
     {
     {
         private WriteableBitmap _layerBitmap;
         private WriteableBitmap _layerBitmap;
         public string Name { get; set; }
         public string Name { get; set; }
-        public bool IsVisible { get; set; } = true;
+        private bool _isVisible = true;
+        public bool IsVisible
+        {
+            get => _isVisible;
+            set
+            {
+                _isVisible = value;
+                RaisePropertyChanged("IsVisible");
+            }
+        }
 
 
         public WriteableBitmap LayerBitmap
         public WriteableBitmap LayerBitmap
         {
         {
@@ -52,22 +59,6 @@ namespace PixiEditor.Models.Layers
             LayerBitmap.Unlock();
             LayerBitmap.Unlock();
         }
         }
 
 
-        public Coordinates[] GetNonTransprarentPixels()
-        {
-            List<Coordinates> coordinates = new List<Coordinates>();
-            for (int y = 0; y < Height; y++)
-            {
-                for (int x = 0; x < Width; x++)
-                {
-                    if (LayerBitmap.GetPixeli(x, y) != 0)
-                    {
-                        coordinates.Add(new Coordinates(x, y));
-                    }
-                }
-            }
-            return coordinates.ToArray();
-        }
-
         public byte[] ConvertBitmapToBytes()
         public byte[] ConvertBitmapToBytes()
         {            
         {            
             LayerBitmap.Lock();
             LayerBitmap.Lock();

+ 3 - 0
PixiEditorDotNetCore3/PixiEditor.csproj

@@ -35,5 +35,8 @@
     <Resource Include="Images\RectangleImage.png" />
     <Resource Include="Images\RectangleImage.png" />
     <Resource Include="Images\transparentbg.png" />
     <Resource Include="Images\transparentbg.png" />
   </ItemGroup>
   </ItemGroup>
+  <ItemGroup>
+    <Folder Include="Helpers\UI\" />
+  </ItemGroup>
   
   
 </Project>
 </Project>

+ 10 - 45
PixiEditorDotNetCore3/ViewModels/ViewModelMain.cs

@@ -18,7 +18,6 @@ using PixiEditor.Models.Images;
 using PixiEditor.Models.IO;
 using PixiEditor.Models.IO;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
-using PixiEditor.Models.Enums;
 
 
 namespace PixiEditor.ViewModels
 namespace PixiEditor.ViewModels
 {
 {
@@ -39,18 +38,6 @@ namespace PixiEditor.ViewModels
         public RelayCommand NewLayerCommand { get; set; }
         public RelayCommand NewLayerCommand { get; set; }
         public RelayCommand ReloadImageCommand { get; set; }
         public RelayCommand ReloadImageCommand { get; set; }
 
 
-        private Image _activeImage;
-
-        public Image ActiveImage
-        {
-            get => _activeImage;
-            set
-            {
-                _activeImage = value;
-                RaisePropertyChanged("ActiveImage");
-            }
-        }
-
         private double _mouseXonCanvas;
         private double _mouseXonCanvas;
 
 
         public double MouseXOnCanvas //Mouse X coordinate relative to canvas
         public double MouseXOnCanvas //Mouse X coordinate relative to canvas
@@ -131,8 +118,6 @@ namespace PixiEditor.ViewModels
         {
         {
             PixiFilesManager.InitializeTempDirectories();
             PixiFilesManager.InitializeTempDirectories();
             BitmapUtility = new BitmapOperationsUtility();
             BitmapUtility = new BitmapOperationsUtility();
-            BitmapUtility.BitmapChanged += BitmapUtility_BitmapChanged;
-            BitmapUtility.LayersChanged += BitmapUtility_LayersChanged;
             SelectToolCommand = new RelayCommand(RecognizeTool);
             SelectToolCommand = new RelayCommand(RecognizeTool);
             GenerateDrawAreaCommand = new RelayCommand(GenerateDrawArea);
             GenerateDrawAreaCommand = new RelayCommand(GenerateDrawArea);
             MouseMoveCommand = new RelayCommand(MouseMove);
             MouseMoveCommand = new RelayCommand(MouseMove);
@@ -153,20 +138,9 @@ namespace PixiEditor.ViewModels
             ToolSize = 1;
             ToolSize = 1;
         }
         }
 
 
-        private void BitmapUtility_LayersChanged(object sender, LayersChangedEventArgs e)
-        {
-            RefreshImage();
-        }
-
-        private void BitmapUtility_BitmapChanged(object sender, BitmapChangedEventArgs e)
-        {
-            RefreshImage();
-        }
-
         public void SetActiveLayer(object parameter)
         public void SetActiveLayer(object parameter)
         {
         {
             BitmapUtility.SetActiveLayer((int)parameter);
             BitmapUtility.SetActiveLayer((int)parameter);
-            ActiveImage.Source = BitmapUtility.GetCombinedBitmaps();
         }
         }
 
 
         #region Undo/Redo
         #region Undo/Redo
@@ -281,7 +255,6 @@ namespace PixiEditor.ViewModels
             {
             {
                 BitmapUtility.Layers.Clear();
                 BitmapUtility.Layers.Clear();
                 BitmapUtility.AddNewLayer("Base Layer", newFile.Width, newFile.Height, true);
                 BitmapUtility.AddNewLayer("Base Layer", newFile.Width, newFile.Height, true);
-                ActiveImage = ImageGenerator.GenerateForPixelArts(newFile.Width, newFile.Height);
             }
             }
         }
         }
         #region SaveFile
         #region SaveFile
@@ -291,14 +264,15 @@ namespace PixiEditor.ViewModels
         /// <param name="parameter"></param>
         /// <param name="parameter"></param>
         private void SaveFile(object parameter)
         private void SaveFile(object parameter)
         {
         {
-            if (Exporter.SavePath == null)
-            {
-                Exporter.Export(FileType.PNG, ActiveImage, new Size(BitmapUtility.ActiveLayer.Width, BitmapUtility.ActiveLayer.Height));
-            }
-            else
-            {
-                Exporter.ExportWithoutDialog(FileType.PNG, ActiveImage);
-            }
+            //TODO: Blend bitmaps and save file
+        //    if (Exporter.SavePath == null)
+        //    {
+        //        Exporter.Export(FileType.PNG, ActiveImage, new Size(BitmapUtility.ActiveLayer.Width, BitmapUtility.ActiveLayer.Height));
+        //    }
+        //    else
+        //    {
+        //        Exporter.ExportWithoutDialog(FileType.PNG, ActiveImage);
+        //    }
         }
         }
         /// <summary>
         /// <summary>
         /// Returns true if file save is possible.
         /// Returns true if file save is possible.
@@ -322,7 +296,6 @@ namespace PixiEditor.ViewModels
             {
             {
                 BitmapUtility.Layers.Clear();
                 BitmapUtility.Layers.Clear();
                 BitmapUtility.AddNewLayer("Base Layer", dialog.FileWidth, dialog.FileHeight, true);
                 BitmapUtility.AddNewLayer("Base Layer", dialog.FileWidth, dialog.FileHeight, true);
-                ActiveImage = ImageGenerator.GenerateForPixelArts(dialog.FileWidth, dialog.FileHeight);
                 BitmapUtility.ActiveLayer.LayerBitmap = Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight);
                 BitmapUtility.ActiveLayer.LayerBitmap = Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight);
             }
             }
         }
         }
@@ -336,17 +309,9 @@ namespace PixiEditor.ViewModels
             MessageBox.Show("This feature is not implemented yet.", "Feature not implemented", MessageBoxButton.OK, MessageBoxImage.Information);
             MessageBox.Show("This feature is not implemented yet.", "Feature not implemented", MessageBoxButton.OK, MessageBoxImage.Information);
         }
         }
 
 
-        public void RefreshImage()
-        {
-            if (ActiveImage != null)
-            {
-                ActiveImage.Source = BitmapUtility.ActiveLayer.LayerBitmap;
-            }
-        }
-
         public void NewLayer(object parameter)
         public void NewLayer(object parameter)
         {
         {
-            BitmapUtility.AddNewLayer("New Layer", BitmapUtility.Layers[0].Width, BitmapUtility.Layers[0].Height);         
+            BitmapUtility.AddNewLayer($"New Layer {BitmapUtility.Layers.Count}", BitmapUtility.Layers[0].Width, BitmapUtility.Layers[0].Height);         
         }
         }
 
 
         public bool CanCreateNewLayer(object parameter)
         public bool CanCreateNewLayer(object parameter)

+ 14 - 4
PixiEditorDotNetCore3/Views/MainWindow.xaml

@@ -5,7 +5,6 @@
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:vm="clr-namespace:PixiEditor.ViewModels"
         xmlns:vm="clr-namespace:PixiEditor.ViewModels"
         xmlns:vws="clr-namespace:PixiEditor.Views"
         xmlns:vws="clr-namespace:PixiEditor.Views"
-        xmlns:enums="clr-namespace:PixiEditor.Models.Enums"
         xmlns:helpers="clr-namespace:PixiEditor.Helpers"
         xmlns:helpers="clr-namespace:PixiEditor.Helpers"
         xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours"
         xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours"
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
@@ -128,7 +127,18 @@
                                 <behaviors:MouseBehaviour MouseX="{Binding MouseXOnCanvas, Mode=OneWayToSource}" MouseY="{Binding MouseYOnCanvas, Mode=OneWayToSource}"/>
                                 <behaviors:MouseBehaviour MouseX="{Binding MouseXOnCanvas, Mode=OneWayToSource}" MouseY="{Binding MouseYOnCanvas, Mode=OneWayToSource}"/>
                             </i:Interaction.Behaviors>
                             </i:Interaction.Behaviors>
                             <Image Source="/Images/transparentbg.png" Height="{Binding BitmapUtility.ActiveLayer.Height}" Width="{Binding BitmapUtility.ActiveLayer.Width}" Opacity="0.2" Stretch="UniformToFill"/>
                             <Image Source="/Images/transparentbg.png" Height="{Binding BitmapUtility.ActiveLayer.Height}" Width="{Binding BitmapUtility.ActiveLayer.Width}" Opacity="0.2" Stretch="UniformToFill"/>
-                            <ContentControl Content="{Binding ActiveImage}"/>
+                            <ItemsControl ItemsSource="{Binding BitmapUtility.Layers}">
+                                <ItemsControl.ItemsPanel>
+                                    <ItemsPanelTemplate>
+                                        <Grid/>
+                                    </ItemsPanelTemplate>
+                                </ItemsControl.ItemsPanel>
+                                <ItemsControl.ItemTemplate>
+                                    <DataTemplate>
+                                        <Image Source="{Binding LayerBitmap}" Visibility="{Binding IsVisible, Converter={StaticResource BoolToVisibilityConverter}}" RenderOptions.BitmapScalingMode="NearestNeighbor" Stretch="Uniform" Width="{Binding Width}" Height="{Binding Height}"/>
+                                    </DataTemplate>
+                                </ItemsControl.ItemTemplate>
+                            </ItemsControl>
                         </Canvas>
                         </Canvas>
                     </vws:MainDrawingPanel.Item>
                     </vws:MainDrawingPanel.Item>
                 </vws:MainDrawingPanel>
                 </vws:MainDrawingPanel>
@@ -211,8 +221,8 @@
                         <xcad:LayoutAnchorable ContentId="layers" Title="Layers" CanHide="False" CanClose="False" CanAutoHide="False" CanDockAsTabbedDocument="False" CanFloat="True">
                         <xcad:LayoutAnchorable ContentId="layers" Title="Layers" CanHide="False" CanClose="False" CanAutoHide="False" CanDockAsTabbedDocument="False" CanFloat="True">
                             <StackPanel  Orientation="Vertical">
                             <StackPanel  Orientation="Vertical">
                                 <Button Command="{Binding NewLayerCommand}" Height="30" Content="New Layer" HorizontalAlignment="Stretch" Margin="5" Style="{StaticResource DarkRoundButton}"/>
                                 <Button Command="{Binding NewLayerCommand}" Height="30" Content="New Layer" HorizontalAlignment="Stretch" Margin="5" Style="{StaticResource DarkRoundButton}"/>
-                                <ItemsControl ItemsSource="{Binding BitmapUtility.Layers}" AlternationCount="9999">
-                                    <ItemsControl.ItemTemplate>
+                                    <ItemsControl ItemsSource="{Binding BitmapUtility.Layers}" AlternationCount="9999">
+                                        <ItemsControl.ItemTemplate>
                                         <DataTemplate>
                                         <DataTemplate>
                                             <Border BorderBrush="Gray" BorderThickness="1">
                                             <Border BorderBrush="Gray" BorderThickness="1">
                                                 <DockPanel>
                                                 <DockPanel>