Skip to content

Commit

Permalink
Reworked the LyricsScroller a little bit.
Browse files Browse the repository at this point in the history
- Not finished yet the LyricsCard has to be reworked too but its usable rn
  • Loading branch information
AlexanderDotH committed May 4, 2023
1 parent e0c0d05 commit 0340576
Show file tree
Hide file tree
Showing 12 changed files with 512 additions and 41 deletions.
2 changes: 1 addition & 1 deletion OpenLyricsClient/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<StyleInclude Source="avares://OpenLyricsClient/Frontend/View/Elements/RadioButton.axaml"/>
<StyleInclude Source="avares://OpenLyricsClient/Frontend/View/Elements/NoteAnimation.axaml"/>
<StyleInclude Source="avares://OpenLyricsClient/Frontend/View/Elements/MaterialCheckBox.axaml"/>
<StyleInclude Source="avares://OpenLyricsClient/Frontend/View/Custom/CustomScrollViewers.axaml"/>
<StyleInclude Source="avares://OpenLyricsClient/Frontend/View/Custom/CustomScrollViewer.axaml"/>
<StyleInclude Source="avares://OpenLyricsClient/Frontend/View/Elements/Expander.axaml"/>
<StyleInclude Source="avares://OpenLyricsClient/Frontend/View/Elements/ContentExpandControl.axaml"/>
<StyleInclude Source="avares://OpenLyricsClient/Frontend/View/Elements/RadioButton.axaml"/>
Expand Down
22 changes: 22 additions & 0 deletions OpenLyricsClient/Backend/Utils/Debouncer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace OpenLyricsClient.Backend.Utils;

public class Debouncer
{
public static Func<T, Task<R>> Debounce<T, R>(Func<T, Task<R>> func, int milliseconds)
{
int last = 0;
return async arg =>
{
int current = Interlocked.Increment(ref last);
await Task.Delay(milliseconds);
if (current == last)
return await func(arg);
return default(R);
};
}

}
58 changes: 28 additions & 30 deletions OpenLyricsClient/Frontend/Animation/SmoothAnimator.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
using System;
using System.Diagnostics;
using System.Threading;

namespace OpenLyricsClient.Frontend.Animation;

