No mipmaps on subtitles

This commit is contained in:
Nick 2022-06-19 18:19:24 -04:00
parent df9fc8121f
commit 56def6cd68
2 changed files with 125 additions and 120 deletions

View File

@ -1,34 +1,34 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using NewHorizons.Utility; using NewHorizons.Utility;
using OWML.Common; using OWML.Common;
using Logger = NewHorizons.Utility.Logger; using Logger = NewHorizons.Utility.Logger;
namespace NewHorizons.Handlers namespace NewHorizons.Handlers
{ {
class SubtitlesHandler : MonoBehaviour class SubtitlesHandler : MonoBehaviour
{ {
public static int SUBTITLE_HEIGHT = 97; public static int SUBTITLE_HEIGHT = 97;
public static int SUBTITLE_WIDTH = 669; // nice public static int SUBTITLE_WIDTH = 669; // nice
public Graphic graphic; public Graphic graphic;
public Image image; public Image image;
public float fadeSpeed = 0.005f; public float fadeSpeed = 0.005f;
public float fade = 1; public float fade = 1;
public bool fadingAway = true; public bool fadingAway = true;
public List<Sprite> possibleSubtitles = new List<Sprite>(); public List<Sprite> possibleSubtitles = new List<Sprite>();
public bool eoteSubtitleHasBeenInserted = false; public bool eoteSubtitleHasBeenInserted = false;
public Sprite eoteSprite; public Sprite eoteSprite;
public int subtitleIndex; public int subtitleIndex;
public System.Random randomizer; public System.Random randomizer;
public static readonly int PAUSE_TIMER_MAX = 50; public static readonly int PAUSE_TIMER_MAX = 50;
public int pauseTimer = PAUSE_TIMER_MAX; public int pauseTimer = PAUSE_TIMER_MAX;
public void CheckForEOTE() public void CheckForEOTE()
@ -41,107 +41,107 @@ namespace NewHorizons.Handlers
eoteSubtitleHasBeenInserted = true; eoteSubtitleHasBeenInserted = true;
} }
} }
} }
public void Start() public void Start()
{ {
randomizer = new System.Random(); randomizer = new System.Random();
GetComponent<CanvasGroup>().alpha = 1; GetComponent<CanvasGroup>().alpha = 1;
graphic = GetComponent<Graphic>(); graphic = GetComponent<Graphic>();
image = GetComponent<UnityEngine.UI.Image>(); image = GetComponent<UnityEngine.UI.Image>();
graphic.enabled = true; graphic.enabled = true;
image.enabled = true; image.enabled = true;
eoteSprite = image.sprite; eoteSprite = image.sprite;
CheckForEOTE(); CheckForEOTE();
image.sprite = null; // Just in case. I don't know how not having the dlc changes the subtitle game object image.sprite = null; // Just in case. I don't know how not having the dlc changes the subtitle game object
AddSubtitles(); AddSubtitles();
} }
private void AddSubtitles() private void AddSubtitles()
{ {
foreach (var mod in Main.MountedAddons.Where(mod => File.Exists($"{mod.ModHelper.Manifest.ModFolderPath}subtitle.png"))) foreach (var mod in Main.MountedAddons.Where(mod => File.Exists($"{mod.ModHelper.Manifest.ModFolderPath}subtitle.png")))
{ {
AddSubtitle(mod, "subtitle.png"); AddSubtitle(mod, "subtitle.png");
} }
} }
public void AddSubtitle(IModBehaviour mod, string filepath) public void AddSubtitle(IModBehaviour mod, string filepath)
{ {
Logger.Log($"Adding subtitle for {mod.ModHelper.Manifest.Name}"); Logger.Log($"Adding subtitle for {mod.ModHelper.Manifest.Name}");
var tex = ImageUtilities.GetTexture(mod, filepath); var tex = ImageUtilities.GetTexture(mod, filepath, false);
if (tex == null) return; if (tex == null) return;
var sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, SUBTITLE_HEIGHT), new Vector2(0.5f, 0.5f), 100.0f); var sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, SUBTITLE_HEIGHT), new Vector2(0.5f, 0.5f), 100.0f);
AddSubtitle(sprite); AddSubtitle(sprite);
} }
public void AddSubtitle(Sprite sprite) public void AddSubtitle(Sprite sprite)
{ {
possibleSubtitles.Add(sprite); possibleSubtitles.Add(sprite);
} }
public void Update() public void Update()
{ {
CheckForEOTE(); CheckForEOTE();
if (possibleSubtitles.Count == 0) return; if (possibleSubtitles.Count == 0) return;
if (image.sprite == null) image.sprite = possibleSubtitles[0]; if (image.sprite == null) image.sprite = possibleSubtitles[0];
// don't fade transition subtitles if there's only one subtitle // don't fade transition subtitles if there's only one subtitle
if (possibleSubtitles.Count <= 1) return; if (possibleSubtitles.Count <= 1) return;
if (pauseTimer > 0) if (pauseTimer > 0)
{ {
pauseTimer--; pauseTimer--;
return; return;
} }
if (fadingAway) if (fadingAway)
{ {
fade -= fadeSpeed; fade -= fadeSpeed;
if (fade <= 0) if (fade <= 0)
{ {
fade = 0; fade = 0;
ChangeSubtitle(); ChangeSubtitle();
fadingAway = false; fadingAway = false;
} }
} }
else else
{ {
fade += fadeSpeed; fade += fadeSpeed;
if (fade >= 1) if (fade >= 1)
{ {
fade = 1; fade = 1;
fadingAway = true; fadingAway = true;
pauseTimer = PAUSE_TIMER_MAX; pauseTimer = PAUSE_TIMER_MAX;
} }
} }
graphic.color = new Color(1, 1, 1, fade); graphic.color = new Color(1, 1, 1, fade);
} }
public void ChangeSubtitle() public void ChangeSubtitle()
{ {
// to pick a new random subtitle without requiring retries, we generate a random offset less than the length of the possible subtitles array // to pick a new random subtitle without requiring retries, we generate a random offset less than the length of the possible subtitles array
// we then add that offset to the current index, modulo NUMBER_OF_POSSIBLE_SUBTITLES // we then add that offset to the current index, modulo NUMBER_OF_POSSIBLE_SUBTITLES
// since the offset can never be NUMBER_OF_POSSIBLE_SUBTITLES, it will never wrap all the way back around to the initial subtitleIndex // since the offset can never be NUMBER_OF_POSSIBLE_SUBTITLES, it will never wrap all the way back around to the initial subtitleIndex
// note, this makes the code more confusing, but Random.Next(min, max) generates a random number on the range [min, max) // note, this makes the code more confusing, but Random.Next(min, max) generates a random number on the range [min, max)
// that is, the below code will generate numbers up to and including Count-1, not Count. // that is, the below code will generate numbers up to and including Count-1, not Count.
var newIndexOffset = randomizer.Next(1, possibleSubtitles.Count); var newIndexOffset = randomizer.Next(1, possibleSubtitles.Count);
subtitleIndex = (subtitleIndex + newIndexOffset) % possibleSubtitles.Count; subtitleIndex = (subtitleIndex + newIndexOffset) % possibleSubtitles.Count;
image.sprite = possibleSubtitles[subtitleIndex]; image.sprite = possibleSubtitles[subtitleIndex];
} }
} }
} }

