Explorar o código

清理代码,整理格式

tanghai %!s(int64=14) %!d(string=hai) anos
pai
achega
16efa31d14

+ 1 - 0
.gitignore

@@ -16,4 +16,5 @@ bin/
 obj/
 Properties/
 Packages/
+_ReSharper.CSharp/
 

+ 4 - 7
CSharp/Editor/App.xaml

@@ -1,7 +1,4 @@
-<Application x:Class="Editor.App"
-		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
-	<Application.Resources>
-
-	</Application.Resources>
-</Application>
+<Application x:Class="Editor.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
+	<Application.Resources></Application.Resources>
+</Application>

+ 3 - 3
CSharp/Editor/App.xaml.cs

@@ -8,13 +8,13 @@ namespace Editor
 		{
 			base.OnStartup(e);
 			RunInDebugMode();
-			this.ShutdownMode = ShutdownMode.OnMainWindowClose;
+			ShutdownMode = ShutdownMode.OnMainWindowClose;
 		}
 
 		private static void RunInDebugMode()
 		{
-			Bootstrapper bootstrapper = new Bootstrapper();
+			var bootstrapper = new Bootstrapper();
 			bootstrapper.Run();
 		}
 	}
-}
+}

+ 11 - 16
CSharp/Editor/Bootstrapper.cs

@@ -1,43 +1,38 @@
 using System.ComponentModel.Composition.Hosting;
 using System.Windows;
-using Microsoft.Practices.Prism.MefExtensions;
+using BehaviorTree;
 using Infrastructure;
+using Microsoft.Practices.Prism.MefExtensions;
 using Microsoft.Practices.Prism.Regions;
-using BehaviorTree;
 
 namespace Editor
 {
-	public partial class Bootstrapper : MefBootstrapper
+	public class Bootstrapper : MefBootstrapper
 	{
 		protected override void ConfigureAggregateCatalog()
 		{
-			this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
-			this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ViewExportAttribute).Assembly));
-			this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BehaviorTreeModule).Assembly));
-		}
-
-		protected override void ConfigureContainer()
-		{
-			base.ConfigureContainer();
+			AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof (Bootstrapper).Assembly));
+			AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof (ViewExportAttribute).Assembly));
+			AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof (BehaviorTreeModule).Assembly));
 		}
 
 		protected override void InitializeShell()
 		{
 			base.InitializeShell();
-			Application.Current.MainWindow = (Shell)this.Shell;
+			Application.Current.MainWindow = (Shell) Shell;
 			Application.Current.MainWindow.Show();
 		}
 
 		protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
 		{
-			var factory = base.ConfigureDefaultRegionBehaviors();
-			factory.AddIfMissing("AutoPopulateExportedViewsBehavior", typeof(AutoPopulateExportedViewsBehavior));
+			IRegionBehaviorFactory factory = base.ConfigureDefaultRegionBehaviors();
+			factory.AddIfMissing("AutoPopulateExportedViewsBehavior", typeof (AutoPopulateExportedViewsBehavior));
 			return factory;
 		}
 
 		protected override DependencyObject CreateShell()
 		{
-			return this.Container.GetExportedValue<Shell>();
+			return Container.GetExportedValue<Shell>();
 		}
 	}
-}
+}

+ 11 - 11
CSharp/Editor/NLog.config

@@ -1,11 +1,11 @@
-<?xml version="1.0" encoding="utf-8"?>
-<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
-		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-	<targets>
-		<target name="file" xsi:type="File" fileName="${basedir}/Log.txt"
-				layout="${longdate} ${threadid:padCharacter=0:padding=4:fixedLength=true} ${callsite:className=false:includeSourcePath=false:methodName=false:fileName=true}  ${message}"/>
-	</targets>
-	<rules>
-		<logger name="*" minlevel="Trace" writeTo="file"/>
-	</rules>
-</nlog>
+<?xml version="1.0" encoding="utf-8"?>
+
+<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+	<targets>
+		<target name="file" xsi:type="File" fileName="${basedir}/Log.txt"
+		        layout="${longdate} ${threadid:padCharacter=0:padding=4:fixedLength=true} ${callsite:className=false:includeSourcePath=false:methodName=false:fileName=true}  ${message}" />
+	</targets>
+	<rules>
+		<logger name="*" minlevel="Trace" writeTo="file" />
+	</rules>
+</nlog>

+ 1 - 0
CSharp/Editor/Packages.config

@@ -1,4 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
+
 <packages>
 	<package id="CommonServiceLocator" version="1.0" />
 	<package id="Prism" version="4.0.0.0" />

