Skip to content

Commit

Permalink
fix(lyrics)!: TextOverlay prototype(not working needs more implementa…
Browse files Browse the repository at this point in the history
…tion)
  • Loading branch information
AlexanderDotH committed May 9, 2023
1 parent aeb64b8 commit 247f39b
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia.Controls;
Expand All @@ -8,6 +9,7 @@
using OpenLyricsClient.Backend.Settings.Sections.Lyrics;
using OpenLyricsClient.Frontend.Models.Pages.Settings;
using OpenLyricsClient.Shared.Structure.Lyrics;
using OpenLyricsClient.Shared.Utils;

namespace OpenLyricsClient.Frontend.Models.Custom;

Expand Down Expand Up @@ -49,9 +51,15 @@ private void LyricHandlerOnLyricChanged(object sender, LyricChangedEventArgs lyr
Lyric = lyricchangedeventargs.LyricPart;
}

public LyricPart[]? Lyrics
public ObservableCollection<LyricPart> Lyrics
{
get => Core.INSTANCE?.SongHandler?.CurrentSong?.Lyrics?.LyricParts!;
get
{
if (!DataValidator.ValidateData(Core.INSTANCE?.SongHandler?.CurrentSong?.Lyrics?.LyricParts))
return new ObservableCollection<LyricPart>();

return new ObservableCollection<LyricPart>(Core.INSTANCE?.SongHandler?.CurrentSong?.Lyrics?.LyricParts);
}
}

public LyricPart? Lyric
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,125 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Reactive;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Avalonia;
using Avalonia.Layout;
using Avalonia.Media;
using DevBase.Extensions;
using DevBase.Generics;
using Microsoft.CodeAnalysis;
using OpenLyricsClient.Backend;
using OpenLyricsClient.Backend.Events.EventArgs;
using OpenLyricsClient.Backend.Settings.Sections.Lyrics;
using OpenLyricsClient.Frontend.Utils;
using OpenLyricsClient.Shared.Structure.Lyrics;
using OpenLyricsClient.Shared.Utils;
using Org.BouncyCastle.Crypto.Parameters;
using ReactiveUI;
using SharpDX;
using Squalr.Engine.Utils.Extensions;

namespace OpenLyricsClient.Frontend.Models.Custom.Tile.Overlays;

public class TextOverlayViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;

private AList<string> _lines;
private ObservableCollection<(Rect, double, string)> _lines;
private LyricPart _lyricPart;
private Typeface _typeface;

public ICommand EffectiveViewportChangedCommand { get; }

public TextOverlayViewModel()
{
this._lines = new AList<string>();
this._lines = new ObservableCollection<(Rect, double, string)>();

this._typeface = new Typeface(FontFamily.Parse(
"avares://Material.Styles/Fonts/Roboto#Roboto"),
FontStyle.Normal, this.LyricsWeight);

this._lyricPart = new LyricPart(0,"");

EffectiveViewportChangedCommand = ReactiveCommand.Create<EffectiveViewportChangedEventArgs>(OnEffectiveViewportChanged);

Core.INSTANCE.SettingsHandler.SettingsChanged += SettingsHandlerOnSettingsChanged;
Core.INSTANCE.LyricHandler.LyricsPercentageUpdated += LyricHandlerOnLyricsPercentageUpdated;
}

private void LyricHandlerOnLyricsPercentageUpdated(object sender, LyricsPercentageUpdatedEventArgs args)
{
if (!args.LyricPart.Equals(this.LyricPart))
return;

UpdatePercentage(this.LyricPart.Part);
OnPropertyChanged("LyricsLines");
}

public void UpdateTextWrappingLines(string text, double width, double height)
private void OnEffectiveViewportChanged(EffectiveViewportChangedEventArgs e)
{
UpdateLyricsWrapping(e.EffectiveViewport.Width, e.EffectiveViewport.Height);
}

public void UpdateLyricsWrapping(double width, double height)
{
if (!DataValidator.ValidateData(this.LyricPart))
return;

UpdateTextWrappingLines(this.LyricPart.Part, width, height);
}

