Sfoglia il codice sorgente

增加行为树复制功能

tanghai 11 anni fa
parent
commit
9a439db1df

+ 6 - 9
CSharp/App/Modules/BehaviorTreeModule/AllTreeView.xaml

@@ -9,12 +9,6 @@
 		d:DesignWidth="1280" 
 		d:DataContext="{d:DesignInstance Tree:AllTreeViewModel}">
 	<DockPanel>
-	<Menu DockPanel.Dock="Top" Height="24">
-		<MenuItem Header="File">
-			<MenuItem Header="打开" Click="MenuItem_Open" />
-			<MenuItem Header="保存" Click="MenuItem_Save" />
-		</MenuItem>
-	</Menu>
 	<Grid>
 		<Grid.RowDefinitions>
 			<RowDefinition/>
@@ -29,15 +23,18 @@
 			<ListBox Name="lbTreeRoots" ItemsSource="{Binding RootList}" VerticalAlignment="Stretch" >
 				<ListBox.ContextMenu>
 					<ContextMenu>
-						<MenuItem Header="新建树" Click="MenuItem_New" />
-						<MenuItem Header="删除树" Click="MenuItem_Remove" />
+						<MenuItem Header="打开" Click="MenuItem_Open" />
+						<MenuItem Header="保存" Click="MenuItem_Save" />
+						<MenuItem Header="新建" Click="MenuItem_New" />
+						<MenuItem Header="删除" Click="MenuItem_Remove" />
+						<MenuItem Header="复制" Click="MenuItem_Copy" />
 					</ContextMenu>
 				</ListBox.ContextMenu>
 				<ListBox.ItemTemplate>
 					<DataTemplate DataType="Tree:TreeNodeViewModel">
 						<StackPanel MouseLeftButtonDown="ListBoxItem_OnMouseLeftButtonDown" MouseLeftButtonUp="ListBoxItem_OnMouseLeftButtonUp" 
 									Orientation="Horizontal" Margin="1,0">
-							<Label Content="{Binding TreeId}" VerticalAlignment="Stretch" Width="20"/>
+							<Label Content="{Binding TreeId}" VerticalAlignment="Stretch" Width="30"/>
 							<Label Content="{Binding Comment}" VerticalAlignment="Stretch" Width="150"/>
 						</StackPanel>
 					</DataTemplate>

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

@@ -49,8 +49,18 @@ namespace Modules.BehaviorTreeModule
 
 		private void MenuItem_New(object sender, RoutedEventArgs e)
 		{
-			TreeViewModel treeViewModel = new TreeViewModel(this.ViewModel);
-			this.ViewModel.Add(treeViewModel);
+			TreeViewModel treeViewModel = this.ViewModel.New();
+			this.treeView.ViewModel = treeViewModel;
+		}
+
+		private void MenuItem_Copy(object sender, RoutedEventArgs e)
+		{
+			if (this.lbTreeRoots.SelectedItem == null)
+			{
+				return;
+			}
+			TreeNodeViewModel treeNodeViewModel = this.lbTreeRoots.SelectedItem as TreeNodeViewModel;
+			TreeViewModel treeViewModel = this.ViewModel.Copy(treeNodeViewModel);
 			this.treeView.ViewModel = treeViewModel;
 		}
 
@@ -60,7 +70,7 @@ namespace Modules.BehaviorTreeModule
 			{
 				return;
 			}
-			var treeNodeViewModel = this.lbTreeRoots.SelectedItem as TreeNodeViewModel;
+			TreeNodeViewModel treeNodeViewModel = this.lbTreeRoots.SelectedItem as TreeNodeViewModel;
 			this.ViewModel.Remove(treeNodeViewModel);
 			this.lbTreeRoots.SelectedItem = null;
 			e.Handled = true;

+ 17 - 12
CSharp/App/Modules/BehaviorTreeModule/AllTreeViewModel.cs

