Subtitles can now be larger, added subtitle to api (outsider compat)

This commit is contained in:
Nick 2024-03-12 16:01:38 -04:00
parent b1b7f43905
commit c615902702
3 changed files with 64 additions and 20 deletions

View File

@ -14,9 +14,6 @@ namespace NewHorizons.Handlers
public static int SUBTITLE_HEIGHT = 97;
public static int SUBTITLE_WIDTH = 669; // nice
public Graphic graphic;
public Image image;
public float fadeSpeed = 0.005f;
public float fade = 1;
public bool fadingAway = true;
@ -29,13 +26,27 @@ namespace NewHorizons.Handlers
public static readonly int PAUSE_TIMER_MAX = 50;
public int pauseTimer = PAUSE_TIMER_MAX;
private Image _subtitleDisplay;
private Graphic _graphic;
private static List<(IModBehaviour mod, string filePath)> _additionalSubtitles = new();
public static void RegisterAdditionalSubtitle(IModBehaviour mod, string filePath)
{
_additionalSubtitles.Add((mod, filePath));
}
public void CheckForEOTE()
{
if (!eoteSubtitleHasBeenInserted)
{
if (Main.HasDLC)
{
if (eoteSprite != null) possibleSubtitles.Insert(0, eoteSprite); // ensure that the Echoes of the Eye subtitle always appears first
if (eoteSprite != null)
{
// Don't make it appear first actually because we have mods to display!
possibleSubtitles.Add(eoteSprite);
}
eoteSubtitleHasBeenInserted = true;
}
}
@ -43,18 +54,24 @@ namespace NewHorizons.Handlers
public void Start()
{
// We preserve the current image to add it to our custom subtitle
// We also need this element to preserve its size
GetComponent<CanvasGroup>().alpha = 1;
graphic = GetComponent<Graphic>();
image = GetComponent<UnityEngine.UI.Image>();
graphic.enabled = true;
image.enabled = true;
var image = GetComponent<Image>();
eoteSprite = image.sprite;
image.sprite = null;
image.enabled = false;
var layout = GetComponent<LayoutElement>();
layout.minHeight = SUBTITLE_HEIGHT;
CheckForEOTE();
image.sprite = null; // Just in case. I don't know how not having the dlc changes the subtitle game object
// We add our subtitles as a child object so that their sizing doesnt shift the layout of the main menu
_subtitleDisplay = new GameObject().AddComponent<Image>();
_subtitleDisplay.transform.parent = transform;
_subtitleDisplay.transform.localPosition = new Vector3(0, 0, 0);
_subtitleDisplay.transform.localScale = new Vector3(0.75f, 0.75f, 0.75f);
_graphic = _subtitleDisplay.gameObject.GetAddComponent<Graphic>();
AddSubtitles();
}
@ -73,6 +90,10 @@ namespace NewHorizons.Handlers
AddSubtitle(mod, "subtitle.png");
}
}
foreach (var pair in _additionalSubtitles)
{
AddSubtitle(pair.mod, pair.filePath);
}
}
public void AddSubtitle(IModBehaviour mod, string filepath)
@ -82,7 +103,7 @@ namespace NewHorizons.Handlers
var tex = ImageUtilities.GetTexture(mod, filepath, false);
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, Mathf.Max(SUBTITLE_HEIGHT, tex.height)), new Vector2(0.5f, 0.5f), 100.0f);
AddSubtitle(sprite);
}
@ -95,12 +116,23 @@ namespace NewHorizons.Handlers
{
CheckForEOTE();
if (possibleSubtitles.Count == 0) return;
if (possibleSubtitles.Count == 0)
{
return;
}
if (image.sprite == null) image.sprite = possibleSubtitles[0];
_subtitleDisplay.transform.localPosition = new Vector3(0, -36, 0);
if (_subtitleDisplay.sprite == null)
{
_subtitleDisplay.sprite = possibleSubtitles[0];
}
// don't fade transition subtitles if there's only one subtitle
if (possibleSubtitles.Count <= 1) return;
if (possibleSubtitles.Count <= 1)
{
return;
}
if (pauseTimer > 0)
{
@ -131,14 +163,16 @@ namespace NewHorizons.Handlers
}
}
graphic.color = new Color(1, 1, 1, fade);
_graphic.color = new Color(1, 1, 1, fade);
}
public void ChangeSubtitle()
{
subtitleIndex = (subtitleIndex + 1) % possibleSubtitles.Count;
image.sprite = possibleSubtitles[subtitleIndex];
_subtitleDisplay.sprite = possibleSubtitles[subtitleIndex];
var ratio = SUBTITLE_WIDTH / _subtitleDisplay.sprite.texture.width;
_subtitleDisplay.rectTransform.sizeDelta = new Vector2(_subtitleDisplay.sprite.texture.width, _subtitleDisplay.sprite.texture.height) * ratio;
}
}
}

View File

@ -207,5 +207,13 @@ namespace NewHorizons
/// <returns></returns>
string GetTranslationForOtherText(string text);
#endregion
/// <summary>
/// Registers a subtitle for the main menu.
/// Call this once before the main menu finishes loading
/// </summary>
/// <param name="mod"></param>
/// <param name="filePath"></param>
void AddSubtitle(IModBehaviour mod, string filePath);
}
}

View File

@ -336,5 +336,7 @@ namespace NewHorizons
public string GetTranslationForUI(string text) => TranslationHandler.GetTranslation(text, TranslationHandler.TextType.UI);
public string GetTranslationForOtherText(string text) => TranslationHandler.GetTranslation(text, TranslationHandler.TextType.OTHER);
public void AddSubtitle(IModBehaviour mod, string filePath) => SubtitlesHandler.RegisterAdditionalSubtitle(mod, filePath);
}
}