Skip to content

Commit

Permalink
Fix texture dropdown being offset by 1 after playing a map
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlackParrot committed Jan 13, 2025
1 parent 61b19d1 commit 1443a61
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 60 deletions.
57 changes: 4 additions & 53 deletions NoteTweaks/Managers/TextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public static Color CheckForInversion(ref this Color color, bool isBomb = false)

internal abstract class Textures
{
private static readonly string[] FileExtensions = { ".png", ".jpg", ".tga" };
private static readonly string ImagePath = Path.Combine(UnityGame.UserDataPath, "NoteTweaks", "Textures", "Notes");
internal static readonly string[] FileExtensions = { ".png", ".jpg", ".tga" };
internal static readonly string ImagePath = Path.Combine(UnityGame.UserDataPath, "NoteTweaks", "Textures", "Notes");

private static readonly Texture2D OriginalArrowGlowTexture = Resources.FindObjectsOfTypeAll<Texture2D>().ToList().First(x => x.name == "ArrowGlow");
internal static readonly Texture2D ReplacementArrowGlowTexture = OriginalArrowGlowTexture.PrepareTexture();
Expand All @@ -41,8 +41,8 @@ internal abstract class Textures
private static readonly Cubemap OriginalNoteTexture = Resources.FindObjectsOfTypeAll<Cubemap>().ToList().First(x => x.name == "NotesReflection");
private static Cubemap _noteTexture = OriginalNoteTexture;
private static Cubemap _bombTexture = OriginalNoteTexture;
private static readonly List<KeyValuePair<string, CubemapFace>> FaceNames = new List<KeyValuePair<string, CubemapFace>>

internal static readonly List<KeyValuePair<string, CubemapFace>> FaceNames = new List<KeyValuePair<string, CubemapFace>>
{
new KeyValuePair<string, CubemapFace>("px", CubemapFace.PositiveX),
new KeyValuePair<string, CubemapFace>("py", CubemapFace.PositiveY),
Expand All @@ -60,55 +60,6 @@ public static string GetLoadedBombTexture()
{
return _bombTexture.name.Split("_"[0]).Last();
}

internal static void LoadTextureChoices()
{
Plugin.Log.Info("Setting texture filenames for dropdown...");
SettingsViewController.NoteTextureChoices.Clear();

if (!Directory.Exists(ImagePath))
{
Directory.CreateDirectory(ImagePath);
}

string[] dirs = Directory.GetDirectories(ImagePath);
foreach (string dir in dirs)
{
int count = 0;

FaceNames.ForEach(pair =>
{
foreach (string extension in FileExtensions)
{
string path = $"{dir}/{pair.Key}{extension}";
if (File.Exists(path))
{
count++;
break;
}
}
});

if (count == 6)
{
SettingsViewController.NoteTextureChoices.Add(dir.Split('\\').Last());
}
}

string[] files = Directory.GetFiles(ImagePath);
foreach (string file in files)
{
if (FileExtensions.Contains(Path.GetExtension(file).ToLower()))
{
SettingsViewController.NoteTextureChoices.Add(Path.GetFileNameWithoutExtension(file));
}
}

SettingsViewController.NoteTextureChoices.Sort();
SettingsViewController.NoteTextureChoices = SettingsViewController.NoteTextureChoices.Prepend("Default").ToList();

Plugin.Log.Info("Set texture filenames");
}

private static void OnNoteImageLoaded(List<KeyValuePair<string, Texture2D>> textures)
{
Expand Down
3 changes: 1 addition & 2 deletions NoteTweaks/UI/PreviewViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class NotePreviewViewController : BSMLAutomaticViewController
private static readonly int Color0 = Shader.PropertyToID("_Color");
private static readonly int Color1 = Shader.PropertyToID("_SimpleColor");

private static readonly List<string> FaceNames = new List<string> { "NoteArrow", "NoteCircleGlow", "Circle" };
internal static readonly List<string> FaceNames = new List<string> { "NoteArrow", "NoteCircleGlow", "Circle" };
private static readonly List<string> GlowNames = new List<string> { "NoteArrowGlow", "AddedNoteCircleGlow" };

public NotePreviewViewController()
Expand Down Expand Up @@ -645,7 +645,6 @@ protected void OnEnable()
NoteContainer = new GameObject("_NoteTweaks_NoteContainer");
DontDestroyOnLoad(NoteContainer);
}
Managers.Textures.LoadTextureChoices();

if (HasInitialized)
{
Expand Down
85 changes: 80 additions & 5 deletions NoteTweaks/UI/SettingsViewController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BeatSaberMarkupLanguage.Attributes;
using BeatSaberMarkupLanguage.Components.Settings;
using BeatSaberMarkupLanguage.ViewControllers;
using JetBrains.Annotations;
using NoteTweaks.Configuration;
using NoteTweaks.Managers;
using UnityEngine;
using Zenject;

Expand Down Expand Up @@ -481,7 +485,7 @@ protected string NoteTexture
_config.NoteTexture = value;
if (LoadTextures)
{
_ = Managers.Textures.LoadNoteTexture(value);
_ = Textures.LoadNoteTexture(value);
}
}
}
Expand Down Expand Up @@ -514,7 +518,7 @@ protected string BombTexture
_config.BombTexture = value;
if (LoadTextures)
{
_ = Managers.Textures.LoadNoteTexture(value, true);
_ = Textures.LoadNoteTexture(value, true);
}
}
}
Expand All @@ -537,7 +541,7 @@ protected bool InvertBombTexture
_config.InvertBombTexture = value;
if (LoadTextures)
{
_ = Managers.Textures.LoadNoteTexture(Plugin.Config.BombTexture, true);
_ = Textures.LoadNoteTexture(Plugin.Config.BombTexture, true);
}
}
}
Expand All @@ -550,12 +554,83 @@ protected bool InvertNoteTexture
_config.InvertNoteTexture = value;
if (LoadTextures)
{
_ = Managers.Textures.LoadNoteTexture(Plugin.Config.NoteTexture);
_ = Textures.LoadNoteTexture(Plugin.Config.NoteTexture);
}
}
}