@@ -6,8 +6,7 @@ using Common.Helper;
 
 namespace Modules.BehaviorTreeModule
 {
-	[Export(contractType: typeof (AllTreeViewModel)),
-	 PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
+	[Export(typeof (AllTreeViewModel)), PartCreationPolicy(CreationPolicy.NonShared)]
 	public class AllTreeViewModel
 	{
 		public int MaxNodeId { get; set; }
@@ -35,7 +34,7 @@ namespace Modules.BehaviorTreeModule
 			var treeDict = new Dictionary<int, List<TreeNodeData>>();
 
 			byte[] bytes = File.ReadAllBytes(file);
-			var allTreeData = ProtobufHelper.FromBytes<AllTreeData>(bytes);
+			AllTreeData allTreeData = ProtobufHelper.FromBytes<AllTreeData>(bytes);
 
 			this.MaxNodeId = 0;
 			this.MaxTreeId = 0;
@@ -61,7 +60,7 @@ namespace Modules.BehaviorTreeModule
 
 			foreach (KeyValuePair<int, List<TreeNodeData>> pair in treeDict)
 			{
-				var treeViewModel = new TreeViewModel(pair.Value) { AllTreeViewModel = this, TreeId = pair.Key };
+				TreeViewModel treeViewModel = new TreeViewModel(this, pair.Value);
 				this.treeViewModelsDict[pair.Key] = treeViewModel;
 				this.RootList.Add(treeViewModel.Root);
 			}
@@ -70,7 +69,7 @@ namespace Modules.BehaviorTreeModule
 		public void Save(string file)
 		{
 			AllTreeData allTreeData = new AllTreeData();
-			foreach (var value in this.treeViewModelsDict.Values)
+			foreach (TreeViewModel value in this.treeViewModelsDict.Values)
 			{
 				List<TreeNodeData> list = value.GetDatas();
 				allTreeData.TreeNodeDatas.AddRange(list);
@@ -83,20 +82,26 @@ namespace Modules.BehaviorTreeModule
 			}
 		}
 
-		public void New(TreeViewModel treeViewModel)
+		public TreeViewModel New()
 		{
+			TreeViewModel treeViewModel = new TreeViewModel(this);
+			this.treeViewModelsDict[treeViewModel.TreeId] = treeViewModel;
+			this.rootList.Add(treeViewModel.Root);
+			return treeViewModel;
 		}
 
-		public void Add(TreeViewModel treeViewModel)
+		public void Remove(TreeNodeViewModel treeNodeViewModel)
 		{
-			this.treeViewModelsDict[treeViewModel.TreeId] = treeViewModel;
-			this.rootList.Add(treeViewModel.Root);
+			this.treeViewModelsDict.Remove(treeNodeViewModel.TreeId);
+			this.rootList.Remove(treeNodeViewModel);
 		}
 
-		public void Remove(TreeNodeViewModel treeViewModel)
+		public TreeViewModel Copy(TreeNodeViewModel treeNodeViewModel)
 		{
-			this.treeViewModelsDict.Remove(treeViewModel.TreeId);
-			this.rootList.Remove(treeViewModel);
+			TreeViewModel treeViewModel = new TreeViewModel(this, treeNodeViewModel.TreeViewModel);
+			this.treeViewModelsDict[treeViewModel.TreeId] = treeViewModel;
+			this.rootList.Add(treeViewModel.Root);
+			return treeViewModel;
 		}
 
 		public TreeViewModel Get(int treeId)

+ 1 - 0
CSharp/App/Modules/BehaviorTreeModule/NodeType.cs

@@ -54,5 +54,6 @@
 		// 跟随unit
 		FollowUnit = 20012,
 		LookAtTarget = 20013,
+		CastNumSpell = 20014,
 	}
 }

+ 9 - 2
CSharp/App/Modules/BehaviorTreeModule/TreeNodeData.cs

@@ -1,10 +1,12 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Runtime.Serialization;
+using Common.Helper;
 
 namespace Modules.BehaviorTreeModule
 {
 	[DataContract]
-	public class TreeNodeData
+	public class TreeNodeData: ICloneable
 	{
 		private readonly List<int> children = new List<int>();
 
@@ -55,5 +57,10 @@ namespace Modules.BehaviorTreeModule
 		/// </summary>
 		[DataMember(Order = 7)]
 		public string Comment { get; set; }
+
+		public object Clone()
+		{
+			return ProtobufHelper.FromBytes<TreeNodeData>(ProtobufHelper.ToBytes(this));
+		}
 	}
 }

+ 21 - 10
CSharp/App/Modules/BehaviorTreeModule/TreeNodeViewModel.cs

@@ -8,7 +8,7 @@ namespace Modules.BehaviorTreeModule
 	{
 		private static double width = 80;
 		private static double height = 50;
-		private readonly TreeViewModel treeViewModel;
+		public TreeViewModel TreeViewModel { get; private set; }
 		private readonly TreeNodeData data;
 		private double x;
 		private double y;
@@ -21,7 +21,7 @@ namespace Modules.BehaviorTreeModule
 
 		public TreeNodeViewModel(TreeViewModel treeViewModel, double x, double y)
 		{
-			this.treeViewModel = treeViewModel;
+			this.TreeViewModel = treeViewModel;
 			this.x = x;
 			this.y = y;
 			this.data = new TreeNodeData();
@@ -34,7 +34,7 @@ namespace Modules.BehaviorTreeModule
 
 		public TreeNodeViewModel(TreeViewModel treeViewModel, TreeNodeViewModel parent)
 		{
-			this.treeViewModel = treeViewModel;
+			this.TreeViewModel = treeViewModel;
 			this.data = new TreeNodeData();
 			this.data.Id = ++treeViewModel.AllTreeViewModel.MaxNodeId;
 			this.data.TreeId = treeViewModel.TreeId;
@@ -46,7 +46,7 @@ namespace Modules.BehaviorTreeModule
 
 		public TreeNodeViewModel(TreeViewModel treeViewModel, TreeNodeData data)
 		{
-			this.treeViewModel = treeViewModel;
+			this.TreeViewModel = treeViewModel;
 			this.data = data;
 			if (this.IsRoot)
 			{
@@ -62,6 +62,17 @@ 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
@@ -182,7 +193,7 @@ namespace Modules.BehaviorTreeModule
 
 				foreach (var childId in this.Children)
 				{
-					TreeNodeViewModel child = this.treeViewModel.Get(childId);
+					TreeNodeViewModel child = this.TreeViewModel.Get(childId);
 					child.ConnectorX2 = Width / 2 + this.X - child.X;
 				}
 			}
@@ -211,7 +222,7 @@ namespace Modules.BehaviorTreeModule
 
 				foreach (var childId in this.Children)
 				{
-					TreeNodeViewModel child = this.treeViewModel.Get(childId);
+					TreeNodeViewModel child = this.TreeViewModel.Get(childId);
 					child.ConnectorY2 = Height + this.Y - child.Y;
 				}
 			}
@@ -307,7 +318,7 @@ namespace Modules.BehaviorTreeModule
 				{
 					return null;
 				}
-				TreeNodeViewModel parent = this.treeViewModel.Get(this.data.Parent);
+				TreeNodeViewModel parent = this.TreeViewModel.Get(this.data.Parent);
 				return parent;
 			}
 			set
@@ -362,7 +373,7 @@ namespace Modules.BehaviorTreeModule
 				}
 
 				int index = this.Parent.Children.IndexOf(this.Id);
-				return index == 0? null : this.treeViewModel.Get(this.Parent.Children[index - 1]);
+				return index == 0? null : this.TreeViewModel.Get(this.Parent.Children[index - 1]);
 			}
 		}
 
