Skip to content

Commit

Permalink
Add speech info window to see the speech recognition result in game.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyfish committed Jun 26, 2024
1 parent 114bfd3 commit 45ca993
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 11 deletions.
25 changes: 25 additions & 0 deletions InfoWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Window x:Class="HellDivers2OneKeyStratagem.InfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"

xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
ui:WindowHelper.UseModernWindowStyle="True"
ui:WindowHelper.SystemBackdropType="Mica"

WindowStartupLocation="CenterScreen"
SizeToContent="WidthAndHeight"
Topmost="True"
ResizeMode="NoResize"

Title="HellDivers2 一键战略信息"

Closed="InfoWindow_OnClosed">

<StackPanel Margin="20,10,20,10">
<Label Name="InfoLabel" Content="暂无信息" />
</StackPanel>

</Window>
23 changes: 23 additions & 0 deletions InfoWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace HellDivers2OneKeyStratagem;

public partial class InfoWindow()
{
private readonly MainWindow _mainWindow = null!;

public InfoWindow(MainWindow mainWindow): this()
{
_mainWindow = mainWindow;

InitializeComponent();
}

public void SetInfo(string info)
{
InfoLabel.Content = info;
}

private void InfoWindow_OnClosed(object? sender, EventArgs e)
{
_mainWindow.NotifyInfoWindowClosed();
}
}
11 changes: 9 additions & 2 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
SizeToContent="WidthAndHeight"
ResizeMode="CanMinimize"

Loaded="Window_Loaded"
Deactivated="Window_Deactivated">
Initialized="MainWindow_OnInitialized"
Deactivated="Window_Deactivated"
Closed="MainWindow_OnClosed">

<ui:SimpleStackPanel Margin="20,0,20,20">

Expand Down Expand Up @@ -133,6 +134,12 @@
</ui:SimpleStackPanel>
</Expander>

<local:MyHorizontalBlock Margin="0, 10, 0, 0">
<CheckBox Name="ShowSpeechInfoWindowCheckBox" Content="显示语音信息窗口"
Checked="ShowSpeechInfoWindowCheckBox_OnChecked"
Unchecked="ShowSpeechInfoWindowCheckBox_OnUnchecked" />
</local:MyHorizontalBlock>

<ui:SimpleStackPanel x:Name="HotKeysStackPanel" Margin="0, 10, 0, 0">

<local:MyHorizontalStackPanel>
Expand Down
59 changes: 50 additions & 9 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using EdgeTTS;
using iNKORE.UI.WPF.Modern.Controls;
using NAudio.Wave;
using NHotkey;
using System.Diagnostics;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using EdgeTTS;
using iNKORE.UI.WPF.Modern.Controls;
using NAudio.Wave;
using NHotkey;
using Brushes = System.Windows.Media.Brushes;
using CheckBox = System.Windows.Controls.CheckBox;
using HorizontalAlignment = System.Windows.HorizontalAlignment;
Expand Down Expand Up @@ -40,10 +40,9 @@ private bool IsLoading
}
}

private async void Window_Loaded(object sender, RoutedEventArgs e)
private async void MainWindow_OnInitialized(object? sender, EventArgs e)
{
IsLoading = true;
BeginInit();

try
{
Expand All @@ -56,7 +55,6 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
}
finally
{
EndInit();
IsLoading = false;
}

Expand Down Expand Up @@ -644,7 +642,9 @@ private async Task StartSpeechTrigger()
_voiceCommand.CommandRecognized += (_, command) =>
{
var failed = command.Score < Settings.VoiceConfidence;
VoiceRecognizeResultLabel.Content = $@"【{(failed ? "失败" : "成功")}】识别阈值:{command.Score:F3} 识别文字:{command.Text} 当前程序:{ActiveWindowMonitor.CurrentProcessFileName}";
var info = $"【{(failed ? "失败" : "成功")}】识别阈值:{command.Score:F3} 识别文字:{command.Text}";
VoiceRecognizeResultLabel.Content = info;
_infoWindow?.SetInfo(info);

if (failed)
return;
Expand Down Expand Up @@ -823,6 +823,7 @@ private void RefreshAfterEnableSpeechTriggerChanged()
SpeechSubSettingsContainer.Visibility = Settings.EnableSpeechTrigger ? Visibility.Visible : Visibility.Collapsed;
EnableSetKeyBySpeechCheckBox.IsEnabled = Settings.EnableSpeechTrigger;
StratagemsContainer.Visibility = Settings.EnableSpeechTrigger || Settings.EnableHotkeyTrigger ? Visibility.Visible : Visibility.Collapsed;
ShowSpeechInfoWindowCheckBox.Visibility = Settings.EnableSpeechTrigger ? Visibility.Visible : Visibility.Collapsed;
}

private void OpenSpeechRecognitionControlPanelButton_OnClick(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -1113,4 +1114,44 @@ private void GenerateTxtButton_Click(object sender, RoutedEventArgs e)
File.WriteAllText(Path.Combine(folder, stratagem.Name), stratagem.Name);
GenerateVoiceMessageLabel.Content = @"txt 生成完毕";
}

private InfoWindow? _infoWindow;

private void ShowInfoWindow()
{
if (_infoWindow != null)
return;

_infoWindow = new InfoWindow(this);
_infoWindow.Show();
}

private void HideInfoWindow()
{
_infoWindow?.Close();
_infoWindow = null;
}

public void NotifyInfoWindowClosed()
{
_infoWindow = null;
ShowSpeechInfoWindowCheckBox.IsChecked = false;
}

private void ShowSpeechInfoWindowCheckBox_OnChecked(object sender, RoutedEventArgs e)
{
ShowInfoWindow();
}

private void ShowSpeechInfoWindowCheckBox_OnUnchecked(object sender, RoutedEventArgs e)
{
HideInfoWindow();
}

private async void MainWindow_OnClosed(object? sender, EventArgs e)
{
HideInfoWindow();
HotkeyGroupManager.ClearHotkeyGroup();
await StopSpeechTrigger();
}
}

0 comments on commit 45ca993

Please sign in to comment.