Procházet zdrojové kódy

treeview放到一个自定义控件中

tanghai před 14 roky
rodič
revize
700fe2916f

+ 60 - 0
CSharp/Data/BehaviorNode.cs

@@ -0,0 +1,60 @@
+using System.Collections.Generic;
+
+namespace Egametang
+{
+	public class BehaviorNode
+	{
+		private int type;
+		private string name;
+		private List<int> args;
+		private List<BehaviorNode> children;
+
+		public int Type
+		{
+			get
+			{
+				return type;
+			}
+			set
+			{
+				type = value;
+			}
+		}
+
+		public string Name
+		{
+			get 
+			{
+				return name; 
+			}
+			set 
+			{
+				name = value; 
+			}
+		}
+
+		public List<int> Args
+		{
+			get
+			{
+				return args;
+			}
+			set
+			{
+				args = value;
+			}
+		}
+
+		public List<BehaviorNode> Children
+		{
+			get
+			{
+				return children;
+			}
+			set
+			{
+				children = value;
+			}
+		}
+	}
+}

+ 54 - 0
CSharp/Data/Model.csproj

@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>8.0.30703</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{3978E513-87FA-44EC-893A-DBD90A5A8808}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>Data</RootNamespace>
+    <AssemblyName>Data</AssemblyName>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="BehaviorNode.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 41 - 0
CSharp/GameEditor/BehaviorTreeView.xaml

@@ -0,0 +1,41 @@
+<UserControl x:Class="Egametang.BehaviorTreeView"
+		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" 
+		mc:Ignorable="d" 
+		d:DesignHeight="300" d:DesignWidth="300">
+	<Grid>
+		<TreeView Grid.Column="0"  HorizontalAlignment="Stretch" Name="behaviorTreeView" 
+				VerticalAlignment="Stretch" Padding="0" BorderThickness="0" 
+				ContextMenuOpening="behaviorTreeView_ContextMenuOpening"
+				ItemsSource="{Binding Children}">
+			<TreeView.ContextMenu>
+				<ContextMenu>
+					<MenuItem Name="newMenuItem" Header="New" Click="newMenuItem_Click" />
+					<MenuItem Name="copyMenuItem" Header="Copy" Click="copyMenuItem_Click" />
+					<MenuItem Name="pasteMenuItem" Header="Paste" Click="pasteMenuItem_Click" />
+					<MenuItem Name="delMenuItem" Header="Delete" Click="delMenuItem_Click" />
+				</ContextMenu>
+			</TreeView.ContextMenu>
+
+			<TreeView.ItemContainerStyle>
+				<Style TargetType="{x:Type TreeViewItem}">
+					<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
+					<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
+					<Setter Property="FontWeight" Value="Normal" />
+					<Style.Triggers>
+						<Trigger Property="IsSelected" Value="True">
+							<Setter Property="FontWeight" Value="Bold" />
+						</Trigger>
+					</Style.Triggers>
+				</Style>
+			</TreeView.ItemContainerStyle>
+			<TreeView.ItemTemplate>
+				<HierarchicalDataTemplate ItemsSource="{Binding Children}">
+					<TextBlock Text="{Binding Name}" />
+				</HierarchicalDataTemplate>
+			</TreeView.ItemTemplate>
+		</TreeView>
+	</Grid>
+</UserControl>

+ 52 - 0
CSharp/GameEditor/BehaviorTreeView.xaml.cs

@@ -0,0 +1,52 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+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;
+
+namespace Egametang
+{
+	/// <summary>
+	/// BehaviorTreeView.xaml 的交互逻辑
+	/// </summary>
+	public partial class BehaviorTreeView : UserControl
+	{
+		public BehaviorTreeView()
+		{
+			InitializeComponent();
+		}
+
+		private void newMenuItem_Click(object sender, RoutedEventArgs e)
+		{
+
+		}
+
+		private void copyMenuItem_Click(object sender, RoutedEventArgs e)
+		{
+
+		}
+
+		private void pasteMenuItem_Click(object sender, RoutedEventArgs e)
+		{
+
+		}
+
+		private void delMenuItem_Click(object sender, RoutedEventArgs e)
+		{
+
+		}
+
+		private void behaviorTreeView_ContextMenuOpening(object sender, ContextMenuEventArgs e)
+		{
+
+		}
+	}
+}

+ 92 - 0
CSharp/GameEditor/View.csproj

@@ -0,0 +1,92 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+    <ProductVersion>8.0.30703</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{FA1C18E4-C928-4900-A77B-6944B3443B7E}</ProjectGuid>
+    <OutputType>WinExe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>Egametang</RootNamespace>
+    <AssemblyName>GameEditor</AssemblyName>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <TargetFrameworkProfile>Client</TargetFrameworkProfile>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+    <PlatformTarget>x86</PlatformTarget>
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+    <PlatformTarget>x86</PlatformTarget>
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="System.Xaml">
+      <RequiredTargetFramework>4.0</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="WindowsBase" />
+    <Reference Include="PresentationCore" />
+    <Reference Include="PresentationFramework" />
+  </ItemGroup>
+  <ItemGroup>
+    <ApplicationDefinition Include="App.xaml">
+      <Generator>MSBuild:Compile</Generator>
+      <SubType>Designer</SubType>
+    </ApplicationDefinition>
+    <Page Include="BehaviorTreeView.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
+    <Page Include="MainWindow.xaml">
+      <Generator>MSBuild:Compile</Generator>
+      <SubType>Designer</SubType>
+    </Page>
+    <Compile Include="App.xaml.cs">
+      <DependentUpon>App.xaml</DependentUpon>
+      <SubType>Code</SubType>
+    </Compile>
+    <Compile Include="BehaviorTreeView.xaml.cs">
+      <DependentUpon>BehaviorTreeView.xaml</DependentUpon>
+    </Compile>
+    <Compile Include="MainWindow.xaml.cs">
+      <DependentUpon>MainWindow.xaml</DependentUpon>
+      <SubType>Code</SubType>
+    </Compile>
+  </ItemGroup>
+  <ItemGroup>
+    <AppDesigner Include="Properties\" />
+  </ItemGroup>
+  <ItemGroup>
+    <Folder Include="Properties\" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 34 - 0
CSharp/ViewModel/BehaviorNodeViewModel.cs

