Переглянути джерело

Zoombox somewhat working?

Krzysztof Krysiński 2 роки тому
батько
коміт
4f626fd172

+ 4 - 0
src/PixiEditor.Avalonia/PixiEditor.AvaloniaUI/Styles/Dock/DockDocumentDataTemplate.axaml

@@ -0,0 +1,4 @@
+<ResourceDictionary xmlns="https://github.com/avaloniaui"
+                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
+    <!-- Add Resources Here -->
+</ResourceDictionary>

+ 1 - 1
src/PixiEditor.Avalonia/PixiEditor.AvaloniaUI/ViewLocator.cs

@@ -36,6 +36,6 @@ public class ViewLocator : IDataTemplate
 
     public bool Match(object? data)
     {
-        return data is ObservableObject || data is IDockable;
+        return data is ObservableObject or IDockable;
     }
 }

+ 4 - 3
src/PixiEditor.Avalonia/PixiEditor.AvaloniaUI/ViewModels/Dock/DockDocumentViewModel.cs

@@ -1,10 +1,11 @@
-using Dock.Model.Avalonia.Controls;
+using PixiEditor.AvaloniaUI.ViewModels.Document;
 
 namespace PixiEditor.AvaloniaUI.ViewModels.Dock;
 
-public class DockDocumentViewModel : global::Dock.Model.Avalonia.Controls.Document
+internal class DockDocumentViewModel : global::Dock.Model.Avalonia.Controls.Document
 {
-    public DockDocumentViewModel()
+    public DockDocumentViewModel(DocumentViewModel documentViewModel)
     {
+        DataContext = documentViewModel;
     }
 }

+ 20 - 8
src/PixiEditor.Avalonia/PixiEditor.AvaloniaUI/ViewModels/Dock/DockFactory.cs

@@ -4,15 +4,25 @@ using Dock.Model.Avalonia;
 using Dock.Model.Avalonia.Controls;
 using Dock.Model.Controls;
 using Dock.Model.Core;
+using PixiEditor.AvaloniaUI.ViewModels.SubViewModels;
 
 namespace PixiEditor.AvaloniaUI.ViewModels.Dock;
 
