Skip to content

Commit

Permalink
Extract editor settings
Browse files Browse the repository at this point in the history
  • Loading branch information
miroiu committed Sep 5, 2022
1 parent 6dcf696 commit 8ab3e90
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 107 deletions.
2 changes: 1 addition & 1 deletion Examples/Nodify.MinimalExample/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<nodifier:NodifyLoader />
<ResourceDictionary Source="pack://application:,,,/Nodify;component/Themes/Dark.xaml" />
<nodifier:NodifyLoader />
<ResourceDictionary>
<Style TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
<Style.Setters>
Expand Down
11 changes: 9 additions & 2 deletions Examples/Nodify.MinimalExample/MinimalApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CustomGraphNode(IGraph graph) : base(graph)

public class MinimalApp
{
public Graph Graph { get; } = new Graph();
public GraphEditor Graph { get; } = new GraphEditor();
private static readonly Random _random = new Random();

public MinimalApp()
Expand Down Expand Up @@ -122,7 +122,14 @@ public void AlignSelection()

public void AddComment()
{
Graph.AddComment("This is a comment sorounding all nodes", Graph.Elements);
if (Graph.SelectedElements.Count > 0)
{
Graph.AddComment(string.Empty, Graph.SelectedElements);
}
else
{
Graph.AddComment("This is a comment sorounding all nodes", Graph.Elements);
}
}

public void FocusRandomNode()
Expand Down
2 changes: 1 addition & 1 deletion Examples/Nodify.MinimalExample/Toolkit/BlueprintGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IBlueprintGraph : IGraph
void Disconnect(IGraphNode node);
}

public class BlueprintGraph : Graph, IBlueprintGraph
public class BlueprintGraph : GraphEditor, IBlueprintGraph
{
private readonly IPendingConnection _pendingConnection;
public override IPendingConnection PendingConnection => _pendingConnection;
Expand Down
77 changes: 76 additions & 1 deletion Nodifier/EditorSettings.cs → Nodifier/Graph/EditorSettings.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,82 @@
using Nodify;
using Stylet;

namespace Nodifier
{
public static class EditorSettings
public interface IEditorSettings
{
double MinViewportZoom { get; set; }
double MaxViewportZoom { get; set; }

double GridSnapSize { get; set; }

bool DisableZooming { get; set; }
bool DisablePanning { get; set; }
bool DisableAutoPanning { get; set; }
bool EnableRealtimeSelection { get; set; }
bool ShowGridLines { get; set; }
}

public class EditorSettings : PropertyChangedBase, IEditorSettings
{
private double _minViewportZoom = 0.1d;
public double MinViewportZoom
{
get => _minViewportZoom;
set => SetAndNotify(ref _minViewportZoom, value);
}

private double _maxViewportZoom = 2d;
public double MaxViewportZoom
{
get => _maxViewportZoom;
set => SetAndNotify(ref _maxViewportZoom, value);
}

private double _gridSnapSize;
public double GridSnapSize
{
get => _gridSnapSize;
set => SetAndNotify(ref _gridSnapSize, value);
}

private bool _disableZooming;
public bool DisableZooming
{
get => _disableZooming;
set => SetAndNotify(ref _disableZooming, value);
}

private bool _disablePanning;
public bool DisablePanning
{
get => _disablePanning;
set => SetAndNotify(ref _disablePanning, value);
}

private bool _disableAutoPanning;
public bool DisableAutoPanning
{
get => _disableAutoPanning;
set => SetAndNotify(ref _disableAutoPanning, value);
}

private bool _enableRealtimeSelection = true;
public bool EnableRealtimeSelection
{
get => _enableRealtimeSelection;
set => SetAndNotify(ref _enableRealtimeSelection, value);
}

private bool _showGridLines = true;
public bool ShowGridLines
{
get => _showGridLines;
set => SetAndNotify(ref _showGridLines, value);
}

#region Global Settings

public static double HandleRightClickAfterPanningThreshold
{
get => NodifyEditor.HandleRightClickAfterPanningThreshold;
Expand Down Expand Up @@ -69,5 +142,7 @@ public static double OptimizeRenderingZoomOutPercent
get => NodifyEditor.OptimizeRenderingZoomOutPercent;
set => NodifyEditor.OptimizeRenderingZoomOutPercent = value;
}

#endregion
}
}
54 changes: 3 additions & 51 deletions Nodifier/Graph/Graph.Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum Alignment
Center
}

public partial class Graph : PropertyChangedBase, IEditor, IViewAware
public partial class GraphEditor : PropertyChangedBase, IEditor, IViewAware
{
private NodifyEditor? _editor;
protected NodifyEditor Editor => _editor ?? throw new InvalidOperationException($"No editor attached. Please implement {nameof(INodifyEditorAware)} in the view and wait for initialization.");
Expand All @@ -33,7 +33,8 @@ public partial class Graph : PropertyChangedBase, IEditor, IViewAware
{ Alignment.Center, EditorCommands.Alignment.Center }
};