private void UpdateTextWrappingLines(string text, double width, double height)
{
AList<string> lines = StringUtils.SplitTextToLines(
text,
width,
height,
new Typeface(FontFamily.Parse(
"avares://Material.Styles/Fonts/Roboto#Roboto"),
FontStyle.Normal, this.LyricsWeight),
this._typeface,
this.LyricsAlignment,
this.LyricsSize);

SetField(ref this._lines, lines);
ObservableCollection<(Rect, double, string)> sizedLines = new ObservableCollection<(Rect, double, string)>();

lines.ForEach(l =>
{
sizedLines.Add((MeasureSingleString(l), CalculatePercentage(l, text), l));
});

SetField(ref this._lines, sizedLines);
}

private void UpdatePercentage(string text)
{
this._lines.ForEach(l =>
{
l.Item2 = CalculatePercentage(l.Item3, text);
});
}

private double CalculatePercentage(string single, string full)
{
double singleWidth = MeasureSingleString(single).Width;
double fullWidth = MeasureSingleString(full).Width;

return (fullWidth * 0.01) * singleWidth;
}

private Rect MeasureSingleString(string line)
{
FormattedText formattedCandidateLine = new FormattedText(
line,
this._typeface,
this.LyricsSize,
this.LyricsAlignment,
TextWrapping.NoWrap,
new Size(double.PositiveInfinity, double.PositiveInfinity));

return formattedCandidateLine.Bounds;
}

private void SettingsHandlerOnSettingsChanged(object sender, SettingsChangedEventArgs settingschangedeventargs)
Expand Down Expand Up @@ -69,7 +150,17 @@ public Thickness LyricsMargin
get => Core.INSTANCE.SettingsHandler.Settings<LyricsSection>().GetValue<Thickness>("Lyrics Margin");
}

public AList<string> LyricsLines
public LyricPart LyricPart
{
get => this._lyricPart;
set
{
SetField(ref this._lyricPart, value);
UpdateLyricsWrapping(400, 400);
}
}

