diff --git a/Samples/360VideoPlayback/cpp/PlaybackPage.xaml.cpp b/Samples/360VideoPlayback/cpp/PlaybackPage.xaml.cpp index 9ff76f55ac..34da7b016d 100644 --- a/Samples/360VideoPlayback/cpp/PlaybackPage.xaml.cpp +++ b/Samples/360VideoPlayback/cpp/PlaybackPage.xaml.cpp @@ -60,8 +60,6 @@ quaternion CreateFromHeadingPitchRoll(double flHeading, double flPitch, double f PlaybackPage::PlaybackPage() { - m_centerX = 0; - m_centerY = 0; m_pitch = 0; m_heading = 0; m_rotationdelta = 1.8f; @@ -72,8 +70,6 @@ PlaybackPage::PlaybackPage() void PlaybackPage::OnLayoutUpdated(Object^ sender, Object^ args) { - m_centerX = this->VideoElement->ActualWidth / 2; - m_centerY = this->VideoElement->ActualHeight / 2; if (this->m_mtcGrid == nullptr) { FrameworkElement^ transportControlsTemplateRoot = (FrameworkElement^)(VisualTreeHelper::GetChild(this->VideoElement->TransportControls, 0)); @@ -105,6 +101,7 @@ void PlaybackPage::OnPointerPressed(Object^ sender, PointerRoutedEventArgs^ e) if (e->OriginalSource != m_mtcGrid && IsMediaAlreadyOpened()) { m_isPointerPress = true; + m_origin = e->GetCurrentPoint(VideoElement)->Position; } e->Handled = true; } @@ -112,7 +109,7 @@ void PlaybackPage::OnPointerPressed(Object^ sender, PointerRoutedEventArgs^ e) void PlaybackPage::OnPointerReleased(Object^ sender, PointerRoutedEventArgs^ e) { - ResetState(); + m_isPointerPress = false; e->Handled = true; } @@ -132,14 +129,23 @@ void PlaybackPage::OnPointerWheelChanged(Object^ sender, PointerRoutedEventArgs^ } +float PlaybackPage::PixelToAngle(float pixel) const +{ + return float(pixel * videoProjection->HorizontalFieldOfViewInDegrees / VideoElement->ActualWidth); +} + void PlaybackPage::OnPointerMoved(Object^ sender, PointerRoutedEventArgs^ e) { if (e->OriginalSource != m_mtcGrid && m_isPointerPress) { - double ChangeX = e->GetCurrentPoint(this->VideoElement)->Position.X - m_centerX; - double ChangeY = m_centerY - (e->GetCurrentPoint(VideoElement)->Position.Y); - this->videoProjection->ViewOrientation = CreateFromHeadingPitchRoll(ChangeX, ChangeY, 0); - } + auto current = e->GetCurrentPoint(this->VideoElement)->Position; + + m_heading += PixelToAngle(m_origin.X - current.X); + m_pitch += PixelToAngle(current.Y - m_origin.Y); + + this->videoProjection->ViewOrientation = CreateFromHeadingPitchRoll(m_heading, m_pitch, 0); + m_origin = current; + } e->Handled = true; } diff --git a/Samples/360VideoPlayback/cpp/PlaybackPage.xaml.h b/Samples/360VideoPlayback/cpp/PlaybackPage.xaml.h index 1d500ef363..f8c59cef22 100644 --- a/Samples/360VideoPlayback/cpp/PlaybackPage.xaml.h +++ b/Samples/360VideoPlayback/cpp/PlaybackPage.xaml.h @@ -25,8 +25,8 @@ namespace _360VideoPlayback private: Windows::Media::Playback::MediaPlayer^ m_player; - // positions hold the center of the window - double m_centerX = 0, m_centerY = 0; + // positions hold the origin of the movement + Windows::Foundation::Point m_origin; // MTC grid to ignore mouse event on the MTC Panel Windows::UI::Xaml::Controls::Grid^ m_mtcGrid; double m_pitch; @@ -45,6 +45,7 @@ namespace _360VideoPlayback void ResetState(); void StartInputLoop(); + float PixelToAngle(float pixel) const; void OnPointerPressed(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e); Windows::Foundation::EventRegistrationToken OnPointerPressedToken; Windows::Foundation::EventRegistrationToken OnMTCPointerPressedToken; diff --git a/Samples/360VideoPlayback/cs/PlaybackPage.xaml.cs b/Samples/360VideoPlayback/cs/PlaybackPage.xaml.cs index cbdde5a345..fa78ba7668 100644 --- a/Samples/360VideoPlayback/cs/PlaybackPage.xaml.cs +++ b/Samples/360VideoPlayback/cs/PlaybackPage.xaml.cs @@ -23,8 +23,8 @@ namespace _360VideoPlayback public sealed partial class PlaybackPage : Page { private MediaPlayer mediaPlayer; - // positions hold the center of the window - private double centerX = 0, centerY = 0; + // positions hold the start of the movement + private Point origin; // MTC grid to ignore mouse event on the MTC Panel Grid mtcGrid = null; private double pitch = 0, heading = 0; @@ -48,8 +48,6 @@ public PlaybackPage() private void PlaybackPage_LayoutUpdated(object sender, object e) { - centerX = VideoElement.ActualWidth / 2; - centerY = VideoElement.ActualHeight / 2; if (mtcGrid == null) { FrameworkElement transportControlsTemplateRoot = (FrameworkElement)VisualTreeHelper.GetChild(VideoElement.TransportControls, 0); @@ -150,20 +148,25 @@ private void OnKeyDown(CoreWindow sender, KeyEventArgs args) } } + private double PixelToAngle(double pixel) => pixel * videoProjection.HorizontalFieldOfViewInDegrees / VideoElement.ActualWidth; + private void OnPointerMoved(object sender, PointerRoutedEventArgs e) { if (e.OriginalSource != mtcGrid && isPointerPress) { - double ChangeX = e.GetCurrentPoint(VideoElement).Position.X - centerX; - double ChangeY = centerY - e.GetCurrentPoint(VideoElement).Position.Y; - videoProjection.ViewOrientation = Helpers.CreateFromHeadingPitchRoll(ChangeX, ChangeY, 0); + var current = e.GetCurrentPoint(VideoElement).Position; + + heading += PixelToAngle(origin.X - current.X); + pitch += PixelToAngle(current.Y - origin.Y); + videoProjection.ViewOrientation = Helpers.CreateFromHeadingPitchRoll(heading, pitch, 0); + origin = current; } e.Handled = true; } private void OnPointerReleased(object sender, PointerRoutedEventArgs e) { - ResetState(); + isPointerPress = false; e.Handled = true; } @@ -172,6 +175,7 @@ private void OnPointerPressed(object sender, PointerRoutedEventArgs e) if (e.OriginalSource != mtcGrid && IsMediaAlreadyOpened()) { isPointerPress = true; + origin = e.GetCurrentPoint(VideoElement).Position; } e.Handled = true; }