Skip to content

Commit

Permalink
修复弹幕重复的问题 (#200)
Browse files Browse the repository at this point in the history
* 修复弹幕重复的问题

* Update
  • Loading branch information
Richasy authored Jan 3, 2024
1 parent 0c7d2e2 commit 54a9c4c
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ private void OnRequestClearDanmaku(object sender, EventArgs e)
_danmakuView?.ClearAll();
}

private void OnDanmakuRequestSeek(object sender, TimeSpan e)
=> _danmakuView.ResetTimePosition(e);

private void OnDanmakuListAdded(object sender, IEnumerable<DanmakuInformation> e)
{
InitializeDanmaku(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private void InitializeDanmakuTimer()
{
_danmakuTimer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(1),
Interval = TimeSpan.FromSeconds(0.5),
};
_danmakuTimer.Tick += OnDanmkuTimerTick;
}
Expand Down Expand Up @@ -73,7 +73,7 @@ private void InitializeDanmaku(IEnumerable<DanmakuInformation> elements)
{
if (_danmakuDictionary.ContainsKey(g.Key))
{
_danmakuDictionary[g.Key] = _danmakuDictionary[g.Key].Concat(g.Value).ToList();
_danmakuDictionary[g.Key] = g.Value.ToList();
}
else
{
Expand Down Expand Up @@ -254,6 +254,6 @@ private void ResizeSubtitle()
scale = 0.4;
}

_subtitleBlock.FontSize = 22 * scale;
_subtitleBlock.FontSize = 20 * scale;
}
}
2 changes: 2 additions & 0 deletions src/App/Controls/Base/BiliPlayerOverlay/BiliPlayerOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ internal override void OnViewModelChanged(DependencyPropertyChangedEventArgs e)
oldViewModel.DanmakuViewModel.LiveDanmakuAdded -= OnLiveDanmakuAdded;
oldViewModel.DanmakuViewModel.SendDanmakuSucceeded -= OnSendDanmakuSucceeded;
oldViewModel.DanmakuViewModel.PropertyChanged -= OnDanmakuViewModelPropertyChanged;
oldViewModel.DanmakuViewModel.RequestSeek -= OnDanmakuRequestSeek;
oldViewModel.PropertyChanged -= OnViewModelPropertyChanged;
oldViewModel.RequestShowTempMessage -= OnRequestShowTempMessage;
}
Expand All @@ -82,6 +83,7 @@ internal override void OnViewModelChanged(DependencyPropertyChangedEventArgs e)
newViewModel.DanmakuViewModel.LiveDanmakuAdded += OnLiveDanmakuAdded;
newViewModel.DanmakuViewModel.SendDanmakuSucceeded += OnSendDanmakuSucceeded;
newViewModel.DanmakuViewModel.PropertyChanged += OnDanmakuViewModelPropertyChanged;
newViewModel.DanmakuViewModel.RequestSeek += OnDanmakuRequestSeek;
newViewModel.PropertyChanged += OnViewModelPropertyChanged;
newViewModel.RequestShowTempMessage += OnRequestShowTempMessage;
}
Expand Down
4 changes: 2 additions & 2 deletions src/App/Controls/Base/BiliTransportControls.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ private void ChangeVisualStateFromDisplayMode()
};
}

private void OnProgressSliderValueChanged(object sender, Microsoft.UI.Xaml.Controls.Primitives.RangeBaseValueChangedEventArgs e)
private void OnProgressSliderValueChanged(object sender, RangeBaseValueChangedEventArgs e)
=> ViewModel.ChangeProgressCommand.Execute(e.NewValue);

private void OnVolumeValueChanged(object sender, Microsoft.UI.Xaml.Controls.Primitives.RangeBaseValueChangedEventArgs e)
private void OnVolumeValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
if (e.NewValue != e.OldValue)
{
Expand Down
16 changes: 16 additions & 0 deletions src/App/Controls/Danmaku/DanmakuView/DanmakuView.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ namespace Bili.Copilot.App.Controls.Danmaku;
/// </summary>
public sealed partial class DanmakuView
{
/// <summary>
/// 重置时间戳,清除该时间戳之后的弹幕.
/// </summary>
/// <param name="position">时间戳.</param>
public void ResetTimePosition(TimeSpan position)
{
var discardDanmaku = _sendDanmakuList.Where(p => p.Time > position.TotalSeconds).ToArray();
foreach (var item in discardDanmaku)
{
Remove(item);
}
}

/// <summary>
/// 添加直播滚动弹幕.
/// </summary>
Expand Down Expand Up @@ -301,6 +314,8 @@ public void Remove(DanmakuModel danmaku)
default:
break;
}

_sendDanmakuList.Remove(danmaku);
}

/// <summary>
Expand All @@ -318,6 +333,7 @@ public void ClearAll()
_bottomContainer.Children.Clear();
_topContainer.Children.Clear();
_scrollContainer.Children.Clear();
_sendDanmakuList.Clear();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public sealed partial class DanmakuView
private readonly List<Storyboard> _topBottomStoryList = new();
private readonly List<Storyboard> _scrollStoryList = new();
private readonly List<Storyboard> _positionStoryList = new();
private readonly List<DanmakuModel> _sendDanmakuList;

private Grid _rootGrid;
private Canvas _canvas;
Expand Down
7 changes: 7 additions & 0 deletions src/App/Controls/Danmaku/DanmakuView/DanmakuView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public DanmakuView()
DefaultStyleKey = typeof(DanmakuView);
DanmakuBold = false;
DanmakuFontFamily = string.Empty;
_sendDanmakuList = new();
}

/// <inheritdoc/>
Expand Down Expand Up @@ -90,6 +91,11 @@ private void AddDanmakuInternal(DanmakuModel m, bool isOwn)
return;
}

if (_sendDanmakuList.Contains(m))
{
return;
}

var danmaku = CreateNewDanmuControl(m);

if (isOwn)
Expand Down Expand Up @@ -187,6 +193,7 @@ private void AddDanmakuInternal(DanmakuModel m, bool isOwn)
});

moveStoryboard.Begin();
_sendDanmakuList.Add(m);
}

private void SetDanmakuSizeZoom(double value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public sealed partial class DanmakuModuleViewModel
/// </summary>
public event EventHandler<LiveDanmakuInformation> LiveDanmakuAdded;

/// <summary>
/// 当请求重新调整弹幕进度时触发的事件.
/// </summary>
public event EventHandler<TimeSpan> RequestSeek;

/// <summary>
/// 弹幕位置的可观察集合.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,8 @@ private async Task<bool> SendLiveDanmakuAsync(string danmakuText)
[RelayCommand]
private void AddLiveDanmaku(LiveDanmakuInformation info)
=> LiveDanmakuAdded?.Invoke(this, info);

[RelayCommand]
private void ResetTimePosition(TimeSpan time)
=> RequestSeek?.Invoke(this, time);
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,15 @@ private void ChangeProgress(double seconds)
{
var ts = TimeSpan.FromSeconds(seconds);
if (Player == null
|| Math.Abs(ts.TotalSeconds - Player.Position.TotalSeconds) < 1)
|| Math.Abs(ts.TotalSeconds - Player.Position.TotalSeconds) < 3)
{
return;
}

Player.SeekTo(ts);
var msg = $"{ResourceToolkit.GetLocalizedString(StringNames.CurrentProgress)}: {TimeSpan.FromSeconds(seconds):g}";
RequestShowTempMessage?.Invoke(this, msg);
DanmakuViewModel.ResetTimePositionCommand.Execute(ts);
}

[RelayCommand]
Expand Down

0 comments on commit 54a9c4c

Please sign in to comment.