+ 4 - 6
CSharp/Editor/Shell.xaml

@@ -1,9 +1,7 @@
-<Window x:Class="Editor.Shell"
-		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-		xmlns:cal="http://www.codeplex.com/CompositeWPF"
-		xmlns:prism="http://www.codeplex.com/prism"
-		Title="Editor" Height="800" Width="1280" WindowStartupLocation="CenterScreen">
+<Window x:Class="Editor.Shell" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cal="http://www.codeplex.com/CompositeWPF"
+        xmlns:prism="http://www.codeplex.com/prism" Title="Editor" Height="800" Width="1280"
+        WindowStartupLocation="CenterScreen">
 	<Grid>
 		<ContentControl Name="treeCanvas" prism:RegionManager.RegionName="TreeCanvasRegion" />
 	</Grid>

+ 4 - 5
CSharp/Editor/Shell.xaml.cs

@@ -1,6 +1,5 @@
-using System.Windows;
 using System.ComponentModel.Composition;
-using System.Diagnostics.CodeAnalysis;
+using System.Windows;
 
 namespace Editor
 {
@@ -13,12 +12,12 @@ namespace Editor
 		}
 
 		[Import]
-		ShellViewModel ViewModel
+		private ShellViewModel ViewModel
 		{
 			set
 			{
-				this.DataContext = value;
+				DataContext = value;
 			}
 		}
 	}
-}
+}

+ 1 - 1
CSharp/Editor/ShellViewModel.cs

@@ -8,4 +8,4 @@ namespace Editor
 	{
 		// This is where any view model logic for the shell would go.
 	}
-}
+}

+ 23 - 14
CSharp/Infrastructure/AutoPopulateExportedViewsBehavior.cs

@@ -14,48 +14,57 @@
 // organization, product, domain name, email address, logo, person,
 // places, or events is intended or should be inferred.
 //===================================================================================
+
 using System;
 using System.ComponentModel.Composition;
 using Microsoft.Practices.Prism.Regions;
 
 namespace Infrastructure
 {
-	[Export(typeof(AutoPopulateExportedViewsBehavior))]
+	[Export(typeof (AutoPopulateExportedViewsBehavior))]
 	[PartCreationPolicy(CreationPolicy.NonShared)]
 	public class AutoPopulateExportedViewsBehavior : RegionBehavior, IPartImportsSatisfiedNotification
 	{
-		protected override void OnAttach()
+		[ImportMany(AllowRecomposition = true)]
+		public Lazy<object, IViewRegionRegistration>[] RegisteredViews
 		{
-			AddRegisteredViews();
+			get;
+			set;
 		}
 
+		#region IPartImportsSatisfiedNotification Members
+
 		public void OnImportsSatisfied()
 		{
 			AddRegisteredViews();
 		}
 
+		#endregion
+
+		protected override void OnAttach()
+		{
+			AddRegisteredViews();
+		}
+
 		private void AddRegisteredViews()
 		{
-			if (this.Region == null)
+			if (Region == null)
 			{
 				return;
 			}
-			
-			foreach (var viewEntry in this.RegisteredViews)
+
+			foreach (var viewEntry in RegisteredViews)
 			{
-				if (viewEntry.Metadata.RegionName == this.Region.Name)
+				if (viewEntry.Metadata.RegionName == Region.Name)
 				{
-					var view = viewEntry.Value;
+					object view = viewEntry.Value;
 
-					if (!this.Region.Views.Contains(view))
+					if (!Region.Views.Contains(view))
 					{
-						this.Region.Add(view);
+						Region.Add(view);
 					}
 				}
 			}
 		}
-
-		[ImportMany(AllowRecomposition = true)]
-		public Lazy<object, IViewRegionRegistration>[] RegisteredViews { get; set; }
 	}
-}
+}

+ 3 - 3
CSharp/Infrastructure/IViewRegionRegistration.cs

@@ -19,9 +19,9 @@ namespace Infrastructure
 {
 	public interface IViewRegionRegistration
 	{
-		string RegionName 
-		{ 
+		string RegionName
+		{
 			get;
 		}
 	}
-}
+}

+ 1 - 0
CSharp/Infrastructure/Packages.config

@@ -1,4 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
+
 <packages>
 	<package id="CommonServiceLocator" version="1.0" />
 	<package id="Prism" version="4.0.0.0" />

+ 15 - 8
CSharp/Infrastructure/ViewExportAttribute.cs

