Просмотр исходного кода

1.实现添加节点,父子节点还有线段连接
2.删除节点,删除节点会递归删除该节点的所有子节点

tanghai 14 лет назад
Родитель
Сommit
5d8aa13973

+ 1 - 1
CSharp/Editor/Editor.csproj

@@ -111,7 +111,7 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <PropertyGroup>
-    <PreBuildEvent>$(SolutionDir)\Tools\Nuget.exe install $(ProjectDir)Packages.config -o $(SolutionDir)Packages</PreBuildEvent>
+    <PreBuildEvent>Nuget.exe install $(ProjectDir)Packages.config -o $(SolutionDir)Packages</PreBuildEvent>
   </PropertyGroup>
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

+ 1 - 1
CSharp/Infrastructure/Infrastructure.csproj

@@ -68,7 +68,7 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <PropertyGroup>
-    <PreBuildEvent>$(SolutionDir)\Tools\Nuget.exe install $(ProjectDir)Packages.config -o $(SolutionDir)Packages</PreBuildEvent>
+    <PreBuildEvent>Nuget.exe install $(ProjectDir)Packages.config -o $(SolutionDir)Packages</PreBuildEvent>
   </PropertyGroup>
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

+ 1 - 1
CSharp/Modules/BehaviorTree/BehaviorTree.csproj

@@ -107,7 +107,7 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <PropertyGroup>
-    <PreBuildEvent>$(SolutionDir)\Tools\Nuget.exe install $(ProjectDir)Packages.config -o $(SolutionDir)Packages</PreBuildEvent>
+    <PreBuildEvent>Nuget.exe install $(ProjectDir)Packages.config -o $(SolutionDir)Packages</PreBuildEvent>
   </PropertyGroup>
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

+ 18 - 8
CSharp/Modules/BehaviorTree/BehaviorTreeView.xaml

@@ -10,37 +10,47 @@
 		mc:Ignorable="d" 
 		d:DesignHeight="600" d:DesignWidth="800">
 	<UserControl.Resources>
-		
+		<LinearGradientBrush x:Key="treeNodeFillBrush" StartPoint="0,0" EndPoint="0,1">
+			<GradientStop Color="White" Offset="0" />
+			<GradientStop Color="#7FC9FF" Offset="0.6" />
+		</LinearGradientBrush>
+		<SolidColorBrush x:Key="treeNodeBorderBrush" Color="Black" />
 	</UserControl.Resources>
-	
+
 	<UserControl.CommandBindings>
 		<CommandBinding Command="ApplicationCommands.New" Executed="MenuNewNode_Executed" />
+		<CommandBinding Command="ApplicationCommands.Delete" Executed="MenuDeleteNode_Executed" />
 	</UserControl.CommandBindings>
 
 	<UserControl.ContextMenu>
 		<ContextMenu>
 			<MenuItem Header="New Node" Command="ApplicationCommands.New" 
 					CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
+			<MenuItem Header="Delete Node" Command="ApplicationCommands.Delete" 
+					CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
 		</ContextMenu>
 	</UserControl.ContextMenu>
 	<Grid>
 		<ListBox Name="listBox" SelectionMode="Extended" ItemsSource="{Binding TreeNodes}" >
 			<ListBox.Resources>
 				<Style TargetType="{x:Type ListBoxItem}">
-					<Setter Property="Canvas.Left" Value="{Binding Mode=TwoWay, Path=X}"/>
-					<Setter Property="Canvas.Top" Value="{Binding Mode=TwoWay, Path=Y}"/>
+					<Setter Property="Canvas.Left" Value="{Binding X}"/>
+					<Setter Property="Canvas.Top" Value="{Binding Y}"/>
 				</Style>
 			</ListBox.Resources>
 			<ListBox.ItemsPanel>
 				<ItemsPanelTemplate>
-					<Canvas Background="Transparent" AllowDrop="True" />
+					<Canvas AllowDrop="True" />
 				</ItemsPanelTemplate>
 			</ListBox.ItemsPanel>
 			<ListBox.ItemTemplate>
 				<DataTemplate>
-					<Rectangle Width="50" Height="40" Cursor="Hand" Fill="Green"
-						MouseDown="ListBoxItem_MouseDown" MouseUp="ListBoxItem_MouseUp" 
-						MouseMove="ListBoxItem_MouseMove" />
+					<Canvas MouseDown="ListBoxItem_MouseDown" MouseUp="ListBoxItem_MouseUp" MouseMove="ListBoxItem_MouseMove">
+						<Rectangle Width="{Binding Width}" Height="{Binding Height}" Cursor="Hand" StrokeThickness="1.3" RadiusX="4" RadiusY="4"
+								Stroke="{StaticResource treeNodeBorderBrush}" Fill="{StaticResource treeNodeFillBrush}" />
+						<Line X1="{Binding ConnectorX1}" Y1="{Binding ConnectorY1}" X2="{Binding ConnectorX2}" Y2="{Binding ConnectorY2}" 
+							  Stroke="Black" StrokeThickness="2" ></Line>
+					</Canvas>
 				</DataTemplate>
 			</ListBox.ItemTemplate>
 		</ListBox>

