Sfoglia il codice sorgente

修复几个节点展开bug

tanghai 11 anni fa
parent
commit
43aea92f41

+ 2 - 0
CSharp/App/Modules/Tree/BehaviorTreeView.xaml

@@ -49,6 +49,8 @@
 							PreviewMouseLeftButtonUp="ListBoxItem_PreviewMouseLeftButtonUp">
 						<Rectangle Name="rectNode" Width="{Binding Width}" Height="{Binding Height}" Cursor="Hand" StrokeThickness="2"
 								RadiusX="40" RadiusY="40" Stroke="{StaticResource treeNodeBorderBrush}" Fill="{StaticResource treeNodeFillBrush}"/>
+						<Label Content="{Binding ParentNum}" Canvas.Left="20" Canvas.Top="5"></Label>
+						<Label Content="{Binding Num}" Canvas.Left="20" Canvas.Top="15"></Label>
 						<Line X1="{Binding ConnectorX1}" Y1="{Binding ConnectorY1}" X2="{Binding ConnectorX2}" Y2="{Binding ConnectorY2}"
 								Stroke="Black" StrokeThickness="2"  />
 					</Canvas>

+ 5 - 2
CSharp/App/Modules/Tree/BehaviorTreeView.xaml.cs

@@ -2,8 +2,9 @@
 using System.ComponentModel.Composition;
 using System.Windows;
 using System.Windows.Input;
-using Infrastructure;
-
+using Infrastructure;
+using Logger;
+
 namespace Tree
 {
 	/// <summary>
@@ -183,11 +184,13 @@ namespace Tree
 			}
 			var item = (FrameworkElement)sender;
 			var moveToNode = item.DataContext as TreeNodeViewModel;
+			Log.Debug("move to node: {0} {1}", moveFromNode.Num, moveToNode.Num);
 			if (this.moveFromNode.Num == moveToNode.Num)
 			{
 				return;
 			}
 			this.ViewModel.MoveToNode(this.moveFromNode, moveToNode);
+			this.moveFromNode = null;
 		}
 	}
 }

+ 20 - 13
CSharp/App/Modules/Tree/BehaviorTreeViewModel.cs

@@ -11,10 +11,6 @@ namespace Tree
 		private readonly ObservableCollection<TreeNodeViewModel> treeNodes =
 			new ObservableCollection<TreeNodeViewModel>();
 
-		// 保存折叠节点的孩子节点
-		private readonly Dictionary<TreeNodeViewModel, ObservableCollection<TreeNodeViewModel>>
-			folderNodeChildren = new Dictionary<TreeNodeViewModel, ObservableCollection<TreeNodeViewModel>>();
-
 		public ObservableCollection<TreeNodeViewModel> TreeNodes
 		{
 			get
@@ -48,8 +44,8 @@ namespace Tree
 		}
 
 		private void RecursionRemove(TreeNodeViewModel treeNodeViewModel)
-		{
-			for (int i = treeNodeViewModel.Children.Count - 1; i >= 0; --i)
+		{
+			for (int i = 0; i < treeNodeViewModel.Children.Count; ++i)
 			{
 				this.RecursionRemove(treeNodeViewModel.Children[i]);
 			}
@@ -85,6 +81,11 @@ namespace Tree
 				this.UnFold(from);
 			}
 
+			if (to.IsFolder)
+			{
+				this.UnFold(to);
+			}
+
 			// from节点不能是to节点的父级节点
 			TreeNodeViewModel tmpNode = to;
 			while (tmpNode != null)
@@ -111,7 +112,6 @@ namespace Tree
 		/// <param name="treeNodeViewModel"></param>
 		public void Fold(TreeNodeViewModel treeNodeViewModel)
 		{
-			this.folderNodeChildren[treeNodeViewModel] = treeNodeViewModel.Children;
 			foreach (var node in treeNodeViewModel.Children)
 			{
 				this.RecursionRemove(node);
@@ -123,22 +123,29 @@ namespace Tree
 		/// <summary>
 		/// 展开节点
 		/// </summary>
-		/// <param name="treeNodeViewModel"></param>
-		public void UnFold(TreeNodeViewModel treeNodeViewModel)
+		/// <param name="unFoldNode"></param>
+		public void UnFold(TreeNodeViewModel unFoldNode)
 		{
-			ObservableCollection<TreeNodeViewModel> children = this.folderNodeChildren[treeNodeViewModel];
-			foreach (var tn in children)
+			foreach (var tn in unFoldNode.Children)
 			{
 				this.RecursionAdd(tn);
 			}
-			treeNodeViewModel.IsFolder = false;
+			unFoldNode.IsFolder = false;
 			BehaviorTreeLayout.ExcuteLayout(this.Root);
 		}
 
 		private void RecursionAdd(TreeNodeViewModel treeNodeViewModel)
 		{
-			this.treeNodes.Add(treeNodeViewModel);
+			if (!this.treeNodes.Contains(treeNodeViewModel))
+			{
+				this.treeNodes.Add(treeNodeViewModel);
+			}
 			ObservableCollection<TreeNodeViewModel> children = treeNodeViewModel.Children;
+
+			if (treeNodeViewModel.IsFolder)
+			{
+				return;
+			}
 			foreach (var tn in children)
 			{
 				this.RecursionAdd(tn);

+ 14 - 1
CSharp/App/Modules/Tree/TreeNodeViewModel.cs

@@ -44,6 +44,18 @@ namespace Tree
 			{
 				return this.num;
 			}
+		}
+
+		public int ParentNum
+		{
+			get
+			{
+				if (this.parent == null)
+				{
+					return -1;
+				}
+				return this.parent.Num;
+			}
 		}
 
 		public static double Width
@@ -217,7 +229,8 @@ namespace Tree
 			}
 			set
 			{
-				this.parent = value;
+				this.parent = value;
+				this.OnPropertyChanged("ParentNum");
 			}
 		}