Skip to content

Commit

Permalink
Fixed action display and hide layers when using color picker tool
Browse files Browse the repository at this point in the history
  • Loading branch information
CPKreu committed Dec 2, 2021
1 parent e0fcab5 commit 905d4ac
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 22 deletions.
17 changes: 15 additions & 2 deletions PixiEditor/Models/Controllers/BitmapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class BitmapManager : NotifyableObject
private int halfSize;
private SKColor _highlightColor;
private PenTool _highlightPen;
private bool hideReferenceLayer;
private bool onlyReferenceLayer;

public BitmapManager(ToolsViewModel tools)
{
Expand All @@ -54,8 +56,6 @@ public BitmapManager(ToolsViewModel tools)

public event EventHandler<DocumentChangedEventArgs> DocumentChanged;

public event EventHandler<SelectedToolEventArgs> SelectedToolChanged;

public MouseMovementController MouseController { get; set; }

public Layer ActiveLayer => ActiveDocument.ActiveLayer;
Expand Down Expand Up @@ -83,6 +83,18 @@ public Document ActiveDocument
#nullable disable
public ObservableCollection<Document> Documents { get; set; } = new ObservableCollection<Document>();

public bool HideReferenceLayer
{
get => hideReferenceLayer;
set => SetProperty(ref hideReferenceLayer, value);
}

public bool OnlyReferenceLayer
{
get => onlyReferenceLayer;
set => SetProperty(ref onlyReferenceLayer, value);
}

public void CloseDocument(Document document)
{
int nextIndex = 0;
Expand Down Expand Up @@ -222,6 +234,7 @@ public void HighlightPixels(Coordinates newPosition)
_highlightPen.Draw(previewLayer, cords, cords, _highlightColor, _tools.ToolSize);

AdjustOffset(newPosition, previewLayer);

}

previewLayer.InvokeLayerBitmapChange();
Expand Down
8 changes: 8 additions & 0 deletions PixiEditor/Models/Tools/Tool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,13 @@ public virtual void OnMouseMove(MouseEventArgs e)
public virtual void AfterAddedUndo(UndoManager undoManager)
{
}

public virtual void OnSelected()
{
}

public virtual void OnDeselected()
{
}
}
}
65 changes: 60 additions & 5 deletions PixiEditor/Models/Tools/Tools/ColorPickerTool.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using PixiEditor.Models.Controllers;
using PixiEditor.Models.DataHolders;
using PixiEditor.Models.Layers;
using PixiEditor.Models.Position;
Expand All @@ -15,18 +16,19 @@ namespace PixiEditor.Models.Tools.Tools
public class ColorPickerTool : ReadonlyTool
{
private readonly DocumentProvider _docProvider;
private readonly BitmapManager _bitmapManager;

public ColorPickerTool(DocumentProvider documentProvider)
public ColorPickerTool(DocumentProvider documentProvider, BitmapManager bitmapManager)
{
ActionDisplay = "Press on pixel to make it the primary color.";
ActionDisplay = "Press on a pixel to make it the primary color. Hold Ctrl to pick from the reference. Hold Ctrl and Alt to blend the reference and canvas color";
_docProvider = documentProvider;
_bitmapManager = bitmapManager;
}

public override bool HideHighlight => true;

public override string Tooltip => "Swaps primary color with selected on canvas. (O)";


public override void Use(List<Coordinates> coordinates)
{
var coords = coordinates.First();
Expand All @@ -35,11 +37,12 @@ public override void Use(List<Coordinates> coordinates)

public SKColor GetColorAt(int x, int y)
{
SKColor? color;
SKColor? color = null;
Document activeDocument = _docProvider.GetDocument();
Layer referenceLayer;

if (Keyboard.IsKeyDown(Key.LeftCtrl) && (referenceLayer = _docProvider.GetReferenceLayer()) is not null)
if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
&& (referenceLayer = _docProvider.GetReferenceLayer()) is not null)
{
double actualX = activeDocument.MouseXOnCanvas * referenceLayer.Width / activeDocument.Width;
double actualY = activeDocument.MouseYOnCanvas * referenceLayer.Height / activeDocument.Height;
Expand All @@ -48,6 +51,13 @@ public SKColor GetColorAt(int x, int y)
y = (int)Round(actualY, MidpointRounding.ToZero);

color = referenceLayer.LayerBitmap.GetSRGBPixel(x, y);

if ((Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt)) && color != null)
{
// TODO: Blend colors
throw new NotImplementedException();
//SKColor? canvasColor = _docProvider.GetRenderer()?.FinalSurface.GetSRGBPixel(x, y);
}
}
else
{
Expand All @@ -56,5 +66,50 @@ public SKColor GetColorAt(int x, int y)

return color ?? SKColors.Transparent;
}

public override void OnKeyDown(KeyEventArgs e)
{
UpdateActionDisplay();
}

public override void OnKeyUp(KeyEventArgs e)
{
UpdateActionDisplay();
}

public override void OnSelected()
{
UpdateActionDisplay();
}

public override void OnDeselected()
{
_bitmapManager.OnlyReferenceLayer = false;
_bitmapManager.HideReferenceLayer = false;
}

private void UpdateActionDisplay()
{
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
{
if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
{
_bitmapManager.HideReferenceLayer = false;
_bitmapManager.OnlyReferenceLayer = false;
ActionDisplay = "Press on a pixel to make the blend of the reference and canvas the primary color. Release Ctrl and Alt to pick from the canvas. Release just Alt to pick from the reference";
return;
}

_bitmapManager.HideReferenceLayer = false;
_bitmapManager.OnlyReferenceLayer = true;
ActionDisplay = "Press on a pixel on the reference to make it the primary color. Release Ctrl to pick from the canvas. Hold Ctrl and Alt to blend the reference and canvas color";
}
else
{
_bitmapManager.HideReferenceLayer = true;
_bitmapManager.OnlyReferenceLayer = false;
ActionDisplay = "Press on a pixel to make it the primary color. Hold Ctrl to pick from the reference. Hold Ctrl and Alt to blend the reference and canvas color";
}
}
}
}
6 changes: 6 additions & 0 deletions PixiEditor/ViewModels/SubViewModels/Main/ToolsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using PixiEditor.Helpers;
using PixiEditor.Models.Controllers;
using PixiEditor.Models.Enums;
using PixiEditor.Models.Events;
using PixiEditor.Models.Position;
using PixiEditor.Models.Tools;
using PixiEditor.Models.Tools.Tools;
Expand Down Expand Up @@ -60,6 +61,8 @@ public int ToolSize

