Skip to content

Commit a0a721c

Browse files
committed
・読み上げるエモートの数を制限できるようにした
・連続する同一エモートをスキップできるようにした
1 parent b317c3c commit a0a721c

File tree

6 files changed

+146
-2
lines changed

6 files changed

+146
-2
lines changed

BouyomiPlugin/ConfigView.xaml

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
d:DesignHeight="1500"
99
d:DataContext="{d:DesignInstance local:ConfigViewModel, IsDesignTimeCreatable=True}"
1010
Title="棒読みちゃん連携プラグイン" Width="439" Background="#F0F0F0">
11+
<Window.Resources>
12+
<local:AndConverter x:Key="AndConverter"/>
13+
</Window.Resources>
1114
<Grid>
1215
<CheckBox x:Name="checkBox" Content="棒読みちゃんにコメントを読んでもらう" IsChecked="{Binding IsEnabled}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
1316
<TabControl HorizontalAlignment="Stretch" Margin="5,30,5,5" VerticalAlignment="Stretch">
@@ -196,6 +199,8 @@
196199
<RowDefinition />
197200
<RowDefinition />
198201
<RowDefinition />
202+
<RowDefinition />
203+
<RowDefinition />
199204
</Grid.RowDefinitions>
200205
<Grid.ColumnDefinitions>
201206
<ColumnDefinition />
@@ -207,7 +212,24 @@
207212
</Grid.ColumnDefinitions>
208213
<CheckBox x:Name="CheckTwitchComment" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" IsChecked="{Binding IsTwitchComment}" Content="コメント" />
209214
<CheckBox IsEnabled="{Binding IsChecked, ElementName=CheckTwitchComment}" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3" IsChecked="{Binding IsTwitchCommentNickname}" Content="コテハン" />
210-
<CheckBox IsEnabled="{Binding IsChecked, ElementName=CheckTwitchComment}" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" IsChecked="{Binding IsTwitchCommentEmoteId}" Content="エモートID" />
215+
<CheckBox x:Name="CheckTwitchCommentEmoteId" IsEnabled="{Binding IsChecked, ElementName=CheckTwitchComment}" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" IsChecked="{Binding IsTwitchCommentEmoteId}" Content="エモートID" />
216+
<TextBox x:Name="TwitchMaxEmotes" Grid.Row="3" Grid.Column="2" MaxLength="3" Text="{Binding TwitchMaxEmotes}" PreviewTextInput="TextBox_PreviewTextInput" TextChanged="TextBox_TextChanged">
217+
<TextBox.IsEnabled>
218+
<MultiBinding Converter="{StaticResource AndConverter}">
219+
<Binding Path="IsChecked" ElementName="CheckTwitchComment"/>
220+
<Binding Path="IsChecked" ElementName="CheckTwitchCommentEmoteId"/>
221+
</MultiBinding>
222+
</TextBox.IsEnabled>
223+
</TextBox>
224+
<Label Content="個まで" Grid.Row="3" Grid.Column="3"/>
225+
<CheckBox Grid.Row="4" Grid.Column="2" Grid.ColumnSpan="4" IsChecked="{Binding IsTwitchSkipSameEmote}" Content="連続する同一エモートを省略">
226+
<CheckBox.IsEnabled>
227+
<MultiBinding Converter="{StaticResource AndConverter}">
228+
<Binding Path="IsChecked" ElementName="CheckTwitchComment"/>
229+
<Binding Path="IsChecked" ElementName="CheckTwitchCommentEmoteId"/>
230+
</MultiBinding>
231+
</CheckBox.IsEnabled>
232+
</CheckBox>
211233
</Grid>
212234
</Grid>
213235
</Border>

BouyomiPlugin/ConfigView.xaml.cs

+40
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.ComponentModel;
44
using System.Linq;
55
using System.Text;
6+
using System.Text.RegularExpressions;
67
using System.Threading.Tasks;
78
using System.Windows;
89
using System.Windows.Controls;
@@ -23,6 +24,7 @@ public partial class ConfigView : Window
2324
public ConfigView()
2425
{
2526
InitializeComponent();
27+
DataObject.AddPastingHandler(TwitchMaxEmotes, TextBoxPastingEventHandler);
2628
_isForceClose = false;
2729
}
2830
protected override void OnClosing(CancelEventArgs e)
@@ -46,5 +48,43 @@ public void ForceClose()
4648
_isForceClose = true;
4749
this.Close();
4850
}
51+
52+
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
53+
{
54+
// 数字のみ入力可
55+
if (!Regex.IsMatch(e.Text, "[0-9]"))
56+
{
57+
e.Handled = true;
58+
}
59+
}
60+
61+
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
62+
{
63+
if (e.Source is TextBox t)
64+
{
65+
if ((0 < t.MaxLength) && (t.MaxLength < t.Text.Length))
66+
{
67+
int start = t.SelectionStart;
68+
t.Text = t.Text.Substring(0, 3);
69+
t.SelectionStart = start;
70+
}
71+
}
72+
}
73+
74+
private static void TextBoxPastingEventHandler(object sender, DataObjectPastingEventArgs e)
75+
{
76+
if (e.Source is TextBox t)
77+
{
78+
if (e.DataObject.GetData(typeof(string)) is string s)
79+
{
80+
// 数字のみ貼り付け可
81+
t.SelectedText = Regex.Replace(s, "[^0-9]", "");
82+
t.SelectionStart += t.SelectionLength;
83+
t.SelectionLength = 0;
84+
e.CancelCommand();
85+
e.Handled = true;
86+
}
87+
}
88+
}
4989
}
5090
}