+ 12 - 0
CSharp/Modules/BehaviorTree/BehaviorTreeView.xaml.cs

@@ -4,6 +4,7 @@ using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Input;
 using Infrastructure;
+using NLog;
 
 namespace BehaviorTree
 {
@@ -20,6 +21,7 @@ namespace BehaviorTree
 		private bool isControlDown = false;
 		private bool isLeftButtonDown = false;
 		private Point origMouseDownPoint;
+		private Logger logger = LogManager.GetCurrentClassLogger();
 
 		public BehaviorTreeView()
 		{
@@ -61,6 +63,16 @@ namespace BehaviorTree
 			e.Handled = true;
 		}
 
+		private void MenuDeleteNode_Executed(object sender, ExecutedRoutedEventArgs e)
+		{
+			if (listBox.SelectedItem == null)
+			{
+				return;
+			}
+			var treeNodeViewModel = listBox.SelectedItem as TreeNodeViewModel;
+			this.ViewModel.Remove(treeNodeViewModel);
+		}
+
 		private void ListBoxItem_MouseDown(object sender, MouseButtonEventArgs e)
 		{
 			if (e.ChangedButton != MouseButton.Left)

+ 2 - 2
CSharp/Modules/BehaviorTree/BehaviorTreeViewModel.cs

@@ -33,9 +33,9 @@ namespace BehaviorTree
 
 		public void Remove(TreeNodeViewModel treeNodeViewModel)
 		{
-			for (int i = 0; i < treeNodeViewModel.Children.Count; ++i)
+			for (int i = treeNodeViewModel.Children.Count - 1; i >= 0; --i)
 			{
-				Remove(treeNodeViewModel.Children[0]);
+				Remove(treeNodeViewModel.Children[i]);
 			}
 			treeNodeViewModel.Parent.Children.Remove(treeNodeViewModel);
 			treeNodes.Remove(treeNodeViewModel);

+ 104 - 0
CSharp/Modules/BehaviorTree/TreeNodeViewModel.cs

@@ -1,18 +1,58 @@
 using System.Collections.ObjectModel;
 using Microsoft.Practices.Prism.ViewModel;
+using NLog;
 
 namespace BehaviorTree
 {
 	public class TreeNodeViewModel : NotificationObject
 	{
+		private static double width = 80;
+		private static double height = 50;
+		private double connectorX2 = 0;
+		private double connectorY2 = 0;
 		private TreeNode treeNode;
 		private TreeNodeViewModel parent;
 		private ObservableCollection<TreeNodeViewModel> children = new ObservableCollection<TreeNodeViewModel>();
+		private Logger logger = LogManager.GetCurrentClassLogger();
 
 		public TreeNodeViewModel(TreeNode treeNode, TreeNodeViewModel parent)
 		{
 			this.treeNode = treeNode;
 			this.parent = parent ?? this;
+			if (this.parent == this)
+			{
+				connectorX2 = 0;
+				connectorY2 = this.Height / 2;
+			}
+			else
+			{
+				connectorX2 = this.Parent.Width + this.Parent.X - this.X;
+				connectorY2 = this.Parent.Height / 2 + this.Parent.Y - this.Y;
+			}
+		}
+
+		public double Width
+		{
+			get
+			{
+				return width;
+			}
+			set
+			{
+				width = value;
+			}
+		}
+
+		public double Height
+		{
+			get
+			{
+				return height;
+			}
+			set
+			{
+				height = value;
+			}
 		}
 
 		public double X
@@ -29,6 +69,13 @@ namespace BehaviorTree
 				}
 				treeNode.X = value;
 				RaisePropertyChanged("X");
+
+				this.ConnectorX2 = this.Parent.Width + this.Parent.X - this.X;
+
+				foreach (var child in Children)
+				{
+					child.ConnectorX2 = this.Width + treeNode.X - child.X;
+				}
 			}
 		}
 
@@ -46,6 +93,63 @@ namespace BehaviorTree
 				}
 				treeNode.Y = value;
 				RaisePropertyChanged("Y");
+
+				ConnectorY2 = this.Parent.Height / 2 + this.Parent.Y - this.Y;
+
+				foreach (var child in Children)
+				{
+					child.ConnectorY2 = this.Height / 2 + treeNode.Y - child.Y;
+				}
+			}
+		}
+
+		public double ConnectorX1
+		{
+			get
+			{
+				return 0;
+			}
+		}
+
+		public double ConnectorY1
+		{
+			get
+			{
+				return this.Height / 2;
+			}
+		}
+
+		public double ConnectorX2
+		{
+			get
+			{
+				if (this.Parent == this)
+				{
+					return 0;
+				}
+				return this.connectorX2;
+			}
+			set
+			{
+				this.connectorX2 = value;
+				RaisePropertyChanged("ConnectorX2");
+			}
+		}
+
+		public double ConnectorY2
+		{
+			get
+			{
+				if (this.Parent == this)
+				{
+					return this.Height / 2;
+				}
+				return this.connectorY2;
+			}
+			set
+			{
+				this.connectorY2 = value;
+				RaisePropertyChanged("ConnectorY2");
 			}
 		}