From 9891bd3695d7b268a3bd13e4ad8131eb20b8bc00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AC=E5=BD=A6=E8=B5=A4=E5=B1=8B=E5=85=88?= Date: Tue, 15 Oct 2024 00:24:53 +0900 Subject: [PATCH] Camera preview tweaks --- Amethyst/MVVM/PluginHost.cs | 8 +-- Amethyst/Pages/General.xaml | 38 +++++++++------ Amethyst/Pages/General.xaml.cs | 89 +++++++++++++++++++++------------- Amethyst/Pages/Settings.xaml | 2 +- 4 files changed, 83 insertions(+), 54 deletions(-) diff --git a/Amethyst/MVVM/PluginHost.cs b/Amethyst/MVVM/PluginHost.cs index 55ce143..abb3250 100644 --- a/Amethyst/MVVM/PluginHost.cs +++ b/Amethyst/MVVM/PluginHost.cs @@ -430,14 +430,16 @@ public class LoadAttemptedPlugin : INotifyPropertyChanged public (LocalisationFileJson Root, string Directory) LocalizationResourcesRoot { get; set; } - public Uri DependencyLinkUri => Uri.TryCreate(DependencyLink, UriKind.RelativeOrAbsolute, out var uri) ? uri : + public Uri DependencyLinkUri => + !string.IsNullOrEmpty(DependencyLink) && Uri.TryCreate(DependencyLink, UriKind.RelativeOrAbsolute, out var uri) ? uri : Uri.TryCreate("https://k2vr.tech", UriKind.RelativeOrAbsolute, out var uri1) ? uri1 : null; public Uri DependencySourceUri => - Uri.TryCreate(DependencySource, UriKind.RelativeOrAbsolute, out var uri) ? uri : + !string.IsNullOrEmpty(DependencySource) && Uri.TryCreate(DependencySource, UriKind.RelativeOrAbsolute, out var uri) ? uri : Uri.TryCreate("https://k2vr.tech", UriKind.RelativeOrAbsolute, out var uri1) ? uri1 : null; - public Uri WebsiteUri => Uri.TryCreate(Website, UriKind.RelativeOrAbsolute, out var uri) ? uri : + public Uri WebsiteUri => + !string.IsNullOrEmpty(Website) && Uri.TryCreate(Website, UriKind.RelativeOrAbsolute, out var uri) ? uri : Uri.TryCreate("https://k2vr.tech", UriKind.RelativeOrAbsolute, out var uri1) ? uri1 : null; public Version Version { get; init; } = new("0.0.0.0"); diff --git a/Amethyst/Pages/General.xaml b/Amethyst/Pages/General.xaml index 5ad79e2..d2076f6 100644 --- a/Amethyst/Pages/General.xaml +++ b/Amethyst/Pages/General.xaml @@ -10,7 +10,8 @@ xmlns:mvvm="using:Amethyst.MVVM" xmlns:classes="using:Amethyst.Classes" xmlns:local="using:Amethyst.Pages" xmlns:util="using:Amethyst.Utils" xmlns:media="using:CommunityToolkit.WinUI.UI.Media" - Loaded="Page_Loaded" NavigationCacheMode="Required" + Loaded="Page_Loaded" Unloaded="Page_OnUnloaded" + NavigationCacheMode="Required" mc:Ignorable="d" Background="Transparent"> @@ -1583,8 +1584,9 @@ - + + @@ -1603,22 +1605,28 @@ - - + + - + - - - - + + + - - + + + + + + + + + diff --git a/Amethyst/Pages/General.xaml.cs b/Amethyst/Pages/General.xaml.cs index 8c9b7e3..ba2042b 100644 --- a/Amethyst/Pages/General.xaml.cs +++ b/Amethyst/Pages/General.xaml.cs @@ -39,6 +39,7 @@ public sealed partial class General : Page, INotifyPropertyChanged { private static string _calibratingDeviceGuid = ""; private bool _autoCalibrationStillPending; + private bool _allowCameraPreviewHandling; private bool _calibrationPending; private bool _generalPageLoadedOnce; @@ -48,8 +49,6 @@ public sealed partial class General : Page, INotifyPropertyChanged private int _previousOffsetPageIndex; private bool _showSkeletonPrevious = true; - private bool _showCameraPrevious = false; - private bool _skeletonDrawingCanvassLoadedOnce; public General() @@ -172,6 +171,10 @@ private void Page_Loaded(object sender, RoutedEventArgs e) // Execute the handler Page_LoadedHandler(); + _allowCameraPreviewHandling = true; + AppPlugins.BaseTrackingDevice.IsCameraEnabled = + AppData.Settings.CameraPreviewEnabled; + // Mark as loaded _generalPageLoadedOnce = true; } @@ -411,10 +414,6 @@ private async void TrackingDeviceTreeView_ItemInvoked(TreeView sender, TreeViewI AppData.Settings.SkeletonPreviewEnabled = true; // Change to show SetSkeletonVisibility(true); // Change to show - _showCameraPrevious = AppData.Settings.CameraPreviewEnabled; // Back up - AppData.Settings.CameraPreviewEnabled = true; // Change to show - SetCameraEnabled(true); // Change to show - // Play a sound AppSounds.PlayAppSound(AppSounds.AppSoundType.Show); @@ -605,10 +604,7 @@ private async void StartAutoCalibrationButton_Click(object sender, RoutedEventAr _autoCalibrationStillPending = false; AppData.Settings.SkeletonPreviewEnabled = _showSkeletonPrevious; // Change to whatever - AppData.Settings.CameraPreviewEnabled = _showCameraPrevious; // Change to whatever - SetSkeletonVisibility(_showSkeletonPrevious); // Change to whatever - SetCameraEnabled(_showCameraPrevious); // Change to whatever } private void CalibrationPointsNumberBox_ValueChanged(NumberBox sender, NumberBoxValueChangedEventArgs args) @@ -650,10 +646,7 @@ private void DiscardCalibrationButton_Click(object sender, RoutedEventArgs e) NoSkeletonTextNotice.Text = Interfacing.LocalizedJsonString("/GeneralPage/Captions/Preview/NoSkeletonText"); AppData.Settings.SkeletonPreviewEnabled = _showSkeletonPrevious; // Change to whatever - AppData.Settings.CameraPreviewEnabled = _showCameraPrevious; // Change to whatever - SetSkeletonVisibility(_showSkeletonPrevious); // Change to whatever - SetCameraEnabled(_showCameraPrevious); // Change to whatever } // Begin abort else @@ -734,10 +727,6 @@ private async void CalibrationButton_Click(object sender, RoutedEventArgs e) AppData.Settings.SkeletonPreviewEnabled = true; // Change to show SetSkeletonVisibility(true); // Change to show - _showCameraPrevious = AppData.Settings.CameraPreviewEnabled; // Back up - AppData.Settings.CameraPreviewEnabled = true; // Change to show - SetCameraEnabled(true); // Change to show - // Play a sound AppSounds.PlayAppSound(AppSounds.AppSoundType.Show); @@ -802,10 +791,6 @@ private async void CalibrationButton_Click(object sender, RoutedEventArgs e) AppData.Settings.SkeletonPreviewEnabled = true; // Change to show SetSkeletonVisibility(true); // Change to show - _showCameraPrevious = AppData.Settings.CameraPreviewEnabled; // Back up - AppData.Settings.CameraPreviewEnabled = true; // Change to show - SetCameraEnabled(true); // Change to show - // Play a sound AppSounds.PlayAppSound(AppSounds.AppSoundType.Show); } @@ -1158,10 +1143,6 @@ private void SkeletonDrawingCanvas_Loaded(object sender, RoutedEventArgs e) Canvas.SetTop(SkeletonDrawingCanvas, 0); } - // Reset camera source if currently in use - if (AppData.Settings.CameraPreviewEnabled) - CameraImage.Source = AppPlugins.BaseTrackingDevice.CameraImage; - // If we've disabled the preview if (!AppData.Settings.SkeletonPreviewEnabled) { @@ -1169,6 +1150,19 @@ private void SkeletonDrawingCanvas_Loaded(object sender, RoutedEventArgs e) (SkeletonDrawingCanvas.Opacity, TrackingStateLabelsPanel.Opacity) = (0, 0); (NoSkeletonNotice.Opacity, OutOfFocusNotice.Opacity) = (0, 0); (DashboardClosedNotice.Opacity, PreviewDisabledNotice.Opacity) = (0, 1); + + // Show/hide the camera preview if it's the only thing active + if (AppPlugins.CurrentServiceEndpoint.IsAmethystVisible && windowActive) + { + (CameraImage.Opacity, AppPlugins.BaseTrackingDevice.IsCameraEnabled) = + (AppData.Settings.CameraPreviewEnabled ? 1 : 0, AppData.Settings.CameraPreviewEnabled); + HandleCameraImagePreview(); // Refresh + } + else + { + (CameraImage.Opacity, AppPlugins.BaseTrackingDevice.IsCameraEnabled) = (0, false); + } + return; // Nothing more to do anyway } @@ -1182,7 +1176,7 @@ private void SkeletonDrawingCanvas_Loaded(object sender, RoutedEventArgs e) (SkeletonDrawingCanvas.Opacity, TrackingStateLabelsPanel.Opacity) = (0, 0); (NoSkeletonNotice.Opacity, OutOfFocusNotice.Opacity) = (0, 0); (DashboardClosedNotice.Opacity, PreviewDisabledNotice.Opacity) = (1, 0); - CameraImage.Opacity = 0; + (CameraImage.Opacity, AppPlugins.BaseTrackingDevice.IsCameraEnabled) = (0, false); return; // Nothing more to do anyway } @@ -1193,11 +1187,14 @@ private void SkeletonDrawingCanvas_Loaded(object sender, RoutedEventArgs e) (SkeletonDrawingCanvas.Opacity, TrackingStateLabelsPanel.Opacity) = (0, 0); (NoSkeletonNotice.Opacity, OutOfFocusNotice.Opacity) = (0, 1); (DashboardClosedNotice.Opacity, PreviewDisabledNotice.Opacity) = (0, 0); - CameraImage.Opacity = 0; + (CameraImage.Opacity, AppPlugins.BaseTrackingDevice.IsCameraEnabled) = (0, false); return; // Nothing more to do anyway } } + // Finally handle everything + HandleCameraImagePreview(); + // Else hide the notices (DashboardClosedNotice.Opacity, PreviewDisabledNotice.Opacity, OutOfFocusNotice.Opacity) = (0, 0, 0); // Only these for now @@ -1370,6 +1367,21 @@ private void SkeletonToggleButton_Click(SplitButton sender, SplitButtonClickEven SkeletonToggleButton.IsChecked ? 1.0 : 0.5; } + private void HandleCameraImagePreview() + { + if (_allowCameraPreviewHandling && AppData.Settings.CameraPreviewEnabled) + { + // Reset camera source if currently in use + AppPlugins.BaseTrackingDevice.IsCameraEnabled = true; + CameraImage.Source = AppPlugins.BaseTrackingDevice.CameraImage; + } + else + { + // Disable the requirement to save resources + AppPlugins.BaseTrackingDevice.IsCameraEnabled = false; + } + } + private void CameraToggleButton_Click(object sender, object args) { // Don't even care if we're not set up yet @@ -1882,10 +1894,10 @@ private void Button_ClickSound(object sender, RoutedEventArgs e) AppSounds.PlayAppSound(AppSounds.AppSoundType.Invoke); } - private void SetSkeletonVisibility(bool visibility) + private void SetSkeletonVisibility(bool visibility, bool force = false) { // Don't even care if we're not set up yet - if (!Shared.General.GeneralTabSetupFinished) return; + if (!Shared.General.GeneralTabSetupFinished && !force) return; Shared.General.ForceRenderCheckBox.IsEnabled = visibility; Shared.General.SkeletonToggleButton.IsChecked = visibility; @@ -1894,18 +1906,19 @@ private void SetSkeletonVisibility(bool visibility) visibility ? "/GeneralPage/Buttons/Skeleton/Hide" : "/GeneralPage/Buttons/Skeleton/Show"); } - private void SetCameraEnabled(bool visibility) + private void SetCameraEnabled(bool visibility, bool force = false) { // Don't even care if we're not set up yet - if (!Shared.General.GeneralTabSetupFinished) return; + if (!Shared.General.GeneralTabSetupFinished && !force) return; CameraToggleButton.IsChecked = visibility; + AppPlugins.BaseTrackingDevice.IsCameraEnabled = visibility; } - private void SetSkeletonForce(bool visibility) + private void SetSkeletonForce(bool visibility, bool force = false) { // Don't even care if we're not set up yet - if (!Shared.General.GeneralTabSetupFinished) return; + if (!Shared.General.GeneralTabSetupFinished && !force) return; Shared.General.ForceRenderCheckBox.IsChecked = visibility; } @@ -1953,9 +1966,9 @@ public void OnPropertyChanged(string propName = null) Shared.General.GeneralTabSetupFinished = false; // Setup the preview button - SetSkeletonVisibility(AppData.Settings.SkeletonPreviewEnabled); - SetCameraEnabled(AppData.Settings.CameraPreviewEnabled); - SetSkeletonForce(AppData.Settings.ForceSkeletonPreview); + SetSkeletonVisibility(AppData.Settings.SkeletonPreviewEnabled, true); + SetCameraEnabled(AppData.Settings.CameraPreviewEnabled, true); + SetSkeletonForce(AppData.Settings.ForceSkeletonPreview, true); // Setup the freeze button ToggleFreezeButton.IsChecked = Interfacing.IsTrackingFrozen; @@ -1990,6 +2003,12 @@ public void OnPropertyChanged(string propName = null) // Setup end Shared.General.GeneralTabSetupFinished = true; } + + private void Page_OnUnloaded(object sender, RoutedEventArgs e) + { + _allowCameraPreviewHandling = false; + AppPlugins.BaseTrackingDevice.IsCameraEnabled = false; + } } public class AttachedString : DependencyObject diff --git a/Amethyst/Pages/Settings.xaml b/Amethyst/Pages/Settings.xaml index 6dfa000..a4dae4d 100644 --- a/Amethyst/Pages/Settings.xaml +++ b/Amethyst/Pages/Settings.xaml @@ -598,7 +598,7 @@ Margin="0,2,15,0" Grid.Row="0">