From c615902702cd39cfe3cf01e2f8e49a76312a51f2 Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 12 Mar 2024 16:01:38 -0400 Subject: [PATCH] Subtitles can now be larger, added subtitle to api (outsider compat) --- NewHorizons/Handlers/SubtitlesHandler.cs | 74 +++++++++++++++++------- NewHorizons/INewHorizons.cs | 8 +++ NewHorizons/NewHorizonsApi.cs | 2 + 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/NewHorizons/Handlers/SubtitlesHandler.cs b/NewHorizons/Handlers/SubtitlesHandler.cs index de283b54..04706388 100644 --- a/NewHorizons/Handlers/SubtitlesHandler.cs +++ b/NewHorizons/Handlers/SubtitlesHandler.cs @@ -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().alpha = 1; - graphic = GetComponent(); - image = GetComponent(); - - graphic.enabled = true; - image.enabled = true; - + var image = GetComponent(); eoteSprite = image.sprite; + image.sprite = null; + image.enabled = false; + var layout = GetComponent(); + 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(); + _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(); 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) { @@ -111,7 +143,7 @@ namespace NewHorizons.Handlers if (fadingAway) { fade -= fadeSpeed; - + if (fade <= 0) { fade = 0; @@ -122,7 +154,7 @@ namespace NewHorizons.Handlers else { fade += fadeSpeed; - + if (fade >= 1) { fade = 1; @@ -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; } } } diff --git a/NewHorizons/INewHorizons.cs b/NewHorizons/INewHorizons.cs index 22132568..4dfd2d1f 100644 --- a/NewHorizons/INewHorizons.cs +++ b/NewHorizons/INewHorizons.cs @@ -207,5 +207,13 @@ namespace NewHorizons /// string GetTranslationForOtherText(string text); #endregion + + /// + /// Registers a subtitle for the main menu. + /// Call this once before the main menu finishes loading + /// + /// + /// + void AddSubtitle(IModBehaviour mod, string filePath); } } diff --git a/NewHorizons/NewHorizonsApi.cs b/NewHorizons/NewHorizonsApi.cs index a96673d5..6a165fa2 100644 --- a/NewHorizons/NewHorizonsApi.cs +++ b/NewHorizons/NewHorizonsApi.cs @@ -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); } }