Эх сурвалжийг харах

增加新功能:子树可以拖动到另一个节点下作为子节点
暂时折叠功能出现问题,等待修复

tanghai 11 жил өмнө
parent
commit
f1346c93ba

+ 3 - 1
CSharp/App/Modules/Tree/BehaviorTreeView.xaml

@@ -44,7 +44,9 @@
 			</ListBox.ItemsPanel>
 			<ListBox.ItemTemplate>
 				<DataTemplate DataType="BehaviorTree:TreeNodeViewModel">
-					<Canvas MouseDown="ListBoxItem_MouseDown" MouseUp="ListBoxItem_MouseUp" MouseMove="ListBoxItem_MouseMove" >
+					<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"
 								RadiusX="40" RadiusY="40" Stroke="{StaticResource treeNodeBorderBrush}" Fill="{StaticResource treeNodeFillBrush}"/>
 						<Line X1="{Binding ConnectorX1}" Y1="{Binding ConnectorY1}" X2="{Binding ConnectorX2}" Y2="{Binding ConnectorY2}"

+ 44 - 39
CSharp/App/Modules/Tree/BehaviorTreeView.xaml.cs

@@ -15,7 +15,8 @@ namespace Tree
 		private const double DragThreshold = 5;
 		private bool isDragging;
 		private bool isLeftButtonDown;
-		private Point origMouseDownPoint;
+		private Point origMouseDownPoint;
+		private TreeNodeViewModel moveFromNode;
 
 		public BehaviorTreeView()
 		{
@@ -58,18 +59,6 @@ namespace Tree
 		}
 
 		private void MenuDeleteNode_Executed(object sender, ExecutedRoutedEventArgs e)
-		{
-			if (this.listBox.SelectedItem == null)
-			{
-				return;
-			}
-			var treeNodeViewModel = this.listBox.SelectedItem as TreeNodeViewModel;
-			this.ViewModel.Remove(treeNodeViewModel);
-			this.listBox.SelectedItem = null;
-			e.Handled = true;
-		}
-
-		private void MenuDeleteNode_Close(object sender, ExecutedRoutedEventArgs e)
 		{
 			if (this.listBox.SelectedItem == null)
 			{
@@ -102,19 +91,6 @@ namespace Tree
 					this.ViewModel.Fold(treeNodeViewModel);	
 				}
 			}
-			else
-			{
-				this.isLeftButtonDown = true;
-
-				var item = (FrameworkElement) sender;
-				var treeNodeViewModel = item.DataContext as TreeNodeViewModel;
-
-				this.listBox.SelectedItem = treeNodeViewModel;
-
-				this.origMouseDownPoint = e.GetPosition(this);
-
-				item.CaptureMouse();
-			}
 			e.Handled = true;
 		}
 
@@ -149,26 +125,29 @@ namespace Tree
 			{
 				return;
 			}
-			if (!treeNodeViewModel.IsRoot)
-			{
-				return;
-			}
 
 			Point curMouseDownPoint;
-			Vector dragDelta;
-			if (this.isDragging)
-			{
+			Vector dragDelta;
+			// 拖动根节点,移动整个树
+			if (this.isDragging && treeNodeViewModel.IsRoot)
+			{
+				if (this.moveFromNode == null || !this.moveFromNode.IsRoot)
+				{
+					return;
+				}
 				curMouseDownPoint = e.GetPosition(this);
 				dragDelta = curMouseDownPoint - this.origMouseDownPoint;
 
 				this.origMouseDownPoint = curMouseDownPoint;
 
-				this.ViewModel.Move(dragDelta.X, dragDelta.Y);
+				this.ViewModel.MoveToPosition(dragDelta.X, dragDelta.Y);
 				return;
-			}
-
-			if (!this.isLeftButtonDown)
-			{
+			}
+
+			if (e.LeftButton != MouseButtonState.Pressed)
+			{
+				this.isDragging = false;
+				this.moveFromNode = null;
 				return;
 			}
 
@@ -180,6 +159,32 @@ namespace Tree
 				this.isDragging = true;
 			}
 			e.Handled = true;
-		}
+		}
+
+		private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e)
+		{
+			origMouseDownPoint = e.GetPosition(this);
+			var item = (FrameworkElement)sender;
+			var treeNodeViewModel = item.DataContext as TreeNodeViewModel;
+
+			this.listBox.SelectedItem = treeNodeViewModel;
+			this.moveFromNode = treeNodeViewModel;
+		}
+
+		private void ListBoxItem_PreviewMouseLeftButtonUp(object sender, MouseEventArgs e)
+		{
+			if (this.moveFromNode == null)
+			{
+				return;
+			}
+			if (this.moveFromNode.IsRoot)
+			{
+				return;
+			}
+			var item = (FrameworkElement)sender;
+			var moveToNode = item.DataContext as TreeNodeViewModel;
+
+			this.ViewModel.MoveToNode(this.moveFromNode, moveToNode);
+		}
 	}
 }

+ 9 - 1
CSharp/App/Modules/Tree/BehaviorTreeViewModel.cs

@@ -73,11 +73,19 @@ namespace Tree
 			}
 		}
 
-		public void Move(double offsetX, double offsetY)
+		public void MoveToPosition(double offsetX, double offsetY)
 		{
 			this.RecursionMove(this.Root, offsetX, offsetY);
 		}
 
+		public void MoveToNode(TreeNodeViewModel from, TreeNodeViewModel to)
+		{
+			from.Parent.Children.Remove(from);
+			to.Children.Add(from);
+			from.Parent = to;
+			BehaviorTreeLayout.ExcuteLayout(this.Root);
+		}
+
 		/// <summary>
 		/// 折叠节点
 		/// </summary>