Skip to content

Commit

Permalink
Fix UI refresh, less stutter
Browse files Browse the repository at this point in the history
  • Loading branch information
Meivyn committed Jul 5, 2024
1 parent c9b01be commit ec50a29
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 177 deletions.
45 changes: 40 additions & 5 deletions PlaylistManager/AffinityPatches/LevelCollectionCellSetDataPatch.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using HMUI;
using PlaylistManager.Configuration;
using System;
using System.Collections.Generic;
using BeatSaberPlaylistsLib.Types;
using PlaylistManager.Utilities;
using HMUI;
using PlaylistManager.Configuration;
using PlaylistManager.UI;
using SiraUtil.Affinity;
using UnityEngine.UI;

/*
* Original Author: Auros
Expand All @@ -13,16 +16,28 @@ namespace PlaylistManager.AffinityPatches
{
internal class LevelCollectionCellSetDataPatch : IAffinity
{
private readonly Dictionary<IPlaylist, AnnotatedBeatmapLevelCollectionCell> eventTable = new();
private readonly HoverHintController hoverHintController;
private readonly PlaylistUpdater playlistUpdater;

public LevelCollectionCellSetDataPatch(HoverHintController hoverHintController)
public LevelCollectionCellSetDataPatch(HoverHintController hoverHintController, PlaylistUpdater playlistUpdater)
{
this.hoverHintController = hoverHintController;
this.playlistUpdater = playlistUpdater;
}

[AffinityPatch(typeof(AnnotatedBeatmapLevelCollectionCell), nameof(AnnotatedBeatmapLevelCollectionCell.SetData))]
private void Patch(AnnotatedBeatmapLevelCollectionCell __instance, ref BeatmapLevelPack beatmapLevelPack, ref Image ____coverImage)
private void Patch(AnnotatedBeatmapLevelCollectionCell __instance, ref BeatmapLevelPack beatmapLevelPack)
{
if (beatmapLevelPack is PlaylistLevelPack playlistLevelPack)
{
var playlist = playlistLevelPack.playlist;
eventTable.Remove(playlist);
eventTable.Add(playlist, __instance);
playlist.SpriteLoaded -= OnSpriteLoaded;
playlist.SpriteLoaded += OnSpriteLoaded;
}

if (PluginConfig.Instance.PlaylistHoverHints)
{
var hoverHint = __instance.GetComponent<HoverHint>();
Expand All @@ -36,5 +51,25 @@ private void Patch(AnnotatedBeatmapLevelCollectionCell __instance, ref BeatmapLe
hoverHint.text = beatmapLevelPack.packName;
}
}

private void OnSpriteLoaded(object sender, EventArgs e)
{
// TODO: Figure out why this doesn't seem to happen.
if (sender is not IPlaylist playlist)
{
return;
}

playlist.SpriteLoaded -= OnSpriteLoaded;

if (!eventTable.TryGetValue(playlist, out var tableCell) || tableCell == null || tableCell._beatmapLevelPack is not PlaylistLevelPack)
{
return;
}

tableCell._coverImage.sprite = playlist.SmallSprite;
// TODO: Figure out why this needs to be done here and in UpdatePlaylist when switching covers. Worth noting that this event is invoked twice as well.
playlistUpdater.RefreshAnnotatedBeatmapCollection(tableCell._beatmapLevelPack);
}
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion PlaylistManager/Installers/PlaylistManagerMenuInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override void InstallBindings()
Container.Bind<ChangelogViewController>().FromNewComponentAsViewController().AsSingle();
Container.BindInterfacesAndSelfTo<ContributorsViewController>().FromNewComponentAsViewController().AsSingle();

Container.BindInterfacesTo<CoverImageUpdater>().AsSingle();
Container.BindInterfacesAndSelfTo<PlaylistUpdater>().AsSingle();
Container.BindInterfacesAndSelfTo<DifficultyHighlighter>().AsSingle();

Container.BindInterfacesTo<RefreshButtonUI>().AsSingle();
Expand Down
8 changes: 8 additions & 0 deletions PlaylistManager/PlaylistManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\BGLib.DotnetExtension.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="BGLib.UnityExtension">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\BGLib.UnityExtension.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="BSML">
<HintPath>$(BeatSaberDir)\Plugins\BSML.dll</HintPath>
<Private>False</Private>
Expand All @@ -66,6 +70,10 @@
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\HMLib.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="HMRendering">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\HMRendering.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="HMUI">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\HMUI.dll</HintPath>
<Private>False</Private>
Expand Down
10 changes: 7 additions & 3 deletions PlaylistManager/UI/AllPacksRefresher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ namespace PlaylistManager.UI
public class AllPacksRefresher : IPMRefreshable
{
private readonly AnnotatedBeatmapLevelCollectionsViewController annotatedBeatmapLevelCollectionsViewController;
private BeatmapLevelsModel beatmapLevelsModel;
private readonly BeatmapLevelsModel beatmapLevelsModel;
private readonly PlaylistUpdater playlistUpdater;

public AllPacksRefresher(AnnotatedBeatmapLevelCollectionsViewController annotatedBeatmapLevelCollectionsViewController, BeatmapLevelsModel beatmapLevelsModel)
private AllPacksRefresher(AnnotatedBeatmapLevelCollectionsViewController annotatedBeatmapLevelCollectionsViewController, BeatmapLevelsModel beatmapLevelsModel, PlaylistUpdater playlistUpdater)
{
this.annotatedBeatmapLevelCollectionsViewController = annotatedBeatmapLevelCollectionsViewController;
this.beatmapLevelsModel = beatmapLevelsModel;
this.playlistUpdater = playlistUpdater;
}

public void Refresh()
{
var annotatedBeatmapLevelCollections = beatmapLevelsModel._customLevelsRepository.beatmapLevelPacks.Concat(PlaylistLibUtils.TryGetAllPlaylistsAsLevelPacks()).ToArray();
var playlistLevelPacks = PlaylistLibUtils.TryGetAllPlaylistsAsLevelPacks();
playlistUpdater.RefreshPlaylistChangedListeners(playlistLevelPacks);
var annotatedBeatmapLevelCollections = beatmapLevelsModel._customLevelsRepository.beatmapLevelPacks.Concat(playlistLevelPacks).ToArray();
var indexToSelect = Array.FindIndex(annotatedBeatmapLevelCollections, (pack) => pack.packID == annotatedBeatmapLevelCollectionsViewController.selectedAnnotatedBeatmapLevelPack.packID);
if (indexToSelect != -1)
{
Expand Down
94 changes: 0 additions & 94 deletions PlaylistManager/UI/CoverImageUpdater.cs

This file was deleted.

Loading

0 comments on commit ec50a29

Please sign in to comment.