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

1.右键新建节点
2.右击节点建立子节点
3.移动节点
4.多选节点
5.多选移动节点

tanghai 14 жил өмнө
parent
commit
43c9e022c6

+ 15 - 12
CSharp/Modules/BehaviorTree/BehaviorTreeView.xaml

@@ -9,35 +9,38 @@
 		x:Class="BehaviorTree.BehaviorTreeView" 
 		mc:Ignorable="d" 
 		d:DesignHeight="600" d:DesignWidth="800">
-
 	<UserControl.Resources>
+		
 	</UserControl.Resources>
+	
 	<UserControl.CommandBindings>
-		<CommandBinding Command="ApplicationCommands.New" Executed="NewNode_Executed" />
+		<CommandBinding Command="ApplicationCommands.New" Executed="MenuNewNode_Executed" />
 	</UserControl.CommandBindings>
 
 	<UserControl.ContextMenu>
-		<ContextMenu Name="cm">
+		<ContextMenu>
 			<MenuItem Header="New Node" Command="ApplicationCommands.New" 
 					CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
 		</ContextMenu>
 	</UserControl.ContextMenu>
 	<Grid>
-		<ListBox Name="lstTree" ItemsSource="{Binding TreeNodes}" >
+		<ListBox Name="listBox" SelectionMode="Extended" ItemsSource="{Binding TreeNodes}" >
+			<ListBox.Resources>
+				<Style TargetType="{x:Type ListBoxItem}">
+					<Setter Property="Canvas.Left" Value="{Binding Mode=TwoWay, Path=X}"/>
+					<Setter Property="Canvas.Top" Value="{Binding Mode=TwoWay, Path=Y}"/>
+				</Style>
+			</ListBox.Resources>
 			<ListBox.ItemsPanel>
 				<ItemsPanelTemplate>
-					<Canvas Background="Green"/>
+					<Canvas Background="Transparent" AllowDrop="True" />
 				</ItemsPanelTemplate>
 			</ListBox.ItemsPanel>
-			<ListBox.ItemContainerStyle>
-				<Style>
-					<Setter Property="Canvas.Left" Value="{Binding Mode=TwoWay, Path=X}"/>
-					<Setter Property="Canvas.Top" Value="{Binding Mode=TwoWay, Path=Y}"/>
-				</Style>
-			</ListBox.ItemContainerStyle>
 			<ListBox.ItemTemplate>
 				<DataTemplate>
-					<TextBlock FontWeight="Bold" TextAlignment="Center" Text="Node" />
+					<Rectangle Width="50" Height="40" Cursor="Hand" Fill="Green"
+						MouseDown="ListBoxItem_MouseDown" MouseUp="ListBoxItem_MouseUp" 
+						MouseMove="ListBoxItem_MouseMove" />
 				</DataTemplate>
 			</ListBox.ItemTemplate>
 		</ListBox>

+ 129 - 14
CSharp/Modules/BehaviorTree/BehaviorTreeView.xaml.cs

@@ -1,28 +1,26 @@
 using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
+using System.ComponentModel.Composition;
 using System.Windows;
 using System.Windows.Controls;
-using System.Windows.Data;
-using System.Windows.Documents;
 using System.Windows.Input;
