Browse Source

Color Picker tool now picks from active render output

Krzysztof Krysiński 4 months ago
parent
commit
748f2b624c

+ 1 - 0
src/PixiEditor/Helpers/ServiceCollectionHelpers.cs

@@ -69,6 +69,7 @@ internal static class ServiceCollectionHelpers
             .AddSingleton<NodeGraphManagerViewModel>()
             .AddSingleton<AutosaveViewModel>()
             .AddSingleton<IColorsHandler, ColorsViewModel>(x => x.GetRequiredService<ColorsViewModel>())
+            .AddSingleton<IWindowHandler, WindowViewModel>(x => x.GetRequiredService<WindowViewModel>())
             .AddSingleton<RegistryViewModel>()
             .AddSingleton(static x => new DiscordViewModel(x.GetService<ViewModels_ViewModelMain>(), "764168193685979138"))
             .AddSingleton<DebugViewModel>()

+ 17 - 4
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/ColorPickerToolExecutor.cs

@@ -13,6 +13,7 @@ internal class ColorPickerToolExecutor : UpdateableChangeExecutor
     private bool includeCanvas;
     private DocumentScope scope;
     private IColorsHandler? colorsViewModel;
+    private IWindowHandler? windowHandler;
 
     public override ExecutionState Start()
     {
@@ -25,8 +26,13 @@ internal class ColorPickerToolExecutor : UpdateableChangeExecutor
         scope = tool.Mode;
         includeReference = tool.PickFromReferenceLayer && document!.ReferenceLayerHandler.ReferenceTexture is not null;
         includeCanvas = tool.PickFromCanvas;
-        
-        colorsViewModel.PrimaryColor = document.PickColor(controller.LastPrecisePosition, scope, includeReference, includeCanvas, document.AnimationHandler.ActiveFrameBindable, document.ReferenceLayerHandler.IsTopMost);
+
+        windowHandler = GetHandler<IWindowHandler>();
+
+        string? customOutput = windowHandler.ActiveWindow is IViewport viewport ? viewport.RenderOutputName : null;
+        customOutput = customOutput == "DEFAULT" ? null : customOutput;
+
+        colorsViewModel.PrimaryColor = document.PickColor(controller.LastPrecisePosition, scope, includeReference, includeCanvas, document.AnimationHandler.ActiveFrameBindable, document.ReferenceLayerHandler.IsTopMost, customOutput);
         return ExecutionState.Success;
     }
 
@@ -34,12 +40,19 @@ internal class ColorPickerToolExecutor : UpdateableChangeExecutor
     {
         if (!includeReference)
             return;
-        colorsViewModel.PrimaryColor = document.PickColor(pos, scope, includeReference, includeCanvas, document.AnimationHandler.ActiveFrameBindable, document.ReferenceLayerHandler.IsTopMost);
+
+        string? customOutput = windowHandler?.ActiveWindow is IViewport viewport ? viewport.RenderOutputName : null;
+        customOutput = customOutput == "DEFAULT" ? null : customOutput;
+
+        colorsViewModel.PrimaryColor = document.PickColor(pos, scope, includeReference, includeCanvas, document.AnimationHandler.ActiveFrameBindable, document.ReferenceLayerHandler.IsTopMost, customOutput);
     }
 
     public override void OnPixelPositionChange(VecI pos)
     {
-        colorsViewModel.PrimaryColor = document.PickColor(pos, scope, includeReference, includeCanvas, document.AnimationHandler.ActiveFrameBindable, document.ReferenceLayerHandler.IsTopMost);
+        string? customOutput = windowHandler?.ActiveWindow is IViewport viewport ? viewport.RenderOutputName : null;
+        customOutput = customOutput == "DEFAULT" ? null : customOutput;
+
+        colorsViewModel.PrimaryColor = document.PickColor(pos, scope, includeReference, includeCanvas, document.AnimationHandler.ActiveFrameBindable, document.ReferenceLayerHandler.IsTopMost, customOutput);
     }
 
     public override void OnLeftMouseButtonUp(VecD argsPositionOnCanvas)

+ 1 - 1
src/PixiEditor/Models/Handlers/IDocument.cs

@@ -61,7 +61,7 @@ internal interface IDocument : IHandler
     public void SetSize(VecI infoSize);
 
     public Color PickColor(VecD controllerLastPrecisePosition, DocumentScope scope, bool includeReference,
-        bool includeCanvas, int frame, bool isTopMost);
+        bool includeCanvas, int frame, bool isTopMost, string? customOutput);
 
     public HashSet<Guid> ExtractSelectedLayers(bool includeFoldersWithMask = false);
     public void UpdateSavedState();