-public class DockFactory : Factory
+internal class DockFactory : Factory
 {
     private DockDock mainLayout;
     private DocumentDock documentDock;
     private ToolDock toolDock;
 
+    private FileViewModel manager;
+
+    public DockFactory(FileViewModel fileViewModel)
+    {
+        manager = fileViewModel;
+    }
+
+    public override IDocumentDock CreateDocumentDock() => new PixiEditorDocumentDock(manager);
+
     public override IRootDock CreateLayout()
     {
         mainLayout = BuildMainLayout();
@@ -77,11 +87,11 @@ public class DockFactory : Factory
 
     private IDockable BuildDocumentDock()
     {
-        documentDock = new PixiEditorDocumentDock()
+        documentDock = new PixiEditorDocumentDock(manager)
         {
             Id = "DocumentsPane",
-            Title = "Document",
-            CanCreateDocument = true,
+            Title = "DocumentsPane",
+            CanCreateDocument = true
         };
 
         return documentDock;
@@ -100,6 +110,8 @@ public class DockFactory : Factory
             Dock = DockMode.Left,
             CanFloat = false,
             GripMode = GripMode.Hidden,
+            Id = "ToolsPane",
+            Title = "ToolsPane",
         };
 
         return toolDock;
@@ -116,14 +128,14 @@ public class DockFactory : Factory
 
         ContextLocator = new Dictionary<string, Func<object?>>()
         {
-            { "MainLayout", () => mainLayout },
-            { "DocumentsPane", () => documentDock },
-            { "ToolsPane", () => toolDock },
+            { "MainLayout", () => layout },
+            { "DocumentsPane", () => layout },
+            { "ToolsPane", () => layout },
         };
 
         HostWindowLocator = new Dictionary<string, Func<IHostWindow?>>()
         {
-            { "MainLayout", () => new HostWindow() },
+            [nameof(IDockWindow)] = () => new HostWindow()
         };
 
         base.InitLayout(layout);

+ 12 - 3
src/PixiEditor.Avalonia/PixiEditor.AvaloniaUI/ViewModels/Dock/PixiEditorDocumentDock.cs

@@ -1,12 +1,17 @@
 using CommunityToolkit.Mvvm.Input;
 using Dock.Model.Avalonia.Controls;
+using PixiEditor.AvaloniaUI.ViewModels.Document;
+using PixiEditor.AvaloniaUI.ViewModels.SubViewModels;
+using PixiEditor.DrawingApi.Core.Numerics;
 
 namespace PixiEditor.AvaloniaUI.ViewModels.Dock;
 
-public class PixiEditorDocumentDock : DocumentDock
+internal class PixiEditorDocumentDock : DocumentDock
 {
-    public PixiEditorDocumentDock()
+    private FileViewModel manager;
+    public PixiEditorDocumentDock(FileViewModel manager)
     {
+        this.manager = manager;
         CreateDocument = new RelayCommand(CreateDockDocument);
     }
 
@@ -17,7 +22,11 @@ public class PixiEditorDocumentDock : DocumentDock
             return;
         }
 
-        var document = new DockDocumentViewModel() { Title = "hello" };
+        var doc = manager.NewDocument(b =>
+        {
+            b.WithSize(new VecI(64, 64));
+        });
+        var document = new DockDocumentViewModel(doc) { Title = "hello" };
 
         Factory?.AddDockable(this, document);
         Factory?.SetActiveDockable(document);

+ 6 - 5
src/PixiEditor.Avalonia/PixiEditor.AvaloniaUI/ViewModels/SubViewModels/FileViewModel.cs

@@ -18,6 +18,7 @@ using PixiEditor.AvaloniaUI.Models.UserData;
 using PixiEditor.AvaloniaUI.ViewModels.Document;
 using PixiEditor.AvaloniaUI.Views.Windows;
 using PixiEditor.DrawingApi.Core.Numerics;
+using PixiEditor.Extensions.Common.Localization;
 using PixiEditor.Extensions.Common.UserPreferences;
 using PixiEditor.Parser;
 
@@ -272,16 +273,16 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
         //TODO: Implement NewFileDialog
         /*NewFileDialog newFile = new NewFileDialog();
         if (newFile.ShowDialog())
-        {
+        {*/
             NewDocument(b => b
-                .WithSize(newFile.Width, newFile.Height)
+                .WithSize(64/*newFile.Width*/, /*newFile.Height*/64)
                 .WithLayer(l => l
                     .WithName(new LocalizedString("BASE_LAYER_NAME"))
-                    .WithSurface(new Surface(new VecI(newFile.Width, newFile.Height)))));
-        }*/
+                    .WithSurface(new Surface(new VecI(/*newFile.Width*/64, /*newFile.Height*/64)))));
+        //}
     }
 
