Skip to content

Commit

Permalink
Implemented custom action display
Browse files Browse the repository at this point in the history
  • Loading branch information
CPKreu committed May 21, 2021
1 parent 5114418 commit de41d9a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 11 deletions.
22 changes: 22 additions & 0 deletions PixiEditor/NotifyableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ public class NotifyableObject : INotifyPropertyChanged
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };

public void AddPropertyChangedCallback(string propertyName, Action action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(propertyName));
}

if (string.IsNullOrWhiteSpace(propertyName))
{
PropertyChanged += (_, _) => action();
return;
}

PropertyChanged += (sender, e) =>
{
if (e.PropertyName == propertyName)
{
action();
}
};
}

protected void RaisePropertyChanged(string property)
{
if (property != null)
Expand Down
11 changes: 2 additions & 9 deletions PixiEditor/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
using System.ComponentModel;
using PixiEditor.Helpers;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace PixiEditor.ViewModels
{
public class ViewModelBase : INotifyPropertyChanged
public class ViewModelBase : NotifyableObject
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };

protected void RaisePropertyChanged(string property)
{
if (property != null) PropertyChanged(this, new PropertyChangedEventArgs(property));
}

protected void CloseButton(object parameter)
{
((Window)parameter).Close();
Expand Down
35 changes: 35 additions & 0 deletions PixiEditor/ViewModels/ViewModelMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ namespace PixiEditor.ViewModels
{
public class ViewModelMain : ViewModelBase
{
private string actionDisplay;
private bool overrideActionDisplay;

public static ViewModelMain Current { get; set; }

public Action CloseAction { get; set; }
Expand Down Expand Up @@ -74,6 +77,36 @@ public class ViewModelMain : ViewModelBase

public IPreferences Preferences { get; set; }

public string ActionDisplay
{
get
{
if (OverrideActionDisplay)
{
return actionDisplay;
}

return BitmapManager.SelectedTool.ActionDisplay;
}
set
{
actionDisplay = value;
}
}

/// <summary>
/// Gets or sets a value indicating whether a custom action display should be used. If false the action display of the selected tool will be used.
/// </summary>
public bool OverrideActionDisplay
{
get => overrideActionDisplay;
set
{
SetProperty(ref overrideActionDisplay, value);
RaisePropertyChanged(nameof(ActionDisplay));
}
}

public bool IsDebug
{
get =>
Expand Down Expand Up @@ -181,6 +214,8 @@ public ViewModelMain(IServiceProvider services)
new Shortcut(Key.F1, MiscSubViewModel.OpenShortcutWindowCommand, "Open the shortcut window", true)));

BitmapManager.PrimaryColor = ColorsSubViewModel.PrimaryColor;

BitmapManager.AddPropertyChangedCallback(nameof(BitmapManager.SelectedTool), () => { if (!OverrideActionDisplay) RaisePropertyChanged(nameof(ActionDisplay)); });
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion PixiEditor/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@
<ColumnDefinition Width="290"/>
</Grid.ColumnDefinitions>
<DockPanel>
<TextBlock Text="{Binding BitmapManager.SelectedTool.ActionDisplay}" Foreground="White" FontSize="15" Margin="10,0,0,0" VerticalAlignment="Center"/>
<TextBlock Text="{Binding ActionDisplay}" Foreground="White" FontSize="15" Margin="10,0,0,0" VerticalAlignment="Center"/>
<StackPanel DockPanel.Dock="Right" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
<TextBlock Text="X:" Foreground="White" FontSize="16"/>
<TextBlock Margin="4,0,10,0" Text="{Binding BitmapManager.ActiveDocument.MouseXOnCanvas, Converter={StaticResource DoubleToIntConverter}}" Foreground="White" FontSize="16"/>
Expand Down
2 changes: 1 addition & 1 deletion PixiEditor/Views/UserControls/PreviewWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="400" x:Name="uc"
Foreground="White">
Foreground="White" Background="Transparent">

<UserControl.Resources>
<BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
Expand Down
20 changes: 20 additions & 0 deletions PixiEditor/Views/UserControls/PreviewWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using PixiEditor.Models.DataHolders;
using PixiEditor.Models.ImageManipulation;
using PixiEditor.ViewModels;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -55,6 +56,25 @@ public PreviewWindow()

MouseMove += PreviewWindow_MouseMove;
MouseRightButtonDown += PreviewWindow_MouseRightButtonDown;
MouseEnter += PreviewWindow_MouseEnter;
MouseLeave += PreviewWindow_MouseLeave;
}

private void PreviewWindow_MouseLeave(object sender, MouseEventArgs e)
{
if (ViewModelMain.Current != null)
{
ViewModelMain.Current.OverrideActionDisplay = false;
}
}

private void PreviewWindow_MouseEnter(object sender, MouseEventArgs e)
{
if (ViewModelMain.Current != null)
{
ViewModelMain.Current.ActionDisplay = "Press right click to copy the color at the color cursor to the main color. Hold shift to copy the color the clipboard.";
ViewModelMain.Current.OverrideActionDisplay = true;
}
}

private void PreviewWindow_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
Expand Down

0 comments on commit de41d9a

Please sign in to comment.