+ 6 - 0
src/PixiEditor/Models/Handlers/IViewport.cs

@@ -0,0 +1,6 @@
+namespace PixiEditor.Models.Handlers;
+
+public interface IViewport
+{
+    public string? RenderOutputName { get; set; }
+}

+ 6 - 0
src/PixiEditor/Models/Handlers/IWindowHandler.cs

@@ -0,0 +1,6 @@
+namespace PixiEditor.Models.Handlers;
+
+public interface IWindowHandler : IHandler
+{
+    public object ActiveWindow { get; }
+}

+ 7 - 4
src/PixiEditor/ViewModels/Document/DocumentViewModel.cs

@@ -710,7 +710,7 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
     /// <param name="includeCanvas">Should the color be picked from the canvas</param>
     /// <param name="referenceTopmost">Is the reference layer topmost. (Only affects the result is includeReference and includeCanvas are set.)</param>
     public Color PickColor(VecD pos, DocumentScope scope, bool includeReference, bool includeCanvas, int frame,
-        bool referenceTopmost = false)
+        bool referenceTopmost = false, string? customOutput = null)
     {
         if (scope == DocumentScope.SingleLayer && includeReference && includeCanvas)
             includeReference = false;
@@ -736,7 +736,10 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
         }
 
         if (includeCanvas)
-            return PickColorFromCanvas((VecI)pos, scope, frame);
+        {
+            return PickColorFromCanvas((VecI)pos, scope, frame, customOutput);
+        }
+
         if (includeReference)
             return PickColorFromReferenceLayer(pos) ?? Colors.Transparent;
         return Colors.Transparent;
@@ -758,7 +761,7 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
         return bitmap.GetSRGBPixel(new VecI((int)transformed.X, (int)transformed.Y));
     }
 
-    public Color PickColorFromCanvas(VecI pos, DocumentScope scope, KeyFrameTime frameTime)
+    public Color PickColorFromCanvas(VecI pos, DocumentScope scope, KeyFrameTime frameTime, string? customOutput = null)
     {
         // there is a tiny chance that the image might get disposed by another thread
         try
@@ -768,7 +771,7 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
             if (scope == DocumentScope.Canvas)
             {
                 using Surface tmpSurface = new Surface(SizeBindable); // new Surface is on purpose, Surface.ForDisplay doesn't work here
-                Renderer.RenderDocument(tmpSurface.DrawingSurface, frameTime, SizeBindable);
+                Renderer.RenderDocument(tmpSurface.DrawingSurface, frameTime, SizeBindable, customOutput);
 
                 return tmpSurface.GetSrgbPixel(pos);
             }

+ 2 - 1
src/PixiEditor/ViewModels/SubViewModels/ViewportWindowViewModel.cs

@@ -5,6 +5,7 @@ using PixiDocks.Core.Docking.Events;
 using PixiEditor.Helpers.UI;
 using PixiEditor.Models.DocumentModels;
 using Drawie.Numerics;
+using PixiEditor.Models.Handlers;
 using PixiEditor.ViewModels.Dock;
 using PixiEditor.ViewModels.Document;
 using PixiEditor.Views.Visuals;
@@ -12,7 +13,7 @@ using PixiEditor.Views.Visuals;
 namespace PixiEditor.ViewModels.SubViewModels;
 #nullable enable
 internal class ViewportWindowViewModel : SubViewModel<WindowViewModel>, IDockableContent, IDockableCloseEvents,
-    IDockableSelectionEvents
+    IDockableSelectionEvents, IViewport
 {
     public DocumentViewModel Document { get; }
     public ExecutionTrigger<VecI> CenterViewportTrigger { get; } = new ExecutionTrigger<VecI>();

+ 2 - 1
src/PixiEditor/ViewModels/SubViewModels/WindowViewModel.cs

@@ -7,6 +7,7 @@ using CommunityToolkit.Mvvm.Input;
 using PixiDocks.Core.Docking;
 using PixiEditor.Models.AnalyticsAPI;
 using PixiEditor.Models.Commands;
+using PixiEditor.Models.Handlers;
 using PixiEditor.UI.Common.Fonts;
 using PixiEditor.ViewModels.Document;
 using PixiEditor.Views;
@@ -21,7 +22,7 @@ namespace PixiEditor.ViewModels.SubViewModels;
 
 #nullable enable
 [Commands_Command.Group("PixiEditor.Window", "WINDOWS")]
-internal class WindowViewModel : SubViewModel<ViewModelMain>
+internal class WindowViewModel : SubViewModel<ViewModelMain>, IWindowHandler
 {
     private CommandController commandController;
     public RelayCommand<string> ShowAvalonDockWindowCommand { get; set; }