Quellcode durchsuchen

行为树节点显示参数

tanghai vor 11 Jahren
Ursprung
Commit
29e840043d

+ 1 - 1
CSharp/App/Modules/BehaviorTreeModule/AllTreeView.xaml

@@ -27,7 +27,7 @@
 						<MenuItem Header="保存" Click="MenuItem_Save" />
 						<MenuItem Header="新建" Click="MenuItem_New" />
 						<MenuItem Header="删除" Click="MenuItem_Remove" />
-						<MenuItem Header="复制" Click="MenuItem_Copy" />
+						<MenuItem Header="克隆" Click="MenuItem_Clone" />
 					</ContextMenu>
 				</ListBox.ContextMenu>
 				<ListBox.ItemTemplate>

+ 7 - 7
CSharp/App/Modules/BehaviorTreeModule/AllTreeView.xaml.cs

@@ -53,14 +53,14 @@ namespace Modules.BehaviorTreeModule
 			this.treeView.ViewModel = treeViewModel;
 		}
 
-		private void MenuItem_Copy(object sender, RoutedEventArgs e)
+		private void MenuItem_Clone(object sender, RoutedEventArgs e)
 		{
 			if (this.lbTreeRoots.SelectedItem == null)
 			{
 				return;
 			}
 			TreeNodeViewModel treeNodeViewModel = this.lbTreeRoots.SelectedItem as TreeNodeViewModel;
-			TreeViewModel treeViewModel = this.ViewModel.Copy(treeNodeViewModel);
+			TreeViewModel treeViewModel = this.ViewModel.Clone(treeNodeViewModel);
 			this.treeView.ViewModel = treeViewModel;
 		}
 
@@ -71,15 +71,15 @@ namespace Modules.BehaviorTreeModule
 				return;
 			}
 			TreeNodeViewModel treeNodeViewModel = this.lbTreeRoots.SelectedItem as TreeNodeViewModel;
