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

棒読みちゃんにTwitchのエモートのIDを読んでもらえるようにした #196

Closed
wants to merge 4 commits into from
Closed
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
70 changes: 68 additions & 2 deletions BouyomiPlugin/ConfigView.xaml

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions BouyomiPlugin/ConfigView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand All @@ -23,6 +24,7 @@ public partial class ConfigView : Window
public ConfigView()
{
InitializeComponent();
DataObject.AddPastingHandler(TwitchMaxEmotes, TextBoxPastingEventHandler);
_isForceClose = false;
}
protected override void OnClosing(CancelEventArgs e)
Expand All @@ -46,5 +48,43 @@ public void ForceClose()
_isForceClose = true;
this.Close();
}

private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
// 数字のみ入力可
if (!Regex.IsMatch(e.Text, "[0-9]"))
{
e.Handled = true;
}
}

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (e.Source is TextBox t)
{
if ((0 < t.MaxLength) && (t.MaxLength < t.Text.Length))
{
int start = t.SelectionStart;
t.Text = t.Text.Substring(0, 3);
t.SelectionStart = start;
}
}
}

private static void TextBoxPastingEventHandler(object sender, DataObjectPastingEventArgs e)
{
if (e.Source is TextBox t)
{
if (e.DataObject.GetData(typeof(string)) is string s)
{
// 数字のみ貼り付け可
t.SelectedText = Regex.Replace(s, "[^0-9]", "");
t.SelectionStart += t.SelectionLength;
t.SelectionLength = 0;
e.CancelCommand();
e.Handled = true;
}
}
}
}
}
24 changes: 24 additions & 0 deletions BouyomiPlugin/ConfigViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,30 @@ public bool IsTwitchCommentNickname
get => _options.IsTwitchCommentNickname;
set => _options.IsTwitchCommentNickname = value;
}
/// <summary>
/// TwitchのコメントのエモートIDを読み上げるか
/// </summary>
public bool IsTwitchCommentEmoteId
{
get => _options.IsTwitchCommentEmoteId;
set => _options.IsTwitchCommentEmoteId = value;
}
/// <summary>
/// TwitchのコメントのエモートIDを何個まで読み上げるか
/// </summary>
public int TwitchMaxEmotes
{
get { return _options.TwitchMaxEmotes; }
set { _options.TwitchMaxEmotes = value; }
}
/// <summary>
/// Twitchのコメントの連続する同一エモートを省略するか
/// </summary>
public bool IsTwitchSkipSameEmote
{
get { return _options.IsTwitchSkipSameEmote; }
set { _options.IsTwitchSkipSameEmote = value; }
}
///// <summary>
///// Twitchのアイテムを読み上げるか
///// </summary>
Expand Down
19 changes: 19 additions & 0 deletions BouyomiPlugin/Converter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
namespace BouyomiPlugin
{
public class AndConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values?.All(value => (bool)value) == true;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
6 changes: 6 additions & 0 deletions BouyomiPlugin/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class Options : DynamicOptionsBase
public bool IsTwitchDisconnect { get { return GetValue(); } set { SetValue(value); } }
public bool IsTwitchComment { get { return GetValue(); } set { SetValue(value); } }
public bool IsTwitchCommentNickname { get { return GetValue(); } set { SetValue(value); } }
public bool IsTwitchCommentEmoteId { get { return GetValue(); } set { SetValue(value); } }
public bool IsTwitchSkipSameEmote { get { return GetValue(); } set { SetValue(value); } }
public int TwitchMaxEmotes { get { return GetValue(); } set { SetValue(value); } }

//ニコ生
public bool IsNicoConnect { get { return GetValue(); } set { SetValue(value); } }
Expand Down Expand Up @@ -178,6 +181,9 @@ protected override void Init()
Dict.Add(nameof(IsTwitchDisconnect), new Item { DefaultValue = false, Predicate = b => true, Serializer = b => b.ToString(), Deserializer = s => bool.Parse(s) });
Dict.Add(nameof(IsTwitchComment), new Item { DefaultValue = true, Predicate = b => true, Serializer = b => b.ToString(), Deserializer = s => bool.Parse(s) });
Dict.Add(nameof(IsTwitchCommentNickname), new Item { DefaultValue = true, Predicate = b => true, Serializer = b => b.ToString(), Deserializer = s => bool.Parse(s) });
Dict.Add(nameof(IsTwitchCommentEmoteId), new Item { DefaultValue = false, Predicate = b => true, Serializer = b => b.ToString(), Deserializer = s => bool.Parse(s) });
Dict.Add(nameof(IsTwitchSkipSameEmote), new Item { DefaultValue = true, Predicate = b => true, Serializer = b => b.ToString(), Deserializer = s => bool.Parse(s) });
Dict.Add(nameof(TwitchMaxEmotes), new Item { DefaultValue = 3, Predicate = n => true, Serializer = n => n.ToString(), Deserializer = n => int.Parse(n) });

//ニコ生
Dict.Add(nameof(IsNicoConnect), new Item { DefaultValue = false, Predicate = b => true, Serializer = b => b.ToString(), Deserializer = s => bool.Parse(s) });
Expand Down
52 changes: 51 additions & 1 deletion BouyomiPlugin/main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,49 @@ public static string ToTextWithImageAlt(this IEnumerable<IMessagePart> parts)
}
return s;
}
public static string ToTextWithImageAlt(this IEnumerable<IMessagePart> parts, int maxImages, bool skipSameImage)
{
int n = 0;
string s = "";
string previousImage = "";
if (parts != null)
{
foreach (var part in parts)
{
if (part is IMessageText text)
{
if (!string.IsNullOrWhiteSpace(text.Text))
{
previousImage = "";
}
s += text;
}
else if (part is IMessageImage image)
{
if ((n < maxImages) && (!skipSameImage || !string.Equals(previousImage, image.Alt)))
{
s += image.Alt;
n++;
}
previousImage = image.Alt;
}
else if(part is IMessageRemoteSvg remoteSvg)
{
if ((n < maxImages) && (!skipSameImage || !string.Equals(previousImage, remoteSvg.Alt)))
{
s += remoteSvg.Alt;
n++;
}
previousImage = remoteSvg.Alt;
}
else
{

}
}
}
return s;
}
}
[Export(typeof(IPlugin))]
public class BouyomiPlugin : IPlugin, IDisposable
Expand Down Expand Up @@ -291,7 +334,14 @@ private static (string name, string comment) GetData(ISiteMessage message, Optio
{
name = (twitchMessage as ITwitchComment).DisplayName;
}
comment = (twitchMessage as ITwitchComment).CommentItems.ToText();
if (options.IsTwitchCommentEmoteId)
{
comment = (twitchMessage as ITwitchComment).CommentItems.ToTextWithImageAlt(options.TwitchMaxEmotes, options.IsTwitchSkipSameEmote);
}
else
{
comment = (twitchMessage as ITwitchComment).CommentItems.ToText();
}
}
break;
}
Expand Down