Skip to content

Commit

Permalink
Refactor Nodes
Browse files Browse the repository at this point in the history
Refactor Nodes
Fixes Issue #1 Refactor Nodes
  • Loading branch information
WaynePacker committed Mar 21, 2018
1 parent 880cc6b commit 2af9af9
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 583 deletions.
6 changes: 2 additions & 4 deletions FlowGraph.UI/FlowGraph.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@
<Compile Include="Interfaces\IRootNodeViewModel.cs" />
<Compile Include="Interfaces\IStandardNodeViewModel.cs" />
<Compile Include="NetworkModel\Base\AConnectionViewModel.cs" />
<Compile Include="NetworkModel\Base\ANodeViewModel.cs" />
<Compile Include="NetworkModel\NodeViewModel.cs" />
<Compile Include="NetworkModel\NodeFactory\NodeViewModelFactory.cs" />
<Compile Include="NetworkModel\PathConnectionViewModel.cs" />
<Compile Include="NetworkModel\RootNodeViewModel.cs" />
<Compile Include="NetworkModel\StandardConnectionViewModel.cs" />
<Compile Include="Toolbar\Toolbar.xaml.cs">
<DependentUpon>Toolbar.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -172,11 +172,9 @@
<Compile Include="MainWindowViewModel.cs" />
<Compile Include="MainWindow_ZoomAndPan.cs" />
<Compile Include="MouseHandlingMode.cs" />
<Compile Include="NetworkModel\StandardConnectionViewModel.cs" />
<Compile Include="NetworkModel\ConnectorType.cs" />
<Compile Include="NetworkModel\ConnectorViewModel.cs" />
<Compile Include="NetworkModel\NetworkViewModel.cs" />
<Compile Include="NetworkModel\NodeViewModel.cs" />
<Compile Include="OverviewWindow.xaml.cs">
<DependentUpon>OverviewWindow.xaml</DependentUpon>
</Compile>
Expand Down
109 changes: 12 additions & 97 deletions FlowGraph.UI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<Window.Resources>


<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Merge in the resource dictionary that is shared between the main window and the overview window. -->
Expand Down Expand Up @@ -53,6 +54,7 @@
It is used to convert the value of 'ContentScale' to the percentage zoom level that is displayed in the UI.
-->
<local:ScaleToPercentConverter x:Key="scaleToPercentConverter" />
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

<!--
This graphic is used to indicate that a connection cannot be made between two particular connectors.
Expand Down Expand Up @@ -317,9 +319,8 @@
</ac:AdornedControl>
</DataTemplate>


<!-- Define a data-template for the 'NodeViewModel' class. -->
<DataTemplate x:Name="NodeControl" DataType="{x:Type NetworkModel:NodeViewModel}" >
<DataTemplate x:Name="TempNodeControl" DataType="{x:Type NetworkModel:NodeViewModel}" >
<!-- The margin has been selected so that the selection rect nicely covers the entire node. -->

<Grid MinWidth="120"
Expand Down Expand Up @@ -366,105 +367,21 @@
Focusable="True"
Cursor="Hand"/>

<!--Parent Connector : Wrap in stack panel for correct cursor-->


<StackPanel Grid.Column="0" Grid.Row="0" Cursor="Hand">
<StackPanel Grid.Column="0" Grid.Row="0" Cursor="Hand"
Visibility="{Binding HasLeftPathConnector, Converter={StaticResource BooleanToVisibilityConverter}}">
<NetworkUI:ConnectorItem
DataContext="{Binding ParentNodeConnection}"
DataContext="{Binding LeftNodeConnection}"
Width="15"
Height="15"
HorizontalAlignment="Left" />
</StackPanel>

<StackPanel Grid.Column="2" Grid.Row="0" Cursor="Hand">
<NetworkUI:ConnectorItem
DataContext="{Binding ChildNodeConnection}"
Width="15"
Height="15"
HorizontalAlignment="Right" />
</StackPanel>

<!-- Displays the node's input connectors. -->
<ItemsControl Grid.Column="0"
Grid.Row="2"
ItemsSource="{Binding InputConnectors}"
ItemTemplate="{StaticResource inputConnectorTemplate}"
Focusable="False" />