@@ -376,7 +387,7 @@ namespace Modules.BehaviorTreeModule
 				}
 
 				int maxIndex = this.Children.Count - 1;
-				return this.treeViewModel.Get(this.Children[maxIndex]);
+				return this.TreeViewModel.Get(this.Children[maxIndex]);
 			}
 		}
 
@@ -384,7 +395,7 @@ namespace Modules.BehaviorTreeModule
 		{
 			get
 			{
-				return this.Children.Count == 0? null : this.treeViewModel.Get(this.Children[0]);
+				return this.Children.Count == 0? null : this.TreeViewModel.Get(this.Children[0]);
 			}
 		}
 

+ 3 - 3
CSharp/App/Modules/BehaviorTreeModule/TreeView.xaml.cs

@@ -161,7 +161,7 @@ namespace Modules.BehaviorTreeModule
 			// one root node
 			if (this.ViewModel.TreeNodes.Count == 0)
 			{
-				var addTreeNode = new TreeNodeViewModel(this.ViewModel, point.X, point.Y)
+				TreeNodeViewModel addTreeNode = new TreeNodeViewModel(this.ViewModel, point.X, point.Y)
 				{
 					Type = (int) NodeType.Selector
 				};
@@ -171,8 +171,8 @@ namespace Modules.BehaviorTreeModule
 			{
 				if (this.listBox.SelectedItem != null)
 				{
-					var parentNode = this.listBox.SelectedItem as TreeNodeViewModel;
-					var addTreeNode = new TreeNodeViewModel(this.ViewModel, parentNode)
+					TreeNodeViewModel parentNode = this.listBox.SelectedItem as TreeNodeViewModel;
+					TreeNodeViewModel addTreeNode = new TreeNodeViewModel(this.ViewModel, parentNode)
 					{
 						Type = (int) NodeType.Selector,
 					};

+ 57 - 19
CSharp/App/Modules/BehaviorTreeModule/TreeViewModel.cs

@@ -22,7 +22,7 @@ namespace Modules.BehaviorTreeModule
 			}
 		}
 
-		public int TreeId { get; set; }
+		public int TreeId { get; private set; }
 
 		public TreeViewModel(AllTreeViewModel allTreeViewModel)
 		{
@@ -32,28 +32,66 @@ namespace Modules.BehaviorTreeModule
 			this.treeNodes.Add(treeNodeViewModel);
 			this.treeNodeDict[treeNodeViewModel.Id] = treeNodeViewModel;
 
-			var treeLayout = new TreeLayout(this);
+			TreeLayout treeLayout = new TreeLayout(this);
 			treeLayout.ExcuteLayout();
 		}
 
-		public TreeViewModel(List<TreeNodeData> treeNodeDatas)
+		public TreeViewModel(AllTreeViewModel allTreeViewModel, List<TreeNodeData> treeNodeDatas)
 		{
+			this.AllTreeViewModel = allTreeViewModel;
+			this.TreeId = treeNodeDatas[0].TreeId;
 			foreach (TreeNodeData treeNodeData in treeNodeDatas)
 			{
 				TreeNodeViewModel treeNodeViewModel = new TreeNodeViewModel(this, treeNodeData);
 				this.treeNodes.Add(treeNodeViewModel);
 				this.treeNodeDict[treeNodeViewModel.Id] = treeNodeViewModel;
 			}
-			var treeLayout = new TreeLayout(this);
+			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()
 		{
-			List<TreeNodeData> treeNodeDatas = new List<TreeNodeData>();
+			var treeNodeDatas = new List<TreeNodeData>();
 			foreach (TreeNodeViewModel treeNodeViewModel in this.treeNodes)
 			{
-				treeNodeDatas.Add(treeNodeViewModel.Data);
+				TreeNodeData treeNodeData = (TreeNodeData)treeNodeViewModel.Data.Clone();
+				treeNodeDatas.Add(treeNodeData);
 			}
 			return treeNodeDatas;
 		}
@@ -91,7 +129,7 @@ namespace Modules.BehaviorTreeModule
 				parent.Children.Add(treeNode.Id);
 			}
 
-			var treeLayout = new TreeLayout(this);
+			TreeLayout treeLayout = new TreeLayout(this);
 			treeLayout.ExcuteLayout();
 		}
 