@@ -14,6 +14,7 @@
 // organization, product, domain name, email address, logo, person,
 // places, or events is intended or should be inferred.
 //===================================================================================
+
 using System;
 using System.ComponentModel.Composition;
 
@@ -23,16 +24,22 @@ namespace Infrastructure
 	[MetadataAttribute]
 	public class ViewExportAttribute : ExportAttribute, IViewRegionRegistration
 	{
-		public ViewExportAttribute() : base(typeof(object))
-		{ }
+		public ViewExportAttribute() : base(typeof (object))
+		{
+		}
 
-		public ViewExportAttribute(string viewName) : base(viewName, typeof(object))
-		{ }
+		public ViewExportAttribute(string viewName) : base(viewName, typeof (object))
+		{
+		}
+
+		#region IViewRegionRegistration Members
 
-		public string RegionName 
+		public string RegionName
 		{
-			get; 
-			set; 
+			get;
+			set;
 		}
+
+		#endregion
 	}
-}
+}

+ 32 - 67
CSharp/Modules/BehaviorTree/BehaviorTreeLayout.cs

@@ -1,55 +1,19 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace BehaviorTree
+namespace BehaviorTree
 {
-	class BehaviorTreeLayout
+	public class BehaviorTreeLayout
 	{
-		private static double xGap = 20;
-		private static double yGap = 10;
-		private TreeNodeViewModel root = null;
-
-		public BehaviorTreeLayout(TreeNodeViewModel root)
-		{
-			this.root = root;
-		}
-
-		public static double XGap
-		{
-			get
-			{
-				return BehaviorTreeLayout.xGap;
-			}
-			set
-			{
-				BehaviorTreeLayout.xGap = value;
-			}
-		}
-
-		public static double YGap
-		{
-			get
-			{
-				return BehaviorTreeLayout.yGap;
-			}
-			set
-			{
-				BehaviorTreeLayout.yGap = value;
-			}
-		}
+		private const double XGap = 20;
+		private const double YGap = 10;
 
-		public void CountPrelim(TreeNodeViewModel treeNode)
+		private static void CountPrelim(TreeNodeViewModel treeNode)
 		{
-			
 			if (treeNode.Index == 0)
 			{
-				treeNode.Prelim = treeNode.Index * (treeNode.Width + BehaviorTreeLayout.XGap);
+				treeNode.Prelim = treeNode.Index * (TreeNodeViewModel.Width + XGap);
 				return;
 			}
 
-			double childrenCenter = 0;
+			double childrenCenter;
 			if (treeNode.Children.Count > 0)
 			{
 				int maxIndex = treeNode.Children.Count - 1;
@@ -62,7 +26,7 @@ namespace BehaviorTree
 			treeNode.Prelim = childrenCenter;
 		}
 
-		public void CountModify(TreeNodeViewModel treeNode, double prelim)
+		private static void CountModify(TreeNodeViewModel treeNode, double prelim)
 		{
 			double childrenCenter = 0;
 			if (treeNode.Children.Count > 0)
@@ -73,18 +37,18 @@ namespace BehaviorTree
 			treeNode.Modify = prelim - childrenCenter;
 		}
 
-		public void CountPrelimAndModify(TreeNodeViewModel treeNode)
+		private static void CountPrelimAndModify(TreeNodeViewModel treeNode)
 		{
 			CountPrelim(treeNode);
 			CountModify(treeNode, treeNode.Prelim);
 
-			foreach (var node in treeNode.Children)
+			foreach (TreeNodeViewModel node in treeNode.Children)
 			{
 				CountPrelimAndModify(node);
 			}
 		}
 
-		public void AjustTwoSubTreeGap(TreeNodeViewModel left, TreeNodeViewModel right)
+		private static void AjustTwoSubTreeGap(TreeNodeViewModel left, TreeNodeViewModel right)
 		{
 			if (left.IsLeaf || right.IsLeaf)
 			{
@@ -95,14 +59,8 @@ namespace BehaviorTree
 			TreeNodeViewModel tRight = right;
 			double leftTreeModify = 0;
 			double rightTreeModify = 0;
-			for (int i = 0; ; ++i)
+			for (int i = 0;; ++i)
 			{
-				if (tLeft.IsLeaf || tRight.IsLeaf)
-				{
-					right.Modify += offset;
-					return;
-				}
-
 				leftTreeModify += tLeft.Modify;
 				rightTreeModify += tRight.Modify;
 
@@ -110,38 +68,45 @@ namespace BehaviorTree
 				tRight = tRight.LeftMostChild;
 
 				double tGap = (tRight.Prelim + rightTreeModify) - (tLeft.Prelim + leftTreeModify);
-				if (tGap - BehaviorTreeLayout.XGap > offset)
+				if (tGap - XGap - TreeNodeViewModel.Width > offset)
 				{
-					offset = tGap - BehaviorTreeLayout.XGap;
+					offset = tGap - XGap - TreeNodeViewModel.Width;
+				}
+
+				if (tLeft.IsLeaf || tRight.IsLeaf)
+				{
+					break;
 				}
 			}
+			right.Modify += offset;
 		}
 
-		public void AjustTreeGap(TreeNodeViewModel treeNode)
+		private static void AjustTreeGap(TreeNodeViewModel treeNode)
 		{
 			for (int i = 0; i < treeNode.Children.Count - 1; ++i)
 			{
-				TreeNodeViewModel left = treeNode.Children[i];
-				TreeNodeViewModel right = treeNode.Children[i + 1];
+				var left = treeNode.Children[i];
+				var right = treeNode.Children[i + 1];
 				AjustTwoSubTreeGap(left, right);
 			}
 		}
 
-		public void ApplyXY(TreeNodeViewModel treeNode)
+		private static void ApplyXY(int level, TreeNodeViewModel treeNode)
 		{
-			treeNode.X = treeNode.Prelim;
-			double realModify = treeNode.Prelim - treeNode.Modify;
-			foreach (var node in treeNode.Children)
+			treeNode.X = treeNode.Prelim + treeNode.Modify;
+			treeNode.Y = level * (TreeNodeViewModel.Height + YGap);
+			++level;
+			foreach (TreeNodeViewModel node in treeNode.Children)
 			{
-
+				ApplyXY(level, node);
 			}
 		}
 
-		public void ExcuteLayout()
+		public static void ExcuteLayout(TreeNodeViewModel root)
 		{
 			CountPrelimAndModify(root);
 			AjustTreeGap(root);
-			ApplyXY(root);
+			ApplyXY(0, root);
 		}
 	}
-}
+}

+ 17 - 13
CSharp/Modules/BehaviorTree/BehaviorTreeModule.cs

@@ -1,13 +1,17 @@
-using Microsoft.Practices.Prism.MefExtensions.Modularity;
-using Microsoft.Practices.Prism.Modularity;
-
-namespace BehaviorTree
-{
-	[ModuleExport(typeof(BehaviorTreeModule))]
-	public class BehaviorTreeModule : IModule
-	{
-		public void Initialize()
-		{
-		}
-	}
-}
+using Microsoft.Practices.Prism.MefExtensions.Modularity;
+using Microsoft.Practices.Prism.Modularity;
+
+namespace BehaviorTree
+{
+	[ModuleExport(typeof (BehaviorTreeModule))]
+	public class BehaviorTreeModule : IModule
+	{
+		#region IModule Members
+
+		public void Initialize()
+		{
+		}
+
+		#endregion
+	}
+}

+ 19 - 25
CSharp/Modules/BehaviorTree/BehaviorTreeView.xaml

@@ -1,21 +1,15 @@
-<UserControl
-		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-		xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
-		xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
-		xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" 
-		xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
-		xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
-		x:Class="BehaviorTree.BehaviorTreeView" 
-		mc:Ignorable="d" 
-		d:DesignHeight="600" d:DesignWidth="800">
+<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+			 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+			 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+			 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
+			 x:Class="BehaviorTree.BehaviorTreeView"
+			 mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="800">
 	<UserControl.Resources>
 		<LinearGradientBrush x:Key="treeNodeFillBrush" StartPoint="0,0" EndPoint="0,1">
 			<GradientStop Color="White" Offset="0" />
 			<GradientStop Color="#7FC9FF" Offset="0.6" />
 		</LinearGradientBrush>
 		<SolidColorBrush x:Key="treeNodeBorderBrush" Color="Black" />
-		<SolidColorBrush x:Key="treeNodeSelectedBorderBrush" Color="Green" />
 	</UserControl.Resources>
 
 	<UserControl.CommandBindings>
@@ -25,18 +19,18 @@
 
 	<UserControl.ContextMenu>
 		<ContextMenu>
-			<MenuItem Header="New" Command="ApplicationCommands.New" 
-					CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
-			<MenuItem Header="Delete" Command="ApplicationCommands.Delete" 
-					CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
+			<MenuItem Header="New" Command="ApplicationCommands.New"
+					  CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
+			<MenuItem Header="Delete" Command="ApplicationCommands.Delete"
+					  CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
 		</ContextMenu>
 	</UserControl.ContextMenu>
 	<Grid>
-		<ListBox Name="listBox" SelectionMode="Extended" ItemsSource="{Binding TreeNodes}" >
+		<ListBox Name="listBox" SelectionMode="Extended" ItemsSource="{Binding TreeNodes}" x:FieldModifier="private">
 			<ListBox.Resources>
 				<Style TargetType="{x:Type ListBoxItem}">
-					<Setter Property="Canvas.Left" Value="{Binding X}"/>
-					<Setter Property="Canvas.Top" Value="{Binding Y}"/>
+					<Setter Property="Canvas.Left" Value="{Binding X}" />
+					<Setter Property="Canvas.Top" Value="{Binding Y}" />
 				</Style>
 			</ListBox.Resources>
 			<ListBox.ItemsPanel>
@@ -46,14 +40,14 @@
 			</ListBox.ItemsPanel>
 			<ListBox.ItemTemplate>
 				<DataTemplate>
-					<Canvas MouseDown="ListBoxItem_MouseDown" MouseUp="ListBoxItem_MouseUp" MouseMove="ListBoxItem_MouseMove" >
-						<Rectangle Name="rectNode" Width="{Binding Width}" Height="{Binding Height}" Cursor="Hand" StrokeThickness="4" RadiusX="4" RadiusY="4"
-								Stroke="{StaticResource treeNodeBorderBrush}" Fill="{StaticResource treeNodeFillBrush}" />
-						<Line X1="{Binding ConnectorX1}" Y1="{Binding ConnectorY1}" X2="{Binding ConnectorX2}" Y2="{Binding ConnectorY2}" 
-								Stroke="Black" StrokeThickness="2" />
+					<Canvas MouseDown="ListBoxItem_MouseDown" MouseUp="ListBoxItem_MouseUp" MouseMove="ListBoxItem_MouseMove">
+						<Rectangle Name="rectNode" Width="{Binding Width}" Height="{Binding Height}" Cursor="Hand" StrokeThickness="4"
+								   RadiusX="4" RadiusY="4" Stroke="{StaticResource treeNodeBorderBrush}" Fill="{StaticResource treeNodeFillBrush}" />
+						<Line X1="{Binding ConnectorX1}" Y1="{Binding ConnectorY1}" X2="{Binding ConnectorX2}" Y2="{Binding ConnectorY2}"
+							  Stroke="Black" StrokeThickness="2" />
 					</Canvas>
 					<DataTemplate.Triggers>
-						<DataTrigger  Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
+						<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
 							<Setter TargetName="rectNode" Property="Stroke" Value="Green" />
 						</DataTrigger>
 					</DataTemplate.Triggers>

+ 32 - 37
CSharp/Modules/BehaviorTree/BehaviorTreeView.xaml.cs

@@ -1,10 +1,8 @@
 using System;
 using System.ComponentModel.Composition;
 using System.Windows;
-using System.Windows.Controls;
 using System.Windows.Input;
 using Infrastructure;
-using NLog;
 
 namespace BehaviorTree
 {
@@ -13,15 +11,14 @@ namespace BehaviorTree
 	/// </summary>
 	[ViewExport(RegionName = "TreeCanvasRegion")]
 	[PartCreationPolicy(CreationPolicy.NonShared)]
-	public partial class BehaviorTreeView : UserControl
+	public partial class BehaviorTreeView
 	{
-		private static readonly double DragThreshold = 5;
+		private const double DragThreshold = 5;
 
-		private bool isDragging = false;
-		private bool isControlDown = false;
-		private bool isLeftButtonDown = false;
+		private bool isControlDown;
+		private bool isDragging;
+		private bool isLeftButtonDown;
 		private Point origMouseDownPoint;
-		private Logger logger = LogManager.GetCurrentClassLogger();
 
 		public BehaviorTreeView()
 		{
@@ -29,15 +26,15 @@ namespace BehaviorTree
 		}
 
 		[Import]
-		BehaviorTreeViewModel ViewModel
+		private BehaviorTreeViewModel ViewModel
 		{
 			get
 			{
-				return this.DataContext as BehaviorTreeViewModel;
+				return DataContext as BehaviorTreeViewModel;
 			}
 			set
 			{
-				this.DataContext = value;
+				DataContext = value;
 			}
 		}
 
@@ -47,16 +44,16 @@ namespace BehaviorTree
 			var treeNode = new TreeNode(point.X, point.Y);
 
 			// one root node
-			if (this.ViewModel.TreeNodes.Count == 0)
+			if (ViewModel.TreeNodes.Count == 0)
 			{
-				this.ViewModel.Add(treeNode, null);
+				ViewModel.Add(treeNode, null);
 			}
 			else
 			{
 				if (listBox.SelectedItem != null)
 				{
 					var treeNodeViewModel = listBox.SelectedItem as TreeNodeViewModel;
-					this.ViewModel.Add(treeNode, treeNodeViewModel);
+					ViewModel.Add(treeNode, treeNodeViewModel);
 				}
 			}
 			listBox.SelectedItem = null;
@@ -70,7 +67,7 @@ namespace BehaviorTree
 				return;
 			}
 			var treeNodeViewModel = listBox.SelectedItem as TreeNodeViewModel;
-			this.ViewModel.Remove(treeNodeViewModel);
+			ViewModel.Remove(treeNodeViewModel);
 			listBox.SelectedItem = null;
 			e.Handled = true;
 		}
@@ -83,16 +80,9 @@ namespace BehaviorTree
 			}
 			isLeftButtonDown = true;
 
