ソースを参照

增加复制子树功能

tanghai 11 年 前
コミット
52025150b1

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

@@ -19,8 +19,8 @@
 			<ColumnDefinition Width="Auto"/>
 			<ColumnDefinition />
 		</Grid.ColumnDefinitions>
-		<GroupBox Grid.Row="0" Grid.Column="0" Header="行为树列表:">
-			<ListBox Name="lbTreeRoots" ItemsSource="{Binding RootList}" VerticalAlignment="Stretch" >
+		<GroupBox Grid.Row="0" Grid.Column="0" Header="行为树列表:" Margin="0,0,0,289" Grid.RowSpan="2">
+			<ListBox Name="lbTreeRoots" ItemsSource="{Binding RootList}" >
 				<ListBox.ContextMenu>
 					<ContextMenu>
 						<MenuItem Header="打开" Click="MenuItem_Open" />
@@ -41,7 +41,7 @@
 				</ListBox.ItemTemplate>
 			</ListBox>
 		</GroupBox>
-		<GroupBox Grid.Row="1" Grid.Column="0" Header="节点编辑:">
+		<GroupBox Grid.Row="1" Grid.Column="0" Header="节点编辑:" Margin="0,116,0,0">
 			<Tree:NodeDataEditor x:Name="nodeDataEditor" VerticalAlignment="Top" />
 		</GroupBox>
 		<GridSplitter Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" Width="3" ShowsPreview="False" VerticalAlignment="Stretch"

+ 0 - 11
CSharp/App/Modules/BehaviorTreeModule/TreeNodeViewModel.cs

@@ -62,17 +62,6 @@ namespace Modules.BehaviorTreeModule
 			}
 		}
 
-		public TreeNodeViewModel(TreeViewModel treeViewModel, TreeNodeViewModel parent, TreeNodeViewModel treeNodeViewModel)
-		{
-			this.TreeViewModel = treeViewModel;
-			this.data = new TreeNodeData();
-			this.data.Id = ++treeViewModel.AllTreeViewModel.MaxNodeId;
-			this.data.TreeId = treeViewModel.TreeId;
-			this.Parent = parent;
-			this.ConnectorX2 = treeNodeViewModel.ConnectorX2;
-			this.connectorY2 = treeNodeViewModel.ConnectorY2;
-		}
-
 		public TreeNodeData Data
 		{
 			get

+ 2 - 0
CSharp/App/Modules/BehaviorTreeModule/TreeView.xaml

@@ -44,6 +44,8 @@
 				<ContextMenu>
 					<MenuItem Header="新建" Click="MenuItem_New" />
 					<MenuItem Header="删除" Click="MenuItem_Remove" />
+					<MenuItem Header="复制" Click="MenuItem_Copy" />
+					<MenuItem Header="粘贴" Click="MenuItem_Paste" />
 					<MenuItem Header="左移" Click="MenuItem_MoveLeft" />
 					<MenuItem Header="右移" Click="MenuItem_MoveRight" />
 					<MenuItem Header="折叠/展开" Click="MenuItem_Fold" />

+ 28 - 8
CSharp/App/Modules/BehaviorTreeModule/TreeView.xaml.cs

@@ -119,8 +119,8 @@ namespace Modules.BehaviorTreeModule
 		private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e)
 		{
 			this.origMouseDownPoint = e.GetPosition(this);
-			var item = (FrameworkElement) sender;
-			var treeNodeViewModel = item.DataContext as TreeNodeViewModel;
+			FrameworkElement item = (FrameworkElement) sender;
+			TreeNodeViewModel treeNodeViewModel = item.DataContext as TreeNodeViewModel;
 
 			this.listBox.SelectedItem = treeNodeViewModel;
 			this.moveFromNode = treeNodeViewModel;
@@ -138,8 +138,8 @@ namespace Modules.BehaviorTreeModule
 			{
 				return;
 			}
-			var item = (FrameworkElement) sender;
-			var moveToNode = item.DataContext as TreeNodeViewModel;
+			FrameworkElement item = (FrameworkElement) sender;
+			TreeNodeViewModel moveToNode = item.DataContext as TreeNodeViewModel;
 			Log.Debug("move to node: {0} {1}", this.moveFromNode.Id, moveToNode.Id);
 			if (this.moveFromNode.Id == moveToNode.Id)
 			{
@@ -189,7 +189,7 @@ namespace Modules.BehaviorTreeModule
 			{
 				return;
 			}
-			var treeNodeViewModel = this.listBox.SelectedItem as TreeNodeViewModel;
+			TreeNodeViewModel treeNodeViewModel = this.listBox.SelectedItem as TreeNodeViewModel;
 			if (treeNodeViewModel.IsRoot)
 			{
 				return;
@@ -205,7 +205,7 @@ namespace Modules.BehaviorTreeModule
 			{
 				return;
 			}
-			var treeNodeViewModel = this.listBox.SelectedItem as TreeNodeViewModel;
+			TreeNodeViewModel treeNodeViewModel = this.listBox.SelectedItem as TreeNodeViewModel;
 
 			if (treeNodeViewModel.IsFold)
 			{
@@ -223,7 +223,7 @@ namespace Modules.BehaviorTreeModule
 			{
 				return;
 			}
-			var treeNodeViewModel = this.listBox.SelectedItem as TreeNodeViewModel;
+			TreeNodeViewModel treeNodeViewModel = this.listBox.SelectedItem as TreeNodeViewModel;
 			this.ViewModel.MoveLeft(treeNodeViewModel);
 		}
 
@@ -233,8 +233,28 @@ namespace Modules.BehaviorTreeModule
 			{
 				return;
 			}
-			var treeNodeViewModel = this.listBox.SelectedItem as TreeNodeViewModel;
+			TreeNodeViewModel treeNodeViewModel = this.listBox.SelectedItem as TreeNodeViewModel;
 			this.ViewModel.MoveRight(treeNodeViewModel);
 		}
+
+		private void MenuItem_Copy(object sender, RoutedEventArgs e)
+		{
+			if (this.listBox.SelectedItem == null)
+			{
+				return;
+			}
+			TreeNodeViewModel treeNodeViewModel = this.listBox.SelectedItem as TreeNodeViewModel;
+			this.ViewModel.Copy(treeNodeViewModel);
+		}
+
+		private void MenuItem_Paste(object sender, RoutedEventArgs e)
+		{
+			if (this.listBox.SelectedItem == null)
+			{
+				return;
+			}
+			TreeNodeViewModel treeNodeViewModel = this.listBox.SelectedItem as TreeNodeViewModel;
+			this.ViewModel.Paste(treeNodeViewModel);
+		}
 	}
 }