public class SmoothAnimator
namespace OpenLyricsClient.Frontend.Animation
{
public static float CalculateStep(float start, float end, float current, float speed)
public class SmoothAnimator
{
float divisor = (end - start);

if (divisor == 0)
return end;

if (divisor == 0)
divisor = 0.1F;

if (current == 0)
current = 1;

float t = (current - start) / divisor;

float speedC = (1.0F / 100.0F) * speed;
float sMul = 1 + speedC;

t = SmoothStep(start, end, t) / sMul;

return (start + (end - start) * t);
}
public static double CalculateStep(double start, double end, double current, double speed)
{
double divisor = (end - start);

private static float SmoothStep(float start, float end, float t)
{
t = Math.Clamp(((t - start) / ((end - start))), 0, 1);
return t * t * (3 - 2 * t);
if (divisor == 0)
return end;

divisor = Math.Abs(divisor) < 0.0001 ? 0.1 : divisor;

if (current == 0)
current = 1;

double t = (current - start) / divisor;

double speedC = (1.0 / 100.0) * speed;
double sMul = 1 + speedC;

t = SmoothStep(start, end, t) / sMul;

return (start + (end - start) * t);
}

private static double SmoothStep(double start, double end, double t)
{
t = Math.Clamp(((t - start) / ((end - start))), 0, 1);
return t * t * (3 - 2 * t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using OpenLyricsClient.Backend.Structure.Lyrics;
using OpenLyricsClient.Backend.Structure.Song;
using OpenLyricsClient.Backend.Utils;
using OpenLyricsClient.Frontend.Models.Pages.Settings;
using Squalr.Engine.Utils.Extensions;

namespace OpenLyricsClient.Frontend.Models.Custom;
Expand Down Expand Up @@ -60,6 +61,9 @@ public LyricsScrollerViewModel()

Core.INSTANCE.SettingsHandler.SettingsChanged += (sender, args) =>
{
if (!args.Section.Equals(typeof(SettingsLyricsViewModel)))
return;
this.CurrentLyricParts = null;
OnPropertyChanged("UiBackground");
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using OpenLyricsClient.Backend;
using OpenLyricsClient.Backend.Events.EventArgs;
using OpenLyricsClient.Backend.Structure.Lyrics;

namespace OpenLyricsClient.Frontend.Models.Custom;

public class NewLyricsScrollerViewModel : ViewModelBase, INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;

private LyricPart? _lyric;

public NewLyricsScrollerViewModel()
{
Core.INSTANCE.LyricHandler.LyricChanged += LyricHandlerOnLyricChanged;
Core.INSTANCE.LyricHandler.LyricsFound += LyricHandlerOnLyricsFound;
Core.INSTANCE.SongHandler.SongChanged += SongHandlerOnSongChanged;
}

private void SongHandlerOnSongChanged(object sender, SongChangedEventArgs songchangedevent)
{
Lyric = null;
OnPropertyChanged("Lyrics");
}

private void LyricHandlerOnLyricsFound(object sender)
{
OnPropertyChanged("Lyrics");
}

private void LyricHandlerOnLyricChanged(object sender, LyricChangedEventArgs lyricchangedeventargs)
{
Lyric = lyricchangedeventargs.LyricPart;
}

public LyricPart[]? Lyrics
{
get => Core.INSTANCE?.SongHandler?.CurrentSong?.Lyrics?.LyricParts!;
}

public LyricPart? Lyric
{
get => this._lyric;
set => SetField(ref this._lyric, value);
}

protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
}
16 changes: 14 additions & 2 deletions OpenLyricsClient/Frontend/Models/Elements/LyricsCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class LyricsCard : TemplatedControl, INotifyPropertyChanged
private NoteAnimation _noteAnimation;
private BlurArea _blurArea;

private LyricPart _lyricPart;
private LyricPart? _lyricPart;
private bool _current;
private double _oldValue;

Expand Down Expand Up @@ -94,6 +94,11 @@ public LyricsCard()
this._alreadySet = false;
};

Core.INSTANCE.SongHandler.SongUpdated += sender =>
{
};

/*LyricsScroller.INSTANCE.BlurChanged += (sender, @event) =>
{
if (@event.LyricPart.Equals(this.LyricPart))
Expand All @@ -112,6 +117,11 @@ public LyricsCard()
Dispatcher.UIThread.InvokeAsync(() =>
{
if (!DataValidator.ValidateData(this._lyricPart))
return;
Percentage = this._lyricPart.Percentage;
/*if (DataValidator.ValidateData(this._blurArea))
{
this._blurArea.Sigma = this.BlurSigma;
Expand Down Expand Up @@ -215,7 +225,9 @@ public double Percentage
if (this._oldValue == value)
return;

this._noteAnimation.Percentage = value;
if (DataValidator.ValidateData(this._noteAnimation))
this._noteAnimation.Percentage = value;

this._oldValue = value;

SetValue(PercentageProperty, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private void RenderTimerOnTick(TimeSpan obj)
float start = this._scrollTo;
float end = this._oldScrollY;

float y = SmoothAnimator.CalculateStep(
float y = (float)SmoothAnimator.CalculateStep(
start,
end,
this._currentScrollOffset,
Expand Down
57 changes: 57 additions & 0 deletions OpenLyricsClient/Frontend/View/Custom/NewLyricsScroller.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<UserControl xmlns="https://github.com/avaloniaui"
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:custom="clr-namespace:OpenLyricsClient.Frontend.Models.Custom"
xmlns:elements="clr-namespace:OpenLyricsClient.Frontend.Models.Elements"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="OpenLyricsClient.Frontend.View.Custom.NewLyricsScroller">

<UserControl.DataContext>
<custom:NewLyricsScrollerViewModel/>
</UserControl.DataContext>

<Grid>
<custom:CustomScrollViewer Name="CTRL_Viewer">
<Panel>
<ItemsRepeater Name="HIDDEN_CTRL_Repeater"
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>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>

<ItemsRepeater Name="CTRL_Repeater"
Items="{Binding Lyrics}">
<ItemsRepeater.ItemTemplate>
<DataTemplate>
<elements:LyricsCard Text="{Binding Part}"
LyricPart="{Binding}"
IgnoreEvents="False"
HorizontalAlignment="Left"
FontWeight="Bold"
FontSize="30"
Margin="0,0,0,70"/>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
<ItemsRepeater.Transitions>
<Transitions>
<ThicknessTransition Property="Margin" Duration="0:0:0.5" Easing="CubicEaseOut" />
<DoubleTransition Property="Opacity" Duration="0:0:5.0" Easing="CubicEaseOut"></DoubleTransition>
</Transitions>
</ItemsRepeater.Transitions>
</ItemsRepeater>
</Panel>
</custom:CustomScrollViewer>
</Grid>

</UserControl>
Loading

0 comments on commit 0340576

Please sign in to comment.