@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Egametang
+{
+	public class BehaviorNodeViewModel : TreeViewItemViewModel
+	{
+		private readonly BehaviorNode node;
+
+		public BehaviorNodeViewModel(BehaviorNode node, BehaviorNodeViewModel parent) :
+			base(parent, false)
+		{
+			this.node = node;
+		}
+
+		public string Name
+		{
+			get
+			{
+				return node.Name;
+			}
+		}
+
+		public virtual void LoadChildren()
+		{
+			foreach (var child in node.Children)
+			{
+				base.Children.Add(new BehaviorNodeViewModel(child, this));
+			}
+		}
+	}
+}

+ 174 - 0
CSharp/ViewModel/TreeViewItemViewModel.cs

@@ -0,0 +1,174 @@
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+
+namespace Egametang
+{
+	/// <summary>
+	/// Base class for all ViewModel classes displayed by TreeViewItems.  
+	/// This acts as an adapter between a raw data object and a TreeViewItem.
+	/// </summary>
+	public class TreeViewItemViewModel : INotifyPropertyChanged
+	{
+		#region Data
+
+		static readonly TreeViewItemViewModel dummyChild = new TreeViewItemViewModel();
+
+		readonly ObservableCollection<TreeViewItemViewModel> children;
+		readonly TreeViewItemViewModel parent;
+
+		bool isExpanded;
+		bool isSelected;
+
+		#endregion // Data
+
+		#region Constructors
+
+		protected TreeViewItemViewModel(TreeViewItemViewModel parent, bool lazyLoadChildren)
+		{
+			this.parent = parent;
+
+			children = new ObservableCollection<TreeViewItemViewModel>();
+
+			if (lazyLoadChildren)
+			{
+				children.Add(dummyChild);
+			}
+		}
+
+		// This is used to create the DummyChild instance.
+		private TreeViewItemViewModel()
+		{
+		}
+
+		#endregion // Constructors
+
+		#region Children
+
+		/// <summary>
+		/// Returns the logical child items of this object.
+		/// </summary>
+		public ObservableCollection<TreeViewItemViewModel> Children
+		{
+			get 
+			{
+				return children; 
+			}
+		}
+
+		#endregion // Children
+
+		#region HasLoadedChildren
+
+		/// <summary>
+		/// Returns true if this object's Children have not yet been populated.
+		/// </summary>
+		public bool HasDummyChild
+		{
+			get 
+			{ 
+				return this.Children.Count == 1 && this.Children[0] == dummyChild; 
+			}
+		}
+
+		#endregion // HasLoadedChildren
+
+		#region IsExpanded
+
+		/// <summary>
+		/// Gets/sets whether the TreeViewItem 
+		/// associated with this object is expanded.
+		/// </summary>
+		public bool IsExpanded
+		{
+			get 
+			{ 
+				return isExpanded; 
+			}
+			set
+			{
+				if (value != isExpanded)
+				{
+					isExpanded = value;
+					this.OnPropertyChanged("IsExpanded");
+				}
+
+				// Expand all the way up to the root.
+				if (isExpanded && parent != null)
+				{
+					parent.IsExpanded = true;
+				}
+
+				// Lazy load the child items, if necessary.
+				if (this.HasDummyChild)
+				{
+					this.Children.Remove(dummyChild);
+					this.LoadChildren();
+				}
+			}
+		}
+
+		#endregion // IsExpanded
+
+		#region IsSelected
+
+		/// <summary>
+		/// Gets/sets whether the TreeViewItem 
+		/// associated with this object is selected.
+		/// </summary>
+		public bool IsSelected
+		{
+			get 
+			{ 
+				return isSelected; 
+			}
+			set
+			{
+				if (value != isSelected)
+				{
+					isSelected = value;
+					this.OnPropertyChanged("IsSelected");
+				}
+			}
+		}
+
+		#endregion // IsSelected
+
+		#region LoadChildren
+
+		/// <summary>
+		/// Invoked when the child items need to be loaded on demand.
+		/// Subclasses can override this to populate the Children collection.
+		/// </summary>
+		protected virtual void LoadChildren()
+		{
+		}
+
+		#endregion // LoadChildren
+
+		#region Parent
+
+		public TreeViewItemViewModel Parent
+		{
+			get 
+			{ 
+				return parent; 
+			}
+		}
+
+		#endregion // Parent
+
+		#region INotifyPropertyChanged Members
+
+		public event PropertyChangedEventHandler PropertyChanged;
+
+		protected virtual void OnPropertyChanged(string propertyName)
+		{
+			if (this.PropertyChanged != null)
+			{
+				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
+			}
+		}
+
+		#endregion // INotifyPropertyChanged Members
+	}
+}

+ 61 - 0
CSharp/ViewModel/ViewModel.csproj

@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>8.0.30703</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{E55C43C7-4CE2-4384-AE8A-39E074F2FC0B}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>Egametang</RootNamespace>
+    <AssemblyName>ViewModel</AssemblyName>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="BehaviorNodeViewModel.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="TreeViewItemViewModel.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\Data\Model.csproj">
+      <Project>{3978E513-87FA-44EC-893A-DBD90A5A8808}</Project>
+      <Name>Model</Name>
+    </ProjectReference>
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>