Skip to content

Commit

Permalink
Camera preview tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
KimihikoAkayasaki committed Oct 14, 2024
1 parent 85d2809 commit 9891bd3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 54 deletions.
8 changes: 5 additions & 3 deletions Amethyst/MVVM/PluginHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
38 changes: 23 additions & 15 deletions Amethyst/Pages/General.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<Page.Resources>
Expand Down Expand Up @@ -1583,8 +1584,9 @@
</Grid.OpacityTransition>

<Grid.Background>
<media:BackdropBlurBrush Amount="25.0"/>
<media:BackdropBlurBrush Amount="25.0" />
</Grid.Background>

<StackPanel Opacity="1.0" HorizontalAlignment="Center" VerticalAlignment="Center"
Margin="0,-10,0,0">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
Expand All @@ -1603,22 +1605,28 @@
<!--Skeleton Disabled-->
<Viewbox Grid.Column="1" HorizontalAlignment="Stretch" Margin="20"
VerticalAlignment="Stretch" Stretch="Uniform" StretchDirection="DownOnly">
<StackPanel Opacity="0.0" HorizontalAlignment="Center" VerticalAlignment="Center"
x:Name="PreviewDisabledNotice" Margin="0,-10,0,0">
<StackPanel.OpacityTransition>
<Grid Padding="35, 20,35,23" CornerRadius="25" x:Name="PreviewDisabledNotice">
<Grid.OpacityTransition>
<ScalarTransition />
</StackPanel.OpacityTransition>
</Grid.OpacityTransition>

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<FontIcon Glyph="&#xE946;" FontSize="45" FontWeight="Bold" Foreground="White" />
<TextBlock Margin="10,0,0,7" FontSize="50" FontWeight="SemiBold" Foreground="White"
Text="{x:Bind util:Translator.Get.String('/GeneralPage/Captions/Preview/Disabled'), Mode=OneWay}" />
</StackPanel>
<Grid.Background>
<media:BackdropBlurBrush Amount="25.0" />
</Grid.Background>

<TextBlock Margin="0,-5,0,0" FontSize="25" FontWeight="Medium" HorizontalAlignment="Center"
Foreground="White" HorizontalTextAlignment="Center"
Text="{x:Bind util:Translator.Get.String('/GeneralPage/Captions/Preview/DisabledText'), Mode=OneWay}" />
</StackPanel>
<StackPanel Opacity="1.0" HorizontalAlignment="Center" VerticalAlignment="Center"
Margin="0,-10,0,0">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<FontIcon Glyph="&#xE946;" FontSize="45" FontWeight="Bold" Foreground="White" />
<TextBlock Margin="10,0,0,7" FontSize="50" FontWeight="SemiBold" Foreground="White"
Text="{x:Bind util:Translator.Get.String('/GeneralPage/Captions/Preview/Disabled'), Mode=OneWay}" />
</StackPanel>

<TextBlock Margin="0,-5,0,0" FontSize="25" FontWeight="Medium" HorizontalAlignment="Center"
Foreground="White" HorizontalTextAlignment="Center"
Text="{x:Bind util:Translator.Get.String('/GeneralPage/Captions/Preview/DisabledText'), Mode=OneWay}" />
</StackPanel>
</Grid>
</Viewbox>

<!--Outta focus-->
Expand Down
89 changes: 54 additions & 35 deletions Amethyst/Pages/General.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -1158,17 +1143,26 @@ 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)
{
// Hide the UI, only show that viewing is disabled
(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
}

Expand All @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Amethyst/Pages/Settings.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@
Margin="0,2,15,0" Grid.Row="0">
<TextBlock HorizontalAlignment="Right" VerticalAlignment="Center"
Text="{x:Bind RestartServiceNoteL1, Mode=OneWay}"
FontWeight="SemiBold"
FontWeight="SemiBold"
Margin="0,0,0,0" Foreground="{ThemeResource ControlDisplayTextBrush}" />
<TextBlock HorizontalAlignment="Right" VerticalAlignment="Center"
Text="{x:Bind RestartServiceNoteL2, Mode=OneWay}"
Expand Down

0 comments on commit 9891bd3

Please sign in to comment.