-			if ((Keyboard.Modifiers & ModifierKeys.Control) != 0)
-			{
-				isControlDown = true;
-			}
-			else
-			{
-				isControlDown = false;
-			}
+			isControlDown = (Keyboard.Modifiers & ModifierKeys.Control) != 0;
 
-			var item = (FrameworkElement)sender;
+			var item = (FrameworkElement) sender;
 			var treeNodeViewModel = item.DataContext as TreeNodeViewModel;
 
 			if (!isControlDown && !listBox.SelectedItems.Contains(treeNodeViewModel))
@@ -115,7 +105,7 @@ namespace BehaviorTree
 				return;
 			}
 
-			var item = (FrameworkElement)sender;
+			var item = (FrameworkElement) sender;
 			var treeNodeViewModel = item.DataContext as TreeNodeViewModel;
 
 			if (isControlDown)
@@ -149,10 +139,12 @@ namespace BehaviorTree
 
 		private void ListBoxItem_MouseMove(object sender, MouseEventArgs e)
 		{
+			Point curMouseDownPoint;
+			Vector dragDelta;
 			if (isDragging)
 			{
-				Point curMouseDownPoint = e.GetPosition(this);
-				var dragDelta = curMouseDownPoint - origMouseDownPoint;
+				curMouseDownPoint = e.GetPosition(this);
+				dragDelta = curMouseDownPoint - origMouseDownPoint;
 
 				origMouseDownPoint = curMouseDownPoint;
 
@@ -164,24 +156,27 @@ namespace BehaviorTree
 				return;
 			}
 
-			var item = (FrameworkElement)sender;
+			if (!isLeftButtonDown)
+			{
+				return;
+			}
+
+			var item = (FrameworkElement) sender;
 			var treeNodeViewModel = item.DataContext as TreeNodeViewModel;
 
 			if (!listBox.SelectedItems.Contains(treeNodeViewModel))
 			{
 				return;
 			}
-			if (isLeftButtonDown)
+
+			curMouseDownPoint = e.GetPosition(this);
+			dragDelta = curMouseDownPoint - origMouseDownPoint;
+			double dragDistance = Math.Abs(dragDelta.Length);
+			if (dragDistance > DragThreshold)
 			{
-				Point curMouseDownPoint = e.GetPosition(this);
-				var dragDelta = curMouseDownPoint - origMouseDownPoint;
-				double dragDistance = Math.Abs(dragDelta.Length);
-				if (dragDistance > DragThreshold)
-				{
-					isDragging = true;
-				}
-				e.Handled = true;
+				isDragging = true;
 			}
+			e.Handled = true;
 		}
 	}
 }

