src/FingersDance.Control.TimeLine/SurfaceTimeLineTest.xaml.cs
changeset 10 e8bfe1102e03
equal deleted inserted replaced
9:215a7412e544 10:e8bfe1102e03
       
     1 using System;
       
     2 using System.Collections.Generic;
       
     3 using System.Linq;
       
     4 using System.Text;
       
     5 using System.Windows;
       
     6 using System.Windows.Controls;
       
     7 using System.Windows.Data;
       
     8 using System.Windows.Documents;
       
     9 using System.Windows.Input;
       
    10 using System.Windows.Media;
       
    11 using System.Windows.Media.Imaging;
       
    12 using System.Windows.Shapes;
       
    13 using System.Windows.Threading;
       
    14 using Microsoft.Surface;
       
    15 using Microsoft.Surface.Presentation;
       
    16 using Microsoft.Surface.Presentation.Controls;
       
    17 
       
    18 namespace FingersDance.Control.TimeLine
       
    19 {
       
    20     /// <summary>
       
    21     /// Interaction logic for SurfaceWindow1.xaml
       
    22     /// </summary>
       
    23     public partial class SurfaceTimeLineTest : SurfaceWindow
       
    24     {
       
    25 
       
    26         DispatcherTimer timer;  // handles position of slider and player
       
    27         bool isDragging = false;
       
    28         bool isPlaying = false;
       
    29 
       
    30 
       
    31 
       
    32         /// <summary>
       
    33         /// Default constructor.
       
    34         /// </summary>
       
    35         public SurfaceTimeLineTest()
       
    36         {
       
    37             InitializeComponent();
       
    38 
       
    39             // Add handlers for Application activation events
       
    40             initplayer();
       
    41             AddActivationHandlers();
       
    42         }
       
    43 
       
    44         #region Handlers
       
    45         /// <summary>
       
    46         /// Occurs when the window is about to close. 
       
    47         /// </summary>
       
    48         /// <param name="e"></param>
       
    49         protected override void OnClosed(EventArgs e)
       
    50         {
       
    51             base.OnClosed(e);
       
    52 
       
    53             // Remove handlers for Application activation events
       
    54             RemoveActivationHandlers();
       
    55         }
       
    56 
       
    57         /// <summary>
       
    58         /// Adds handlers for Application activation events.
       
    59         /// </summary>
       
    60         private void AddActivationHandlers()
       
    61         {
       
    62             // Subscribe to surface application activation events
       
    63             ApplicationLauncher.ApplicationActivated += OnApplicationActivated;
       
    64             ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed;
       
    65             ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated;
       
    66         }
       
    67 
       
    68         /// <summary>
       
    69         /// Removes handlers for Application activation events.
       
    70         /// </summary>
       
    71         private void RemoveActivationHandlers()
       
    72         {
       
    73             // Unsubscribe from surface application activation events
       
    74             ApplicationLauncher.ApplicationActivated -= OnApplicationActivated;
       
    75             ApplicationLauncher.ApplicationPreviewed -= OnApplicationPreviewed;
       
    76             ApplicationLauncher.ApplicationDeactivated -= OnApplicationDeactivated;
       
    77         }
       
    78 
       
    79         /// <summary>
       
    80         /// This is called when application has been activated.
       
    81         /// </summary>
       
    82         /// <param name="sender"></param>
       
    83         /// <param name="e"></param>
       
    84         private void OnApplicationActivated(object sender, EventArgs e)
       
    85         {
       
    86             //TODO: enable audio, animations here
       
    87         }
       
    88 
       
    89         /// <summary>
       
    90         /// This is called when application is in preview mode.
       
    91         /// </summary>
       
    92         /// <param name="sender"></param>
       
    93         /// <param name="e"></param>
       
    94         private void OnApplicationPreviewed(object sender, EventArgs e)
       
    95         {
       
    96             //TODO: Disable audio here if it is enabled
       
    97 
       
    98             //TODO: optionally enable animations here
       
    99         }
       
   100 
       
   101         /// <summary>
       
   102         ///  This is called when application has been deactivated.
       
   103         /// </summary>
       
   104         /// <param name="sender"></param>
       
   105         /// <param name="e"></param>
       
   106         private void OnApplicationDeactivated(object sender, EventArgs e)
       
   107         {
       
   108             //TODO: disable audio, animations here
       
   109         }
       
   110         #endregion
       
   111 
       
   112         #region SurfaceWindows events
       
   113 
       
   114         private void SurfaceWindow_Activated(object sender, EventArgs e)
       
   115         {
       
   116             timer = new DispatcherTimer();
       
   117             //timer.Interval = TimeSpan.FromSeconds(1);
       
   118             timer.Interval = TimeSpan.FromMilliseconds(250);
       
   119             timer.Tick += new EventHandler(timer_Tick);
       
   120         }
       
   121         #endregion
       
   122 
       
   123         #region Timer
       
   124         void timer_Tick(object sender, EventArgs e)
       
   125         {
       
   126             //Slider Position
       
   127             labelPosition.Content = sliderBase.Value;
       
   128             //Media Position ToString and TotalMilliseconds
       
   129             labelMediaPositionToString.Content=mediaelement.Position.ToString();
       
   130             labelMediaPositionContent.Content = mediaelement.Position.TotalMilliseconds;
       
   131             //Si le slider n'est pas en train d'etre bougé, on change sa valeur
       
   132             if (!isDragging)
       
   133             {
       
   134                 sliderBase.Value = mediaelement.Position.TotalMilliseconds;
       
   135             }
       
   136 
       
   137         }
       
   138         #endregion
       
   139 
       
   140         #region MediaElement events
       
   141         private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
       
   142         {
       
   143             mediaelement.Stop();
       
   144         }
       
   145 
       
   146         private void mediaelement_MediaOpened(object sender, RoutedEventArgs e)
       
   147         {
       
   148             labelDuration.Content = mediaelement.NaturalDuration.ToString();
       
   149 
       
   150             sliderBase.Maximum = mediaelement.NaturalDuration.TimeSpan.TotalMilliseconds;
       
   151             labelSliderMax.Content = sliderBase.Maximum;
       
   152             //initialisation du timer
       
   153             timer.Start();
       
   154         }
       
   155         #endregion
       
   156 
       
   157         #region Slider
       
   158 
       
   159         private void sliderBase_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
       
   160         {
       
   161             //TODO
       
   162             if (!isDragging)
       
   163             {
       
   164 
       
   165                 int SliderValue = (int)sliderBase.Value;
       
   166 
       
   167                 // Overloaded constructor takes the arguments days, hours, minutes, seconds, miniseconds.
       
   168                 // Create a TimeSpan with miliseconds equal to the slider value.
       
   169                 TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);
       
   170                 mediaelement.Position = ts;
       
   171                 mediaelement.Play();
       
   172             }
       
   173             
       
   174         }
       
   175 
       
   176         private void sliderBase_DragEnter(object sender, DragEventArgs e)
       
   177         {
       
   178             isDragging = true;
       
   179             mediaelement.Pause();
       
   180         }
       
   181 
       
   182         private void sliderBase_DragLeave(object sender, DragEventArgs e)
       
   183         {
       
   184             isDragging = false;
       
   185             //mediaelement.Position = TimeSpan.FromSeconds(sliderBase.Value);
       
   186 
       
   187         }
       
   188 
       
   189         private void sliderBase_ContactEnter(object sender, ContactEventArgs e)
       
   190         {
       
   191             isDragging = true;
       
   192             mediaelement.Pause();
       
   193         }
       
   194 
       
   195         private void sliderBase_ContactLeave(object sender, ContactEventArgs e)
       
   196         {
       
   197             isDragging = false;
       
   198             mediaelement.Position = TimeSpan.FromSeconds(sliderBase.Value);
       
   199         }
       
   200 
       
   201         private void sliderBase_ContactChanged(object sender, ContactEventArgs e)
       
   202         {
       
   203            // isDragging = false;
       
   204             //mediaelement.Position = TimeSpan.FromMilliseconds(sliderBase.Value);
       
   205         }
       
   206         #endregion
       
   207 
       
   208         #region Player
       
   209         private void initplayer()
       
   210         {
       
   211             mediaelement.LoadedBehavior = MediaState.Manual;        
       
   212         }
       
   213 
       
   214         private void butonPlayPause_Click(object sender, RoutedEventArgs e)
       
   215         {
       
   216             if (!isPlaying)//Play
       
   217             {
       
   218                 isPlaying = true;
       
   219                 try
       
   220                 {
       
   221                     mediaelement.Play();
       
   222                 }
       
   223                 catch (Exception ex) { }
       
   224             }
       
   225             else//Pause
       
   226             {
       
   227                 isPlaying = false;
       
   228                 try
       
   229                 {
       
   230                     mediaelement.Pause();
       
   231                 }
       
   232                 catch (Exception exx) { }
       
   233             }
       
   234         }
       
   235         private void SurfaceButton_Click(object sender, RoutedEventArgs e)
       
   236         {
       
   237             mediaelement.Stop();
       
   238         }
       
   239 
       
   240         private void SurfaceButton_ContactDown(object sender, ContactEventArgs e)
       
   241         {
       
   242             mediaelement.Stop();
       
   243         }
       
   244 
       
   245     #endregion
       
   246 
       
   247 
       
   248 
       
   249 
       
   250 
       
   251     }
       
   252         
       
   253 
       
   254 }