Quellcode durchsuchen

Implement very basic drawing

Equbuxu vor 3 Jahren
Ursprung
Commit
5524d83f80

+ 13 - 15
src/PixiEditor/Models/Controllers/MouseInputFilter.cs

@@ -1,17 +1,14 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Windows;
+using System.Windows;
 using System.Windows.Input;
+using ChunkyImageLib.DataHolders;
+using PixiEditor.Models.Events;
 
 namespace PixiEditor.Models.Controllers;
 
 internal class MouseInputFilter
 {
-    public EventHandler<MouseButton> OnMouseDown;
-    public EventHandler OnMouseMove;
+    public EventHandler<MouseOnCanvasEventArgs> OnMouseDown;
+    public EventHandler<VecD> OnMouseMove;
     public EventHandler<MouseButton> OnMouseUp;
 
 
@@ -22,22 +19,23 @@ internal class MouseInputFilter
         [MouseButton.Middle] = MouseButtonState.Released,
     };
 
-    public void MouseDown(object args) => MouseDown(((MouseButtonEventArgs)args).ChangedButton);
-    public void MouseDown(MouseButton button)
+    public void MouseDown(object args) => MouseDown((MouseOnCanvasEventArgs)args);
+    public void MouseDown(MouseOnCanvasEventArgs args)
     {
+        var button = args.Button;
+
         if (button is MouseButton.XButton1 or MouseButton.XButton2)
             return;
         if (buttonStates[button] == MouseButtonState.Pressed)
             return;
         buttonStates[button] = MouseButtonState.Pressed;
 
-        OnMouseDown?.Invoke(this, button);
+        OnMouseDown?.Invoke(this, args);
     }
 
-    public void MouseMove(object args) => OnMouseMove?.Invoke(this, EventArgs.Empty);
-    public void MouseMove(MouseEventArgs args) => OnMouseMove?.Invoke(this, EventArgs.Empty);
+    public void MouseMove(object args) => OnMouseMove?.Invoke(this, (VecD)args);
 
-    public void MouseUp(object args) => MouseUp(((MouseButtonEventArgs)args).ChangedButton);
+    public void MouseUp(object args) => MouseUp((MouseButton)args);
     public void MouseUp(object sender, Point p, MouseButton button) => MouseUp(button);
     public void MouseUp(MouseButton button)
     {
@@ -49,4 +47,4 @@ internal class MouseInputFilter
 
         OnMouseUp?.Invoke(this, button);
     }
-}
+}

+ 2 - 2
src/PixiEditor/Models/DocumentModels/DocumentUpdater.cs

@@ -131,7 +131,7 @@ internal class DocumentUpdater
 
     private void ProcessSelection(Selection_ChangeInfo info)
     {
-        doc.SetSelectionPath(info.NewPath);
+        doc.InternalUpdateSelectionPath(info.NewPath);
     }
 
     private void ProcessLayerLockTransparency(LayerLockTransparency_ChangeInfo info)
@@ -211,7 +211,7 @@ internal class DocumentUpdater
 
         doc.Bitmaps = newBitmaps;
 
-        doc.SetSize(info.Size);
+        doc.InternalSetSize(info.Size);
         doc.SetVerticalSymmetryAxisX(info.VerticalSymmetryAxisX);
         doc.SetHorizontalSymmetryAxisY(info.HorizontalSymmetryAxisY);
 

+ 15 - 0
src/PixiEditor/Models/Events/MouseOnCanvasEventArgs.cs

@@ -0,0 +1,15 @@
+using System.Windows.Input;
+using ChunkyImageLib.DataHolders;
+
+namespace PixiEditor.Models.Events;
+internal class MouseOnCanvasEventArgs : EventArgs
+{
+    public MouseOnCanvasEventArgs(MouseButton button, VecD positionOnCanvas)
+    {
+        Button = button;
+        PositionOnCanvas = positionOnCanvas;
+    }
+
+    public MouseButton Button { get; }
+    public VecD PositionOnCanvas { get; }
+}

+ 53 - 5
src/PixiEditor/ViewModels/SubViewModels/Document/DocumentViewModel.cs

@@ -1,4 +1,5 @@
-using System.Windows.Media;
+using System.Windows.Input;
+using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using ChunkyImageLib.DataHolders;
 using ChunkyImageLib.Operations;
@@ -6,6 +7,7 @@ using PixiEditor.ChangeableDocument.Actions.Generated;
 using PixiEditor.ChangeableDocument.Changeables.Interfaces;
 using PixiEditor.ChangeableDocument.Enums;
 using PixiEditor.Helpers;
+using PixiEditor.Models.BitmapActions;
 using PixiEditor.Models.DocumentModels;
 using PixiEditor.Models.Position;
 using PixiEditor.ViewModels.Prototype;
