Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved code into utility assembly and exposing it in an easier to cons…

…ume way for WPF applications.
  • Loading branch information...
commit 27f35a80183b37f446d812d65a40dc64c9c925c8 1 parent 2a0a722
@etihwddot authored
View
6 NiteWpfDemo/NiteWpfDemo.sln
@@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NiteWpfDemo", "src\NiteWpfDemo\NiteWpfDemo.csproj", "{0761A22A-D597-457C-9BA7-E0CC3080BD6E}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nui.Utility.Windows", "src\Nui.Utility.Windows\Nui.Utility.Windows.csproj", "{B8D0520B-1712-40FE-95E1-7ABC92801025}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
@@ -13,6 +15,10 @@ Global
{0761A22A-D597-457C-9BA7-E0CC3080BD6E}.Debug|x86.Build.0 = Debug|x86
{0761A22A-D597-457C-9BA7-E0CC3080BD6E}.Release|x86.ActiveCfg = Release|x86
{0761A22A-D597-457C-9BA7-E0CC3080BD6E}.Release|x86.Build.0 = Release|x86
+ {B8D0520B-1712-40FE-95E1-7ABC92801025}.Debug|x86.ActiveCfg = Debug|x86
+ {B8D0520B-1712-40FE-95E1-7ABC92801025}.Debug|x86.Build.0 = Debug|x86
+ {B8D0520B-1712-40FE-95E1-7ABC92801025}.Release|x86.ActiveCfg = Release|x86
+ {B8D0520B-1712-40FE-95E1-7ABC92801025}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
View
40 NiteWpfDemo/src/NiteWpfDemo/MainWindow.xaml
@@ -2,10 +2,44 @@
x:Class="NiteWpfDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="WPF Demo"
+ xmlns:nui="clr-namespace:Nui.Utility.Windows;assembly=Nui.Utility.Windows"
+ Title="WPF Demo"
Height="480"
Width="640">
- <Grid>
- <TextBlock x:Name="Status" VerticalAlignment="Center" HorizontalAlignment="Center">Loading...</TextBlock>
+
+ <Window.Resources>
+ <Style x:Key="SessionStateStyle" TargetType="{x:Type TextBlock}">
+ <Setter Property="Margin" Value="16" />
+ <Setter Property="HorizontalAlignment" Value="Center" />
+ <Setter Property="Text" Value="Loading..." />
+ <Style.Triggers>
+ <DataTrigger Binding="{Binding State}" Value="{x:Static nui:SessionState.Idle}">
+ <Setter Property="Text" Value="Wave to start." />
+ </DataTrigger>
+ <DataTrigger Binding="{Binding State}" Value="{x:Static nui:SessionState.Running}">
+ <Setter Property="Text" Value="Session running." />
+ </DataTrigger>
+ </Style.Triggers>
+ </Style>
+
+ <Style x:Key="PointOutputStyle" TargetType="{x:Type TextBlock}">
+ <Setter Property="Margin" Value="16" />
+ <Setter Property="HorizontalAlignment" Value="Center" />
+ <Setter Property="Text" Value="{Binding Point}" />
+ </Style>
+ </Window.Resources>
+
+ <Grid>
+ <Grid.RowDefinitions>
+ <RowDefinition Height="Auto" />
+ <RowDefinition Height="Auto" />
+ <RowDefinition Height="*" />
+ </Grid.RowDefinitions>
+
+ <!-- Session State -->
+ <TextBlock Style="{StaticResource SessionStateStyle}" />
+
+ <!-- Point Data -->
+ <TextBlock Style="{StaticResource PointOutputStyle}" Grid.Row="1"></TextBlock>
</Grid>
</Window>
View
88 NiteWpfDemo/src/NiteWpfDemo/MainWindow.xaml.cs
@@ -1,11 +1,6 @@
-using System;
-using System.ComponentModel;
-using System.Threading;
using System.Windows;
-using System.Windows.Threading;
-using xn;
-using xnv;
+using Nui.Utility.Windows;
namespace NiteWpfDemo
{
@@ -13,87 +8,12 @@ public partial class MainWindow : Window
{
public MainWindow()
{
- Loaded += MainWindow_Loaded;
- Closing += MainWindow_Closing;
+ m_session = new NuiSession(this);
+ DataContext = m_session;
InitializeComponent();
}
- private void MainWindow_Closing(object sender, CancelEventArgs e)
- {
- if (m_thread != null)
- {
- m_running = false;
- m_thread.Join();
- }
- }
-
- private void MainWindow_Loaded(object sender, RoutedEventArgs e)
- {
- m_thread = new Thread(CreateAndRun);
- m_running = true;
- m_thread.Start();
- }
-
- private void CreateAndRun()
- {
- using (Context context = new Context(@"data\openNI.xml"))
- {
- SessionManager sessionManager = new SessionManager(context, "Wave", "RaiseHand");
- BeginInvoke(() => { Status.Text = "Session created, wave to start session."; });
-
- sessionManager.SessionStart += SessionManager_SessionStart;
- sessionManager.SessionEnd += SessionManager_SessionEnd;
-
- PointControl pointControl = new PointControl();
- pointControl.PrimaryPointCreate += PointControl_PrimaryPointCreate;
- pointControl.PrimaryPointDestroy += PointControl_PrimaryPointDestroy;
- pointControl.PrimaryPointUpdate += PointControl_PrimaryPointUpdate;
-
- PointDenoiser denoiser = new PointDenoiser();
- denoiser.AddListener(pointControl);
- sessionManager.AddListener(denoiser);
-
- while (m_running)
- {
- context.WaitAndUpdateAll();
- sessionManager.Update(context);
- }
- }
- }
-
- private void PointControl_PrimaryPointUpdate(ref HandPointContext context)
- {
- HandPointContext contextCopy = context;
- BeginInvoke(() => { Status.Text = string.Format("Point updated\nX: {0:#.##}\nY: {1:#.##}\nZ: {2:#.##}.", contextCopy.ptPosition.X, contextCopy.ptPosition.Y, contextCopy.ptPosition.Z); });
- }
-
- private void PointControl_PrimaryPointDestroy(uint id)
- {
- BeginInvoke(() => { Status.Text = "Point destroyed."; });
- }
-
- private void PointControl_PrimaryPointCreate(ref HandPointContext context, ref Point3D ptFocus)
- {
- BeginInvoke(() => { Status.Text = "Point created."; });
- }
-
- private void SessionManager_SessionStart(ref Point3D position)
- {
- BeginInvoke(() => { Status.Text = "Session Started"; });
- }
-
- private void SessionManager_SessionEnd()
- {
- BeginInvoke(() => { Status.Text = "Session Ended, wave to start it again."; });
- }
-
- private void BeginInvoke(Action action)
- {
- Dispatcher.BeginInvoke(DispatcherPriority.Background, action);
- }
-
- volatile bool m_running;
- Thread m_thread;
+ NuiSession m_session;
}
}
View
7 NiteWpfDemo/src/NiteWpfDemo/NiteWpfDemo.csproj
@@ -115,9 +115,10 @@
<AppDesigner Include="Properties\" />
</ItemGroup>
<ItemGroup>
- <Content Include="Data\openNI.xml">
- <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
- </Content>
+ <ProjectReference Include="..\Nui.Utility.Windows\Nui.Utility.Windows.csproj">
+ <Project>{B8D0520B-1712-40FE-95E1-7ABC92801025}</Project>
+ <Name>Nui.Utility.Windows</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.
View
2  NiteWpfDemo/src/NiteWpfDemo/Properties/AssemblyInfo.cs
@@ -6,9 +6,7 @@
[assembly: AssemblyTitle("NiteWpfDemo")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("NiteWpfDemo")]
-[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
View
0  NiteWpfDemo/src/NiteWpfDemo/Data/openNI.xml → ...o/src/Nui.Utility.Windows/Data/openNI.xml
File renamed without changes
View
14 NiteWpfDemo/src/Nui.Utility.Windows/DispatcherUtility.cs
@@ -0,0 +1,14 @@
+
+using System;
+using System.Windows.Threading;
+
+namespace Nui.Utility.Windows
+{
+ public static class DispatcherUtility
+ {
+ public static void BeginInvoke(this Dispatcher dispatcher, Action action)
+ {
+ dispatcher.BeginInvoke(DispatcherPriority.Background, action);
+ }
+ }
+}
View
17 NiteWpfDemo/src/Nui.Utility.Windows/NotifyPropertyChangedBase.cs
@@ -0,0 +1,17 @@
+
+using System.ComponentModel;
+
+namespace Nui.Utility.Windows
+{
+ public abstract class NotifyPropertyChangedBase : INotifyPropertyChanged
+ {
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ protected void RaisePropertyChanged(string property)
+ {
+ PropertyChangedEventHandler handler = PropertyChanged;
+ if (handler != null)
+ handler(this, new PropertyChangedEventArgs(property));
+ }
+ }
+}
View
33 NiteWpfDemo/src/Nui.Utility.Windows/NotifyPropertyChangedDispatcherBase.cs
@@ -0,0 +1,33 @@
+
+using System;
+using System.Threading;
+using System.Windows.Threading;
+
+namespace Nui.Utility.Windows
+{
+ public abstract class NotifyPropertyChangedDispatcherBase : NotifyPropertyChangedBase
+ {
+ protected NotifyPropertyChangedDispatcherBase()
+ {
+ if (m_dispatcher == null)
+ throw new InvalidOperationException("This Dispatcher-affined object cannot be created on a thread that has no associated Dispatcher.");
+ }
+
+ public void VerifyAccess()
+ {
+ m_dispatcher.VerifyAccess();
+ }
+
+ protected bool CheckAccess()
+ {
+ return m_dispatcher.CheckAccess();
+ }
+
+ protected Dispatcher Dispatcher
+ {
+ get { return m_dispatcher; }
+ }
+
+ readonly Dispatcher m_dispatcher = Dispatcher.FromThread(Thread.CurrentThread);
+ }
+}
View
101 NiteWpfDemo/src/Nui.Utility.Windows/Nui.Utility.Windows.csproj
@@ -0,0 +1,101 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>9.0.30729</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{B8D0520B-1712-40FE-95E1-7ABC92801025}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Nui.Utility.Windows</RootNamespace>
+ <AssemblyName>Nui.Utility.Windows</AssemblyName>
+ <TargetFrameworkVersion>v3.5</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>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+ <DebugSymbols>true</DebugSymbols>
+ <OutputPath>bin\x86\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <DebugType>full</DebugType>
+ <PlatformTarget>x86</PlatformTarget>
+ <ErrorReport>prompt</ErrorReport>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+ <OutputPath>bin\x86\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <Optimize>true</Optimize>
+ <DebugType>pdbonly</DebugType>
+ <PlatformTarget>x86</PlatformTarget>
+ <ErrorReport>prompt</ErrorReport>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="OpenNI.net, Version=1.0.0.25, Culture=neutral, PublicKeyToken=6b43d0c6cf74ee7f, processorArchitecture=x86">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\lib\OpenNI.net.dll</HintPath>
+ </Reference>
+ <Reference Include="PresentationCore">
+ <RequiredTargetFramework>3.0</RequiredTargetFramework>
+ </Reference>
+ <Reference Include="PresentationFramework">
+ <RequiredTargetFramework>3.0</RequiredTargetFramework>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Core">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ </Reference>
+ <Reference Include="System.Xml.Linq">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ </Reference>
+ <Reference Include="System.Data.DataSetExtensions">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ </Reference>
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ <Reference Include="WindowsBase">
+ <RequiredTargetFramework>3.0</RequiredTargetFramework>
+ </Reference>
+ <Reference Include="XnVNITE.net, Version=1.3.0.18, Culture=neutral, processorArchitecture=x86">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\lib\XnVNITE.net.dll</HintPath>
+ </Reference>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="DispatcherUtility.cs" />
+ <Compile Include="NotifyPropertyChangedBase.cs" />
+ <Compile Include="NotifyPropertyChangedDispatcherBase.cs" />
+ <Compile Include="SessionState.cs" />
+ <Compile Include="OpenNISession.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Content Include="Data\openNI.xml">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
+ </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>
View
149 NiteWpfDemo/src/Nui.Utility.Windows/OpenNISession.cs
@@ -0,0 +1,149 @@
+
+using System.ComponentModel;
+using System.Threading;
+using System.Windows;
+using xn;
+using xnv;
+
+namespace Nui.Utility.Windows
+{
+ public sealed class NuiSession : NotifyPropertyChangedDispatcherBase
+ {
+ public NuiSession(Window window)
+ {
+ m_window = window;
+ window.Loaded += Window_Loaded;
+ window.Closing += Window_Closing;
+ }
+
+ public static readonly string StateProperty = "State";
+ public SessionState State
+ {
+ get
+ {
+ VerifyAccess();
+ return m_state;
+ }
+ private set
+ {
+ VerifyAccess();
+
+ if (m_state != value)
+ {
+ m_state = value;
+ RaisePropertyChanged(StateProperty);
+ }
+ }
+ }
+
+ public static readonly string PointProperty = "Point";
+ public System.Windows.Media.Media3D.Point3D? Point
+ {
+ get
+ {
+ VerifyAccess();
+ return m_point;
+ }
+ private set
+ {
+ VerifyAccess();
+
+ if (m_point != value)
+ {
+ m_point = value;
+ RaisePropertyChanged(PointProperty);
+ }
+ }
+ }
+
+ private void Window_Loaded(object sender, RoutedEventArgs e)
+ {
+ // only listen to the first loaded
+ m_window.Loaded -= Window_Loaded;
+
+ // create a processing thread
+ m_thread = new Thread(CreateAndRun);
+ m_running = true;
+ m_thread.Start();
+ }
+
+ private void Window_Closing(object sender, CancelEventArgs e)
+ {
+ if (m_thread != null)
+ {
+ m_running = false;
+ m_thread.Join();
+ }
+ }
+
+ private void CreateAndRun()
+ {
+ using (Context context = new Context(@"data\openNI.xml"))
+ {
+ SessionManager sessionManager = new SessionManager(context, "Wave", "RaiseHand");
+
+ // update the state
+ Dispatcher.BeginInvoke(() => { State = SessionState.Idle; });
+
+ sessionManager.SessionStart += SessionManager_SessionStart;
+ sessionManager.SessionEnd += SessionManager_SessionEnd;
+
+ PointControl pointControl = new PointControl();
+ pointControl.PrimaryPointCreate += PointControl_PrimaryPointCreate;
+ pointControl.PrimaryPointDestroy += PointControl_PrimaryPointDestroy;
+ pointControl.PrimaryPointUpdate += PointControl_PrimaryPointUpdate;
+
+ PointDenoiser denoiser = new PointDenoiser();
+ denoiser.AddListener(pointControl);
+ sessionManager.AddListener(denoiser);
+
+ while (m_running)
+ {
+ context.WaitAndUpdateAll();
+ sessionManager.Update(context);
+ }
+ }
+ }
+
+ private void PointControl_PrimaryPointUpdate(ref HandPointContext context)
+ {
+ System.Windows.Media.Media3D.Point3D? point = PointFromContext(context);
+ Dispatcher.BeginInvoke(() => { Point = point; });
+ }
+
+ private void PointControl_PrimaryPointDestroy(uint id)
+ {
+ Dispatcher.BeginInvoke(() => { Point = null; });
+ }
+
+ private void PointControl_PrimaryPointCreate(ref HandPointContext context, ref Point3D ptfocus)
+ {
+ System.Windows.Media.Media3D.Point3D? point = PointFromContext(context);
+ Dispatcher.BeginInvoke(() => { Point = point; });
+ }
+
+ private System.Windows.Media.Media3D.Point3D? PointFromContext(HandPointContext context)
+ {
+ return new System.Windows.Media.Media3D.Point3D(context.ptPosition.X, context.ptPosition.Y, context.ptPosition.Z);
+ }
+
+ private void SessionManager_SessionEnd()
+ {
+ // update the state
+ Dispatcher.BeginInvoke(() => { State = SessionState.Idle; });
+ }
+
+ private void SessionManager_SessionStart(ref Point3D position)
+ {
+ // update the state
+ Dispatcher.BeginInvoke(() => { State = SessionState.Running; });
+ }
+
+ readonly Window m_window;
+
+ volatile bool m_running;
+ Thread m_thread;
+ SessionState m_state;
+ private System.Windows.Media.Media3D.Point3D? m_point;
+ }
+}
View
11 NiteWpfDemo/src/Nui.Utility.Windows/Properties/AssemblyInfo.cs
@@ -0,0 +1,11 @@
+
+using System.Reflection;
+
+[assembly: AssemblyTitle("Nui.Utility")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyProduct("Nui.Utility")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
View
10 NiteWpfDemo/src/Nui.Utility.Windows/SessionState.cs
@@ -0,0 +1,10 @@
+
+namespace Nui.Utility.Windows
+{
+ public enum SessionState
+ {
+ None,
+ Idle,
+ Running,
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.