public event EventHandler Initialized;
public event EventHandler? Initialized;
public IEditorSettings Settings { get; } = new EditorSettings();

private Point _viewportLocation;
public Point ViewportLocation
Expand All @@ -56,55 +57,6 @@ public double ViewportZoom
set => SetAndNotify(ref _viewportZoom, value);
}

private double _minViewportZoom = 0.1d;
public double MinViewportZoom
{
get => _minViewportZoom;
set => SetAndNotify(ref _minViewportZoom, value);
}

private double _maxViewportZoom = 2d;
public double MaxViewportZoom
{
get => _maxViewportZoom;
set => SetAndNotify(ref _maxViewportZoom, value);
}

private double _gridSnapSize;
public double GridSnapSize
{
get => _gridSnapSize;
set => SetAndNotify(ref _gridSnapSize, value);
}

private bool _disableZooming;
public bool DisableZooming
{
get => _disableZooming;
set => SetAndNotify(ref _disableZooming, value);
}

private bool _disablePanning;
public bool DisablePanning
{
get => _disablePanning;
set => SetAndNotify(ref _disablePanning, value);
}

private bool _disableAutoPanning;
public bool DisableAutoPanning
{
get => _disableAutoPanning;
set => SetAndNotify(ref _disableAutoPanning, value);
}

private bool _enableRealtimeSelection = true;
public bool EnableRealtimeSelection
{
get => _enableRealtimeSelection;
set => SetAndNotify(ref _enableRealtimeSelection, value);
}