public IEnumerable<Tool> ToolSet { get; private set; }

public event EventHandler<SelectedToolEventArgs> SelectedToolChanged;

public ToolsViewModel(ViewModelMain owner)
: base(owner)
{
Expand Down Expand Up @@ -87,12 +90,15 @@ public void SetActiveTool(Tool tool)
if (ActiveTool != null)
{
activeTool.IsActive = false;
ActiveTool.OnDeselected();
}

LastActionTool = ActiveTool;
SelectedToolChanged?.Invoke(this, new SelectedToolEventArgs(ActiveTool, tool));
ActiveTool = tool;

tool.IsActive = true;
ActiveTool.OnSelected();
SetToolCursor(tool.GetType());

if (Owner.StylusSubViewModel != null)
Expand Down
2 changes: 1 addition & 1 deletion PixiEditor/ViewModels/ViewModelMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void Setup(IServiceProvider services)

BitmapManager.PrimaryColor = ColorsSubViewModel.PrimaryColor;

BitmapManager.SelectedToolChanged += BitmapManager_SelectedToolChanged;
ToolsSubViewModel.SelectedToolChanged += BitmapManager_SelectedToolChanged;
}

/// <summary>
Expand Down
29 changes: 15 additions & 14 deletions PixiEditor/Views/UserControls/DrawingViewPort.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,26 @@
</ImageBrush>
</Canvas.Background>

<Image Source="{Binding ReferenceLayerRenderer.FinalBitmap}"
VerticalAlignment="Stretch" Stretch="Uniform"
Visibility="{Binding ReferenceLayer.IsVisible, Converter={BoolToVisibilityConverter}}"
HorizontalAlignment="Stretch"
Width="{Binding Width}" Height="{Binding Height}"
RenderOptions.BitmapScalingMode="NearestNeighbor"/>
<local:ReferenceLayerView ReferenceLayerRenderer="{Binding ReferenceLayerRenderer}"
ShowReferenceLayer="{Binding ReferenceLayer.IsVisible}"
HideReferenceLayer="{Binding XamlAccesibleViewModel.BitmapManager.HideReferenceLayer}"
Width="{Binding Width}" Height="{Binding Height}"
RenderOptions.BitmapScalingMode="NearestNeighbor"/>

<Image Source="{Binding PreviewLayerRenderer.FinalBitmap}" Panel.ZIndex="2"
RenderOptions.BitmapScalingMode="NearestNeighbor" Stretch="Uniform"
Width="{Binding Width}"
Height="{Binding Height}"/>
RenderOptions.BitmapScalingMode="NearestNeighbor" Stretch="Uniform"
Width="{Binding Width}" Height="{Binding Height}"/>