BouyomiPlugin/ConfigViewModel.cs

+16
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,22 @@ public bool IsTwitchCommentEmoteId
305305
get => _options.IsTwitchCommentEmoteId;
306306
set => _options.IsTwitchCommentEmoteId = value;
307307
}
308+
/// <summary>
309+
/// TwitchのコメントのエモートIDを何個まで読み上げるか
310+
/// </summary>
311+
public int TwitchMaxEmotes
312+
{
313+
get { return _options.TwitchMaxEmotes; }
314+
set { _options.TwitchMaxEmotes = value; }
315+
}
316+
/// <summary>
317+
/// Twitchのコメントの連続する同一エモートを省略するか
318+
/// </summary>
319+
public bool IsTwitchSkipSameEmote
320+
{
321+
get { return _options.IsTwitchSkipSameEmote; }
322+
set { _options.IsTwitchSkipSameEmote = value; }
323+
}
308324
///// <summary>
309325
///// Twitchのアイテムを読み上げるか
310326
///// </summary>

BouyomiPlugin/Converter.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Linq;
4+
using System.Windows.Data;
5+
namespace BouyomiPlugin
6+
{
7+
public class AndConverter : IMultiValueConverter
8+
{
9+
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
10+
{
11+
return values?.All(value => (bool)value) == true;
12+
}
13+
14+
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
15+
{
16+
throw new NotImplementedException();
17+
}
18+
}
19+
}

BouyomiPlugin/Options.cs

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class Options : DynamicOptionsBase
5454
public bool IsTwitchComment { get { return GetValue(); } set { SetValue(value); } }
5555
public bool IsTwitchCommentNickname { get { return GetValue(); } set { SetValue(value); } }
5656
public bool IsTwitchCommentEmoteId { get { return GetValue(); } set { SetValue(value); } }
57+
public bool IsTwitchSkipSameEmote { get { return GetValue(); } set { SetValue(value); } }
58+
public int TwitchMaxEmotes { get { return GetValue(); } set { SetValue(value); } }
5759

5860
//ニコ生
5961
public bool IsNicoConnect { get { return GetValue(); } set { SetValue(value); } }
@@ -180,6 +182,8 @@ protected override void Init()
180182
Dict.Add(nameof(IsTwitchComment), new Item { DefaultValue = true, Predicate = b => true, Serializer = b => b.ToString(), Deserializer = s => bool.Parse(s) });
181183
Dict.Add(nameof(IsTwitchCommentNickname), new Item { DefaultValue = true, Predicate = b => true, Serializer = b => b.ToString(), Deserializer = s => bool.Parse(s) });
182184
Dict.Add(nameof(IsTwitchCommentEmoteId), new Item { DefaultValue = false, Predicate = b => true, Serializer = b => b.ToString(), Deserializer = s => bool.Parse(s) });
185+
Dict.Add(nameof(IsTwitchSkipSameEmote), new Item { DefaultValue = true, Predicate = b => true, Serializer = b => b.ToString(), Deserializer = s => bool.Parse(s) });
186+
Dict.Add(nameof(TwitchMaxEmotes), new Item { DefaultValue = 3, Predicate = n => true, Serializer = n => n.ToString(), Deserializer = n => int.Parse(n) });
183187

184188
//ニコ生
185189
Dict.Add(nameof(IsNicoConnect), new Item { DefaultValue = false, Predicate = b => true, Serializer = b => b.ToString(), Deserializer = s => bool.Parse(s) });

BouyomiPlugin/main.cs

+44-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,49 @@ public static string ToTextWithImageAlt(this IEnumerable<IMessagePart> parts)
5252
}
5353
return s;
5454
}
55+
public static string ToTextWithImageAlt(this IEnumerable<IMessagePart> parts, int maxImages, bool skipSameImage)
56+
{
57+
int n = 0;
58+
string s = "";
59+
string previousImage = "";
60+
if (parts != null)
61+
{
62+
foreach (var part in parts)
63+
{
64+
if (part is IMessageText text)
65+
{
66+
if (!string.IsNullOrWhiteSpace(text.Text))
67+
{
68+
previousImage = "";
69+
}
70+
s += text;
71+
}
72+
else if (part is IMessageImage image)
73+
{
74+
if ((n < maxImages) && (!skipSameImage || !string.Equals(previousImage, image.Alt)))
75+
{
76+
s += image.Alt;
77+
n++;
78+
}
79+
previousImage = image.Alt;
80+
}
81+
else if(part is IMessageRemoteSvg remoteSvg)
82+
{
83+
if ((n < maxImages) && (!skipSameImage || !string.Equals(previousImage, remoteSvg.Alt)))
84+
{
85+
s += remoteSvg.Alt;
86+
n++;
87+
}
88+
previousImage = remoteSvg.Alt;
89+
}
90+
else
91+
{
92+
93+
}
94+
}
95+
}
96+
return s;
97+
}
5598
}
5699
[Export(typeof(IPlugin))]
57100
public class BouyomiPlugin : IPlugin, IDisposable
@@ -293,7 +336,7 @@ private static (string name, string comment) GetData(ISiteMessage message, Optio
293336
}
294337
if (options.IsTwitchCommentEmoteId)
295338
{
296-
comment = (twitchMessage as ITwitchComment).CommentItems.ToTextWithImageAlt();
339+
comment = (twitchMessage as ITwitchComment).CommentItems.ToTextWithImageAlt(options.TwitchMaxEmotes, options.IsTwitchSkipSameEmote);
297340
}
298341
else
299342
{

0 commit comments

Comments
 (0)