@@ -105,6 +107,8 @@ internal class DocumentViewModel : NotifyableObject
     private int horizontalSymmetryAxisY;
 
     private SKPath selectionPath = new SKPath();
+    private Guid testLayerGuid = Guid.NewGuid();
+
 
     public DocumentViewModel(DocumentManagerViewModel owner, string name)
     {
@@ -150,9 +154,11 @@ internal class DocumentViewModel : NotifyableObject
         var previewSize = StructureMemberViewModel.CalculatePreviewSize(SizeBindable);
         PreviewBitmap = new WriteableBitmap(previewSize.X, previewSize.Y, 96, 96, PixelFormats.Pbgra32, null);
         PreviewSurface = SKSurface.Create(new SKImageInfo(previewSize.X, previewSize.Y, SKColorType.Bgra8888), PreviewBitmap.BackBuffer, PreviewBitmap.BackBufferStride);
+
+        Helpers.ActionAccumulator.AddFinishedActions(new CreateStructureMember_Action(StructureRoot.GuidValue, testLayerGuid, 0, StructureMemberType.Layer));
     }
 
-    public void SetSize(VecI size)
+    public void InternalSetSize(VecI size)
     {
         this.size = size;
         RaisePropertyChanged(nameof(SizeBindable));
@@ -190,15 +196,57 @@ internal class DocumentViewModel : NotifyableObject
 
     public void AddOrUpdateViewport(ViewportInfo info)
     {
-        //Helpers.ActionAccumulator.AddActions(new RefreshViewport_PassthroughAction(info));
+        Helpers.ActionAccumulator.AddActions(new RefreshViewport_PassthroughAction(info));
     }
 
     public void RemoveViewport(Guid viewportGuid)
     {
-        //Helpers.ActionAccumulator.AddActions(new RemoveViewport_PassthroughAction(viewportGuid));
+        Helpers.ActionAccumulator.AddActions(new RemoveViewport_PassthroughAction(viewportGuid));
+    }
+
+    public void OnKeyDown(Key args)
+    {
+
+    }
+
+    public void OnKeyUp(Key args)
+    {
+
+    }
+
+    private bool drawing = false;
+    public void OnCanvasLeftMouseButtonDown(VecD pos)
+    {
+        drawing = true;
+        Helpers.ActionAccumulator.AddActions(new LineBasedPen_Action(
+            testLayerGuid,
+            SKColors.Black,
+            (VecI)pos,
+            (int)1,
+            false,
+            false));
+    }
+
+    public void OnCanvasMouseMove(VecD newPos)
+    {
+        if (!drawing)
+            return;
+        Helpers.ActionAccumulator.AddActions(new LineBasedPen_Action(
+            testLayerGuid,
+            SKColors.Black,
+            (VecI)newPos,
+            (int)1,
+            false,
+            false));
+    }
+
+    public void OnCanvasLeftMouseButtonUp()
+    {
+        drawing = false;
+        Helpers.ActionAccumulator.AddFinishedActions(new EndLineBasedPen_Action());
     }
 
-    public void SetSelectionPath(SKPath selectionPath)
+    public void InternalUpdateSelectionPath(SKPath selectionPath)
     {
         (var toDispose, this.selectionPath) = (this.selectionPath, selectionPath);
         toDispose.Dispose();

+ 29 - 27
src/PixiEditor/ViewModels/SubViewModels/Main/IoViewModel.cs

@@ -1,12 +1,15 @@
 using System.Windows;
 using System.Windows.Input;
+using ChunkyImageLib.DataHolders;
 using PixiEditor.Helpers;
 using PixiEditor.Models.Commands;
 using PixiEditor.Models.Commands.Commands;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Events;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools.Tools;
+using PixiEditor.ViewModels.SubViewModels.Document;
 using PixiEditor.Views;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main;
@@ -14,11 +17,8 @@ namespace PixiEditor.ViewModels.SubViewModels.Main;
 internal class IoViewModel : SubViewModel<ViewModelMain>
 {
     public RelayCommand MouseMoveCommand { get; set; }
-
     public RelayCommand MouseDownCommand { get; set; }
-
     public RelayCommand PreviewMouseMiddleButtonCommand { get; set; }
-
     public RelayCommand MouseUpCommand { get; set; }
 
     private bool restoreToolOnKeyUp = false;
@@ -69,20 +69,21 @@ internal class IoViewModel : SubViewModel<ViewModelMain>
 
     private void OnKeyDown(KeyEventArgs args)
     {
-        /*
+
         var key = args.Key;
         if (key == Key.System)
             key = args.SystemKey;
 
         ProcessShortcutDown(args.IsRepeat, key);
 
-        if (Owner.BitmapManager.ActiveDocument != null)
+        if (Owner.DocumentManagerSubViewModel.ActiveDocument != null)
         {
-            Owner.BitmapManager.InputTarget.OnKeyDown(key);
+            //Owner.DocumentManagerSubViewModel.InputTarget.OnKeyDown(key);
+            Owner.DocumentManagerSubViewModel.ActiveDocument.OnKeyDown(key);
         }
 
         HandleTransientKey(args, true);
-        */
+
     }
 
     private void HandleTransientKey(KeyEventArgs args, bool state)
@@ -121,6 +122,7 @@ internal class IoViewModel : SubViewModel<ViewModelMain>
 
         Owner.ShortcutController.KeyPressed(key, Keyboard.Modifiers);
 
+        // this was commented out before prototype integration
         //public void KeyPressed(Key key, ModifierKeys modifiers)
         //{
         //    if (!ShortcutExecutionBlocked)
@@ -147,18 +149,20 @@ internal class IoViewModel : SubViewModel<ViewModelMain>
 
     private void OnKeyUp(KeyEventArgs args)
     {
-        /*
         var key = args.Key;
         if (key == Key.System)
             key = args.SystemKey;
 
         ProcessShortcutUp(new(key, args.KeyboardDevice.Modifiers));
 
-        if (Owner.BitmapManager.ActiveDocument != null)
-            Owner.BitmapManager.InputTarget.OnKeyUp(key);
+        if (Owner.DocumentManagerSubViewModel.ActiveDocument is not null)
+        {
+            Owner.DocumentManagerSubViewModel.ActiveDocument.OnKeyUp(key);
+            //Owner.BitmapManager.InputTarget.OnKeyUp(key);
+        }
+
 
         HandleTransientKey(args, false);
-        */
     }
 
     private void ProcessShortcutUp(KeyCombination shortcut)
@@ -172,19 +176,18 @@ internal class IoViewModel : SubViewModel<ViewModelMain>
         }
     }
 
-    private void OnMouseDown(object sender, MouseButton button)
+    private void OnMouseDown(object sender, MouseOnCanvasEventArgs args)
     {
-        /*
-        if (button == MouseButton.Left)
+        if (args.Button == MouseButton.Left)
         {
-            BitmapManager bitmapManager = Owner.BitmapManager;
-            var activeDocument = bitmapManager.ActiveDocument;
+            DocumentManagerViewModel docManager = Owner.DocumentManagerSubViewModel;
+            var activeDocument = docManager.ActiveDocument;
             if (activeDocument == null)
                 return;
 
-            bitmapManager.InputTarget.OnLeftMouseButtonDown(activeDocument.MouseXOnCanvas, activeDocument.MouseYOnCanvas);
+            //docManager.InputTarget.OnLeftMouseButtonDown(activeDocument.MouseXOnCanvas, activeDocument.MouseYOnCanvas);
+            docManager.ActiveDocument.OnCanvasLeftMouseButtonDown(args.PositionOnCanvas);
         }
-        */
     }
 
     private void OnPreviewMiddleMouseButton(object sender)
@@ -217,28 +220,27 @@ internal class IoViewModel : SubViewModel<ViewModelMain>
         }
     }
 