@@ -113,7 +151,7 @@ namespace Modules.BehaviorTreeModule
 
 		public void Remove(TreeNodeViewModel treeNodeViewModel)
 		{
-			List<int> allId = new List<int>();
+			var allId = new List<int>();
 			this.GetChildrenIdAndSelf(treeNodeViewModel, allId);
 
 			foreach (int childId in allId)
@@ -129,7 +167,7 @@ namespace Modules.BehaviorTreeModule
 				parent.Children.Remove(treeNodeViewModel.Id);
 			}
 
-			var treeLayout = new TreeLayout(this);
+			TreeLayout treeLayout = new TreeLayout(this);
 			treeLayout.ExcuteLayout();
 		}
 
@@ -137,7 +175,7 @@ namespace Modules.BehaviorTreeModule
 		{
 			treeNodeViewModel.X += offsetX;
 			treeNodeViewModel.Y += offsetY;
-			foreach (var childId in treeNodeViewModel.Children)
+			foreach (int childId in treeNodeViewModel.Children)
 			{
 				TreeNodeViewModel child = this.Get(childId);
 				this.RecursionMove(child, offsetX, offsetY);
@@ -178,7 +216,7 @@ namespace Modules.BehaviorTreeModule
 			from.Parent.Children.Remove(from.Id);
 			to.Children.Add(from.Id);
 			from.Parent = to;
-			var treeLayout = new TreeLayout(this);
+			TreeLayout treeLayout = new TreeLayout(this);
 			treeLayout.ExcuteLayout();
 		}
 
@@ -188,7 +226,7 @@ namespace Modules.BehaviorTreeModule
 		/// <param name="treeNodeViewModel"></param>
 		public void Fold(TreeNodeViewModel treeNodeViewModel)
 		{
-			List<int> allChildId = new List<int>();
+			var allChildId = new List<int>();
 			this.GetAllChildrenId(treeNodeViewModel, allChildId);
 
 			foreach (int childId in allChildId)
@@ -199,7 +237,7 @@ namespace Modules.BehaviorTreeModule
 
 			treeNodeViewModel.IsFold = true;
 
-			var treeLayout = new TreeLayout(this);
+			TreeLayout treeLayout = new TreeLayout(this);
 			treeLayout.ExcuteLayout();
 		}
 
@@ -211,7 +249,7 @@ namespace Modules.BehaviorTreeModule
 		{
 			treeNodeViewModel.IsFold = false;
 
-			List<int> allChildId = new List<int>();
+			var allChildId = new List<int>();
 			this.GetAllChildrenId(treeNodeViewModel, allChildId);
 
 			foreach (int childId in allChildId)
@@ -220,7 +258,7 @@ namespace Modules.BehaviorTreeModule
 				this.treeNodes.Add(child);
 			}
 
-			var treeLayout = new TreeLayout(this);
+			TreeLayout treeLayout = new TreeLayout(this);
 			treeLayout.ExcuteLayout();
 		}
 
