Browse Source

Open file

Added:
- Open file from computer
- Key binding to open file

Improved:
- Zoom on mouse scroll
- Changed a bit colors on popups
flabbet 6 years ago
parent
commit
faef7fc615

+ 52 - 0
PixiEditor/Models/ImportFileDialog.cs

@@ -0,0 +1,52 @@
+using PixiEditor.Views;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+
+namespace PixiEditor.Models
+{
+    class ImportFileDialog : CustomDialog
+    {
+        private int _fileWidth;
+
+        public int FileWidth
+        {
+            get { return _fileWidth; }
+            set { if (_fileWidth != value) { _fileWidth = value; RaisePropertyChanged("Width"); } }
+        }
+
+
+        private int _fileHeight;
+
+        public int FileHeight
+        {
+            get { return _fileHeight; }
+            set { if (_fileHeight != value) { _fileHeight = value; RaisePropertyChanged("FileHeight"); } }
+        }
+
+
+        private string _filePath;
+
+        public string FilePath
+        {
+            get { return _filePath; }
+            set { if (_filePath != value) { _filePath = value; RaisePropertyChanged("FilePath"); } }
+        }
+
+        public override bool ShowDialog()
+        {
+            ImportFilePopup popup = new ImportFilePopup();
+            popup.ShowDialog();
+            if (popup.DialogResult == true)
+            {
+                FileHeight = popup.ImportHeight;
+                FileWidth = popup.ImportWidth;
+                FilePath = popup.FilePath;
+            }
+            return (bool)popup.DialogResult;
+        }
+    }
+}

+ 33 - 0
PixiEditor/Models/Importer.cs

@@ -0,0 +1,33 @@
+using PixiEditor.Helpers;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Media.Imaging;
+using System.Windows.Media;
+
+namespace PixiEditor.Models
+{
+    public class Importer : NotifyableObject
+    { 
+        /// <summary>
+        /// Imports image from path and resizes it to given dimensions
+        /// </summary>
+        /// <param name="path">Path of image.</param>
+        /// <param name="width">New width of image.</param>
+        /// <param name="height">New height of image.</param>
+        /// <returns></returns>
+        public static WriteableBitmap ImportImage(string path, int width, int height)
+        {
+            Uri uri = new Uri(path);
+            BitmapImage bitmap = new BitmapImage();
+            bitmap.BeginInit();
+            bitmap.UriSource = uri;
+            bitmap.DecodePixelWidth = width;
+            bitmap.DecodePixelHeight = height;
+            bitmap.EndInit();
+            return new WriteableBitmap(bitmap);
+        }
+    }
+}

+ 6 - 1
PixiEditor/Models/Tools/ToolSet.cs

@@ -116,7 +116,12 @@ namespace PixiEditor.Models.Tools
             bm.FillRectangle(x1, y1, x2, y2, color);
             return bm;
         }
