Skip to content

Commit

Permalink
feat(lyrics)!: newline calculation for the new lyric-stile
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDotH committed May 8, 2023
1 parent c9ea05e commit aeb64b8
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public JObject Defaults()
Selection = EnumLyricsDisplayMode.KARAOKE,
ArtworkBackground = false,
LyricsBlur = false,
LyricsAlignment = HorizontalAlignment.Left,
LyricsAlignment = TextAlignment.Left,
LyricsMargin = new Thickness(0,0,0,70),
LyricsWeight = FontWeight.Bold,
LyricsSize = 30
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Structure
public FontWeight LyricsWeight { get; set; }

[JsonProperty("Lyrics Alignment")]
public HorizontalAlignment LyricsAlignment { get; set; }
public TextAlignment LyricsAlignment { get; set; }

[JsonProperty("Lyrics Margin")]
public Thickness LyricsMargin { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia;
using Avalonia.Layout;
using Avalonia.Media;
using DevBase.Generics;
using OpenLyricsClient.Backend;
using OpenLyricsClient.Backend.Events.EventArgs;
using OpenLyricsClient.Backend.Settings.Sections.Lyrics;
using OpenLyricsClient.Frontend.Utils;

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

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


private AList<string> _lines;

public TextOverlayViewModel()
{
this._lines = new AList<string>();

Core.INSTANCE.SettingsHandler.SettingsChanged += SettingsHandlerOnSettingsChanged;
}

public 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.LyricsAlignment,
this.LyricsSize);

SetField(ref this._lines, lines);
}

private void SettingsHandlerOnSettingsChanged(object sender, SettingsChangedEventArgs settingschangedeventargs)
{
OnPropertyChanged("LyricsSize");
Expand All @@ -37,16 +59,21 @@ public FontWeight LyricsWeight
get => Core.INSTANCE.SettingsHandler.Settings<LyricsSection>().GetValue<FontWeight>("Lyrics Weight");
}

public HorizontalAlignment LyricsAlignment
public TextAlignment LyricsAlignment
{
get => Core.INSTANCE.SettingsHandler.Settings<LyricsSection>().GetValue<HorizontalAlignment>("Lyrics Alignment");
get => Core.INSTANCE.SettingsHandler.Settings<LyricsSection>().GetValue<TextAlignment>("Lyrics Alignment");
}

public Thickness LyricsMargin
{
get => Core.INSTANCE.SettingsHandler.Settings<LyricsSection>().GetValue<Thickness>("Lyrics Margin");
}


public AList<string> LyricsLines
{
get => this._lines;
}

protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
Expand Down
52 changes: 52 additions & 0 deletions OpenLyricsClient/Frontend/Utils/StringUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections.Generic;
using System.Text;
using Avalonia;
using Avalonia.Media;
using DevBase.Generics;

namespace OpenLyricsClient.Frontend.Utils;

public class StringUtils
{
public static AList<string> SplitTextToLines(string text, double width, double height, Typeface typeface, TextAlignment alignment, double fontSize)
{
string[] words = text.Split(' ');

AList<string> lines = new AList<string>();

StringBuilder currentLine = new StringBuilder();

TextWrapping textWrapping = TextWrapping.NoWrap;
Size constraint = new Size(width, height);

for (int i = 0; i < words.Length; i++)
{
string word = words[i];

StringBuilder candidateLine = new StringBuilder(currentLine.ToString());

if (candidateLine.Length > 0)
candidateLine.Append(' ');
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);
}
}

if (currentLine.Length > 0)
{
lines.Add(currentLine.ToString());
}

return lines;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:overlays="clr-namespace:OpenLyricsClient.Frontend.Models.Custom.Tile.Overlays"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="OpenLyricsClient.Frontend.View.Custom.Tile.Overlays.TextOverlay">
<TextBlock.DataContext>
<overlays:TextOverlayViewModel/>
</TextBlock.DataContext>



Welcome to Avalonia!
<ItemsControl Name="PART_Items"
Items="{Binding LyricsLines}" EffectiveViewportChanged="">

</ItemsControl>
</UserControl>

0 comments on commit aeb64b8

Please sign in to comment.