+ 17 - 20
CSharp/Modules/BehaviorTree/BehaviorTreeViewModel.cs

@@ -1,13 +1,13 @@
 using System.Collections.ObjectModel;
 using System.ComponentModel.Composition;
 
-namespace BehaviorTree
-{
-	[Export(typeof(BehaviorTreeViewModel))]
-	[PartCreationPolicy(CreationPolicy.NonShared)]
-	class BehaviorTreeViewModel
+namespace BehaviorTree
+{
+	[Export(typeof (BehaviorTreeViewModel))]
+	[PartCreationPolicy(CreationPolicy.NonShared)]
+	internal class BehaviorTreeViewModel
 	{
-		private ObservableCollection<TreeNodeViewModel> treeNodes = new ObservableCollection<TreeNodeViewModel>();
+		private readonly ObservableCollection<TreeNodeViewModel> treeNodes = new ObservableCollection<TreeNodeViewModel>();
 
 		public ObservableCollection<TreeNodeViewModel> TreeNodes
 		{
@@ -15,20 +15,16 @@ namespace BehaviorTree
 			{
 				return treeNodes;
 			}
-			set
-			{
-				treeNodes = value;
-			}
 		}
 
-		public TreeNodeViewModel Root
+		private TreeNodeViewModel Root
 		{
 			get
 			{
-				return treeNodes.Count > 0? treeNodes[0] : null;
+				return treeNodes.Count == 0? null : treeNodes[0];
 			}
-		}
-
+		}
+
 		public void Add(TreeNode treeNode, TreeNodeViewModel parent)
 		{
 			var treeNodeViewModel = new TreeNodeViewModel(treeNode, parent);
@@ -37,8 +33,9 @@ namespace BehaviorTree
 			{
 				parent.Children.Add(treeNodeViewModel);
 			}
-		}
-
+			BehaviorTreeLayout.ExcuteLayout(Root);
+		}
+
 		public void Remove(TreeNodeViewModel treeNodeViewModel)
 		{
 			for (int i = treeNodeViewModel.Children.Count - 1; i >= 0; --i)
@@ -47,7 +44,7 @@ namespace BehaviorTree
 			}
 			treeNodeViewModel.Parent.Children.Remove(treeNodeViewModel);
 			treeNodes.Remove(treeNodeViewModel);
-		}
-	}
-}
-
+			BehaviorTreeLayout.ExcuteLayout(Root);
+		}
+	}
+}