public ObservableCollection<(Rect, double, string)> LyricsLines
{
get => this._lines;
}
Expand Down
2 changes: 2 additions & 0 deletions OpenLyricsClient/Frontend/Utils/StringUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ public static AList<string> SplitTextToLines(string text, double width, double h
candidateLine.Append(word);

FormattedText formattedCandidateLine = new FormattedText(candidateLine.ToString(), typeface, fontSize, alignment, textWrapping, constraint);

if (formattedCandidateLine.Bounds.Width <= width)
{
currentLine = candidateLine;
}
else
{

lines.Add(currentLine.ToString());
currentLine.Clear();
currentLine.Append(word);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ private Size GetRenderedSize(int index)
foreach (FormattedTextLine line in text.GetLines())
{
lineSize += line.Height;
}
Expand Down
22 changes: 6 additions & 16 deletions OpenLyricsClient/Frontend/View/Custom/NewLyricsScroller.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:custom="clr-namespace:OpenLyricsClient.Frontend.Models.Custom"
xmlns:elements="clr-namespace:OpenLyricsClient.Frontend.Models.Elements"
xmlns:tile="clr-namespace:OpenLyricsClient.Frontend.View.Custom.Tile"
xmlns:lyrics="clr-namespace:OpenLyricsClient.Shared.Structure.Lyrics;assembly=OpenLyricsClient.Shared"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="OpenLyricsClient.Frontend.View.Custom.NewLyricsScroller">

Expand All @@ -18,14 +20,8 @@
Items="{Binding Lyrics}"
IsVisible="False">
<ItemsRepeater.ItemTemplate>
<DataTemplate>
<elements:LyricsCard Text="{Binding Part}"
LyricPart="{Binding}"
IgnoreEvents="False"
HorizontalAlignment="Left"
FontWeight="Bold"
FontSize="30"
Margin="0,0,0,70"/>
<DataTemplate DataType="lyrics:LyricPart">
<tile:LyricsTile LyricPart="{Binding}"/>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
Expand All @@ -34,14 +30,8 @@
Items="{Binding Lyrics}"
Margin="0,0,0,0">
<ItemsRepeater.ItemTemplate>
<DataTemplate>
<elements:LyricsCard Text="{Binding Part}"
LyricPart="{Binding}"
IgnoreEvents="False"
HorizontalAlignment="Left"
FontWeight="Bold"
FontSize="30"
Margin="0,0,0,70"/>
<DataTemplate DataType="lyrics:LyricPart">
<tile:LyricsTile LyricPart="{Binding}"/>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
<ItemsRepeater.Transitions>
Expand Down
19 changes: 12 additions & 7 deletions OpenLyricsClient/Frontend/View/Custom/NewLyricsScroller.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -21,6 +22,7 @@
using OpenLyricsClient.Frontend.Models.Custom;
using OpenLyricsClient.Frontend.Structure.Enum;
using OpenLyricsClient.Frontend.Utils;
using OpenLyricsClient.Frontend.View.Custom.Tile;
using Squalr.Engine.Utils.Extensions;

namespace OpenLyricsClient.Frontend.View.Custom;
Expand Down Expand Up @@ -65,6 +67,7 @@ public NewLyricsScroller()

this._frameRate = 144;

this.DataContext = new NewLyricsScrollerViewModel();
this._viewModel = this.DataContext as NewLyricsScrollerViewModel;

this._hiddenRepeater = this.Get<ItemsRepeater>(nameof(HIDDEN_CTRL_Repeater));
Expand All @@ -74,7 +77,7 @@ public NewLyricsScroller()

this._uiThreadRenderTimer = new UiThreadRenderTimer(144);
this._uiThreadRenderTimer.Tick += UiThreadRenderTimerOnTick;

AttachedToVisualTree += OnAttachedToVisualTree;
}

Expand Down Expand Up @@ -160,7 +163,7 @@ private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArg

private async Task CalculateOffset()
{
var debounced = Debouncer.Debounce<LyricPart[], double>(
var debounced = Debouncer.Debounce<ObservableCollection<LyricPart>, double>(
async lyrics =>
await Dispatcher.UIThread.InvokeAsync(() =>
{
Expand All @@ -179,7 +182,7 @@ await Dispatcher.UIThread.InvokeAsync(() =>
}
}

private double GetRenderedOffset(LyricPart lyricPart, LyricPart[] lyricParts)
private double GetRenderedOffset(LyricPart lyricPart, ObservableCollection<LyricPart> lyricParts)
{
if (!DataValidator.ValidateData(lyricPart, lyricParts))
return 0;
Expand All @@ -201,14 +204,14 @@ private double GetRenderedOffset(LyricPart lyricPart, LyricPart[] lyricParts)
return position;
}

private int GetIndexOfLyric(LyricPart lyricPart, LyricPart[] lyricParts) => lyricParts.IndexOf(lyricPart);
private int GetIndexOfLyric(LyricPart lyricPart, ObservableCollection<LyricPart> lyricParts) => lyricParts.IndexOf(lyricPart);

private Size GetRenderedSize(int index)
{
if (!DataValidator.ValidateData(this._viewModel.Lyrics))
return new Size();

if (index > this._viewModel.Lyrics.Length || index < 0)
if (index > this._viewModel.Lyrics.Count || index < 0)
return new Size();

try
Expand All @@ -220,6 +223,8 @@ private Size GetRenderedSize(int index)
if (itemContainer == null)
itemContainer = this._hiddenRepeater.GetOrCreateElement(index);

((LyricsTile)itemContainer).UpdateViewPort(this.Bounds.Width, this.Bounds.Height);

Size constraint = new Size(this.Bounds.Width, this.Bounds.Height);
itemContainer.Measure(constraint);

Expand Down Expand Up @@ -252,7 +257,7 @@ public float CalcSpeed()
float highest = 0;
int hSum = 0;

for (int i = 0; i < this._viewModel!.Lyrics.Length; i++)
for (int i = 0; i < this._viewModel!.Lyrics.Count; i++)
{
LyricPart currentPart = this._viewModel!.Lyrics[i];

Expand All @@ -278,7 +283,7 @@ public float CalcSpeed()
}
}

float speed = (sum / this._viewModel.Lyrics.Length);
float speed = (sum / this._viewModel.Lyrics.Count);

float hSA = highest / hSum;

Expand Down
Loading

0 comments on commit 247f39b

Please sign in to comment.