-
+        /// <summary>
+        /// Calculates center of thickness * thickness rectangle
+        /// </summary>
+        /// <param name="startPosition">Top left position of rectangle</param>
+        /// <param name="thickness">Thickness of rectangle</param>
+        /// <returns></returns>
         private static DCords CalculateThicknessCenter(Coordinates startPosition, int thickness)
         {
             int x1, x2, y1, y2;

+ 3 - 8
PixiEditor/PixiEditor.csproj

@@ -92,7 +92,8 @@
     <Compile Include="Models\Enums\FileType.cs" />
     <Compile Include="Models\ExColor.cs" />
     <Compile Include="Models\Exporter.cs" />
-    <Compile Include="Models\FeedbackDialog.cs" />
+    <Compile Include="Models\Importer.cs" />
+    <Compile Include="Models\ImportFileDialog.cs" />
     <Compile Include="Models\LayerGenerator.cs" />
     <Compile Include="Models\Layer.cs" />
     <Compile Include="Models\MousePositionConverter.cs" />
@@ -103,14 +104,12 @@
     <Compile Include="Models\Tools\ToolType.cs" />
     <Compile Include="Models\UndoManager.cs" />
     <Compile Include="ViewModels\FeedbackDialogViewModel.cs" />
+    <Compile Include="ViewModels\ImportFilePopupViewModel.cs" />
     <Compile Include="ViewModels\MenuButtonViewModel.cs" />
     <Compile Include="ViewModels\NewFileMenuViewModel.cs" />
     <Compile Include="ViewModels\SaveFilePopupViewModel.cs" />
     <Compile Include="ViewModels\ViewModelBase.cs" />
     <Compile Include="ViewModels\ViewModelMain.cs" />
-    <Compile Include="Views\FeedbackDialog.xaml.cs">
-      <DependentUpon>FeedbackDialog.xaml</DependentUpon>
-    </Compile>
     <Compile Include="Views\ImportFilePopup.xaml.cs">
       <DependentUpon>ImportFilePopup.xaml</DependentUpon>
     </Compile>
@@ -130,10 +129,6 @@
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>
     </Page>
-    <Page Include="Views\FeedbackDialog.xaml">
-      <SubType>Designer</SubType>
-      <Generator>MSBuild:Compile</Generator>
-    </Page>
     <Page Include="Views\ImportFilePopup.xaml">
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>

+ 127 - 0
PixiEditor/ViewModels/ImportFilePopupViewModel.cs

@@ -0,0 +1,127 @@
+using Microsoft.Win32;
+using PixiEditor.Helpers;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Media.Imaging;
+
+namespace PixiEditor.ViewModels
+{
+    class ImportFilePopupViewModel : ViewModelBase
+    {
+
+        public RelayCommand CloseButtonCommand { get; set; }
+        public RelayCommand DragMoveCommand { get; set; }
+        public RelayCommand ChoosePathCommand { get; set; }
+        public RelayCommand OkCommand { get; set; }
+
+
+        private string _pathButtonBorder = "#f08080";
+
+        public string PathButtonBorder
+        {
+            get { return _pathButtonBorder; }
+            set { if (_pathButtonBorder != value) { _pathButtonBorder = value; RaisePropertyChanged("PathButtonBorder"); } }
+        }
+
+
+        private bool _pathIsCorrect;
+
+        public bool PathIsCorrect
+        {
+            get { return _pathIsCorrect; }
+            set { if (_pathIsCorrect != value) { _pathIsCorrect = value; RaisePropertyChanged("PathIsCorrect"); } }
+        }
+
+
+        private string _filePath;
+
+        public string FilePath
+        {
+            get { return _filePath; }
+            set { if (_filePath != value) { _filePath = value; RaisePropertyChanged("FilePath"); } }
+        }
+
+
+        private int _importWidth = 16;
+
+        public int ImportWidth
+        {
+            get { return _importWidth; }
+            set { if (_importWidth != value) { _importWidth = value; RaisePropertyChanged("ImportWidth"); } }
+        }
+
+
+        private int _importHeight = 16;
+
+        public int ImportHeight
+        {
+            get { return _importHeight; }
+            set { if (_importHeight != value) { _importHeight = value; RaisePropertyChanged("ImportHeight"); } }
+        }
+
+        public ImportFilePopupViewModel()
+        {
+            CloseButtonCommand = new RelayCommand(CloseWindow);
+            DragMoveCommand = new RelayCommand(MoveWindow);
+            ChoosePathCommand = new RelayCommand(ChoosePath);
+            OkCommand = new RelayCommand(OkButton, CanClickOk);
+        }
+
+        /// <summary>
+        /// Command that handles Path choosing to save file
+        /// </summary>
+        /// <param name="parameter"></param>
+        private void ChoosePath(object parameter)
+        {
+            OpenFileDialog path = new OpenFileDialog()
+            {
+                Title = "Import path",
+                CheckPathExists = true,
+                Filter = "Image Files|*.png;*.jpeg;*.jpg"
+            };
+            if (path.ShowDialog() == true)
+            {
+                if (string.IsNullOrEmpty(path.FileName) == false)
+                {
+                    PathButtonBorder = "#b8f080";
+                    PathIsCorrect = true;
+                    FilePath = path.FileName;
+                    BitmapImage bitmap = new BitmapImage(new Uri(path.FileName));
+                    ImportHeight = (int)bitmap.Height;
+                    ImportWidth = (int)bitmap.Width;
+                }
+                else
+                {
+                    PathButtonBorder = "#f08080";
+                    PathIsCorrect = false;
+                }
+            }
+        }
+
+        private void CloseWindow(object parameter)
+        {
+            ((Window)parameter).DialogResult = false;
+            base.CloseButton(parameter);
+        }
+
+        private void MoveWindow(object parameter)
+        {
+            base.DragMove(parameter);
+        }
+
+        private void OkButton(object parameter)
+        {
+            ((Window)parameter).DialogResult = true;
+            base.CloseButton(parameter);
+        }
+
+        private bool CanClickOk(object property)
+        {
+            return PathIsCorrect == true;
+        }
+    }
+}

+ 20 - 1
PixiEditor/ViewModels/ViewModelMain.cs

@@ -42,6 +42,7 @@ namespace PixiEditor.ViewModels
         public RelayCommand RedoCommand { get; set; }
         public RelayCommand MouseUpCommand { get; set; }
         public RelayCommand RecenterZoomboxCommand { get; set; }
+        public RelayCommand OpenFileCommand { get; set; }
 
         private Layer _activeLayer;
 
@@ -127,6 +128,7 @@ namespace PixiEditor.ViewModels
             RedoCommand = new RelayCommand(Redo, CanRedo);
             MouseUpCommand = new RelayCommand(MouseUp);
             RecenterZoomboxCommand = new RelayCommand(RecenterZoombox);
+            OpenFileCommand = new RelayCommand(OpenFile);
             primaryToolSet = new ToolSet();
             UndoManager.SetMainRoot(this);
         }