+ 1 - 0
CSharp/Modules/BehaviorTree/Packages.config

@@ -1,4 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
+
 <packages>
 	<package id="CommonServiceLocator" version="1.0" />
 	<package id="Google.ProtocolBuffers" version="2.4.1.473" />

+ 10 - 33
CSharp/Modules/BehaviorTree/TreeNode.cs

@@ -1,52 +1,29 @@
-
-namespace BehaviorTree
+namespace BehaviorTree
 {
 	public class TreeNode
 	{
-		private double x = 0.0;
-		private double y = 0.0;
-		private int type = 0;
-
 		public TreeNode(double x, double y)
 		{
-			this.x = x;
-			this.y = y;
+			X = x;
+			Y = y;
 		}
 
 		public double X
 		{
-			get
-			{
-				return x;
-			}
-			set
-			{
-				x = value;
-			}
+			get;
+			set;
 		}
 
 		public double Y
 		{
-			get
-			{
-				return y;
-			}
-			set
-			{
-				y = value;
-			}
+			get;
+			set;
 		}
 
 		public int Type
 		{
-			get
-			{
-				return type;
-			}
-			set
-			{
-				type = value;
-			}
+			get;
+			set;
 		}
 	}
-}
+}

+ 40 - 64
CSharp/Modules/BehaviorTree/TreeNodeViewModel.cs

