Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Reactive.Disposables;
- using System.Reactive.Linq;
- using Microsoft.Kinect;
- using Microsoft.Kinect.Toolkit;
- using Microsoft.Practices.EnterpriseLibrary.Common.Utility;
- namespace Foo
- {
- /// <summary>
- /// An abstract base class of classes that observes a Kinect's XxxFrameReady event(s).
- /// </summary>
- public abstract class KinectObserverBase : ObserverBase
- {
- private readonly KinectSensorChooser _sensorChooser;
- /// <summary>
- /// Initializes a new instance of the <see cref="KinectObserverBase"/> class.
- /// </summary>
- /// <exception cref="System.ArgumentNullException">
- /// <paramref name="sensorChooser"/> is <c>null</c>.
- /// </exception>
- protected KinectObserverBase(KinectSensorChooser sensorChooser)
- {
- Guard.ArgumentNotNull(sensorChooser, "sensorChooser");
- _sensorChooser = sensorChooser;
- }
- /// <summary>
- /// Subscribes to observable subject(s).
- /// </summary>
- /// <returns>
- /// The subscription(s). Typically, the disposable returned from the Subscribe extension method of
- /// <seealso cref="IObservable{T}" /> should be returned.
- /// </returns>
- protected override IDisposable SubscribeToObservable()
- {
- var subscriptions = new List<IDisposable>();
- var kinect = _sensorChooser.Kinect;
- if (kinect != null)
- subscriptions.Add(SubscribeToFrameReady(kinect));
- subscriptions.Add(SubscribeToKinectChanged());
- return new CompositeDisposable(subscriptions);
- }
- /// <summary>
- /// Subscribes to frame ready event(s) of the specified <seealso cref="KinectSensor"/>.
- /// To subscribe, Rx should be used in derived classes.
- /// </summary>
- /// <param name="kinect">The <seealso cref="KinectSensor"/>.</param>
- /// <returns>
- /// The subscription(s) to the frame ready event(s) of the <paramref name="kinect"/>.
- /// </returns>
- protected abstract IDisposable SubscribeToFrameReady(KinectSensor kinect);
- private IDisposable SubscribeToKinectChanged()
- {
- return Observable.FromEventPattern<KinectChangedEventArgs>(eh => _sensorChooser.KinectChanged += eh,
- eh => _sensorChooser.KinectChanged -= eh)
- .Subscribe(ep =>
- {
- StopObserving();
- StartObserving();
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement