Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

登録するアプリ名と変更できるように修正 #65

Merged
merged 5 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SFVvCommon/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Common
public const string regStyleName = "StyleName";
public const string regPort = "Port";
public const string regAttributes = "Attributes";
public const string regAppName = "AppName";


/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions SFVvCommon/SapiStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public SapiStyle() : base()
/// <param name="styleName">スタイル名</param>
/// <param name="iD">ID</param>
/// <param name="clsid">SAPIエンジンのクラスID</param>
public SapiStyle(string name, string styleName, int iD, int port, Guid clsid) : base(name, styleName, iD, port)
public SapiStyle(string appName, string name, string styleName, int iD, int port, Guid clsid) : base(appName, name, styleName, iD, port)
{
CLSID = clsid;
}
Expand All @@ -30,7 +30,7 @@ public SapiStyle(string name, string styleName, int iD, int port, Guid clsid) :
/// </summary>
/// <param name="voicevoxStyle">VOICEVOXスタイル</param>
/// <param name="clsid">SAPIエンジンのクラスID</param>
public SapiStyle(VoicevoxStyle voicevoxStyle, Guid clsid) : this(voicevoxStyle.Name, voicevoxStyle.StyleName, voicevoxStyle.ID, voicevoxStyle.Port, clsid)
public SapiStyle(VoicevoxStyle voicevoxStyle, Guid clsid) : this(voicevoxStyle.AppName, voicevoxStyle.Name, voicevoxStyle.StyleName, voicevoxStyle.ID, voicevoxStyle.Port, clsid)
{
}

Expand All @@ -41,7 +41,7 @@ public string SpaiName
{
get
{
return "VOICEVOX " + Name + " " + StyleName;
return AppName + " " + Name + " " + StyleName;
}
}

Expand Down
9 changes: 8 additions & 1 deletion SFVvCommon/StyleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@ public class StyleBase
{
public StyleBase()
{
AppName = "VOICEVOX";
Name = "";
StyleName = "";
ID = 0;
Port = 0;
}

public StyleBase(string name, string styleName, int iD, int port)
public StyleBase(string appName, string name, string styleName, int iD, int port)
{
AppName = appName ?? throw new ArgumentNullException(nameof(appName));
Name = name ?? throw new ArgumentNullException(nameof(name));
StyleName = styleName ?? throw new ArgumentNullException(nameof(styleName));
ID = iD;
Port = port;
}

/// <summary>
/// アプリ名
/// </summary>
public string AppName { get; set; }

/// <summary>
/// 話者名
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion SFVvCommon/VoicevoxStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class VoicevoxStyle : StyleBase
/// <param name="name"></param>
/// <param name="styleName"></param>
/// <param name="iD"></param>
public VoicevoxStyle(string name, string styleName, int iD, int port) : base(name, styleName, iD, port)
public VoicevoxStyle(string appName, string name, string styleName, int iD, int port) : base(appName, name, styleName, iD, port)
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion Setting/View/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void AddTabControl()
string styleName = (string)tokenKey.GetValue(Common.regStyleName);
int id = (int)tokenKey.GetValue(Common.regSpeakerNumber, 0);
int port = (int)tokenKey.GetValue(Common.regPort, 50021);
styles.Add(new VoicevoxStyle(name, styleName, id, port));
styles.Add(new VoicevoxStyle("VOICEVOX", name, styleName, id, port));
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions StyleRegistrationTool/Model/NameAndPort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StyleRegistrationTool.Model
{
/// <summary>
/// 名前とポート番号
/// </summary>
public class NameAndPort
{
public NameAndPort()
{

}

public NameAndPort(string name, int port)
{
Name = name ?? "";
Port = port;
}

/// <summary>
/// アプリ名
/// </summary>
public string Name { get; set; } = "";

/// <summary>
/// ポート番号
/// </summary>
public int Port { get; set; } = 50021;

public override bool Equals(object obj)
{
var port = obj as NameAndPort;
return port != null &&
Name == port.Name &&
Port == port.Port;
}

public static bool operator ==(NameAndPort port1, NameAndPort port2)
{
return EqualityComparer<NameAndPort>.Default.Equals(port1, port2);
}

public static bool operator !=(NameAndPort port1, NameAndPort port2)
{
return !(port1 == port2);
}
}
}
5 changes: 2 additions & 3 deletions StyleRegistrationTool/StyleRegistrationTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Model\NameAndPort.cs" />
<Compile Include="View\ChangePortWindow.xaml.cs">
<DependentUpon>ChangePortWindow.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -158,9 +159,7 @@
<ItemGroup>
<Resource Include="icon\users-alt.ico" />
</ItemGroup>
<ItemGroup>
<Folder Include="Model\" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Resource Include="Licence\WindowsAPICodePackLicence.txt" />
</ItemGroup>
Expand Down
22 changes: 16 additions & 6 deletions StyleRegistrationTool/View/ChangePortWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@
xmlns:local="clr-namespace:StyleRegistrationTool.View"
mc:Ignorable="d"
Title="ポートの変更" SizeToContent="WidthAndHeight" ResizeMode="NoResize" WindowStartupLocation="CenterOwner">
<Window.Resources>
<local:BoolNegativeConverter x:Key="BoolConverter"/>
</Window.Resources>
<Grid Margin="3">
<StackPanel>
<ComboBox x:Name="portComboBox" IsEditable="True" Width="150" Margin="3">
<ComboBoxItem Content="50021(VOICEVOX)"/>
<ComboBoxItem Content="50031(COEIROINK)"/>
<ComboBoxItem Content="50073(LMROID)"/>
</ComboBox>
<DockPanel>
<TextBlock DockPanel.Dock="Left" Text="プリセット" Margin="3" Width="45" VerticalAlignment="Center"/>
<ComboBox x:Name="portComboBox" SelectedItem="{Binding SelectedPreset}" DisplayMemberPath="Name" Width="150" Margin="3" IsTextSearchEnabled="False"/>
</DockPanel>
<DockPanel>
<TextBlock DockPanel.Dock="Left" Text="アプリ名" Margin="3" Width="45"/>
<TextBox Text="{Binding AppName, UpdateSourceTrigger=PropertyChanged}" Margin="3"/>
</DockPanel>
<DockPanel>
<TextBlock DockPanel.Dock="Left" Text="ポート" Margin="3" Width="45"/>
<TextBox x:Name="PortTextBox" Text="{Binding Port, UpdateSourceTrigger=PropertyChanged}" Margin="3"/>
</DockPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="OK" Width="60" Margin="3" IsDefault="True" Click="OkButton_Click"/>
<Button Content="OK" Width="60" Margin="3" IsDefault="True" Click="OkButton_Click" IsEnabled="{Binding ElementName=PortTextBox, Path=(Validation.HasError), Converter={StaticResource BoolConverter}}"/>
<Button Content="キャンセル" Width="60" Margin="3" IsCancel="True"/>
</StackPanel>
</StackPanel>
Expand Down
130 changes: 96 additions & 34 deletions StyleRegistrationTool/View/ChangePortWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,107 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Data;

namespace StyleRegistrationTool.View
{
/// <summary>
/// ChangePortWindow.xaml の相互作用ロジック
/// </summary>
public partial class ChangePortWindow : Window
public partial class ChangePortWindow : Window , INotifyPropertyChanged
{
/// <summary>
/// ポート番号
/// </summary>
public int Port
public ChangePortWindow(string appName, int port)
{
InitializeComponent();

DataContext = this;

//プリセット作成
portComboBox.Items.Add(new Model.NameAndPort("VOICEVOX", 50021));
portComboBox.Items.Add(new Model.NameAndPort("COEIROINK", 50031));
portComboBox.Items.Add(new Model.NameAndPort("LMROID", 50073));

SelectedPreset = new Model.NameAndPort(appName, port);
}

#region INotifyPropertyChangedの実装
public event PropertyChangedEventHandler PropertyChanged;

private List<PropertyChangedEventArgs> propertyChangedEventArgsList = new List<PropertyChangedEventArgs>(3);

private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
get
//eventArgsを使いまわしするための仕組み。newで逐一作成するよりも早い??
PropertyChangedEventArgs eventArgs = propertyChangedEventArgsList.FirstOrDefault(x => x.PropertyName == propertyName);
if (eventArgs == null)
{
string temp = portComboBox.Text;
return int.Parse(ExtractNumber(temp));
eventArgs = new PropertyChangedEventArgs(propertyName);
propertyChangedEventArgsList.Add(eventArgs);
}
PropertyChanged?.Invoke(this, eventArgs);
}
#endregion

private Model.NameAndPort _nameAndPort = new Model.NameAndPort();
/// <summary>
/// アプリ名とポート名
/// </summary>
public Model.NameAndPort SelectedPreset
{
get => _nameAndPort;
set
{
portComboBox.Text = value.ToString();
if (_nameAndPort == value) return;
_nameAndPort = value;
RaisePropertyChanged();

//SelectedIndexを-1にした場合、valueにnullが入るので確認
if (value == null) return;
Port = value.Port;
AppName = value.Name;
//入力の値がプリセットに含まれていない場合、未選択にする。
if (!portComboBox.Items.Contains(SelectedPreset))
{
portComboBox.SelectedIndex = -1;
}
}
}

public ChangePortWindow(int port)
private int _port = 50021;
/// <summary>
/// ポート番号
/// </summary>
public int Port
{
InitializeComponent();

Port = port;
get => _port;
set
{
if (_port == value) return;
_port = value;
SelectedPreset = new Model.NameAndPort(AppName, value);
RaisePropertyChanged();
}
}

private string _appName = "VOICEVOX";
/// <summary>
/// 与えられた文字列から、数字列を抽出し最初の数字列を返します。
/// アプリ名
/// </summary>
/// <param name="str">対象の文字列</param>
/// <returns>抽出した数字列</returns>
private string ExtractNumber(string str)
public string AppName
{
Regex regex = new Regex(@"[0-9]+", RegexOptions.IgnoreCase);
IEnumerable<Match> matchCollection = regex.Matches(str).Cast<Match>();
IEnumerable<string> numberWords = matchCollection.Select(match => match.Value);
if (numberWords.Count() == 0)
get => _appName;
set
{
return "";
if (_appName == value) return;
_appName = value;
SelectedPreset = new Model.NameAndPort(value, Port);
RaisePropertyChanged();
}
return numberWords.First();
}

/// <summary>
Expand All @@ -57,16 +111,24 @@ private string ExtractNumber(string str)
/// <param name="e"></param>
private void OkButton_Click(object sender, RoutedEventArgs e)
{
string temp = portComboBox.Text;
if (int.TryParse(ExtractNumber(temp), out int value))
{
DialogResult = true;
this.Close();
}
else
{
MessageBox.Show("数値変換できません。", "スタイル登録ツール", MessageBoxButton.OK, MessageBoxImage.Error);
}
DialogResult = true;
this.Close();
}
}

/// <summary>
/// boolを反転するコンバーター
/// </summary>
public class BoolNegativeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(value is bool && (bool)value);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(value is bool && (bool)value);
}
}
}
3 changes: 2 additions & 1 deletion StyleRegistrationTool/View/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Margin="3,3,3,0" Text="VOICEVOX"/>
<TextBlock Grid.Row="0" Grid.Column="0" Margin="3,3,3,0" Text="{Binding AppName}"/>
<TextBlock Grid.Row="0" Grid.Column="2" Margin="3,3,3,0" Text="SAPI"/>
<DockPanel Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="3" Margin="0,5,0,0">
<Button DockPanel.Dock="Left" Margin="3" Width="70" IsCancel="True" Command="{Binding ChangePortCommand}">ポート変更</Button>
Expand Down Expand Up @@ -49,6 +49,7 @@
<ListView x:Name="SapiStyleList" Grid.Row="1" Grid.Column="2" Margin="3" ItemsSource="{Binding SapiStyles}" SelectionChanged="SapiStyleList_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="アプリ名" DisplayMemberBinding="{Binding AppName}"/>
<GridViewColumn Header="キャラ" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="スタイル" DisplayMemberBinding="{Binding StyleName}"/>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}"/>
Expand Down
Loading