-    private void OnMouseMove(object sender, EventArgs args)
+    private void OnMouseMove(object sender, VecD pos)
     {
-        /*
-        var activeDocument = Owner.BitmapManager.ActiveDocument;
-        if (activeDocument == null)
+        var activeDocument = Owner.DocumentManagerSubViewModel.ActiveDocument;
+        if (activeDocument is null)
             return;
-        Owner.BitmapManager.InputTarget.OnMouseMove(activeDocument.MouseXOnCanvas, activeDocument.MouseYOnCanvas);*/
+        //Owner.DocumentManagerSubViewModel.InputTarget.OnMouseMove(activeDocument.MouseXOnCanvas, activeDocument.MouseYOnCanvas);
+        activeDocument.OnCanvasMouseMove(pos);
     }
 
     private void OnMouseUp(object sender, MouseButton button)
     {
-        /*
-        if (Owner.BitmapManager.ActiveDocument == null)
+        if (Owner.DocumentManagerSubViewModel.ActiveDocument is null)
             return;
         if (button == MouseButton.Left)
         {
-            Owner.BitmapManager.InputTarget.OnLeftMouseButtonUp();
+            //Owner.BitmapManager.InputTarget.OnLeftMouseButtonUp();
+            Owner.DocumentManagerSubViewModel.ActiveDocument.OnCanvasLeftMouseButtonUp();
         }
         else if (button == MouseButton.Middle)
         {
             ChangeToolState<MoveViewportTool>(false);
         }
-        */
     }
 }