@@ -230,7 +268,7 @@ namespace Modules.BehaviorTreeModule
 			{
 				return;
 			}
-			var parent = treeNodeViewModel.Parent;
+			TreeNodeViewModel parent = treeNodeViewModel.Parent;
 			int index = parent.Children.IndexOf(treeNodeViewModel.Id);
 			if (index == 0)
 			{
@@ -239,7 +277,7 @@ namespace Modules.BehaviorTreeModule
 			parent.Children.Remove(treeNodeViewModel.Id);
 			parent.Children.Insert(index - 1, treeNodeViewModel.Id);
 
-			var treeLayout = new TreeLayout(this);
+			TreeLayout treeLayout = new TreeLayout(this);
 			treeLayout.ExcuteLayout();
 		}
 
@@ -249,7 +287,7 @@ namespace Modules.BehaviorTreeModule
 			{
 				return;
 			}
-			var parent = treeNodeViewModel.Parent;
+			TreeNodeViewModel parent = treeNodeViewModel.Parent;
 			int index = parent.Children.IndexOf(treeNodeViewModel.Id);
 			if (index == parent.Children.Count - 1)
 			{
@@ -258,7 +296,7 @@ namespace Modules.BehaviorTreeModule
 			parent.Children.Remove(treeNodeViewModel.Id);
 			parent.Children.Insert(index + 1, treeNodeViewModel.Id);
 
-			var treeLayout = new TreeLayout(this);
+			TreeLayout treeLayout = new TreeLayout(this);
 			treeLayout.ExcuteLayout();
 		}
 	}