@@ -248,7 +250,7 @@ namespace PixiEditor.ViewModels
                 Layers.Clear();
                 Layers.Add(new Layer(newFile.Width, newFile.Height));
                 ActiveLayer = Layers[0];
-            }            
+            }
         }
         #region SaveFile
         /// <summary>
@@ -277,6 +279,23 @@ namespace PixiEditor.ViewModels
         }
         #endregion
 
+        /// <summary>
+        /// Opens file from path.
+        /// </summary>
+        /// <param name="parameter"></param>
+        public void OpenFile(object parameter)
+        {
+            ImportFileDialog dialog = new ImportFileDialog();
+            if (dialog.ShowDialog() == true)
+            {
+                Layers.Clear();
+                Layers.Add(new Layer(dialog.FileWidth, dialog.FileHeight));
+                ActiveLayer = Layers[0];
+                ActiveLayer.LayerBitmap = Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight);
+                RefreshImage();
+            }
+        }
+
         /// <summary>
         /// For now, shows not implemented info, lol.
         /// </summary>

+ 48 - 3
PixiEditor/Views/ImportFilePopup.xaml

@@ -4,9 +4,54 @@
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:PixiEditor.Views"
+        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
+        xmlns:vm="clr-namespace:PixiEditor.ViewModels"
+        xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours"
         mc:Ignorable="d"
-        Title="ImportFilePopup" Height="450" Width="800">
-    <Grid>
-        
+        Title="ImportFilePopup" Height="350" Width="300" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Name="importFilePopup" DataContext="{DynamicResource ImportFilePopupViewModel}">
+    <Window.Resources>
+        <vm:ImportFilePopupViewModel x:Key="ImportFilePopupViewModel"/>
+    </Window.Resources>
+    <Grid Background="#303030">
+        <Grid.RowDefinitions>
+            <RowDefinition Height="4*"/>
+            <RowDefinition Height="67*"/>
+        </Grid.RowDefinitions>
+        <Grid Grid.Row="0" Background="#FF2C2C2C">
+            <i:Interaction.Triggers>
+                <i:EventTrigger EventName="MouseDown">
+                    <i:InvokeCommandAction Command="{Binding DragMoveCommand}"/>
+                </i:EventTrigger>
+            </i:Interaction.Triggers>
+            <Button Width="20" Height="20" VerticalAlignment="Top" HorizontalAlignment="Right" BorderThickness="0" Command="{Binding CloseButtonCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
+                <Button.Background>
+                    <ImageBrush ImageSource="/PixiEditor;component/Images/Cross.png" Stretch="Uniform"/>
+                </Button.Background>
+            </Button>
+        </Grid>
+        <StackPanel Grid.Row="1" >
+            <Label Height="40" Width="120" VerticalAlignment="Top" Content="Import" Foreground="Snow" HorizontalContentAlignment="Center" FontSize="24" Margin="0,10,0,0"/>
+        <StackPanel Background="#2D2D2D" Height="85" Width="190" Margin="0,10,0,0">
+                <DockPanel Height="20" Margin="0,15,0,0" Width="150">
+                    <TextBlock Width="40" HorizontalAlignment="Left" Text="Width:" Foreground="Snow" FontSize="14"/>
+                    <TextBox Style="{StaticResource DarkTextBoxStyle}" Width="75" Margin="-10,0,0,0" Text="{Binding ImportWidth, Mode=TwoWay}">
+                        <i:Interaction.Behaviors>
+                            <behaviors:AllowableCharactersTextBoxBehavior RegularExpression="^[0-9./-]+$" MaxLength="4"/>
+                        </i:Interaction.Behaviors>
+                    </TextBox>
+                </DockPanel>
+                <DockPanel Height="20" Margin="0,15,0,0" Width="150">
+                    <TextBlock Width="45" HorizontalAlignment="Left" Text="Height:" Foreground="Snow" FontSize="14"/>
+                    <TextBox Style="{StaticResource DarkTextBoxStyle}" Width="75" Margin="-15,0,0,0" Text="{Binding ImportHeight, Mode=TwoWay}">
+                        <i:Interaction.Behaviors>
+                            <behaviors:AllowableCharactersTextBoxBehavior RegularExpression="^[0-9./-]+$" MaxLength="4"/>
+                        </i:Interaction.Behaviors>
+                    </TextBox>
+                </DockPanel>
+
+            </StackPanel>
+            <Button Grid.Row="1" BorderThickness="1" Foreground="Snow" Height="40" Width="160" Margin="0,30,0,0" Content="File Path" Background="#303030" BorderBrush="{Binding PathButtonBorder}" Command="{Binding ChoosePathCommand}"/>
+        </StackPanel>
+        <Button Grid.Row="1" Height="30" Width="60" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="10" Style="{StaticResource DarkRoundButton}" Content="OK" Command="{Binding OkCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
     </Grid>
 </Window>