View File

@ -15,6 +15,11 @@ namespace NewHorizons.Utility
private static List<Texture2D> _generatedTextures = new List<Texture2D>(); private static List<Texture2D> _generatedTextures = new List<Texture2D>();
public static Texture2D GetTexture(IModBehaviour mod, string filename) public static Texture2D GetTexture(IModBehaviour mod, string filename)
{
return GetTexture(mod, filename, true);
}
public static Texture2D GetTexture(IModBehaviour mod, string filename, bool useMipmaps)
{ {
// Copied from OWML but without the print statement lol // Copied from OWML but without the print statement lol
var path = mod.ModHelper.Manifest.ModFolderPath + filename; var path = mod.ModHelper.Manifest.ModFolderPath + filename;
@ -28,7 +33,7 @@ namespace NewHorizons.Utility
try try
{ {
var data = File.ReadAllBytes(path); var data = File.ReadAllBytes(path);
var texture = new Texture2D(2, 2); var texture = new Texture2D(2, 2, TextureFormat.RGBA32, useMipmaps);
texture.name = Path.GetFileNameWithoutExtension(path); texture.name = Path.GetFileNameWithoutExtension(path);
texture.LoadImage(data); texture.LoadImage(data);
_loadedTextures.Add(path, texture); _loadedTextures.Add(path, texture);