<!-- Displays the node's output connectors. -->
<ItemsControl Grid.Column="2"
Grid.Row="2"
ItemsSource="{Binding OutputConnectors}"
ItemTemplate="{StaticResource outputConnectorTemplate}"
Focusable="False" />
</Grid>
</Grid>


<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="true">
<Setter TargetName="node" Property="Stroke" Value="{StaticResource selectedNodeBorderBrush}"/>
<Setter TargetName="node" Property="Fill" Value="{StaticResource selectedNodeFillBrush}"/>
</DataTrigger>
</DataTemplate.Triggers>


</DataTemplate>




<DataTemplate x:Name="RootNodeControl" DataType="{x:Type NetworkModel:RootNodeViewModel}" >
<!-- The margin has been selected so that the selection rect nicely covers the entire node. -->

<Grid MinWidth="120"
Margin="10 6"
SizeChanged="Node_SizeChanged" >
<!-- This rectangle is the main visual for the node. -->

<Rectangle x:Name="node"
Stroke="{StaticResource nodeBorderBrush}"
StrokeThickness="1"
Fill="{StaticResource nodeFillBrush}" >
<Rectangle.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete"
Command="{StaticResource Commands.DeleteNode}"
CommandParameter="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" />
</ContextMenu>
</Rectangle.ContextMenu>
</Rectangle>

<!--
This grid contains the node's connectors.
The margin is negative so that the connectors overlap the body of the node and it's selection border.
-->
<Grid Margin="-6,4,-6,4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" MinWidth="10" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<!-- spacer -->
<RowDefinition Height="10" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<!-- The name of the node. -->
<TextBlock Grid.Column="1"
Grid.Row="0"
Text="{Binding Name}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Focusable="True"
Cursor="Hand"/>

<!--Child Connector : Wrap in stack panel for correct cursor-->
<StackPanel Grid.Column="2" Grid.Row="0" Cursor="Hand">
<StackPanel Grid.Column="2" Grid.Row="0" Cursor="Hand"
Visibility="{Binding HasRightPathConnector, Converter={StaticResource BooleanToVisibilityConverter}}">
<NetworkUI:ConnectorItem
DataContext="{Binding ChildNodeConnection}"
DataContext="{Binding RightNodeConnection}"
Width="15"
Height="15"
HorizontalAlignment="Right" />
Expand Down Expand Up @@ -493,10 +410,8 @@
<Setter TargetName="node" Property="Fill" Value="{StaticResource selectedNodeFillBrush}"/>
</DataTrigger>
</DataTemplate.Triggers>


</DataTemplate>

</ResourceDictionary>

</Window.Resources>
Expand Down Expand Up @@ -630,7 +545,7 @@
Command="{StaticResource Commands.ZoomOut}"
ToolTip="Zooms in on the canvas"
/>

</ContextMenu>
</Window.ContextMenu>

Expand Down
13 changes: 5 additions & 8 deletions FlowGraph.UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using FlowGraph.UI.Interfaces;
using FlowGraph.UI.NetworkModel;
using FlowGraph.UI.NetworkModel.Base;
using FlowGraph.UI.NetworkModel.NodeFactory;
using FlowGraph.UI.NetworkModel;
using NetworkUI;
using System.Windows;
using System.Windows.Input;
Expand Down Expand Up @@ -127,7 +124,7 @@ private void CreateRootNode_Executed(object sender, ExecutedRoutedEventArgs e)
/// </summary>
private void DeleteNode_Executed(object sender, ExecutedRoutedEventArgs e)
{
var node = (ANodeViewModel)e.Parameter;
var node = (NodeViewModel)e.Parameter;
this.ViewModel.DeleteNode(node);
}

Expand All @@ -146,7 +143,7 @@ private void DeleteConnection_Executed(object sender, ExecutedRoutedEventArgs e)
private void CreateStandardNode()
{
var newNodePosition = Mouse.GetPosition(networkControl);
this.ViewModel.CreateNode<NodeViewModel>("New Standard Node!", newNodePosition, true);
ViewModel.CreateNode("Standard Node", newNodePosition, centerNode: true, hasLeftPath: true, hasRightPath: true);
}