-using System.Windows.Media;
-using System.Windows.Media.Imaging;
-using System.Windows.Navigation;
-using System.Windows.Shapes;
-using System.ComponentModel.Composition;
 using Infrastructure;
 
 namespace BehaviorTree
 {
 	/// <summary>
-	/// TreeCanvasView.xaml 的交互逻辑
+	/// BehaviorTreeView.xaml 的交互逻辑
 	/// </summary>
 	[ViewExport(RegionName = "TreeCanvasRegion")]
 	[PartCreationPolicy(CreationPolicy.NonShared)]
 	public partial class BehaviorTreeView : UserControl
 	{
+		private static readonly double DragThreshold = 5;
+
+		private bool isDragging = false;
+		private bool isControlDown = false;
+		private bool isLeftButtonDown = false;
+		private Point origMouseDownPoint;
+
 		public BehaviorTreeView()
 		{
 			InitializeComponent();
@@ -41,11 +39,128 @@ namespace BehaviorTree
 			}
 		}
 
-		private void NewNode_Executed(object sender, ExecutedRoutedEventArgs e)
+		private void MenuNewNode_Executed(object sender, ExecutedRoutedEventArgs e)
 		{
-			Point point = Mouse.GetPosition(lstTree);
+			Point point = Mouse.GetPosition(listBox);
 			var treeNode = new TreeNode(point.X, point.Y);
-			this.ViewModel.Add(treeNode, null);
+
+			// one root node
+			if (this.ViewModel.TreeNodes.Count == 0)
+			{
+				this.ViewModel.Add(treeNode, null);
+			}
+			else
+			{
+				if (listBox.SelectedItem != null)
+				{
+					var treeNodeViewModel = listBox.SelectedItem as TreeNodeViewModel;
+					this.ViewModel.Add(treeNode, treeNodeViewModel);
+				}
+			}
+			listBox.SelectedItem = null;
+			e.Handled = true;
+		}
+
+		private void ListBoxItem_MouseDown(object sender, MouseButtonEventArgs e)
+		{
+			if (e.ChangedButton != MouseButton.Left)
+			{
+				return;
+			}
+			var item = (FrameworkElement)sender;
+			var treeNodeViewModel = item.DataContext as TreeNodeViewModel;
+
+			isLeftButtonDown = true;
+
+			if ((Keyboard.Modifiers & ModifierKeys.Control) != 0)
+			{
+				isControlDown = true;
+			}
+			else
+			{
+				isControlDown = false;
+
+				if (!listBox.SelectedItems.Contains(treeNodeViewModel))
+				{
+					listBox.SelectedItems.Clear();
+					listBox.SelectedItems.Add(treeNodeViewModel);
+				}
+			}
+
+			item.CaptureMouse();
+			origMouseDownPoint = e.GetPosition(this);
+			e.Handled = true;
+		}
+
+		private void ListBoxItem_MouseUp(object sender, MouseButtonEventArgs e)
+		{
+			if (!isLeftButtonDown)
+			{
+				isDragging = false;
+				return;
+			}
+
+			var item = (FrameworkElement)sender;
+			var treeNodeViewModel = item.DataContext as TreeNodeViewModel;
+
+			if (!isDragging)
+			{
+				if (isLeftButtonDown && isControlDown)
+				{
+					if (listBox.SelectedItems.Contains(treeNodeViewModel))
+					{
+						listBox.SelectedItems.Remove(treeNodeViewModel);
+					}
+					else
+					{
+						listBox.SelectedItems.Add(treeNodeViewModel);
+					}
+				}
+				else
+				{
+					if (listBox.SelectedItems.Count != 1 || listBox.SelectedItem != treeNodeViewModel)
+					{
+						listBox.SelectedItems.Clear();
+						listBox.SelectedItem = treeNodeViewModel;
+						listBox.SelectedItems.Add(treeNodeViewModel);
+					}
+				}
+			}
+
+			isLeftButtonDown = false;
+			isControlDown = false;
+			isDragging = false;
+
+			item.ReleaseMouseCapture();
+			e.Handled = true;
+		}
+
+		private void ListBoxItem_MouseMove(object sender, MouseEventArgs e)
+		{
+			if (isDragging)
+			{
+				Point curMouseDownPoint = e.GetPosition(this);
+				var dragDelta = curMouseDownPoint - origMouseDownPoint;
+
+				origMouseDownPoint = curMouseDownPoint;
+
+				foreach (TreeNodeViewModel item in listBox.SelectedItems)
+				{
+					item.X += dragDelta.X;
+					item.Y += dragDelta.Y;
+				}
+			}
+			else if (isLeftButtonDown)
+			{
+				Point curMouseDownPoint = e.GetPosition(this);
+				var dragDelta = curMouseDownPoint - origMouseDownPoint;
+				double dragDistance = Math.Abs(dragDelta.Length);
+				if (dragDistance > DragThreshold)
+				{
+					isDragging = true;
+				}
+				e.Handled = true;
+			}
 		}
 	}
 }

+ 3 - 9
CSharp/Modules/BehaviorTree/BehaviorTreeViewModel.cs

@@ -1,12 +1,6 @@
-using System.ComponentModel.Composition;
-using Microsoft.Practices.Prism.ViewModel;
-using NLog;
-using System.Windows.Input;
-using Microsoft.Practices.Prism.Commands;
-using System.Collections.ObjectModel;
-using System.Windows;
-using Google.ProtocolBuffers;
-
+using System.Collections.ObjectModel;
+using System.ComponentModel.Composition;
+
 namespace BehaviorTree
 {
 	[Export(typeof(BehaviorTreeViewModel))]

+ 1 - 5
CSharp/Modules/BehaviorTree/TreeNode.cs

@@ -1,8 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
+
 namespace BehaviorTree
 {
 	public class TreeNode

+ 1 - 5
CSharp/Modules/BehaviorTree/TreeNodeViewModel.cs

@@ -1,9 +1,5 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
+using System.Collections.ObjectModel;
 using Microsoft.Practices.Prism.ViewModel;
-using System.Collections.ObjectModel;
 
 namespace BehaviorTree
 {