[UIComponent("selectedNoteTexture")]
public DropDownListSetting noteTextureDropDown;

[UIValue("noteTextureChoices")]
internal static List<object> NoteTextureChoices = new List<object>();
private List<object> NoteTextureChoices => LoadTextureChoices();

[UIAction("#post-parse")]
public void UpdateTextureList()
{
UpdateTextureChoices();
}

private void UpdateTextureChoices()
{
if (noteTextureDropDown == null)
{
return;
}

noteTextureDropDown.Values = NoteTextureChoices;
noteTextureDropDown.UpdateChoices();
}

private List<object> LoadTextureChoices()
{
Plugin.Log.Info("Setting texture filenames for dropdown...");
List<object> choices = new List<object>();

if (!Directory.Exists(Textures.ImagePath))
{
Directory.CreateDirectory(Textures.ImagePath);
}

string[] dirs = Directory.GetDirectories(Textures.ImagePath);
foreach (string dir in dirs)
{
int count = 0;

Textures.FaceNames.ForEach(pair =>
{
foreach (string extension in Textures.FileExtensions)
{
string path = $"{dir}/{pair.Key}{extension}";
if (File.Exists(path))
{
count++;
break;
}
}
});

if (count == 6)
{
choices.Add(dir.Split('\\').Last());
}
}

string[] files = Directory.GetFiles(Textures.ImagePath);
foreach (string file in files)
{
if (Textures.FileExtensions.Contains(Path.GetExtension(file).ToLower()))
{
choices.Add(Path.GetFileNameWithoutExtension(file));
}
}

choices.Sort();
choices = choices.Prepend("Default").ToList();

Plugin.Log.Info("Set texture filenames");

return choices;
}
}
}

0 comments on commit 1443a61

Please sign in to comment.