void IViewAware.AttachView(UIElement view)
{
if (view is INodifyEditorAware editorAware)
Expand Down
4 changes: 2 additions & 2 deletions Nodifier/Graph/Graph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Nodifier
{
public partial class Graph : IGraph
public partial class GraphEditor : IGraph
{
protected readonly BindableCollection<IGraphElement> _elements = new BindableCollection<IGraphElement>();
public IReadOnlyCollection<IGraphElement> Elements => _elements;
Expand All @@ -19,7 +19,7 @@ public partial class Graph : IGraph

public virtual IPendingConnection PendingConnection { get; }

public Graph()
public GraphEditor()
{
PendingConnection = new PendingConnection(this);
}
Expand Down
8 changes: 5 additions & 3 deletions Nodifier/Graph/GraphExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ public static void FocusLocation(this IEditor graph, double x, double y)
graph.FocusLocation(new Point(x, y));
}

public static void AddComment(this IGraph graph, string text, IEnumerable<IGraphElement> nodes)
public static CommentNode AddComment(this IGraph graph, string text, IEnumerable<IGraphElement> nodes)
{
var bounds = nodes.GetBoundingBox();
graph.AddElement(new CommentNode(graph)
var comment = new CommentNode(graph)
{
Location = bounds.Location,
CommentSize = bounds.Size,
Title = text
});
};
graph.AddElement(comment);
return comment;
}

private static (Point Location, Size Size) GetBoundingBox(this IEnumerable<IGraphElement> nodes)
Expand Down
107 changes: 73 additions & 34 deletions Nodifier/Graph/GraphView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,84 @@
xmlns:s="https://github.com/canton7/Stylet"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:DataContext="{d:DesignInstance Type=vm:Graph, IsDesignTimeCreatable=False}">
<nodify:NodifyEditor ItemsSource="{Binding Elements}"
d:DataContext="{d:DesignInstance Type=vm:GraphEditor, IsDesignTimeCreatable=False}">
<UserControl.Resources>
<GeometryDrawing x:Key="SmallGridGeometry"
Geometry="M0,0 L0,1 0.03,1 0.03,0.03 1,0.03 1,0 Z"
Brush="{DynamicResource NodifyEditor.SelectionRectangleBackgroundBrush}" />

<GeometryDrawing x:Key="LargeGridGeometry"
Geometry="M0,0 L0,1 0.015,1 0.015,0.015 1,0.015 1,0 Z"
Brush="{DynamicResource NodifyEditor.SelectionRectangleBackgroundBrush}" />

<DrawingBrush x:Key="SmallGridLinesDrawingBrush"
TileMode="Tile"
ViewportUnits="Absolute"
Viewport="0 0 20 20"
Transform="{Binding ViewportTransform, ElementName=EditorInstance}"
Drawing="{StaticResource SmallGridGeometry}" />

<DrawingBrush x:Key="LargeGridLinesDrawingBrush"
TileMode="Tile"
ViewportUnits="Absolute"
Opacity="0.5"
Viewport="0 0 100 100"
Transform="{Binding ViewportTransform, ElementName=EditorInstance}"
Drawing="{StaticResource LargeGridGeometry}" />
</UserControl.Resources>

<Grid Background="{StaticResource NodifyEditor.BackgroundBrush}">
<nodify:NodifyEditor ItemsSource="{Binding Elements}"
SelectedItems="{Binding SelectedElements}"
Connections="{Binding Connections}"
PendingConnection="{Binding PendingConnection}"
ViewportLocation="{Binding ViewportLocation}"
ViewportSize="{Binding ViewportSize, Mode=OneWayToSource}"
ViewportZoom="{Binding ViewportZoom}"
DisablePanning="{Binding DisablePanning}"
DisableZooming="{Binding DisableZooming}"
DisableAutoPanning="{Binding DisableAutoPanning}"
MinViewportZoom="{Binding MinViewportZoom}"
MaxViewportZoom="{Binding MaxViewportZoom}"
GridCellSize="{Binding GridSnapSize}"
EnableRealtimeSelection="{Binding EnableRealtimeSelection}"
DisablePanning="{Binding Settings.DisablePanning}"
DisableZooming="{Binding Settings.DisableZooming}"
DisableAutoPanning="{Binding Settings.DisableAutoPanning}"
MinViewportZoom="{Binding Settings.MinViewportZoom}"
MaxViewportZoom="{Binding Settings.MaxViewportZoom}"
GridCellSize="{Binding Settings.GridSnapSize}"
EnableRealtimeSelection="{Binding Settings.EnableRealtimeSelection}"
x:Name="EditorInstance">
<nodify:NodifyEditor.ItemContainerStyle>
<Style TargetType="nodify:ItemContainer">
<Setter Property="Location" Value="{Binding Location, Mode=TwoWay}" />
<Setter Property="ActualSize" Value="{Binding Size, Mode=OneWayToSource}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="IsSelectable" Value="{Binding IsSelectable}" />
<Setter Property="IsDraggable" Value="{Binding IsDraggable}" />
</Style>
</nodify:NodifyEditor.ItemContainerStyle>
<nodify:NodifyEditor.ItemTemplate>
<DataTemplate>
<ContentControl s:View.Model="{Binding}" />
</DataTemplate>
</nodify:NodifyEditor.ItemTemplate>
<nodify:NodifyEditor.PendingConnectionTemplate>
<DataTemplate>
<ContentControl s:View.Model="{Binding}" />
</DataTemplate>
</nodify:NodifyEditor.PendingConnectionTemplate>
<nodify:NodifyEditor.ConnectionTemplate>
<DataTemplate>
<ContentControl s:View.Model="{Binding}" />
</DataTemplate>
</nodify:NodifyEditor.ConnectionTemplate>
</nodify:NodifyEditor>
<nodify:NodifyEditor.Style>
<Style TargetType="{x:Type nodify:NodifyEditor}" BasedOn="{StaticResource {x:Type nodify:NodifyEditor}}">
<Style.Triggers>
<DataTrigger Binding="{Binding Settings.ShowGridLines}" Value="True">
<Setter Property="Background" Value="{StaticResource SmallGridLinesDrawingBrush}" />
</DataTrigger>
</Style.Triggers>
</Style>
</nodify:NodifyEditor.Style>
<nodify:NodifyEditor.ItemContainerStyle>
<Style TargetType="nodify:ItemContainer">
<Setter Property="Location" Value="{Binding Location, Mode=TwoWay}" />
<Setter Property="ActualSize" Value="{Binding Size, Mode=OneWayToSource}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="IsSelectable" Value="{Binding IsSelectable}" />
<Setter Property="IsDraggable" Value="{Binding IsDraggable}" />
</Style>
</nodify:NodifyEditor.ItemContainerStyle>
<nodify:NodifyEditor.ItemTemplate>
<DataTemplate>
<ContentControl s:View.Model="{Binding}" />
</DataTemplate>
</nodify:NodifyEditor.ItemTemplate>
<nodify:NodifyEditor.PendingConnectionTemplate>
<DataTemplate>
<ContentControl s:View.Model="{Binding}" />
</DataTemplate>
</nodify:NodifyEditor.PendingConnectionTemplate>
<nodify:NodifyEditor.ConnectionTemplate>
<DataTemplate>
<ContentControl s:View.Model="{Binding}" />
</DataTemplate>
</nodify:NodifyEditor.ConnectionTemplate>
</nodify:NodifyEditor>

<Grid Background="{StaticResource LargeGridLinesDrawingBrush}"
Panel.ZIndex="-2" />
</Grid>
</UserControl>
9 changes: 1 addition & 8 deletions Nodifier/Graph/IEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@ public interface IEditor
Point ViewportLocation { get; set; }
Size ViewportSize { get; }
double ViewportZoom { get; set; }
double MinViewportZoom { get; set; }
double MaxViewportZoom { get; set; }

double GridSnapSize { get; set; }

bool DisableZooming { get; set; }
bool DisablePanning { get; set; }
bool DisableAutoPanning { get; set; }
bool EnableRealtimeSelection { get; set; }
IEditorSettings Settings { get; }

void FocusLocation(Point location);
void ZoomIn();
Expand Down
Loading

0 comments on commit 8ab3e90

Please sign in to comment.