-    private DocumentViewModel NewDocument(Action<DocumentViewModelBuilder> builder)
+    public DocumentViewModel NewDocument(Action<DocumentViewModelBuilder> builder)
     {
         var doc = DocumentViewModel.Build(builder);
         AddDocumentViewModelToTheSystem(doc);

+ 1 - 1
src/PixiEditor.Avalonia/PixiEditor.AvaloniaUI/ViewModels/SubViewModels/LayoutDockViewModel.cs

@@ -15,7 +15,7 @@ internal class LayoutDockViewModel : SubViewModel<ViewModelMain>
 
     public LayoutDockViewModel(ViewModelMain owner) : base(owner)
     {
-        DockFactory factory = new();
+        DockFactory factory = new(owner.FileSubViewModel);
         Layout = factory.CreateLayout();
         if (Layout is { })
         {

+ 2 - 0
src/PixiEditor.Avalonia/PixiEditor.AvaloniaUI/Views/Dock/DocumentTemplate.axaml

@@ -8,6 +8,8 @@
              xmlns:ui="clr-namespace:PixiEditor.Extensions.UI;assembly=PixiEditor.Extensions"
              xmlns:xaml="clr-namespace:PixiEditor.AvaloniaUI.Models.Commands.XAML"
              xmlns:palettes="clr-namespace:PixiEditor.Views.UserControls.Palettes"
+             xmlns:document="clr-namespace:PixiEditor.AvaloniaUI.ViewModels.Document"
+             xmlns:viewModels1="clr-namespace:PixiEditor.AvaloniaUI.ViewModels"
              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
              x:Class="PixiEditor.AvaloniaUI.Views.Dock.DocumentTemplate">
     <userControls:Viewport

+ 4 - 5
src/PixiEditor.Avalonia/PixiEditor.AvaloniaUI/Views/Main/Viewport.axaml

@@ -111,7 +111,6 @@
         </Border>
             </views:TogglableFlyout.Child>
         </views:TogglableFlyout>
-        <!--TODO: Originally Dimensions and RealDimensions had Binding with OneWayToSource, but it resulted in stack overflow. Investigate-->
         <zoombox:Zoombox
             Tag="{Binding ElementName=vpUc}"
             x:Name="zoombox"
@@ -119,8 +118,8 @@
             Scale="{Binding ZoomboxScale, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:Viewport}, Mode=OneWayToSource}"
             Center="{Binding Center, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:Viewport}, Mode=OneWayToSource}"
             Angle="{Binding Angle, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:Viewport}, Mode=OneWayToSource}"
-            RealDimensions="{Binding RealDimensions, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:Viewport}, Mode=OneWay}"
-            Dimensions="{Binding Dimensions, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:Viewport}, Mode=OneWay}"
+            RealDimensions="{Binding RealDimensions, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:Viewport}, Mode=OneWayToSource}"
+            Dimensions="{Binding Dimensions, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:Viewport}, Mode=OneWayToSource}"
             ZoomMode="{Binding ZoomMode, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:Viewport}, Mode=TwoWay}"
             ZoomOutOnClick="{Binding ZoomOutOnClick, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:Viewport}, Mode=TwoWay}"
             FlipX="{Binding FlipX, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:Viewport}, Mode=TwoWay}"
@@ -151,8 +150,8 @@
                         <!--TODO: RenderOptions.BitmapInterpolationMode="{Binding ReferenceLayerScale, Converter={converters:ScaleToBitmapScalingModeConverter}}"-->
                         <Image
                             Focusable="False"
-                            Width="{Binding Document.ReferenceLayerViewModel.ReferenceBitmap.Width}"
-                            Height="{Binding Document.ReferenceLayerViewModel.ReferenceBitmap.Height}"
+                            Width="{Binding Document.ReferenceLayerViewModel.ReferenceBitmap.Size.Width}"
+                            Height="{Binding Document.ReferenceLayerViewModel.ReferenceBitmap.Size.Height}"
                             Source="{Binding Document.ReferenceLayerViewModel.ReferenceBitmap, Mode=OneWay}"
                             IsVisible="{Binding Document.ReferenceLayerViewModel.IsVisibleBindable}"
                             SizeChanged="OnReferenceImageSizeChanged"

+ 2 - 0
src/PixiEditor.Avalonia/PixiEditor.AvaloniaUI/Views/MainView.axaml

@@ -10,6 +10,8 @@
              xmlns:viewModels="clr-namespace:PixiEditor.ViewModels"
              xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
              xmlns:palettes="clr-namespace:PixiEditor.Views.UserControls.Palettes"
+             xmlns:dock="clr-namespace:PixiEditor.AvaloniaUI.ViewModels.Dock"
+             xmlns:zoombox="clr-namespace:PixiEditor.Zoombox;assembly=PixiEditor.Zoombox"
              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
              x:Class="PixiEditor.AvaloniaUI.Views.MainView"
              x:DataType="viewModels1:ViewModelMain" Background="{DynamicResource ThemeBackgroundBrush}">

+ 2 - 2
src/PixiEditor.UI.Common/Controls/Dock/Controls/DockControl.axaml

@@ -15,9 +15,9 @@
                         x:DataType="core:IDock"
                         x:CompileBindings="True">
           <ContentControl.DataTemplates>
-            <DataTemplate DataType="dmc:IDocumentContent">
+            <!--<DataTemplate DataType="dmc:IDocumentContent"> This should be added in the application
               <DocumentContentControl />
-            </DataTemplate>
+            </DataTemplate>-->
             <DataTemplate DataType="dmc:IToolContent">
               <ToolContentControl />
             </DataTemplate>

+ 10 - 6
src/PixiEditor.Zoombox/Zoombox.axaml

@@ -1,4 +1,4 @@
-<ContentControl
+<UserControl
     x:Class="PixiEditor.Zoombox.Zoombox"
     xmlns="https://github.com/avaloniaui"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -8,19 +8,23 @@
     mc:Ignorable="d"
     x:Name="uc"
     d:DesignHeight="450"
+    PointerWheelChanged="OnScroll"
+    PointerPressed="OnMouseDown"
+    PointerReleased="OnMouseUp"
+    PointerMoved="OnMouseMove"
     d:DesignWidth="800">
     <Canvas
-        PointerPressed="OnMouseDown"
-        PointerReleased="OnMouseUp"
-        PointerMoved="OnMouseMove"
-        PointerWheelChanged="OnScroll"
         ClipToBounds="True"
         x:Name="mainCanvas"
+        Width="0.01"
+        Height="0.01"
         Background="Transparent"
         SizeChanged="OnMainCanvasSizeChanged">
         <Grid
             x:Name="mainGrid"
             SizeChanged="OnGridSizeChanged"
+            Width="1"
+            Height="1"
             Canvas.Left="{Binding ElementName=uc, Path=CanvasX}"
             Canvas.Top="{Binding ElementName=uc, Path=CanvasY}">
             <Grid.RenderTransform>
@@ -39,4 +43,4 @@
                 Content="{Binding AdditionalContent, ElementName=uc}" />
         </Grid>
     </Canvas>
-</ContentControl>
+</UserControl>

+ 12 - 4
src/PixiEditor.Zoombox/Zoombox.axaml.cs

@@ -11,7 +11,7 @@ using PixiEditor.Zoombox.Operations;
 
 namespace PixiEditor.Zoombox;
 
-public partial class Zoombox : ContentControl, INotifyPropertyChanged
+public partial class Zoombox : UserControl, INotifyPropertyChanged
 {
         public static readonly StyledProperty<ZoomboxMode> ZoomModeProperty =
             AvaloniaProperty.Register<Zoombox, ZoomboxMode>(nameof(ZoomMode), defaultValue: ZoomboxMode.Normal);
@@ -43,6 +43,9 @@ public partial class Zoombox : ContentControl, INotifyPropertyChanged
         public static readonly StyledProperty<bool> FlipYProperty =
             AvaloniaProperty.Register<Zoombox, bool>(nameof(FlipY), defaultValue: false);
 
+        public static readonly StyledProperty<AvaloniaObject> AdditionalContentProperty =
+            AvaloniaProperty.Register<Zoombox, AvaloniaObject>(nameof(AdditionalContent));
+
     public static readonly RoutedEvent<ViewportRoutedEventArgs> ViewportMovedEvent = RoutedEvent.Register<Zoombox, ViewportRoutedEventArgs>(
         nameof(ViewportMoved), RoutingStrategies.Bubble);
 
@@ -106,6 +109,12 @@ public partial class Zoombox : ContentControl, INotifyPropertyChanged
         set => SetValue(RealDimensionsProperty, value);
     }
 
+    public AvaloniaObject AdditionalContent
+    {
+        get => (AvaloniaObject)GetValue(AdditionalContentProperty);
+        set => SetValue(AdditionalContentProperty, value);
+    }
+
     public event EventHandler<ViewportRoutedEventArgs> ViewportMoved
     {
         add => AddHandler(ViewportMovedEvent, value);
@@ -396,10 +405,10 @@ public partial class Zoombox : ContentControl, INotifyPropertyChanged
 
     private void OnScroll(object sender, PointerWheelEventArgs e)
     {
-        double abs = Math.Abs(e.Delta.Y / 100.0);
+        double abs = Math.Abs(-e.Delta.Y / 100.0);
         for (int i = 0; i < abs; i++)
         {
-            ZoomInto(ToVecD(e.GetPosition(mainCanvas)), e.Delta.Y / 100.0);
+            ZoomInto(ToVecD(e.GetPosition(mainCanvas)), -e.Delta.Y / 100.0);
         }
     }
 
@@ -435,7 +444,6 @@ public partial class Zoombox : ContentControl, INotifyPropertyChanged
     private static void OnPropertyChange(AvaloniaPropertyChangedEventArgs e)
     {
         Zoombox? zoombox = (Zoombox)e.Sender;
-
        OnPropertyChange(zoombox);
     }