@@ -1,21 +1,17 @@
 using System.Collections.ObjectModel;
 using Microsoft.Practices.Prism.ViewModel;
-using NLog;
 
 namespace BehaviorTree
 {
 	public class TreeNodeViewModel : NotificationObject
 	{
-		private double prelim = 0;
-		private double modify = 0;
-		private static double width = 80;
-		private static double height = 50;
-		private double connectorX2 = 0;
-		private double connectorY2 = 0;
-		private TreeNode treeNode;
-		private TreeNodeViewModel parent;
+		private const double width = 80;
+		private const double height = 50;
+		private readonly TreeNode treeNode;
 		private ObservableCollection<TreeNodeViewModel> children = new ObservableCollection<TreeNodeViewModel>();
-		private Logger logger = LogManager.GetCurrentClassLogger();
+		private double connectorX2;
+		private double connectorY2;
+		private TreeNodeViewModel parent;
 
 		public TreeNodeViewModel(TreeNode treeNode, TreeNodeViewModel parent)
 		{
@@ -24,61 +20,41 @@ namespace BehaviorTree
 			if (this.parent == this)
 			{
 				connectorX2 = 0;
-				connectorY2 = this.Height / 2;
+				connectorY2 = Height / 2;
 			}
 			else
 			{
-				connectorX2 = this.Parent.Width + this.Parent.X - this.X;
-				connectorY2 = this.Parent.Height / 2 + this.Parent.Y - this.Y;
+				connectorX2 = Width + Parent.X - X;
+				connectorY2 = Height / 2 + Parent.Y - Y;
 			}
 		}
 
-		public double Width
+		public static double Width
 		{
 			get
 			{
 				return width;
 			}
-			set
-			{
-				width = value;
-			}
 		}
 
-		public double Height
+		public static double Height
 		{
 			get
 			{
 				return height;
 			}
-			set
-			{
-				height = value;
-			}
 		}
 
 		public double Prelim
 		{
-			get
-			{
-				return prelim;
-			}
-			set
-			{
-				prelim = value;
-			}
+			get;
+			set;
 		}
 
 		public double Modify
 		{
-			get
-			{
-				return modify;
-			}
-			set
-			{
-				modify = value;
-			}
+			get;
+			set;
 		}
 
 		public double X
@@ -96,11 +72,11 @@ namespace BehaviorTree
 				treeNode.X = value;
 				RaisePropertyChanged("X");
 
-				this.ConnectorX2 = this.Parent.Width + this.Parent.X - this.X;
+				ConnectorX2 = Width + Parent.X - X;
 
-				foreach (var child in Children)
+				foreach (TreeNodeViewModel child in Children)
 				{
-					child.ConnectorX2 = this.Width + treeNode.X - child.X;
+					child.ConnectorX2 = Width + treeNode.X - child.X;
 				}
 			}
 		}