+ 52 - 1
CSharp/App/Modules/BehaviorTreeModule/TreeViewModel.cs

@@ -25,6 +25,8 @@ namespace Modules.BehaviorTreeModule
 
 		public int TreeId { get; private set; }
 
+		public int copyId;
+
 		public TreeViewModel(AllTreeViewModel allTreeViewModel)
 		{
 			this.AllTreeViewModel = allTreeViewModel;
@@ -62,7 +64,7 @@ namespace Modules.BehaviorTreeModule
 			return treeNodeDatas;
 		}
 
-		public AllTreeViewModel AllTreeViewModel { get; set; }
+		public AllTreeViewModel AllTreeViewModel { get; private set; }
 
 		public TreeNodeViewModel Root
 		{
@@ -92,6 +94,7 @@ namespace Modules.BehaviorTreeModule
 
 			if (parent != null)
 			{
+				treeNode.Parent = parent;
 				parent.Children.Add(treeNode.Id);
 			}
 
@@ -266,6 +269,54 @@ namespace Modules.BehaviorTreeModule
 			treeLayout.ExcuteLayout();
 		}
 
+		public void Copy(TreeNodeViewModel copyTreeNodeViewModel)
+		{
+			copyId = copyTreeNodeViewModel.Id;
+		}
+
+		public void Paste(TreeNodeViewModel pasteTreeNodeViewModel)
+		{
+			if (copyId == 0)
+			{
+				return;
+			}
+
+			TreeNodeViewModel copyTreeNodeViewModel = treeNodeDict[copyId];
+			// copy节点不能是paste节点的父级节点
+			TreeNodeViewModel tmpNode = pasteTreeNodeViewModel;
+			while (tmpNode != null)
+			{
+				if (tmpNode.IsRoot)
+				{
+					break;
+				}
+				if (tmpNode.Id == copyTreeNodeViewModel.Id)
+				{
+					return;
+				}
+				tmpNode = tmpNode.Parent;
+			}
+			copyId = 0;
+			CopyTree(copyTreeNodeViewModel, pasteTreeNodeViewModel);
+		}
+
+		private void CopyTree(TreeNodeViewModel copyTreeNodeViewModel, TreeNodeViewModel parent)
+		{
+			TreeNodeData newTreeNodeData = (TreeNodeData)copyTreeNodeViewModel.Data.Clone();
+			newTreeNodeData.Id = ++this.AllTreeViewModel.MaxNodeId;
+			newTreeNodeData.TreeId = this.TreeId;
+			newTreeNodeData.Children.Clear();
+			TreeNodeViewModel newTreeNodeViewModel = new TreeNodeViewModel(this, newTreeNodeData);
+
+			this.Add(newTreeNodeViewModel, parent);
+
+			foreach (int childId in copyTreeNodeViewModel.Children)
+			{
+				TreeNodeViewModel child = this.Get(childId);
+				this.CopyTree(child, newTreeNodeViewModel);
+			}
+		}
+
 		public object Clone()
 		{
 			int treeId = ++AllTreeViewModel.MaxTreeId;