Microsoft® Surface® Live Stream Code Sample

ive Stream enables businesses to engage their customers face-to-face using the most recent and relevant Twitter™, Flickr®, and RSS newsfeeds.

C# (5.9 MB)
 
 
 
 
 
4.3 Star
(4)
6,626 times
Add to favorites
4/19/2011
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using LiveStream.Helpers;
using LiveStream.Properties;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

namespace LiveStream.Controls
{
    /// <summary>
    /// Controls the visibility of the AdminPanel.
    /// </summary>
    public partial class AdminLayer : SurfaceUserControl
    {
        /// <summary>
        /// A timer which closes the admin panel after a specified interval.
        /// </summary>
        private DispatcherTimer _idleTimer;

        /// <summary>
        /// Initializes a new instance of the <see cref="AdminLayer"/> class.
        /// </summary>
        public AdminLayer()
        {
            InitializeComponent();

            if (DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }

            (Application.Current.MainWindow as SurfaceWindow).PreviewContactDown += SurfaceWindow_PreviewContactDown;
            _panelContainer.ScatterManipulationDelta += (sender, e) => BeginIdleTimeout();
            _panelContainer.IsAnyContactCapturedWithinChanged += AdminPanel_IsAnyContactCapturedWithinChanged;
            _idleTimer = new DispatcherTimer { Interval = Settings.Default.AdminTimeoutDelay };
            _idleTimer.Tick += new EventHandler(IdleTimer_Tick);
        }

        /// <summary>
        /// Handles the PreviewContactDown event of the SurfaceWindow control. When the admin tag is placed, show the admin panel.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Microsoft.Surface.Presentation.ContactEventArgs"/> instance containing the event data.</param>
        private void SurfaceWindow_PreviewContactDown(object sender, ContactEventArgs e)
        {
            if (e.Contact.Tag.Type == TagType.Byte && e.Contact.Tag.Byte.Value == Settings.Default.AdminByteTag)
            {
                if (!AppState.Instance.IsAdminTagPresent)
                {
                    _panelContainer.Orientation = Math.Round(e.Contact.GetOrientation(this) / 90) * 90;
                    if (!SurfaceEnvironment.IsRunningInSimulator)
                    {
                        _panelContainer.Orientation -= 90;
                    }

                    _panelContainer.Center = new Point(ActualWidth / 2, ActualHeight / 2);
                    _panel.Setup();
                    Audio.Instance.PlayCue("adminPanel_open");
                    AppState.Instance.IsAdminTagPresent = true;
                }

                BeginIdleTimeout();
            }
        }

        #region IsAdminTagPresent

        /// <summary>
        /// Gets or sets a value indicating whether the admin panel should be shown.
        /// </summary>
        /// <value>
        /// <c>true</c> if the admin panel should be shown; otherwise, <c>false</c>.
        /// </value>
        public bool IsAdminTagPresent
        {
            get { return (bool)GetValue(IsAdminTagPresentProperty); }
            set { SetValue(IsAdminTagPresentProperty, value); }
        }

        /// <summary>
        /// Identifies the IsAdminTagPresent dependency property.
        /// </summary>
        public static readonly DependencyProperty IsAdminTagPresentProperty = DependencyProperty.Register("IsAdminTagPresent", typeof(bool), typeof(AdminLayer), new PropertyMetadata(false, (sender, e) => (sender as AdminLayer).UpdateIsAdminTagPresent()));

        /// <summary>
        /// Shows or hides the admin panel.
        /// </summary>
        private void UpdateIsAdminTagPresent()
        {
            (Resources[IsAdminTagPresent ? "ShowAdminPanel" : "HideAdminPanel"] as Storyboard).Begin(this);
        }

        #endregion

        /// <summary>
        /// Cancels the idle timeout.
        /// </summary>
        private void CancelIdleTimeout()
        {
            _idleTimer.Stop();
            River.SetTimeoutDelay(_panelContainer, TimeSpan.Zero);
        }

        /// <summary>
        /// Begins the idle timeout.
        /// </summary>
        private void BeginIdleTimeout()
        {
            CancelIdleTimeout();
            _idleTimer.Start();
            River.SetTimeoutDelay(_panelContainer, Settings.Default.AdminTimeoutDelay);
        }

        /// <summary>
        /// When the idle timer ticks, close the admin panel.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void IdleTimer_Tick(object sender, EventArgs e)
        {
            CancelIdleTimeout();
            AppState.Instance.IsAdminTagPresent = false;
        }

        /// <summary>
        /// Cancel and restart the idle timer when child controls capture contacts.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private void AdminPanel_IsAnyContactCapturedWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (_panelContainer.IsAnyContactCapturedWithin)
            {
                CancelIdleTimeout();
            }
            else
            {
                BeginIdleTimeout();
            }
        }
    }
}