/// <summary>
Expand All @@ -155,7 +152,7 @@ private void CreateStandardNode()
private void CreateRootNode()
{
var newNodePosition = Mouse.GetPosition(networkControl);
this.ViewModel.CreateNode<RootNodeViewModel>("New Root Node!", newNodePosition, true);
ViewModel.CreateNode("Root Node", newNodePosition, centerNode: true, hasLeftPath: false, hasRightPath: true);
}

/// <summary>
Expand All @@ -168,7 +165,7 @@ private void Node_SizeChanged(object sender, SizeChangedEventArgs e)
// has changed. Push the size of the node through to the view-model.
//
var element = (FrameworkElement)sender;
var node = (ANodeViewModel)element.DataContext;
var node = (NodeViewModel)element.DataContext;
node.Size = new Size(element.ActualWidth, element.ActualHeight);
}
}
Expand Down
51 changes: 25 additions & 26 deletions FlowGraph.UI/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using FlowGraph.UI.Interfaces;
using FlowGraph.UI.NetworkModel;
using FlowGraph.UI.NetworkModel.Base;
using FlowGraph.UI.NetworkModel.NodeFactory;
using System;
using System.Diagnostics;
Expand Down Expand Up @@ -148,9 +147,9 @@ public AConnectionViewModel ConnectionDragStarted(ConnectorViewModel draggedOutC
case ConnectorType.Path:
{
//
// The user is dragging out a parent connector (an input) and will connect it to a child connector (an output).
// The user is dragging out a path connector.
//
if (draggedOutConnector.Name == "Parent")
if (draggedOutConnector.Name == NodeViewModel.DefaultLeftNodeConnectorName)
{
connection.DestConnector = draggedOutConnector;
connection.SourceConnectorHotspot = curDragPoint;
Expand Down Expand Up @@ -183,7 +182,7 @@ public AConnectionViewModel ConnectionDragStarted(ConnectorViewModel draggedOutC
/// </summary>
public void QueryConnnectionFeedback(ConnectorViewModel draggedOutConnector, ConnectorViewModel draggedOverConnector, out object feedbackIndicator, out bool connectionOk)
{
connectionOk = IsValidParentChildPathConnection(draggedOutConnector, draggedOverConnector) ||
connectionOk = IsValidPathConnection(draggedOutConnector, draggedOverConnector) ||
IsValidInputOutputConnection(draggedOutConnector, draggedOverConnector);

if (connectionOk)
Expand All @@ -205,7 +204,7 @@ public void ConnectionDragging(Point curDragPoint, AConnectionViewModel connecti

/// <summary>
/// Called when the user has finished dragging out the new connection.
/// Only one parent- child connection is allowed
/// Only one left- right connection is allowed
/// invalid connections are removed
/// </summary>
///
Expand All @@ -222,7 +221,7 @@ public void ConnectionDragCompleted(AConnectionViewModel newConnection, Connecto
}


var validPathConnection = IsValidParentChildPathConnection(connectorDraggedOut, connectorDraggedOver);
var validPathConnection = IsValidPathConnection(connectorDraggedOut, connectorDraggedOver);
bool connectionOk = validPathConnection ||
IsValidInputOutputConnection(connectorDraggedOut, connectorDraggedOver);

Expand All @@ -234,24 +233,24 @@ public void ConnectionDragCompleted(AConnectionViewModel newConnection, Connecto

if (validPathConnection)
{
var isConnectingFromChildToParent = connectorDraggedOut.Name == "Child";
var isConnectingFromLeftToRight = connectorDraggedOut.Name == NodeViewModel.DefaultRightNodeConnectorName;

var sourceConnector = isConnectingFromChildToParent ? connectorDraggedOut : connectorDraggedOver;
var destConnector = isConnectingFromChildToParent ? connectorDraggedOver : connectorDraggedOut;
var sourceConnector = isConnectingFromLeftToRight ? connectorDraggedOut : connectorDraggedOver;
var destConnector = isConnectingFromLeftToRight ? connectorDraggedOver : connectorDraggedOut;

var sourceChildConnection = sourceConnector.ParentNode.ChildNodeConnection;
var destinationParentConnection = (destConnector.ParentNode as NodeViewModel)?.ParentNodeConnection;
var sourceRightConnection = sourceConnector.ParentNode.RightNodeConnection;
var destinationLeftConnection = destConnector.ParentNode.LeftNodeConnection;


if (sourceChildConnection.IsConnected)
if (sourceRightConnection != null && sourceRightConnection.IsConnected)
{
var existingConnection = sourceChildConnection.AttachedConnections.Single(c => c.DestConnector != null);
var existingConnection = sourceRightConnection.AttachedConnections.Single(c => c.DestConnector != null);
Network.Connections.Remove(existingConnection);
}

if (destinationParentConnection != null && destinationParentConnection.IsConnected)
if (destinationLeftConnection != null && destinationLeftConnection.IsConnected)
{
var existingConnection = destinationParentConnection.AttachedConnections.Single(c => c.SourceConnector != null);
var existingConnection = destinationLeftConnection.AttachedConnections.Single(c => c.SourceConnector != null);
Network.Connections.Remove(existingConnection);
}
}
Expand All @@ -277,7 +276,7 @@ public void ConnectionDragCompleted(AConnectionViewModel newConnection, Connecto
newConnection.SourceConnector = connectorDraggedOver;
}

private bool IsValidParentChildPathConnection(ConnectorViewModel connector1, ConnectorViewModel connector2)
private bool IsValidPathConnection(ConnectorViewModel connector1, ConnectorViewModel connector2)
{
if (connector1 == connector2)
return false;
Expand Down Expand Up @@ -354,7 +353,7 @@ public void DeleteSelectedNodes()
/// Delete the node from the view-model.
/// Also deletes any connections to or from the node.
/// </summary>
public void DeleteNode(ANodeViewModel node)
public void DeleteNode(NodeViewModel node)
{
Network.Connections.RemoveRange(node.AttachedConnections);
Network.Nodes.Remove(node);
Expand All @@ -364,15 +363,15 @@ public void DeleteNode(ANodeViewModel node)
/// Creates the node according to the type using the factory method
/// and add it to the view-model.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name">Name of the the node</param>
/// <param name="nodeLocation"></param>
/// <param name="centerNode">Should the node be centered</param>
/// <returns></returns>
public ANodeViewModel CreateNode<T>(string name, Point nodeLocation, bool centerNode)
where T : INodeViewModel
public NodeViewModel CreateNode(string name, Point nodeLocation, bool centerNode, bool hasLeftPath, bool hasRightPath)
{
var node = NodeViewModelFactory.Create<T>(name, nodeLocation, 2, 2);
var node = NodeViewModelFactory.Create(name, nodeLocation, hasLeftPath, hasRightPath);

NodeViewModelFactory.AttachInputAndOutputConnectors(node, 2, 2);//This is preliminary to test things out

if (centerNode)
CenterNode(node);
Expand Down Expand Up @@ -407,7 +406,7 @@ public void DeleteConnection(AConnectionViewModel connection)
///
/// </summary>
/// <param name="node"></param>
private void CenterNode(ANodeViewModel node)
private void CenterNode(NodeViewModel node)
{
EventHandler<EventArgs> sizeChangedEventHandler = null;
sizeChangedEventHandler =
Expand Down Expand Up @@ -439,8 +438,8 @@ private void PopulateWithTestData()
//
// Create some nodes and add them to the view-model.
//
var node1 = (NodeViewModel)CreateNode<NodeViewModel>("Node1", new Point(100, 60), false);
var node2 = (NodeViewModel)CreateNode<NodeViewModel>("Node2", new Point(350, 80), false);
var node1 = CreateNode("Test Node1", new Point(100, 60), false, true, true);
var node2 = CreateNode("Test Node2", new Point(350, 80), false, true, true);

//
// Create a connection between the nodes.
Expand All @@ -453,8 +452,8 @@ private void PopulateWithTestData()

AConnectionViewModel pathconnection = new PathConnectionViewModel
{
SourceConnector = node1.ChildNodeConnection,
DestConnector = node2.ParentNodeConnection
SourceConnector = node1.RightNodeConnection,
DestConnector = node2.LeftNodeConnection
};

//
Expand Down
Loading

0 comments on commit 2af9af9

Please sign in to comment.