@@ -120,11 +96,11 @@ namespace BehaviorTree
 				treeNode.Y = value;
 				RaisePropertyChanged("Y");
 
-				ConnectorY2 = this.Parent.Height / 2 + this.Parent.Y - this.Y;
+				ConnectorY2 = Height / 2 + Parent.Y - Y;
 
-				foreach (var child in Children)
+				foreach (TreeNodeViewModel child in Children)
 				{
-					child.ConnectorY2 = this.Height / 2 + treeNode.Y - child.Y;
+					child.ConnectorY2 = Height / 2 + treeNode.Y - child.Y;
 				}
 			}
 		}
@@ -141,7 +117,7 @@ namespace BehaviorTree
 		{
 			get
 			{
-				return this.Height / 2;
+				return Height / 2;
 			}
 		}
 
@@ -149,15 +125,15 @@ namespace BehaviorTree
 		{
 			get
 			{
-				if (this.Parent == this)
+				if (Parent == this)
 				{
 					return 0;
 				}
-				return this.connectorX2;
+				return connectorX2;
 			}
 			set
 			{
-				this.connectorX2 = value;
+				connectorX2 = value;
 				RaisePropertyChanged("ConnectorX2");
 			}
 		}
@@ -166,15 +142,15 @@ namespace BehaviorTree
 		{
 			get
 			{
-				if (this.Parent == this)
+				if (Parent == this)
 				{
-					return this.Height / 2;
+					return Height / 2;
 				}
-				return this.connectorY2;
+				return connectorY2;
 			}
 			set
 			{
-				this.connectorY2 = value;
+				connectorY2 = value;
 				RaisePropertyChanged("ConnectorY2");
 			}
 		}
@@ -224,19 +200,19 @@ namespace BehaviorTree
 		{
 			get
 			{
-				if (this.Parent == this)
+				if (Parent == this)
 				{
 					return null;
 				}
 
-				int index = this.Parent.Children.IndexOf(this);
+				int index = Parent.Children.IndexOf(this);
 				if (index == 0)
 				{
 					return null;
 				}
 				else
 				{
-					return this.Parent.Children[index - 1];
+					return Parent.Children[index - 1];
 				}
 			}
 		}
@@ -245,7 +221,7 @@ namespace BehaviorTree
 		{
 			get
 			{
-				int index = this.Parent.Children.IndexOf(this);
+				int index = Parent.Children.IndexOf(this);
 				return index;
 			}
 		}
@@ -254,13 +230,13 @@ namespace BehaviorTree
 		{
 			get
 			{
-				if (this.Children.Count == 0)
+				if (Children.Count == 0)
 				{
 					return null;
 				}
 
-				int maxIndex = this.Children.Count - 1;
-				return this.Children[Index];
+				int maxIndex = Children.Count - 1;
+				return Children[Index];
 			}
 		}
 
@@ -268,11 +244,11 @@ namespace BehaviorTree
 		{
 			get
 			{
-				if (this.Children.Count == 0)
+				if (Children.Count == 0)
 				{
 					return null;
 				}
-				return this.Children[0];
+				return Children[0];
 			}
 		}
 
@@ -280,8 +256,8 @@ namespace BehaviorTree
 		{
 			get
 			{
-				return this.Children.Count == 0;
+				return Children.Count == 0;
 			}
 		}
 	}
-}
+}