+ 32 - 1
PixiEditor/Views/ImportFilePopup.xaml.cs

@@ -1,4 +1,5 @@
-using System;
+using PixiEditor.ViewModels;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -19,9 +20,39 @@ namespace PixiEditor.Views
     /// </summary>
     public partial class ImportFilePopup : Window
     {
+        ImportFilePopupViewModel dc = new ImportFilePopupViewModel();
         public ImportFilePopup()
         {
             InitializeComponent();
+            this.DataContext = dc;
+        }
+
+
+
+
+        public int ImportHeight
+        {
+            get
+            {
+                return dc.ImportHeight;
+            }
+            set { dc.ImportWidth = value; }
+        }
+
+
+
+        public int ImportWidth
+        {
+            get { return dc.ImportWidth; }
+            set { dc.ImportWidth = value; }
+        }
+
+
+
+        public string FilePath
+        {
+            get { return dc.FilePath; }
+            set { dc.FilePath = value; }
         }
     }
 }

+ 1 - 1
PixiEditor/Views/MainDrawingPanel.xaml

@@ -9,7 +9,7 @@
              xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
              mc:Ignorable="d" 
              d:DesignHeight="450" d:DesignWidth="800" x:Name="mainDrawingPanel">
-    <xctk:Zoombox IsAnimated="False" Name="Zoombox" KeepContentInBounds="True" Loaded="Zoombox_Loaded">
+    <xctk:Zoombox IsAnimated="False" Name="Zoombox" KeepContentInBounds="True" Loaded="Zoombox_Loaded" ZoomModifiers="None" >
         <i:Interaction.Triggers>
             <i:EventTrigger EventName="MouseMove">
                 <i:InvokeCommandAction Command="{Binding MouseMoveCommand, ElementName=mainDrawingPanel, Mode=OneWay}"/>

+ 3 - 0
PixiEditor/Views/MainWindow.xaml

@@ -62,6 +62,8 @@
         <KeyBinding Key="U"
                     Command="{Binding SelectToolCommand}"
                     CommandParameter="Lighten"/>
+        <KeyBinding Key="O" Modifiers="Ctrl"
+                    Command="{Binding OpenFileCommand}"/>
 
 
     </Window.InputBindings>
@@ -84,6 +86,7 @@
                     <StackPanel>
                         <Button Style="{StaticResource MenuInsideButtonStyle}" Content="New" Command="{Binding GenerateDrawAreaCommand}"/>
                         <Button Style="{StaticResource MenuInsideButtonStyle}" Content="Save" Command="{Binding SaveFileCommand}"/>
+                        <Button Style="{StaticResource MenuInsideButtonStyle}" Content="Open" Command="{Binding OpenFileCommand}"/>
                     </StackPanel>
                 </vws:MenuButton.Item>
             </vws:MenuButton>

+ 2 - 2
PixiEditor/Views/SaveFilePopup.xaml

@@ -8,7 +8,7 @@
         xmlns:vm="clr-namespace:PixiEditor.ViewModels"
         xmlns:helpers="clr-namespace:PixiEditor.Helpers.Behaviours"
         mc:Ignorable="d"
-        Title="SaveFilePopup" Height="250" Width="400" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen"  Name="saveFilePopup">
+        Title="SaveFilePopup" Height="250" Width="400" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Name="saveFilePopup">
     <Grid Background="#303030">
         <Grid.RowDefinitions>
             <RowDefinition Height="20*"/>
@@ -27,7 +27,7 @@
             </Button>
         </Grid>
         <TextBlock Grid.Row="1" Height="30" Width="120" Foreground="Snow" VerticalAlignment="Top" HorizontalAlignment="Center" Text="File settings" TextAlignment="Center" FontSize="20"/>
-        <DockPanel Grid.Row="1" Height="70" Width="320" Margin="0,0,0,100" Background="#FF2E2E2E">
+        <DockPanel Grid.Row="1" Height="70" Width="320" Margin="0,0,0,100" Background="#2D2D2D">
             <TextBlock Foreground="Snow" Width="40" Height="40" HorizontalAlignment="Left" Margin="50,0,0,0" Text="Width:" TextAlignment="Center" Padding="0,11.5,0,0"/>
             <TextBox Style="{StaticResource DarkTextBoxStyle}" Width="70" Height="20" HorizontalAlignment="Left" Margin="5,0,0,0" Text="{Binding ElementName=saveFilePopup, Path=SaveWidth, Mode=TwoWay}">
                 <i:Interaction.Behaviors>