+ 3 - 0
src/PixiEditor/Views/MainWindow.xaml

@@ -503,6 +503,9 @@
                                 <ui:DocumentsTemplateSelector.DocumentsViewTemplate>
                                     <DataTemplate DataType="{x:Type doc:DocumentViewModel}">
                                         <usercontrols:Viewport
+                                            MouseDownCommand="{Binding ElementName=mainWindow, Path=DataContext.IoSubViewModel.MouseDownCommand}"
+                                            MouseMoveCommand="{Binding ElementName=mainWindow, Path=DataContext.IoSubViewModel.MouseMoveCommand}"
+                                            MouseUpCommand="{Binding ElementName=mainWindow, Path=DataContext.IoSubViewModel.MouseUpCommand}"
                                             Document="{Binding}"/>
                                         <!--<usercontrols:DrawingViewPort
                                         CenterViewportTrigger="{Binding CenterViewportTrigger}"

+ 4 - 22
src/PixiEditor/Views/UserControls/Viewport.xaml

@@ -56,28 +56,10 @@
                         Width="{Binding Document.Width}"
                         Height="{Binding Document.Height}"
                         Source="{Binding TargetBitmap}"
-                        RenderOptions.BitmapScalingMode="{Binding Zoombox.Scale, Converter={converters:ScaleToBitmapScalingModeConverter}}">
-                        <i:Interaction.Triggers>
-                            <i:EventTrigger
-                                EventName="MouseDown">
-                                <i:InvokeCommandAction
-                                    Command="{Binding MouseDownCommand}"
-                                    PassEventArgsToCommand="True" />
-                            </i:EventTrigger>
-                            <i:EventTrigger
-                                EventName="MouseMove">
-                                <i:InvokeCommandAction
-                                    Command="{Binding MouseMoveCommand}"
-                                    PassEventArgsToCommand="True" />
-                            </i:EventTrigger>
-                            <i:EventTrigger
-                                EventName="MouseUp">
-                                <i:InvokeCommandAction
-                                    Command="{Binding MouseUpCommand}"
-                                    PassEventArgsToCommand="True" />
-                            </i:EventTrigger>
-                        </i:Interaction.Triggers>
-                    </Image>
+                        MouseDown="Image_MouseDown"
+                        MouseMove="Image_MouseMove"
+                        MouseUp="Image_MouseUp"
+                        RenderOptions.BitmapScalingMode="{Binding Zoombox.Scale, Converter={converters:ScaleToBitmapScalingModeConverter}}"/>
                     <sym:SymmetryOverlay
                         ZoomboxScale="{Binding Zoombox.Scale}"
                         HorizontalAxisVisible="{Binding Document.HorizontalSymmetryAxisEnabledBindable}"

+ 32 - 0
src/PixiEditor/Views/UserControls/Viewport.xaml.cs

@@ -5,6 +5,7 @@ using System.Windows.Data;
 using System.Windows.Input;
 using System.Windows.Media.Imaging;
 using ChunkyImageLib.DataHolders;
+using PixiEditor.Models.Events;
 using PixiEditor.Models.Position;
 using PixiEditor.ViewModels.SubViewModels.Document;
 using PixiEditor.Zoombox;
@@ -253,4 +254,35 @@ internal partial class Viewport : UserControl, INotifyPropertyChanged
     {
         PropertyChanged?.Invoke(this, new(nameof(ReferenceLayerScale)));
     }
+
+    private void Image_MouseDown(object sender, MouseButtonEventArgs e)
+    {
+        if (MouseDownCommand is null)
+            return;
+        var pos = e.GetPosition((IInputElement)sender);
+        VecD conv = new VecD(pos.X, pos.Y);
+        var parameter = new MouseOnCanvasEventArgs(e.ChangedButton, conv);
+
+        if (MouseDownCommand.CanExecute(parameter))
+            MouseDownCommand.Execute(parameter);
+    }
+
+    private void Image_MouseMove(object sender, MouseEventArgs e)
+    {
+        if (MouseMoveCommand is null)
+            return;
+        var pos = e.GetPosition((IInputElement)sender);
+        VecD conv = new VecD(pos.X, pos.Y);
+
+        if (MouseMoveCommand.CanExecute(conv))
+            MouseMoveCommand.Execute(conv);
+    }
+
+    private void Image_MouseUp(object sender, MouseButtonEventArgs e)
+    {
+        if (MouseUpCommand is null)
+            return;
+        if (MouseUpCommand.CanExecute(e.ChangedButton))
+            MouseUpCommand.Execute(e.ChangedButton);
+    }
 }