From e8457dafc86f9e3571ca11c2a486c5ca9b2126b5 Mon Sep 17 00:00:00 2001 From: Adam Kauffman Date: Wed, 27 Oct 2021 22:09:41 -0700 Subject: [PATCH] Changing HudScale from pause menu works now Remove redundant `\` from build event OwmlDir can be dynamic with %appdata% Make UniqueName match Remove events from list when they have passed RecalculatePosition from Configure to fix HudScale Add comments Organize code blocks --- ClockLib/ClockLib.csproj | 18 +-- ClockLib/ClockLib.csproj.user | 6 +- ClockLib/OWClock.cs | 177 ++++++++++++++++++++-------- ClockLib/Properties/AssemblyInfo.cs | 8 +- ClockLib/manifest.json | 2 +- 5 files changed, 148 insertions(+), 63 deletions(-) diff --git a/ClockLib/ClockLib.csproj b/ClockLib/ClockLib.csproj index 474d710..c83021a 100644 --- a/ClockLib/ClockLib.csproj +++ b/ClockLib/ClockLib.csproj @@ -116,19 +116,19 @@ rmdir /s /q "$(OwmlDir)\Mods\$(ModUniqueName)" md "$(OwmlDir)\Mods\$(ModUniqueName)" copy /y "$(TargetPath)" "$(OwmlDir)\Mods\$(ModUniqueName)" - copy /y "$(ProjectDir)\default-config.json" "$(OwmlDir)\Mods\$(ModUniqueName)" - copy /y "$(ProjectDir)\manifest.json" "$(OwmlDir)\Mods\$(ModUniqueName)" - copy /y "$(ProjectDir)\events.json" "$(OwmlDir)\Mods\$(ModUniqueName)" + copy /y "$(ProjectDir)default-config.json" "$(OwmlDir)\Mods\$(ModUniqueName)" + copy /y "$(ProjectDir)manifest.json" "$(OwmlDir)\Mods\$(ModUniqueName)" + copy /y "$(ProjectDir)events.json" "$(OwmlDir)\Mods\$(ModUniqueName)" - md "$(ProjectDir)\Release" - copy /y "$(ProjectDir)\default-config.json" "$(ProjectDir)\Release" - copy /y "$(ProjectDir)\manifest.json" "$(ProjectDir)\Release" - copy /y "$(ProjectDir)\events.json" "$(ProjectDir)\Release" - copy /y "$(ProjectDir)\bin\Release\Clock.dll" "$(ProjectDir)\Release" + md "$(ProjectDir)Release" + copy /y "$(ProjectDir)default-config.json" "$(ProjectDir)Release" + copy /y "$(ProjectDir)manifest.json" "$(ProjectDir)Release" + copy /y "$(ProjectDir)events.json" "$(ProjectDir)Release" + copy /y "$(ProjectDir)bin\Release\Clock.dll" "$(ProjectDir)Release" cd "$(ProjectDir)" tar.exe -a -c -f Release.zip "Release" rmdir /s /q "$(ProjectDir)\Release" - + diff --git a/ClockLib/ClockLib.csproj.user b/ClockLib/ClockLib.csproj.user index b4ef6a3..480e8da 100644 --- a/ClockLib/ClockLib.csproj.user +++ b/ClockLib/ClockLib.csproj.user @@ -2,8 +2,8 @@ ProjectFiles - E:\Program Files\Epic Games\OuterWilds - C:\Users\Jamie\AppData\Roaming\OuterWildsModManager\OWML - OWClock + C:\Program Files (x86)\Steam\steamapps\common\Outer Wilds + %appdata%\OuterWildsModManager\OWML + clubby789.OWClock \ No newline at end of file diff --git a/ClockLib/OWClock.cs b/ClockLib/OWClock.cs index 8d5d01e..9280329 100644 --- a/ClockLib/OWClock.cs +++ b/ClockLib/OWClock.cs @@ -3,6 +3,7 @@ using OWML.ModHelper; using System; using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace Clock @@ -10,12 +11,13 @@ namespace Clock public class OWClock : ModBehaviour { private static EventFile _save; - private readonly List _eventListStr = new List(); - private readonly List> _eventList = new List>(); + private List _eventList; private Font _hudFont; private float _xPos; private float _yPos; private float _width; + private int _displayHeight; + private int _displayWidth; internal static IModHelper Helper; public static bool CountUp { get; private set; } @@ -25,43 +27,34 @@ public class OWClock : ModBehaviour public static List EnabledTypes { get; private set; } = new List(); public static EventFile Save { get => _save; set => _save = value; } + #region "Magic methods from Unity MonoBehaviour" +#pragma warning disable IDE0051 // Remove unused private members + /// + /// Called once when the mod is loaded + /// private void Start() +#pragma warning restore IDE0051 // Remove unused private members { Helper = ModHelper; Save = EventFile.LoadSaveFile(); _hudFont = Resources.Load(@"fonts/english - latin/SpaceMono-Regular_Dynamic"); ModHelper.Menus.PauseMenu.OnInit += AddMenuItem; - ModHelper.Console.WriteLine($"My mod {nameof(Clock)} is loaded!", MessageType.Success); + ModHelper.Console.WriteLine($"OWClock mod loaded at " + DateTime.Now.ToString("s"), MessageType.Success); - GlobalMessenger.AddListener("GraphicSettingsUpdated", RecalculatePosition); - } - - private void AddMenuItem() - { - var eventMenu = ModHelper.Menus.PauseMenu.Copy("ADD EVENT"); - var openInputButton = ModHelper.Menus.PauseMenu.ResumeButton.Duplicate("ADD EVENT"); - openInputButton.OnClick += EventPopup; - - var eventMenu2 = ModHelper.Menus.PauseMenu.Copy("DEBUG TIME"); - var openInputButton2 = ModHelper.Menus.PauseMenu.ResumeButton.Duplicate("DEBUG TIME"); - openInputButton2.OnClick += LogTime; - } + GlobalMessenger.AddListener("GraphicSettingsUpdated", GetDisplaySettings); - private void LogTime() - { - var currentTime = TimeLoop.GetSecondsElapsed(); - ModHelper.Console.WriteLine($"Time is {currentTime}"); - } - - private void RecalculatePosition(GraphicSettings settings) - { - _yPos = settings.displayResHeight - 60f; - _xPos = Milliseconds ? settings.displayResWidth * (1-HudScale/100) - 80f : settings.displayResWidth * (1 - HudScale / 100) - 20f; - _width = settings.displayResWidth * (HudScale/100); + // We need the wake event to reload our eventlist because we are going to remove expired items from the list as they pass. + ModHelper.Events.Subscribe(Events.AfterAwake); + ModHelper.Events.Event += OnEvent; } +#pragma warning disable IDE0051 // Remove unused private members + /// + /// OnGUI is called for rendering and handling GUI events by Unity. + /// private void OnGUI() +#pragma warning restore IDE0051 // Remove unused private members { if (GUIMode.IsHiddenMode() || PlayerState.UsingShipComputer()) { @@ -88,14 +81,17 @@ private void OnGUI() style.fontSize = 20; int shown = 0; + // Loop until desired number of events are shown // OR we reach end of list float yOff = 0; - for (int i = 0; (i < Save.eventList.Count) && (shown < EventCount); i++) + for (int i = 0; (i < _eventList.Count) && (shown < EventCount); i++) { - var timeEvent = Save.eventList[i]; + var timeEvent = _eventList[i]; if (timeEvent.Timestamp < elapsed) { + // If the event has passed we should stop looking at it. + _eventList.RemoveAt(i); continue; } @@ -110,22 +106,125 @@ private void OnGUI() GUIContent guiText = new GUIContent($"{timeString} - {timeEvent.Name}"); float labelSize = style.CalcHeight(guiText, _width); yOff += labelSize; - GUI.Label(new Rect(_xPos, _yPos - yOff, _width, labelSize), $"{timeString} - {timeEvent.Name}", style); + GUI.Label(new Rect(_xPos, _yPos - yOff, _width, labelSize), guiText, style); shown++; } } + /// + /// Called by OWML; once at the start and upon each config setting change. + /// + /// The new settings passed from OWML + public override void Configure(IModConfig config) + { + CountUp = config.GetSettingsValue("Count Up"); + Milliseconds = config.GetSettingsValue("Count In Milliseconds"); + EventCount = config.GetSettingsValue("Events to Display"); + HudScale = config.GetSettingsValue("HudScale"); + EnabledTypes.Clear(); + for (int i = 0; i < Enum.GetNames(typeof(TimeEvent.Type)).Length; i++) + { + var name = Enum.GetName(typeof(TimeEvent.Type), i); + if (config.GetSettingsValue(name)) + { + EnabledTypes.Add(i); + } + } + + // When the HudScale changes, we need to scale the HUD + RecalculatePosition(); + } + + #endregion + + #region "Event Handlers" + /// + /// Handles all ModHelper.Events you Subscribe to. + /// + /// From UnityEngine.CoreModule + /// The event that is happening. + private void OnEvent(MonoBehaviour behaviour, Events ev) + { + ModHelper.Console.WriteLine("Behaviour name: " + behaviour.name); + + // Start the list over from the save file when you wake up. This allows us to remove events as they happen. + if (behaviour.GetType() == typeof(PlayerBody) && ev == Events.AfterAwake) + { + ModHelper.Console.WriteLine("Loading the event list for the clock."); + _eventList = Save.eventList.ToList(); + } + } + + /// + /// Adds custom menu items to the game. + /// + private void AddMenuItem() + { + var addEventMenu = ModHelper.Menus.PauseMenu.Copy("ADD EVENT"); + var addEventInputButton = ModHelper.Menus.PauseMenu.ResumeButton.Duplicate("ADD EVENT"); + addEventInputButton.OnClick += EventPopup; + + var debugEventMenu = ModHelper.Menus.PauseMenu.Copy("DEBUG TIME"); + var debugEventInputButton = ModHelper.Menus.PauseMenu.ResumeButton.Duplicate("DEBUG TIME"); + debugEventInputButton.OnClick += LogTime; + } + + /// + /// Print the current time to the OWML log window. + /// + private void LogTime() + { + var currentTime = TimeLoop.GetSecondsElapsed(); + ModHelper.Console.WriteLine($"Time is {currentTime}"); + } + + /// + /// Handles the GraphicSettingsUpdated event from Unity. + /// + /// The settings passed from Unity. + private void GetDisplaySettings(GraphicSettings settings) + { + // Store the current resolution so we can update our position based on changes to HudScale later. + _displayHeight = settings.displayResHeight; + _displayWidth = settings.displayResWidth; + RecalculatePosition(); + } + + /// + /// Get user input for the menu item "ADD EVENT". + /// private void EventPopup() { var popup = ModHelper.Menus.PopupManager.CreateInputPopup(InputType.Text, "Event Name"); popup.OnConfirm += AddEvent; } + /// + /// Save the new event that the user created from the menu. + /// + /// Name of the event. private void AddEvent(string text) { Save.AddEvent(TimeLoop.GetSecondsElapsed(), text); } + #endregion + + /// + /// Determine where the Time events and clock should be positioned in the HUD. + /// + private void RecalculatePosition() + { + _yPos = _displayHeight - 60f; + _xPos = Milliseconds ? _displayWidth * (1 - HudScale / 100) - 80f : _displayWidth * (1 - HudScale / 100) - 20f; + _width = _displayWidth * (HudScale / 100); + } + + /// + /// Format for the clock. + /// + /// From TimeLoop.GetSecondsElapsed + /// The clock display in string format, ready to be put on a GUI.Label static string ParseTime(float timestamp) { var minutes = Mathf.Floor(timestamp / 60f).ToString().PadLeft(2, '0'); @@ -136,24 +235,8 @@ static string ParseTime(float timestamp) var milliseconds = Math.Truncate((timestamp - Math.Floor(timestamp)) * 1000).ToString().PadLeft(3, '0'); clock = $"{clock}.{milliseconds}"; } - return clock; - } - public override void Configure(IModConfig config) - { - CountUp = config.GetSettingsValue("Count Up"); - Milliseconds = config.GetSettingsValue("Count In Milliseconds"); - EventCount = config.GetSettingsValue("Events to Display"); - HudScale = config.GetSettingsValue("HudScale"); - EnabledTypes.Clear(); - for (int i = 0; i < Enum.GetNames(typeof(TimeEvent.Type)).Length; i++) - { - var name = Enum.GetName(typeof(TimeEvent.Type), i); - if (config.GetSettingsValue(name)) - { - EnabledTypes.Add(i); - } - } + return clock; } } } diff --git a/ClockLib/Properties/AssemblyInfo.cs b/ClockLib/Properties/AssemblyInfo.cs index c130097..dcf9a74 100644 --- a/ClockLib/Properties/AssemblyInfo.cs +++ b/ClockLib/Properties/AssemblyInfo.cs @@ -1,4 +1,5 @@ -using System.Reflection; +using System.Resources; +using System.Reflection; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following @@ -31,5 +32,6 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.5.0.0")] -[assembly: AssemblyFileVersion("0.5.0.0")] +[assembly: AssemblyVersion("0.6.1.0")] +[assembly: AssemblyFileVersion("0.6.1.0")] +[assembly: NeutralResourcesLanguage("en-US")] diff --git a/ClockLib/manifest.json b/ClockLib/manifest.json index 4d54664..ef98dc6 100644 --- a/ClockLib/manifest.json +++ b/ClockLib/manifest.json @@ -4,6 +4,6 @@ "name": "Clock", "uniqueName": "clubby789.OWClock", "description": "Adds a clock overlay and eventlist", - "version": "0.5.0", + "version": "0.6.1", "owmlVersion": "2.1.0" } \ No newline at end of file