From e97f570f9b6c2ef92ac526612ea5ad16a8765df2 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Tue, 19 Jan 2016 16:30:16 +0100 Subject: [PATCH] Added taking of photos --- YJMPD-UWP/App.xaml.cs | 10 +++- .../EventArgs/GamePlayersUpdatedEventArgs.cs | 7 +-- .../Helpers/EventArgs/PhotoTakenEventArgs.cs | 14 +++++ YJMPD-UWP/Helpers/Settings.cs | 4 +- YJMPD-UWP/Helpers/Util.cs | 2 - YJMPD-UWP/MainPage.xaml.cs | 1 - YJMPD-UWP/Model/GameHandler.cs | 15 +++-- YJMPD-UWP/Model/NetworkHandler.cs | 2 +- YJMPD-UWP/Model/Object/Player.cs | 8 +-- YJMPD-UWP/Model/Object/Statistics.cs | 6 +- YJMPD-UWP/Model/PhotoHandler.cs | 55 ++++++++++++++++++- YJMPD-UWP/ViewModels/GameVM.cs | 8 +-- YJMPD-UWP/ViewModels/PhotoVM.cs | 51 +++++++++++++++-- YJMPD-UWP/ViewModels/ScoreVM.cs | 5 +- YJMPD-UWP/Views/GameView.xaml.cs | 15 +---- YJMPD-UWP/Views/PhotoView.xaml | 15 ++++- YJMPD-UWP/Views/PhotoView.xaml.cs | 19 +++---- YJMPD-UWP/Views/ScoreView.xaml.cs | 11 ---- YJMPD-UWP/Views/WaitingView.xaml.cs | 25 +++------ YJMPD-UWP/YJMPD-UWP.csproj | 1 + 20 files changed, 167 insertions(+), 107 deletions(-) create mode 100644 YJMPD-UWP/Helpers/EventArgs/PhotoTakenEventArgs.cs diff --git a/YJMPD-UWP/App.xaml.cs b/YJMPD-UWP/App.xaml.cs index c070221..ea3df7a 100644 --- a/YJMPD-UWP/App.xaml.cs +++ b/YJMPD-UWP/App.xaml.cs @@ -9,7 +9,6 @@ using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; -using YJMPD_UWP.Helpers; namespace YJMPD_UWP { @@ -59,6 +58,15 @@ namespace YJMPD_UWP } } + private static PhotoHandler photohandler = new PhotoHandler(); + + public static PhotoHandler Photo + { + get + { + return photohandler; + } + } // ========================= // STATIC HELPER FUNCTIONS diff --git a/YJMPD-UWP/Helpers/EventArgs/GamePlayersUpdatedEventArgs.cs b/YJMPD-UWP/Helpers/EventArgs/GamePlayersUpdatedEventArgs.cs index 8a38b82..2aaff9f 100644 --- a/YJMPD-UWP/Helpers/EventArgs/GamePlayersUpdatedEventArgs.cs +++ b/YJMPD-UWP/Helpers/EventArgs/GamePlayersUpdatedEventArgs.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using YJMPD_UWP.Model.Object; +using YJMPD_UWP.Model.Object; namespace YJMPD_UWP.Helpers.EventArgs { diff --git a/YJMPD-UWP/Helpers/EventArgs/PhotoTakenEventArgs.cs b/YJMPD-UWP/Helpers/EventArgs/PhotoTakenEventArgs.cs new file mode 100644 index 0000000..861df96 --- /dev/null +++ b/YJMPD-UWP/Helpers/EventArgs/PhotoTakenEventArgs.cs @@ -0,0 +1,14 @@ +using Windows.UI.Xaml.Media.Imaging; + +namespace YJMPD_UWP.Helpers.EventArgs +{ + public class PhotoTakenEventArgs : System.EventArgs + { + public SoftwareBitmapSource Photo { get; private set; } + + public PhotoTakenEventArgs(SoftwareBitmapSource photo) + { + Photo = photo; + } + } +} diff --git a/YJMPD-UWP/Helpers/Settings.cs b/YJMPD-UWP/Helpers/Settings.cs index bb490a6..9b9d08a 100644 --- a/YJMPD-UWP/Helpers/Settings.cs +++ b/YJMPD-UWP/Helpers/Settings.cs @@ -1,6 +1,4 @@ -using System; -using System.Diagnostics; -using Windows.Foundation.Collections; +using Windows.Foundation.Collections; using Windows.Storage; using YJMPD_UWP.Model.Object; diff --git a/YJMPD-UWP/Helpers/Util.cs b/YJMPD-UWP/Helpers/Util.cs index cad26b9..c5756e1 100644 --- a/YJMPD-UWP/Helpers/Util.cs +++ b/YJMPD-UWP/Helpers/Util.cs @@ -6,10 +6,8 @@ using System.Threading.Tasks; using Windows.Data.Xml.Dom; using Windows.Devices.Geolocation; using Windows.Services.Maps; -using Windows.UI; using Windows.UI.Notifications; using Windows.UI.Popups; -using Windows.UI.Xaml.Controls.Maps; namespace YJMPD_UWP.Helpers { diff --git a/YJMPD-UWP/MainPage.xaml.cs b/YJMPD-UWP/MainPage.xaml.cs index 38e5833..27c9071 100644 --- a/YJMPD-UWP/MainPage.xaml.cs +++ b/YJMPD-UWP/MainPage.xaml.cs @@ -9,7 +9,6 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Navigation; -using System.Diagnostics; namespace YJMPD_UWP { diff --git a/YJMPD-UWP/Model/GameHandler.cs b/YJMPD-UWP/Model/GameHandler.cs index 8338333..534bbe0 100644 --- a/YJMPD-UWP/Model/GameHandler.cs +++ b/YJMPD-UWP/Model/GameHandler.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Threading.Tasks; -using Windows.Foundation; +using Windows.UI.Xaml.Media.Imaging; using YJMPD_UWP.Helpers; using YJMPD_UWP.Helpers.EventArgs; using YJMPD_UWP.Model.Object; @@ -23,6 +22,8 @@ namespace YJMPD_UWP.Model public List Players { get; private set; } + public bool Selected { get; private set; } + private void UpdateGameStatus(GameStatus status) { Status = status; @@ -95,7 +96,7 @@ namespace YJMPD_UWP.Model { //Do stuff - + /* UpdateGameStatus(GameStatus.SEARCHING); Search(); @@ -108,11 +109,12 @@ namespace YJMPD_UWP.Model Debug.WriteLine("Searching"); Task.Delay(TimeSpan.FromMilliseconds(50)); } - }); + });*/ UpdateGameStatus(GameStatus.WAITING); + await Task.Delay(TimeSpan.FromSeconds(5)); UpdateGameStatus(GameStatus.STARTED); @@ -143,7 +145,10 @@ namespace YJMPD_UWP.Model App.Navigate(typeof(WaitingView)); break; case GameStatus.STARTED: - App.Navigate(typeof(GameView)); + if (Selected && App.Photo.Photo == null) + App.Navigate(typeof(PhotoView)); + else + App.Navigate(typeof(GameView)); break; case GameStatus.ENDED: App.Navigate(typeof(ScoreView)); diff --git a/YJMPD-UWP/Model/NetworkHandler.cs b/YJMPD-UWP/Model/NetworkHandler.cs index 8116205..b1b1263 100644 --- a/YJMPD-UWP/Model/NetworkHandler.cs +++ b/YJMPD-UWP/Model/NetworkHandler.cs @@ -138,7 +138,7 @@ namespace YJMPD_UWP.Model StreamSocketControl controller = client.Control; controller.KeepAlive = true; - await client.ConnectAsync(new HostName("imegumii.space"), "3333"); + await client.ConnectAsync(new HostName(Settings.Values["hostname"] as string), Settings.Values["port"] as string); din = new StreamReader(client.InputStream.AsStreamForRead()); diff --git a/YJMPD-UWP/Model/Object/Player.cs b/YJMPD-UWP/Model/Object/Player.cs index 2e4bea4..01e0b57 100644 --- a/YJMPD-UWP/Model/Object/Player.cs +++ b/YJMPD-UWP/Model/Object/Player.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace YJMPD_UWP.Model.Object +namespace YJMPD_UWP.Model.Object { public class Player { diff --git a/YJMPD-UWP/Model/Object/Statistics.cs b/YJMPD-UWP/Model/Object/Statistics.cs index faaea50..01c7def 100644 --- a/YJMPD-UWP/Model/Object/Statistics.cs +++ b/YJMPD-UWP/Model/Object/Statistics.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; namespace YJMPD_UWP.Model.Object { diff --git a/YJMPD-UWP/Model/PhotoHandler.cs b/YJMPD-UWP/Model/PhotoHandler.cs index c82b34f..cc8526a 100644 --- a/YJMPD-UWP/Model/PhotoHandler.cs +++ b/YJMPD-UWP/Model/PhotoHandler.cs @@ -1,13 +1,62 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Windows.Graphics.Imaging; +using Windows.Media.Capture; +using Windows.Storage; +using Windows.Storage.Streams; +using Windows.UI.Xaml.Media.Imaging; +using YJMPD_UWP.Helpers.EventArgs; namespace YJMPD_UWP.Model { public class PhotoHandler { + public delegate void OnPhotoTakenHandler(object sender, PhotoTakenEventArgs e); + public event OnPhotoTakenHandler OnPhotoTaken; + public SoftwareBitmapSource Photo { get; private set; } + + public PhotoHandler() + { + + } + + private void UpdatePhotoTaken(SoftwareBitmapSource photo) + { + Photo = photo; + + if (OnPhotoTaken == null) return; + + OnPhotoTaken(this, new PhotoTakenEventArgs(photo)); + } + + public async void Take() + { + CameraCaptureUI captureUI = new CameraCaptureUI(); + captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png; + captureUI.PhotoSettings.AllowCropping = false; + captureUI.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.Large3M; + + StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo); + + if (photo == null) + { + // User cancelled photo capture + return; + } + + IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read); + BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); + SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); + + SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap, + BitmapPixelFormat.Bgra8, + BitmapAlphaMode.Premultiplied); + + SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource(); + await bitmapSource.SetBitmapAsync(softwareBitmapBGR8); + + UpdatePhotoTaken(bitmapSource); + } } } diff --git a/YJMPD-UWP/ViewModels/GameVM.cs b/YJMPD-UWP/ViewModels/GameVM.cs index 7b2fdb3..8e7fbf4 100644 --- a/YJMPD-UWP/ViewModels/GameVM.cs +++ b/YJMPD-UWP/ViewModels/GameVM.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace YJMPD_UWP.ViewModels +namespace YJMPD_UWP.ViewModels { public class GameVM : TemplateVM { diff --git a/YJMPD-UWP/ViewModels/PhotoVM.cs b/YJMPD-UWP/ViewModels/PhotoVM.cs index 5838103..583a089 100644 --- a/YJMPD-UWP/ViewModels/PhotoVM.cs +++ b/YJMPD-UWP/ViewModels/PhotoVM.cs @@ -1,16 +1,57 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Windows.UI.Xaml.Media; namespace YJMPD_UWP.ViewModels { public class PhotoVM : TemplateVM { + private ImageSource photo; + public PhotoVM() : base("Photo") { + App.Photo.OnPhotoTaken += Photo_OnPhotoTaken; + } + private void Photo_OnPhotoTaken(object sender, Helpers.EventArgs.PhotoTakenEventArgs e) + { + dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => + { + photo = e.Photo; + NotifyPropertyChanged(nameof(Photo)); + NotifyPropertyChanged(nameof(PhotoVisible)); + NotifyPropertyChanged(nameof(ControlsVisible)); + }); + } + + public bool PhotoVisible + { + get + { + return photo != null; + } + } + + public bool ControlsVisible + { + get + { + return !PhotoVisible; + } + } + + public string TimeOut + { + get + { + return "Take a photo within 1 minute"; + } + } + + public ImageSource Photo + { + get + { + return photo; + } } } } diff --git a/YJMPD-UWP/ViewModels/ScoreVM.cs b/YJMPD-UWP/ViewModels/ScoreVM.cs index 1cb0442..a4c8286 100644 --- a/YJMPD-UWP/ViewModels/ScoreVM.cs +++ b/YJMPD-UWP/ViewModels/ScoreVM.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using YJMPD_UWP.Model.Object; namespace YJMPD_UWP.ViewModels diff --git a/YJMPD-UWP/Views/GameView.xaml.cs b/YJMPD-UWP/Views/GameView.xaml.cs index 35f8783..0101d7c 100644 --- a/YJMPD-UWP/Views/GameView.xaml.cs +++ b/YJMPD-UWP/Views/GameView.xaml.cs @@ -1,17 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.Foundation; -using Windows.Foundation.Collections; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.UI.Xaml.Data; -using Windows.UI.Xaml.Input; -using Windows.UI.Xaml.Media; -using Windows.UI.Xaml.Navigation; +using Windows.UI.Xaml.Controls; using YJMPD_UWP.ViewModels; namespace YJMPD_UWP.Views diff --git a/YJMPD-UWP/Views/PhotoView.xaml b/YJMPD-UWP/Views/PhotoView.xaml index 0469134..08dfff9 100644 --- a/YJMPD-UWP/Views/PhotoView.xaml +++ b/YJMPD-UWP/Views/PhotoView.xaml @@ -3,13 +3,22 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:YJMPD_UWP.Views" + xmlns:convert="using:YJMPD_UWP.Helpers.Converter" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> + + + + - - - + + + + +