Advertisement
atsukanrock

A Kinect version of sub class of the ObserverBase

Jul 16th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reactive.Disposables;
  4. using System.Reactive.Linq;
  5. using Microsoft.Kinect;
  6. using Microsoft.Kinect.Toolkit;
  7. using Microsoft.Practices.EnterpriseLibrary.Common.Utility;
  8.  
  9. namespace Foo
  10. {
  11.     /// <summary>
  12.     /// An abstract base class of classes that observes a Kinect's XxxFrameReady event(s).
  13.     /// </summary>
  14.     public abstract class KinectObserverBase : ObserverBase
  15.     {
  16.         private readonly KinectSensorChooser _sensorChooser;
  17.  
  18.         /// <summary>
  19.         /// Initializes a new instance of the <see cref="KinectObserverBase"/> class.
  20.         /// </summary>
  21.         /// <exception cref="System.ArgumentNullException">
  22.         /// <paramref name="sensorChooser"/> is <c>null</c>.
  23.         /// </exception>
  24.         protected KinectObserverBase(KinectSensorChooser sensorChooser)
  25.         {
  26.             Guard.ArgumentNotNull(sensorChooser, "sensorChooser");
  27.             _sensorChooser = sensorChooser;
  28.         }
  29.  
  30.         /// <summary>
  31.         /// Subscribes to observable subject(s).
  32.         /// </summary>
  33.         /// <returns>
  34.         /// The subscription(s). Typically, the disposable returned from the Subscribe extension method of
  35.         /// <seealso cref="IObservable{T}" /> should be returned.
  36.         /// </returns>
  37.         protected override IDisposable SubscribeToObservable()
  38.         {
  39.             var subscriptions = new List<IDisposable>();
  40.  
  41.             var kinect = _sensorChooser.Kinect;
  42.             if (kinect != null)
  43.                 subscriptions.Add(SubscribeToFrameReady(kinect));
  44.  
  45.             subscriptions.Add(SubscribeToKinectChanged());
  46.  
  47.             return new CompositeDisposable(subscriptions);
  48.         }
  49.  
  50.         /// <summary>
  51.         /// Subscribes to frame ready event(s) of the specified <seealso cref="KinectSensor"/>.
  52.         /// To subscribe, Rx should be used in derived classes.
  53.         /// </summary>
  54.         /// <param name="kinect">The <seealso cref="KinectSensor"/>.</param>
  55.         /// <returns>
  56.         /// The subscription(s) to the frame ready event(s) of the <paramref name="kinect"/>.
  57.         /// </returns>
  58.         protected abstract IDisposable SubscribeToFrameReady(KinectSensor kinect);
  59.  
  60.         private IDisposable SubscribeToKinectChanged()
  61.         {
  62.             return Observable.FromEventPattern<KinectChangedEventArgs>(eh => _sensorChooser.KinectChanged += eh,
  63.                                                                        eh => _sensorChooser.KinectChanged -= eh)
  64.                              .Subscribe(ep =>
  65.                              {
  66.                                  StopObserving();
  67.                                  StartObserving();
  68.                              });
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement