From a9af62878ddb3603b5344189ae7bd864b6b854ac Mon Sep 17 00:00:00 2001 From: FreezeDriedMangoes Date: Sun, 22 May 2022 09:48:25 -0400 Subject: [PATCH 1/5] feat: added SubtitleHandler to allow mods to add custom subtitles --- .../External/Configs/StarSystemConfig.cs | 2 + NewHorizons/Handlers/SubtitlesHandler.cs | 109 ++++++++++++++++++ NewHorizons/Handlers/TitleSceneHandler.cs | 6 + NewHorizons/Main.cs | 6 + 4 files changed, 123 insertions(+) create mode 100644 NewHorizons/Handlers/SubtitlesHandler.cs diff --git a/NewHorizons/External/Configs/StarSystemConfig.cs b/NewHorizons/External/Configs/StarSystemConfig.cs index 196bc7b6..22201a15 100644 --- a/NewHorizons/External/Configs/StarSystemConfig.cs +++ b/NewHorizons/External/Configs/StarSystemConfig.cs @@ -4,6 +4,8 @@ namespace NewHorizons.External.Configs { public class StarSystemConfig { + public string subtitleFilepath; + [DefaultValue(true)] public bool canEnterViaWarpDrive = true; [DefaultValue(true)] public bool enableTimeLoop = true; [DefaultValue(true)] public bool destroyStockPlanets = true; diff --git a/NewHorizons/Handlers/SubtitlesHandler.cs b/NewHorizons/Handlers/SubtitlesHandler.cs new file mode 100644 index 00000000..a7966fd1 --- /dev/null +++ b/NewHorizons/Handlers/SubtitlesHandler.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; +using UnityEngine.InputSystem; +using UnityEngine.UI; +using NewHorizons.Utility; +using OWML.Common; + +namespace NewHorizons.Handlers +{ + class SubtitlesHandler : MonoBehaviour + { + public static int SUBTITLE_HEIGHT = 97; + public static int SUBTITLE_WIDTH = 669; // nice + + Graphic graphic; + Image image; + + public float fadeSpeed = 0.01f; + float fade = 1; + bool fadingAway = true; + + static List possibleSubtitles = new List(); + static bool eoteSubtitleHasBeenInserted = false; + int subtitleIndex; + + System.Random randomizer; + + public void Start() + { + randomizer = new System.Random(); + + graphic = GetComponent(); + image = GetComponent(); + + if (!eoteSubtitleHasBeenInserted) + { + possibleSubtitles.Insert(0, image.sprite); // ensure that the Echoes of the Eye subtitle always appears first + eoteSubtitleHasBeenInserted = true; + } + } + + public static void AddSubtitle(IModBehaviour mod, string filepath) + { + + var tex = ImageUtilities.GetTexture(mod, filepath); + if (tex == null) return; + + // var sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.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); + } + + public static void AddSubtitle(Sprite sprite) + { + possibleSubtitles.Add(sprite); + } + + public void Update() + { + if (fadingAway) + { + fade -= fadeSpeed; + + if (fade <= 0) + { + fade = 0; + ChangeSubtitle(); + fadingAway = false; + } + } + else + { + fade += fadeSpeed; + + if (fade >= 1) + { + fade = 1; + fadingAway = true; + } + } + + graphic.color = new Color(1, 1, 1, fade); + } + + 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 + // 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 + + // 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. + var newIndexOffset = randomizer.Next(1, possibleSubtitles.Count); + subtitleIndex = (subtitleIndex % newIndexOffset) % possibleSubtitles.Count; + + image.sprite = possibleSubtitles[subtitleIndex]; + } + } +} diff --git a/NewHorizons/Handlers/TitleSceneHandler.cs b/NewHorizons/Handlers/TitleSceneHandler.cs index f0e82aa0..2b8d0740 100644 --- a/NewHorizons/Handlers/TitleSceneHandler.cs +++ b/NewHorizons/Handlers/TitleSceneHandler.cs @@ -11,6 +11,12 @@ namespace NewHorizons.Handlers { public static class TitleSceneHandler { + public static void InitSubtitles() + { + GameObject subtitleContainer = GameObject.Find("TitleMenu/TitleCanvas/TitleLayoutGroup/Logo_EchoesOfTheEye"); + subtitleContainer.AddComponent(); + } + public static void DisplayBodyOnTitleScreen(List bodies) { //Try loading one planet why not diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index a1203e28..860117e8 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -199,6 +199,7 @@ namespace NewHorizons if (scene.name == "TitleScreen" && _useCustomTitleScreen) { TitleSceneHandler.DisplayBodyOnTitleScreen(BodyDict.Values.ToList().SelectMany(x => x).ToList()); + TitleSceneHandler.InitSubtitles(); } if (scene.name == "EyeOfTheUniverse" && IsWarping) @@ -313,6 +314,11 @@ namespace NewHorizons if (name != "SolarSystem") SetDefaultSystem(name); } + if (starSystemConfig.subtitleFilepath != null) + { + SubtitlesHandler.AddSubtitle(mod, starSystemConfig.subtitleFilepath); + } + var system = new NewHorizonsSystem(name, starSystemConfig, mod); SystemDict[name] = system; } From 4ae8d71f2c20c2d556e4139f13e8fc2cedff4e8e Mon Sep 17 00:00:00 2001 From: FreezeDriedMangoes Date: Sun, 22 May 2022 09:49:22 -0400 Subject: [PATCH 2/5] fix: disabled subtitle transitions when there's no subtitles to transition --- NewHorizons/Handlers/SubtitlesHandler.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NewHorizons/Handlers/SubtitlesHandler.cs b/NewHorizons/Handlers/SubtitlesHandler.cs index a7966fd1..a74e753d 100644 --- a/NewHorizons/Handlers/SubtitlesHandler.cs +++ b/NewHorizons/Handlers/SubtitlesHandler.cs @@ -67,6 +67,9 @@ namespace NewHorizons.Handlers public void Update() { + // don't fade transition subtitles if there's only one subtitle + if (possibleSubtitles.Count <= 1) return; + if (fadingAway) { fade -= fadeSpeed; From e636e2a84410252f963a93da292be5b7c27229a2 Mon Sep 17 00:00:00 2001 From: FreezeDriedMangoes Date: Sun, 22 May 2022 10:21:52 -0400 Subject: [PATCH 3/5] feat: renamed subtitleFilepath to subtitle, fixed a modulus that was supposed to be a plus, added a todo --- .../External/Configs/StarSystemConfig.cs | 2 +- NewHorizons/Handlers/SubtitlesHandler.cs | 19 +++++-------------- NewHorizons/Handlers/TitleSceneHandler.cs | 2 ++ NewHorizons/Main.cs | 4 ++-- 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/NewHorizons/External/Configs/StarSystemConfig.cs b/NewHorizons/External/Configs/StarSystemConfig.cs index 22201a15..9e6b902c 100644 --- a/NewHorizons/External/Configs/StarSystemConfig.cs +++ b/NewHorizons/External/Configs/StarSystemConfig.cs @@ -4,7 +4,7 @@ namespace NewHorizons.External.Configs { public class StarSystemConfig { - public string subtitleFilepath; + public string subtitle; [DefaultValue(true)] public bool canEnterViaWarpDrive = true; [DefaultValue(true)] public bool enableTimeLoop = true; diff --git a/NewHorizons/Handlers/SubtitlesHandler.cs b/NewHorizons/Handlers/SubtitlesHandler.cs index a74e753d..4bae3112 100644 --- a/NewHorizons/Handlers/SubtitlesHandler.cs +++ b/NewHorizons/Handlers/SubtitlesHandler.cs @@ -1,16 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; using UnityEngine; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using UnityEngine; -using UnityEngine.InputSystem; using UnityEngine.UI; using NewHorizons.Utility; using OWML.Common; @@ -44,6 +33,7 @@ namespace NewHorizons.Handlers if (!eoteSubtitleHasBeenInserted) { + // TODO: only insert if hasDLC possibleSubtitles.Insert(0, image.sprite); // ensure that the Echoes of the Eye subtitle always appears first eoteSubtitleHasBeenInserted = true; } @@ -103,8 +93,9 @@ namespace NewHorizons.Handlers // 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. - var newIndexOffset = randomizer.Next(1, possibleSubtitles.Count); - subtitleIndex = (subtitleIndex % newIndexOffset) % possibleSubtitles.Count; + var newIndexOffset = randomizer.Next(1, possibleSubtitles.Count); + subtitleIndex = (subtitleIndex + newIndexOffset) % possibleSubtitles.Count; + NewHorizons.Utility.Logger.Log("NEW SPRITE INDEX OFFSET " + newIndexOffset + " NEW SPRITE INDEX " + subtitleIndex); image.sprite = possibleSubtitles[subtitleIndex]; } diff --git a/NewHorizons/Handlers/TitleSceneHandler.cs b/NewHorizons/Handlers/TitleSceneHandler.cs index 2b8d0740..0f3149a9 100644 --- a/NewHorizons/Handlers/TitleSceneHandler.cs +++ b/NewHorizons/Handlers/TitleSceneHandler.cs @@ -15,6 +15,8 @@ namespace NewHorizons.Handlers { GameObject subtitleContainer = GameObject.Find("TitleMenu/TitleCanvas/TitleLayoutGroup/Logo_EchoesOfTheEye"); subtitleContainer.AddComponent(); + + // TODO: if no subtitleContainer found, make one } public static void DisplayBodyOnTitleScreen(List bodies) diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 860117e8..9a45ac13 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -314,9 +314,9 @@ namespace NewHorizons if (name != "SolarSystem") SetDefaultSystem(name); } - if (starSystemConfig.subtitleFilepath != null) + if (starSystemConfig.subtitle != null) { - SubtitlesHandler.AddSubtitle(mod, starSystemConfig.subtitleFilepath); + SubtitlesHandler.AddSubtitle(mod, starSystemConfig.subtitle); } var system = new NewHorizonsSystem(name, starSystemConfig, mod); From a42ef6f46a16fdff74313dcb7b3671956a95630a Mon Sep 17 00:00:00 2001 From: FreezeDriedMangoes Date: Sun, 22 May 2022 10:32:56 -0400 Subject: [PATCH 4/5] feat: changed fade timer constants --- NewHorizons/Handlers/SubtitlesHandler.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/NewHorizons/Handlers/SubtitlesHandler.cs b/NewHorizons/Handlers/SubtitlesHandler.cs index 4bae3112..64e0759d 100644 --- a/NewHorizons/Handlers/SubtitlesHandler.cs +++ b/NewHorizons/Handlers/SubtitlesHandler.cs @@ -14,7 +14,7 @@ namespace NewHorizons.Handlers Graphic graphic; Image image; - public float fadeSpeed = 0.01f; + public float fadeSpeed = 0.005f; float fade = 1; bool fadingAway = true; @@ -24,6 +24,9 @@ namespace NewHorizons.Handlers System.Random randomizer; + static readonly int PAUSE_TIMER_MAX = 50; + int pauseTimer = PAUSE_TIMER_MAX; + public void Start() { randomizer = new System.Random(); @@ -60,6 +63,12 @@ namespace NewHorizons.Handlers // don't fade transition subtitles if there's only one subtitle if (possibleSubtitles.Count <= 1) return; + if (pauseTimer > 0) + { + pauseTimer--; + return; + } + if (fadingAway) { fade -= fadeSpeed; @@ -79,6 +88,7 @@ namespace NewHorizons.Handlers { fade = 1; fadingAway = true; + pauseTimer = PAUSE_TIMER_MAX; } } @@ -95,7 +105,6 @@ namespace NewHorizons.Handlers // that is, the below code will generate numbers up to and including Count-1, not Count. var newIndexOffset = randomizer.Next(1, possibleSubtitles.Count); subtitleIndex = (subtitleIndex + newIndexOffset) % possibleSubtitles.Count; - NewHorizons.Utility.Logger.Log("NEW SPRITE INDEX OFFSET " + newIndexOffset + " NEW SPRITE INDEX " + subtitleIndex); image.sprite = possibleSubtitles[subtitleIndex]; } From 3ec36b7b188e42a8cb66a8762f3f175001936de7 Mon Sep 17 00:00:00 2001 From: FreezeDriedMangoes Date: Sun, 22 May 2022 11:19:57 -0400 Subject: [PATCH 5/5] feat: handling for when the player does not have the dlc --- NewHorizons/Handlers/SubtitlesHandler.cs | 12 +++++++++--- NewHorizons/Handlers/TitleSceneHandler.cs | 10 ++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/NewHorizons/Handlers/SubtitlesHandler.cs b/NewHorizons/Handlers/SubtitlesHandler.cs index 64e0759d..26cdf081 100644 --- a/NewHorizons/Handlers/SubtitlesHandler.cs +++ b/NewHorizons/Handlers/SubtitlesHandler.cs @@ -31,20 +31,24 @@ namespace NewHorizons.Handlers { randomizer = new System.Random(); + GetComponent().alpha = 1; graphic = GetComponent(); image = GetComponent(); + graphic.enabled = true; + image.enabled = true; + + if (!Main.HasDLC) image.sprite = null; // Just in case. I don't know how not having the dlc changes the subtitle game object + if (!eoteSubtitleHasBeenInserted) { - // TODO: only insert if hasDLC - possibleSubtitles.Insert(0, image.sprite); // ensure that the Echoes of the Eye subtitle always appears first + if (image.sprite != null) possibleSubtitles.Insert(0, image.sprite); // ensure that the Echoes of the Eye subtitle always appears first eoteSubtitleHasBeenInserted = true; } } public static void AddSubtitle(IModBehaviour mod, string filepath) { - var tex = ImageUtilities.GetTexture(mod, filepath); if (tex == null) return; @@ -60,6 +64,8 @@ namespace NewHorizons.Handlers public void Update() { + if (image.sprite == null) image.sprite = possibleSubtitles[0]; + // don't fade transition subtitles if there's only one subtitle if (possibleSubtitles.Count <= 1) return; diff --git a/NewHorizons/Handlers/TitleSceneHandler.cs b/NewHorizons/Handlers/TitleSceneHandler.cs index 0f3149a9..9a28229b 100644 --- a/NewHorizons/Handlers/TitleSceneHandler.cs +++ b/NewHorizons/Handlers/TitleSceneHandler.cs @@ -14,9 +14,15 @@ namespace NewHorizons.Handlers public static void InitSubtitles() { GameObject subtitleContainer = GameObject.Find("TitleMenu/TitleCanvas/TitleLayoutGroup/Logo_EchoesOfTheEye"); - subtitleContainer.AddComponent(); + + if (subtitleContainer == null) + { + Logger.LogError("No subtitle container found! Failed to load subtitles."); + return; + } - // TODO: if no subtitleContainer found, make one + subtitleContainer.SetActive(true); + subtitleContainer.AddComponent(); } public static void DisplayBodyOnTitleScreen(List bodies)