<Image VerticalAlignment="Top" HorizontalAlignment="Left" Source="{Binding Renderer.FinalBitmap}"
RenderOptions.BitmapScalingMode="NearestNeighbor" Stretch="Uniform" />
RenderOptions.BitmapScalingMode="NearestNeighbor" Stretch="Uniform"
Visibility="{Binding XamlAccesibleViewModel.BitmapManager.OnlyReferenceLayer, Converter={InverseBoolToVisibilityConverter}}"/>

<local:PlainLayerView TargetLayer="{Binding ActiveSelection.SelectionLayer}"
VerticalAlignment="Top" HorizontalAlignment="Left"
Width="{Binding ActiveSelection.SelectionLayer.Width}"
Height="{Binding ActiveSelection.SelectionLayer.Height}"
Margin="{Binding ActiveSelection.SelectionLayer.Offset}" />
VerticalAlignment="Top" HorizontalAlignment="Left"
Width="{Binding ActiveSelection.SelectionLayer.Width}"
Height="{Binding ActiveSelection.SelectionLayer.Height}"
Margin="{Binding ActiveSelection.SelectionLayer.Offset}"/>

<Grid ShowGridLines="True" Width="{Binding Width}" Height="{Binding Height}" Panel.ZIndex="10"
Visibility="{Binding GridLinesVisible, Converter={StaticResource BoolToVisibilityConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:DrawingViewPort}}}">
<Rectangle Focusable="False">
Expand Down
12 changes: 12 additions & 0 deletions PixiEditor/Views/UserControls/ReferenceLayerView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<UserControl x:Class="PixiEditor.Views.UserControls.ReferenceLayerView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PixiEditor.Views.UserControls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Name="uc">
<Image Source="{Binding ReferenceLayerRenderer.FinalBitmap, ElementName=uc}"
Visibility="{Binding ReferenceLayerVisibility, ElementName=uc}"/>
</UserControl>
67 changes: 67 additions & 0 deletions PixiEditor/Views/UserControls/ReferenceLayerView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using PixiEditor.Models.Controllers;
using System.Windows;
using System.Windows.Controls;

namespace PixiEditor.Views.UserControls
{
/// <summary>
/// Interaction logic for ReferenceLayerView.xaml
/// </summary>
public partial class ReferenceLayerView : UserControl
{
public static readonly DependencyProperty ReferenceLayerRendererProperty =
DependencyProperty.Register(nameof(ReferenceLayerRenderer), typeof(SingleLayerRenderer), typeof(ReferenceLayerView));

public SingleLayerRenderer ReferenceLayerRenderer
{
get => (SingleLayerRenderer)GetValue(ReferenceLayerRendererProperty);
set => SetValue(ReferenceLayerRendererProperty, value);
}

public static readonly DependencyProperty ShowReferenceLayerProperty =
DependencyProperty.Register(nameof(ShowReferenceLayer), typeof(bool), typeof(ReferenceLayerView), new PropertyMetadata(UpdateVisibility));

public bool ShowReferenceLayer
{
get => (bool)GetValue(ShowReferenceLayerProperty);
set => SetValue(ShowReferenceLayerProperty, value);
}

public static readonly DependencyProperty HideReferenceLayerProperty =
DependencyProperty.Register(nameof(HideReferenceLayer), typeof(bool), typeof(ReferenceLayerView), new PropertyMetadata(UpdateVisibility));

public bool HideReferenceLayer
{
get => (bool)GetValue(HideReferenceLayerProperty);
set => SetValue(HideReferenceLayerProperty, value);
}

internal static readonly DependencyPropertyKey ReferenceLayerVisibilityKey =
DependencyProperty.RegisterReadOnly(nameof(ReferenceLayerVisibility), typeof(Visibility), typeof(ReferenceLayerView), new FrameworkPropertyMetadata());

public Visibility ReferenceLayerVisibility
{
get => (Visibility)GetValue(ReferenceLayerVisibilityKey.DependencyProperty);
private set => SetValue(ReferenceLayerVisibilityKey, value);
}

public ReferenceLayerView()
{
InitializeComponent();
}

private static void UpdateVisibility(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var view = obj as ReferenceLayerView;

Visibility visibility = view.ShowReferenceLayer ? Visibility.Visible : Visibility.Collapsed;

if (view.HideReferenceLayer)
{
visibility = Visibility.Collapsed;
}

view.ReferenceLayerVisibility = visibility;
}
}
}

0 comments on commit 905d4ac

Please sign in to comment.