-			this.ViewModel.Remove(treeNodeViewModel);
+			this.ViewModel.Remove(treeNodeViewModel.TreeId);
 			this.lbTreeRoots.SelectedItem = null;
 			e.Handled = true;
 		}
 
 		private void ListBoxItem_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
 		{
-			var item = (FrameworkElement) sender;
-			var treeNodeViewModel = item.DataContext as TreeNodeViewModel;
+			FrameworkElement item = (FrameworkElement) sender;
+			TreeNodeViewModel treeNodeViewModel = item.DataContext as TreeNodeViewModel;
 			if (this.treeView.ViewModel != null)
 			{
 				if (this.treeView.ViewModel.TreeId == treeNodeViewModel.TreeId)
@@ -92,8 +92,8 @@ namespace Modules.BehaviorTreeModule
 
 		private void ListBoxItem_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
 		{
-			var item = (FrameworkElement) sender;
-			var treeNodeViewModel = item.DataContext as TreeNodeViewModel;
+			FrameworkElement item = (FrameworkElement) sender;
+			TreeNodeViewModel treeNodeViewModel = item.DataContext as TreeNodeViewModel;
 
 			this.lbTreeRoots.SelectedItem = treeNodeViewModel;
 		}

+ 6 - 5
CSharp/App/Modules/BehaviorTreeModule/AllTreeViewModel.cs

@@ -90,15 +90,16 @@ namespace Modules.BehaviorTreeModule
 			return treeViewModel;
 		}
 
-		public void Remove(TreeNodeViewModel treeNodeViewModel)
+		public void Remove(int treeId)
 		{
-			this.treeViewModelsDict.Remove(treeNodeViewModel.TreeId);
-			this.rootList.Remove(treeNodeViewModel);
+			TreeViewModel treeViewModel = treeViewModelsDict[treeId];
+			this.treeViewModelsDict.Remove(treeId);
+			this.rootList.Remove(treeViewModel.Root);
 		}
 
-		public TreeViewModel Copy(TreeNodeViewModel treeNodeViewModel)
+		public TreeViewModel Clone(TreeNodeViewModel treeNodeViewModel)
 		{
-			TreeViewModel treeViewModel = new TreeViewModel(this, treeNodeViewModel.TreeViewModel);
+			TreeViewModel treeViewModel = (TreeViewModel) treeNodeViewModel.TreeViewModel.Clone();
 			this.treeViewModelsDict[treeViewModel.TreeId] = treeViewModel;
 			this.rootList.Add(treeViewModel.Root);
 			return treeViewModel;

+ 8 - 5
CSharp/App/Modules/BehaviorTreeModule/TreeView.xaml

@@ -7,7 +7,10 @@
              mc:Ignorable="d"
              d:DesignHeight="300" d:DesignWidth="300"
 			 d:DataContext="{d:DesignInstance tree:TreeViewModel}">
+
 	<UserControl.Resources>
+		<tree:ListToStringConverter x:Key="ListToStringConverter"/>
+		
 		<LinearGradientBrush x:Key="treeNodeSelectorFillBrush" StartPoint="0,0" EndPoint="0,1">
 			<GradientStop Color="White" Offset="0" />
 			<GradientStop Color="#FFF37FFF" Offset="0.6" />
@@ -62,19 +65,19 @@
 					<Canvas MouseDown="ListBoxItem_MouseDown" MouseUp="ListBoxItem_MouseUp" MouseMove="ListBoxItem_MouseMove"
 							PreviewMouseLeftButtonDown="ListBoxItem_PreviewMouseLeftButtonDown"
 							PreviewMouseLeftButtonUp="ListBoxItem_PreviewMouseLeftButtonUp">
-						<Rectangle Name="rectNode" Width="{Binding Width}" Height="{Binding Height}" Cursor="Hand" StrokeThickness="2"
+						<Rectangle Name="rectNode" Width="{Binding Width}" Height="{Binding Height}" Cursor="Hand" StrokeThickness="0"
 								RadiusX="10" RadiusY="10" Stroke="{StaticResource treeNodeBorderBrush}" Fill="{StaticResource treeNodeSelectorFillBrush}"/>
 						<Label Content="+" Visibility="{Binding IsFold, Converter={StaticResource FolderVisiableConverter}}" Canvas.Left="60" Canvas.Top="10"/>
-						<StackPanel>
-							<Label Content="{Binding Type, Converter={StaticResource NodeTypeToStringConverter}}"/>
-							<Label Content="{Binding Comment}"/>
-						</StackPanel>
+						<Label Content="{Binding Type, Converter={StaticResource NodeTypeToStringConverter}}" Canvas.Top="0"/>
+						<Label Content="{Binding Args, Converter={StaticResource ListToStringConverter}, UpdateSourceTrigger=PropertyChanged}" Canvas.Top="15"/>
+						<Label Content="{Binding Comment}" Canvas.Top="30"/>
 						<Line X1="{Binding ConnectorX1}" Y1="{Binding ConnectorY1}" X2="{Binding ConnectorX2}" Y2="{Binding ConnectorY2}"
 								Stroke="Black" StrokeThickness="2"  />
 					</Canvas>
 					<DataTemplate.Triggers>
 						<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
 							<Setter TargetName="rectNode" Property="Stroke" Value="Green" />
+							<Setter TargetName="rectNode" Property="StrokeThickness" Value="2" />
 						</DataTrigger>
 						<DataTrigger Binding="{Binding Type, Converter={StaticResource NodeTypeColorConverter}}" Value="selector">
 							<Setter TargetName="rectNode" Property="Fill" Value="{StaticResource treeNodeSelectorFillBrush}" />

+ 33 - 39
CSharp/App/Modules/BehaviorTreeModule/TreeViewModel.cs

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.ComponentModel.Composition;
 using Microsoft.Practices.Prism.Mvvm;
@@ -6,7 +7,7 @@ using Microsoft.Practices.Prism.Mvvm;
 namespace Modules.BehaviorTreeModule
 {
 	[Export(typeof (TreeViewModel)), PartCreationPolicy(CreationPolicy.NonShared)]
-	public class TreeViewModel: BindableBase
+	public class TreeViewModel: BindableBase, ICloneable
 	{
 		private readonly ObservableCollection<TreeNodeViewModel> treeNodes =
 				new ObservableCollection<TreeNodeViewModel>();
@@ -27,7 +28,7 @@ namespace Modules.BehaviorTreeModule
 		public TreeViewModel(AllTreeViewModel allTreeViewModel)
 		{
 			this.AllTreeViewModel = allTreeViewModel;
-			this.TreeId = ++allTreeViewModel.MaxTreeId;
+			this.TreeId = ++AllTreeViewModel.MaxTreeId;
 			TreeNodeViewModel treeNodeViewModel = new TreeNodeViewModel(this, 300, 100);
 			this.treeNodes.Add(treeNodeViewModel);
 			this.treeNodeDict[treeNodeViewModel.Id] = treeNodeViewModel;
@@ -49,42 +50,7 @@ namespace Modules.BehaviorTreeModule
 			TreeLayout treeLayout = new TreeLayout(this);
 			treeLayout.ExcuteLayout();
 		}
-
-		public TreeViewModel(AllTreeViewModel allTreeViewModel, TreeViewModel copyTree)
-		{
-			this.AllTreeViewModel = allTreeViewModel;
-			this.TreeId = ++this.AllTreeViewModel.MaxTreeId;
-			// 旧id和新id的映射关系
-			var idMapping = new Dictionary<int, int>();
-			idMapping[0] = 0;
-			List<TreeNodeData> treeNodeDatas = copyTree.GetDatas();
-			foreach (TreeNodeData treeNodeData in treeNodeDatas)
-			{
-				int newId = ++this.AllTreeViewModel.MaxNodeId;
-				idMapping[treeNodeData.Id] = newId;
-				treeNodeData.Id = newId;
-				treeNodeData.TreeId = this.TreeId;
-			}
-
-			foreach (TreeNodeData treeNodeData in treeNodeDatas)
-			{
-				treeNodeData.Parent = idMapping[treeNodeData.Parent];
-				for (int i = 0; i < treeNodeData.Children.Count; ++i)
-				{
-					treeNodeData.Children[i] = idMapping[treeNodeData.Children[i]];
-				}
-			}
-
-			foreach (TreeNodeData treeNodeData in treeNodeDatas)
-			{
-				TreeNodeViewModel treeNodeViewModel = new TreeNodeViewModel(this, treeNodeData);
-				this.treeNodes.Add(treeNodeViewModel);
-				this.treeNodeDict[treeNodeViewModel.Id] = treeNodeViewModel;
-			}
-			TreeLayout treeLayout = new TreeLayout(this);
-			treeLayout.ExcuteLayout();
-		}
-
+		
 		public List<TreeNodeData> GetDatas()
 		{
 			var treeNodeDatas = new List<TreeNodeData>();
@@ -299,5 +265,33 @@ namespace Modules.BehaviorTreeModule
 			TreeLayout treeLayout = new TreeLayout(this);
 			treeLayout.ExcuteLayout();
 		}
+
+		public object Clone()
+		{
+			int treeId = ++AllTreeViewModel.MaxTreeId;
+			List<TreeNodeData> treeNodeDatas = this.GetDatas();
+			// 旧id和新id的映射关系
+			var idMapping = new Dictionary<int, int>();
+			idMapping[0] = 0;
+			foreach (TreeNodeData treeNodeData in treeNodeDatas)
+			{
+				int newId = ++this.AllTreeViewModel.MaxNodeId;
+				idMapping[treeNodeData.Id] = newId;
+				treeNodeData.Id = newId;
+				treeNodeData.TreeId = treeId;
+			}
+
+			foreach (TreeNodeData treeNodeData in treeNodeDatas)
+			{
+				treeNodeData.Parent = idMapping[treeNodeData.Parent];
+				for (int i = 0; i < treeNodeData.Children.Count; ++i)
+				{
+					treeNodeData.Children[i] = idMapping[treeNodeData.Children[i]];
+				}
+			}
+
+			TreeViewModel clone = new TreeViewModel(this.AllTreeViewModel, treeNodeDatas);
+			return clone;
+		}
 	}
 }