From f8a233317844b62c21625f137d3470d46d5420bc Mon Sep 17 00:00:00 2001 From: aubergine10 Date: Wed, 23 Feb 2022 02:15:40 +0000 Subject: [PATCH 01/10] Interface group --- TLM/TLM/State/ConfigData/Main.cs | 18 +- TLM/TLM/State/GlobalConfig.cs | 6 +- TLM/TLM/State/OptionsTabs/GeneralTab.cs | 196 +----------------- .../OptionsTabs/GeneralTab_InterfaceGroup.cs | 171 +++++++++++++++ TLM/TLM/TLM.csproj | 1 + TLM/TLM/U/UOpacityValue.cs | 17 +- TLM/TLM/UI/TrafficManagerTool.cs | 58 +++--- 7 files changed, 235 insertions(+), 232 deletions(-) create mode 100644 TLM/TLM/State/OptionsTabs/GeneralTab_InterfaceGroup.cs diff --git a/TLM/TLM/State/ConfigData/Main.cs b/TLM/TLM/State/ConfigData/Main.cs index 7339f024d..97c700428 100644 --- a/TLM/TLM/State/ConfigData/Main.cs +++ b/TLM/TLM/State/ConfigData/Main.cs @@ -45,14 +45,14 @@ public class Main { public bool EnableTutorial = true; /// Determines if the main menu shall be displayed in a tiny format. - [Obsolete("Do not use. TM:PE now has UI scale slider")] + [Obsolete("Use GuiScale instead")] public bool TinyMainMenu = true; - /// User interface transparency, unit: percents, range: 0..100. - [Obsolete("Value is not used anymore, use GuiOpacity instead")] + /// User interface transparency, unit: percents, range: 0..90. + [Obsolete("Use GuiOpacity instead")] public byte GuiTransparency = 75; - /// User interface opacity, unit: percents, range: 0..100. + /// User interface opacity, unit: percents, range: 10..100. public byte GuiOpacity = 75; /// User interface scale for TM:PE. Unit: percents, range: 30..200f. @@ -60,15 +60,17 @@ public class Main { /// /// if checked, size remains constant but pixel count changes when resolution changes. Quality drops with lower resolutions. - /// if unchecked checked, size changes constant but pixel count remains the same. Maintains same image quality for all resolution. + /// if unchecked, size changes constant but pixel count remains the same. Maintains same image quality for all resolution. /// public bool GuiScaleToResolution = true; - /// - /// Overlay transparency - /// + /// Overlay transparency, unit: percents, range: 0..90 + [Obsolete("Use OverlayOpacity instead")] public byte OverlayTransparency = 40; + /// Overlay icons opacity, unit: percent, range: 10..100. + public byte OverlayOpacity = 60; + /// /// Extended mod compatibility check /// diff --git a/TLM/TLM/State/GlobalConfig.cs b/TLM/TLM/State/GlobalConfig.cs index 5d7f6f948..d611960db 100644 --- a/TLM/TLM/State/GlobalConfig.cs +++ b/TLM/TLM/State/GlobalConfig.cs @@ -15,7 +15,7 @@ namespace TrafficManager.State { public class GlobalConfig : GenericObservable { public const string FILENAME = "TMPE_GlobalConfig.xml"; public const string BACKUP_FILENAME = FILENAME + ".bak"; - private static int LATEST_VERSION = 19; + private static int LATEST_VERSION = 20; public static GlobalConfig Instance { get => instance; @@ -101,10 +101,12 @@ private static GlobalConfig WriteDefaultConfig(GlobalConfig oldConfig, conf.Main.MainMenuPosLocked = oldConfig.Main.MainMenuPosLocked; conf.Main.GuiOpacity = oldConfig.Main.GuiOpacity; - conf.Main.OverlayTransparency = oldConfig.Main.OverlayTransparency; + conf.Main.OverlayOpacity = oldConfig.Main.OverlayOpacity; conf.Main.EnableTutorial = oldConfig.Main.EnableTutorial; conf.Main.DisplayedTutorialMessages = oldConfig.Main.DisplayedTutorialMessages; + + conf.Main.OpenUrlsInSteamOverlay = oldConfig.Main.OpenUrlsInSteamOverlay; } modifiedTime = WriteConfig(conf); diff --git a/TLM/TLM/State/OptionsTabs/GeneralTab.cs b/TLM/TLM/State/OptionsTabs/GeneralTab.cs index 2c634825b..45b1bb9c3 100644 --- a/TLM/TLM/State/OptionsTabs/GeneralTab.cs +++ b/TLM/TLM/State/OptionsTabs/GeneralTab.cs @@ -21,14 +21,6 @@ public static class GeneralTab { Handler = WhatsNew.OpenModal, }; - public static CheckboxOption OpenUrlsInSteamOverlay = - new (nameof(GlobalConfig.Instance.Main.OpenUrlsInSteamOverlay), Options.PersistTo.Global) { - Label = "Checkbox:Use Steam Overlay to show TM:PE website links", - Tooltip = "Checkbox.Tooltip:When disabled, website links will open in your default web browser", - Handler = OnOpenUrlsInSteamOverlayChanged, - Value = GlobalConfig.Instance.Main.OpenUrlsInSteamOverlay, - }; - private static UICheckBox _instantEffectsToggle; [UsedImplicitly] @@ -105,55 +97,6 @@ internal static void MakeSettings_General(ExtUITabstrip tabStrip) { SetupSpeedLimitsPanel(group); - group = tab.AddGroup(T("General.Group:Interface")); - - _lockButtonToggle = group.AddCheckbox( - text: T("General.Checkbox:Lock main menu button position"), - defaultValue: GlobalConfig.Instance.Main.MainMenuButtonPosLocked, - eventCallback: OnLockButtonChanged) as UICheckBox; - _lockMenuToggle = group.AddCheckbox( - text: T("General.Checkbox:Lock main menu window position"), - defaultValue: GlobalConfig.Instance.Main.MainMenuPosLocked, - eventCallback: OnLockMenuChanged) as UICheckBox; - - _useUUI = group.AddCheckbox( - text: T("General.Checkbox:Use UnifiedUI"), - defaultValue: GlobalConfig.Instance.Main.UseUUI, - eventCallback: OnUseUUIChanged) as UICheckBox; - - _guiScaleSlider = group.AddSlider( - text: T("General.Slider:GUI scale") + ":", - min: 50, - max: 200, - step: 5, - defaultValue: GlobalConfig.Instance.Main.GuiScale, - eventCallback: OnGuiScaleChanged) as UISlider; - _guiScaleSlider.parent.Find("Label").width = 500; - - _guiOpacitySlider = group.AddSlider( - text: T("General.Slider:Window transparency") + ":", - min: 0, - max: 100, - step: 5, - defaultValue: GlobalConfig.Instance.Main.GuiOpacity, - eventCallback: OnGuiOpacityChanged) as UISlider; - _guiOpacitySlider.parent.Find("Label").width = 500; - - _overlayTransparencySlider = group.AddSlider( - text: T("General.Slider:Overlay transparency") + ":", - min: 0, - max: 100, - step: 5, - defaultValue: GlobalConfig.Instance.Main.OverlayTransparency, - eventCallback: OnOverlayTransparencyChanged) as UISlider; - _overlayTransparencySlider.parent.Find("Label").width = 500; - _enableTutorialToggle = group.AddCheckbox( - T("General.Checkbox:Enable tutorials"), - GlobalConfig.Instance.Main.EnableTutorial, - OnEnableTutorialsChanged) as UICheckBox; - - OpenUrlsInSteamOverlay.AddUI(group); - group = tab.AddGroup(T("General.Group:Simulation")); string[] simPrecisionOptions = new[] { @@ -174,6 +117,8 @@ internal static void MakeSettings_General(ExtUITabstrip tabStrip) { defaultValue: Options.instantEffects, eventCallback: OnInstantEffectsChanged) as UICheckBox; + GeneralTab_InterfaceGroup.AddUI(tab); + group = tab.AddGroup(T("General.Group:Compatibility")); _scanForKnownIncompatibleModsToggle @@ -247,43 +192,6 @@ private static void OnLanguageChanged(int newLanguageIndex) { Options.RebuildOptions(); } - private static void OnLockButtonChanged(bool newValue) { - Log._Debug($"Button lock changed to {newValue}"); - if (Options.IsGameLoaded(false)) { - ModUI.Instance.MainMenuButton.SetPosLock(newValue); - } - - GlobalConfig.Instance.Main.MainMenuButtonPosLocked = newValue; - GlobalConfig.WriteConfig(); - } - - private static void OnLockMenuChanged(bool newValue) { - Log._Debug($"Menu lock changed to {newValue}"); - if (Options.IsGameLoaded(false)) { - ModUI.Instance.MainMenu.SetPosLock(newValue); - } - - GlobalConfig.Instance.Main.MainMenuPosLocked = newValue; - GlobalConfig.WriteConfig(); - } - - private static void OnUseUUIChanged(bool newValue) { - Log._Debug($"Use UUI set to {newValue}"); - GlobalConfig.Instance.Main.UseUUI = newValue; - GlobalConfig.WriteConfig(); - var button = ModUI.GetTrafficManagerTool()?.UUIButton; - if (button) { - button.isVisible = newValue; - } - ModUI.Instance?.MainMenuButton?.UpdateButtonSkinAndTooltip(); - } - - private static void OnEnableTutorialsChanged(bool newValue) { - Log._Debug($"Enable tutorial messages changed to {newValue}"); - GlobalConfig.Instance.Main.EnableTutorial = newValue; - GlobalConfig.WriteConfig(); - } - private static void OnShowCompatibilityCheckErrorChanged(bool newValue) { Log._Debug($"Show mod compatibility error changed to {newValue}"); GlobalConfig.Instance.Main.ShowCompatibilityCheckErrorMessage = newValue; @@ -301,67 +209,6 @@ private static void OnScanForKnownIncompatibleModsChanged(bool newValue) { } } - private static void OnGuiOpacityChanged(float newVal) { - if (!Options.IsGameLoaded()) { - return; - } - - SetGuiTransparency((byte)Mathf.RoundToInt(newVal)); - _guiOpacitySlider.tooltip - = string.Format( - T("General.Tooltip.Format:Window transparency: {0}%"), - GlobalConfig.Instance.Main.GuiOpacity); - if (TMPELifecycle.Instance.IsGameLoaded) { - _guiOpacitySlider.RefreshTooltip(); - } - - GlobalConfig.WriteConfig(); - Log._Debug($"GuiTransparency changed to {GlobalConfig.Instance.Main.GuiOpacity}"); - } - - private static void OnGuiScaleChanged(float newVal) { - ModUI.Instance.Events.UiScaleChanged(); - SetGuiScale(newVal); - _guiScaleSlider.tooltip - = string.Format( - T("General.Tooltip.Format:GUI scale: {0}%"), - GlobalConfig.Instance.Main.GuiScale); - if (TMPELifecycle.Instance.IsGameLoaded) { - _guiScaleSlider.RefreshTooltip(); - } - - GlobalConfig.WriteConfig(); - Log._Debug($"GuiScale changed to {GlobalConfig.Instance.Main.GuiScale}"); - } - - /// User clicked [scale GUI to screen resolution] checkbox. - private static void OnGuiScaleToResChanged(float newVal) { - SetGuiScale(newVal); - if (TMPELifecycle.Instance.IsGameLoaded) { - _guiScaleSlider.RefreshTooltip(); - } - - GlobalConfig.WriteConfig(); - Log._Debug($"GuiScale changed to {GlobalConfig.Instance.Main.GuiScale}"); - } - - private static void OnOverlayTransparencyChanged(float newVal) { - if (!Options.IsGameLoaded()) { - return; - } - - SetOverlayTransparency((byte)Mathf.RoundToInt(newVal)); - _overlayTransparencySlider.tooltip = string.Format( - T("General.Tooltip.Format:Overlay transparency: {0}%"), - GlobalConfig.Instance.Main.OverlayTransparency); - GlobalConfig.WriteConfig(); - if (TMPELifecycle.Instance.IsGameLoaded) { - _overlayTransparencySlider.RefreshTooltip(); - } - - Log._Debug($"Overlay transparency changed to {GlobalConfig.Instance.Main.OverlayTransparency}"); - } - private static void OnIgnoreDisabledModsChanged(bool newValue) { Log._Debug($"Ignore disabled mods changed to {newValue}"); GlobalConfig.Instance.Main.IgnoreDisabledMods = newValue; @@ -441,14 +288,6 @@ private static void OnRoadSignsThemeChanged(int newThemeIndex) { GlobalConfig.WriteConfig(); } - private static void OnOpenUrlsInSteamOverlayChanged(bool val) { - var current = GlobalConfig.Instance.Main.OpenUrlsInSteamOverlay; - if (current == val) return; - - GlobalConfig.Instance.Main.OpenUrlsInSteamOverlay = val; - GlobalConfig.WriteConfig(); - } - private static void OnSimulationAccuracyChanged(int newAccuracy) { if (!Options.IsGameLoaded()) { return; @@ -474,37 +313,6 @@ public static void SetIgnoreDisabledMods(bool value) { } } - public static void SetGuiTransparency(byte val) { - bool isChanged = val != GlobalConfig.Instance.Main.GuiOpacity; - GlobalConfig.Instance.Main.GuiOpacity = val; - - if (isChanged && _guiOpacitySlider != null) { - _guiOpacitySlider.value = val; - - U.UOpacityValue opacity = UOpacityValue.FromOpacity(0.01f * val); - ModUI.Instance.Events.OpacityChanged(opacity); - } - } - - public static void SetGuiScale(float val) { - bool changed = (int)val != (int)GlobalConfig.Instance.Main.GuiScale; - GlobalConfig.Instance.Main.GuiScale = val; - - if (changed && _guiScaleSlider != null) { - _guiScaleSlider.value = val; - ModUI.Instance.Events.UiScaleChanged(); - } - } - - public static void SetOverlayTransparency(byte val) { - bool changed = val != GlobalConfig.Instance.Main.OverlayTransparency; - GlobalConfig.Instance.Main.OverlayTransparency = val; - - if (changed && _overlayTransparencySlider != null) { - _overlayTransparencySlider.value = val; - } - } - public static void SetSimulationAccuracy(SimulationAccuracy newAccuracy) { Options.simulationAccuracy = newAccuracy; if (_simulationAccuracyDropdown != null) { diff --git a/TLM/TLM/State/OptionsTabs/GeneralTab_InterfaceGroup.cs b/TLM/TLM/State/OptionsTabs/GeneralTab_InterfaceGroup.cs new file mode 100644 index 000000000..63a3a3787 --- /dev/null +++ b/TLM/TLM/State/OptionsTabs/GeneralTab_InterfaceGroup.cs @@ -0,0 +1,171 @@ +namespace TrafficManager.State { + using ICities; + using TrafficManager.Lifecycle; + using TrafficManager.U; + using TrafficManager.UI; + using TrafficManager.UI.Helpers; + + public static class GeneralTab_InterfaceGroup { + + public static CheckboxOption MainMenuButtonPosLocked = + new(nameof(Main.MainMenuButtonPosLocked), Options.PersistTo.Global) { + Label = "General.Checkbox:Lock main menu button position", + Handler = OnMainMenuButtonPosLockedChanged, + }; + + public static CheckboxOption MainMenuPosLocked = + new(nameof(Main.MainMenuPosLocked), Options.PersistTo.Global) { + Label = "General.Checkbox:Lock main menu window position", + Handler = OnMainMenuPosLockedChanged, + }; + + public static CheckboxOption UseUUI = + new(nameof(Main.UseUUI), Options.PersistTo.Global) { + Label = "General.Checkbox:Use UnifiedUI", + Handler = OnUseUUIChanged, + }; + + public static SliderOption GuiScale = + new(nameof(Main.GuiScale), Options.PersistTo.Global) { + Label = "General.Slider:GUI scale", + Tooltip = "%", + Min = 50, + Max = 200, + Handler = OnGuiScaleChanged, + }; + + public static SliderOption GuiOpacity = + new(nameof(Main.GuiOpacity), Options.PersistTo.Global) { + Label = "General.Slider:Window opacity", + Tooltip = "%", + Min = TrafficManagerTool.MINIMUM_OPACITY, + Max = TrafficManagerTool.MAXIMUM_OPACITY, + Handler = OnGuiOpacityChanged, + }; + + public static SliderOption OverlayOpacity = + new(nameof(Main.OverlayOpacity), Options.PersistTo.Global) { + Label = "General.Slider:Overlay opacity", + Tooltip = "%", + Min = TrafficManagerTool.MINIMUM_OPACITY, + Max = TrafficManagerTool.MAXIMUM_OPACITY, + Handler = OnOverlayOpacityChanged, + }; + + public static CheckboxOption EnableTutorial = + new(nameof(Main.EnableTutorial), Options.PersistTo.Global) { + Label = "General.Checkbox:Enable tutorials", + Handler = OnEnableTutorialChanged, + }; + + public static CheckboxOption OpenUrlsInSteamOverlay = + new(nameof(Main.OpenUrlsInSteamOverlay), Options.PersistTo.Global) { + Label = "Checkbox:Use Steam Overlay to show TM:PE website links", + Tooltip = "Checkbox.Tooltip:When disabled, website links will open in your default web browser", + Handler = OnOpenUrlsInSteamOverlayChanged, + }; + + private static ConfigData.Main Main => GlobalConfig.Instance.Main; + + internal static void AddUI(UIHelperBase tab) { + var group = tab.AddGroup(T("General.Group:Interface")); + + MainMenuButtonPosLocked.AddUI(group) + .Value = Main.MainMenuButtonPosLocked; + MainMenuPosLocked.AddUI(group) + .Value = Main.MainMenuPosLocked; + UseUUI.AddUI(group) + .Value = Main.UseUUI; + GuiScale.AddUI(group) + .Value = Main.GuiScale; + GuiOpacity.AddUI(group) + .Value = Main.GuiOpacity; + OverlayOpacity.AddUI(group) + .Value = Main.OverlayOpacity; + EnableTutorial.AddUI(group) + .Value = Main.EnableTutorial; + OpenUrlsInSteamOverlay.AddUI(group) + .Value = Main.OpenUrlsInSteamOverlay; + } + + private static string T(string key) => Translation.Options.Get(key); + + private static void OnMainMenuButtonPosLockedChanged(bool val) { + if (Main.MainMenuButtonPosLocked == val) return; + + Main.MainMenuButtonPosLocked = val; + GlobalConfig.WriteConfig(); + + if (TMPELifecycle.InGameOrEditor()) { + ModUI.Instance.MainMenuButton.SetPosLock(val); + } + } + + private static void OnMainMenuPosLockedChanged(bool val) { + if (Main.MainMenuPosLocked == val) return; + + Main.MainMenuPosLocked = val; + GlobalConfig.WriteConfig(); + + if (TMPELifecycle.InGameOrEditor()) { + ModUI.Instance.MainMenu.SetPosLock(val); + } + } + + private static void OnUseUUIChanged(bool val) { + if (Main.UseUUI == val) return; + + Main.UseUUI = val; + GlobalConfig.WriteConfig(); + + if (TMPELifecycle.InGameOrEditor()) { + var button = ModUI.GetTrafficManagerTool()?.UUIButton; + if (button) button.isVisible = val; + ModUI.Instance?.MainMenuButton?.UpdateButtonSkinAndTooltip(); + } + } + + private static void OnGuiScaleChanged(float val) { + if (Main.GuiScale == val) return; + + Main.GuiScale = val; + GlobalConfig.WriteConfig(); + + if (TMPELifecycle.InGameOrEditor()) + ModUI.Instance?.Events.UiScaleChanged(); + } + + private static void OnGuiOpacityChanged(float val) { + if (Main.GuiOpacity == val) return; + + Main.GuiOpacity = GuiOpacity.Save(); + GlobalConfig.WriteConfig(); + + if (TMPELifecycle.InGameOrEditor()) { + var opacity = UOpacityValue.FromOpacityPercent(Main.GuiOpacity); + ModUI.Instance?.Events.OpacityChanged(opacity); + } + } + + private static void OnOverlayOpacityChanged(float val) { + if (Main.OverlayOpacity == val) return; + + Main.OverlayOpacity = OverlayOpacity.Save(); + GlobalConfig.WriteConfig(); + } + + private static void OnEnableTutorialChanged(bool val) { + if (Main.EnableTutorial == val) return; + + Main.EnableTutorial = val; + GlobalConfig.WriteConfig(); + } + + private static void OnOpenUrlsInSteamOverlayChanged(bool val) { + if (Main.OpenUrlsInSteamOverlay == val) return; + + Main.OpenUrlsInSteamOverlay = val; + GlobalConfig.WriteConfig(); + } + } +} \ No newline at end of file diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index 7048ab2c0..38a76ed42 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -149,6 +149,7 @@ + diff --git a/TLM/TLM/U/UOpacityValue.cs b/TLM/TLM/U/UOpacityValue.cs index 5748a6202..1c9984f27 100644 --- a/TLM/TLM/U/UOpacityValue.cs +++ b/TLM/TLM/U/UOpacityValue.cs @@ -13,8 +13,17 @@ public struct UOpacityValue { /// New Opacity struct. public static UOpacityValue FromOpacity(float v) { return new() { - value_ = Mathf.Clamp(value: v, min: 0f, max: 1f), - }; + value_ = Mathf.Clamp(value: v, min: 0f, max: 1f), + }; + } + + /// Create from opacity value where 100 is opaque, 0 is invisible. + /// Range 0..100. + /// New Opacity struct. + public static UOpacityValue FromOpacityPercent(byte v) { + return new() { + value_ = Mathf.Clamp(value: 0.01f * v, min: 0f, max: 1f), + }; } /// Create from transparency value where 0 is opaque, 1f is invisible. @@ -23,8 +32,8 @@ public static UOpacityValue FromOpacity(float v) { [UsedImplicitly] public static UOpacityValue FromTransparency(float v) { return new() { - value_ = 1f - Mathf.Clamp(value: v, min: 0f, max: 1f), - }; + value_ = 1f - Mathf.Clamp(value: v, min: 0f, max: 1f), + }; } [UsedImplicitly] diff --git a/TLM/TLM/UI/TrafficManagerTool.cs b/TLM/TLM/UI/TrafficManagerTool.cs index 1ffd348c3..27afbc3fe 100644 --- a/TLM/TLM/UI/TrafficManagerTool.cs +++ b/TLM/TLM/UI/TrafficManagerTool.cs @@ -56,6 +56,13 @@ public class TrafficManagerTool private CursorInfo nopeCursor_; + public const float DEBUG_CLOSE_LOD = 300f; + + /// Square of the distance, where overlays are not rendered. + public const float MAX_OVERLAY_DISTANCE_SQR = 600f * 600f; + + internal const float MAX_ZOOM = 0.05f; + /// Maximum error of HitPos field. internal const float MAX_HIT_ERROR = 2.5f; @@ -63,6 +70,15 @@ public class TrafficManagerTool internal const float NODE_DETECTION_RADIUS = 75f; internal const float PRECISE_NODE_DETECTION_RADIUS = 15f; + /// Convert 0..100 opacity value to 0..1f alpha value. + internal const float TO_ALPHA = 0.01f; + + /// Minimum opacity value. Also affects sliders in mod options. + internal const byte MINIMUM_OPACITY = 10; + + /// Maximum opacity value. Also affects sliders in mod options. + internal const byte MAXIMUM_OPACITY = 100; + internal static ushort HoveredNodeId; internal static ushort HoveredSegmentId; @@ -74,11 +90,6 @@ public class TrafficManagerTool private static bool _mouseClickProcessed; - public const float DEBUG_CLOSE_LOD = 300f; - - /// Square of the distance, where overlays are not rendered. - public const float MAX_OVERLAY_DISTANCE_SQR = 600f * 600f; - private IDictionary legacySubTools_; private IDictionary subTools_; @@ -109,7 +120,11 @@ public class TrafficManagerTool /// public static bool IsUndergroundMode => IsValidUndergroundMode(InfoManager.instance.CurrentMode); - internal static float OverlayAlpha => TransparencyToAlpha(GlobalConfig.Instance.Main.OverlayTransparency); + internal static float OverlayAlpha + => TO_ALPHA * Mathf.Clamp( + GlobalConfig.Instance.Main.OverlayOpacity, + MINIMUM_OPACITY, + MAXIMUM_OPACITY); static TrafficManagerTool() { } @@ -194,21 +209,20 @@ internal float GetBaseZoom() { return Screen.height / 1200f; } - internal const float MAX_ZOOM = 0.05f; - - internal static float GetWindowAlpha() { - return Mathf.Clamp(GlobalConfig.Instance.Main.GuiOpacity, 0f, 100f) / 100f; - } - - internal static float GetHandleAlpha(bool hovered) { - byte transparency = GlobalConfig.Instance.Main.OverlayTransparency; - if (hovered) { - // reduce transparency when handle is hovered - transparency = (byte)Math.Min(20, transparency >> 2); - } + internal static float GetWindowAlpha() + => TO_ALPHA * Mathf.Clamp( + GlobalConfig.Instance.Main.GuiOpacity, + MINIMUM_OPACITY, + MAXIMUM_OPACITY); - return TransparencyToAlpha(transparency); - } + /// + /// Get alpha value for an overlay icon, taking in to account hovered state. + /// + /// Set true if mouse is over handle. + /// Returns alpha value in range 0.1..1f. + internal static float GetHandleAlpha(bool hovered) => hovered + ? TO_ALPHA * MAXIMUM_OPACITY + : OverlayAlpha; /// Gives convenient access to NetTool from the original game. private NetTool NetTool { @@ -222,10 +236,6 @@ private NetTool NetTool { } } - private static float TransparencyToAlpha(byte transparency) { - return Mathf.Clamp(100 - transparency, 0f, 100f) / 100f; - } - internal void Initialize() { Log.Info("TrafficManagerTool: Initialization running now."); Guide = new GuideHandler(); From be34a30bfd78b99dab1d40ed1d0afe37bc724d74 Mon Sep 17 00:00:00 2001 From: aubergine10 Date: Wed, 23 Feb 2022 03:09:11 +0000 Subject: [PATCH 02/10] Minor improvements to localisation group --- TLM/TLM/State/OptionsTabs/GeneralTab_LocalisationGroup.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TLM/TLM/State/OptionsTabs/GeneralTab_LocalisationGroup.cs b/TLM/TLM/State/OptionsTabs/GeneralTab_LocalisationGroup.cs index 8a88d2b70..00c06576c 100644 --- a/TLM/TLM/State/OptionsTabs/GeneralTab_LocalisationGroup.cs +++ b/TLM/TLM/State/OptionsTabs/GeneralTab_LocalisationGroup.cs @@ -17,7 +17,7 @@ public static class GeneralTab_LocalisationGroup { // TODO: Implement global config updates direct from CheckboxOption public static CheckboxOption DisplaySpeedLimitsMph = - new ("DebugCheckboxA", Options.PersistTo.None) { + new (nameof(GlobalConfig.Instance.Main.DisplaySpeedLimitsMph), Options.PersistTo.Global) { Label = "Checkbox:Display speed limits mph", Translator = TSpeedLimits, Handler = OnDisplaySpeedLimitsMphChanged, @@ -31,8 +31,8 @@ internal static void AddUI(UIHelperBase tab) { AddLanguageDropDown(group); - DisplaySpeedLimitsMph.Value = GlobalConfig.Instance.Main.DisplaySpeedLimitsMph; - DisplaySpeedLimitsMph.AddUI(group); + DisplaySpeedLimitsMph.AddUI(group) + .Value = GlobalConfig.Instance.Main.DisplaySpeedLimitsMph; AddRoadSignThemeDropDown(group); } From d03d8bea8eef84ef74b6a4df510becf05d58965b Mon Sep 17 00:00:00 2001 From: aubergine10 Date: Wed, 23 Feb 2022 03:39:13 +0000 Subject: [PATCH 03/10] Suppress SA1401 in `Main` --- TLM/TLM/State/ConfigData/Main.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/TLM/TLM/State/ConfigData/Main.cs b/TLM/TLM/State/ConfigData/Main.cs index 97c700428..36cb72d8c 100644 --- a/TLM/TLM/State/ConfigData/Main.cs +++ b/TLM/TLM/State/ConfigData/Main.cs @@ -1,12 +1,14 @@ namespace TrafficManager.State.ConfigData { using System; using System.Collections.Generic; + using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Xml.Serialization; using JetBrains.Annotations; using TrafficManager.UI.MainMenu; using TrafficManager.UI.SubTools.SpeedLimits; + [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "Reviewed.")] public class Main { /// Whether floating keybinds panel is visible. public bool KeybindsPanelVisible = true; From 3f53089e99a099077ef468ac472df495e30b8729 Mon Sep 17 00:00:00 2001 From: aubergine10 Date: Wed, 23 Feb 2022 03:41:00 +0000 Subject: [PATCH 04/10] Remove misplaced options. Mod checker stuff is global, not savegame. --- TLM/TLM/State/Options.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/TLM/TLM/State/Options.cs b/TLM/TLM/State/Options.cs index 1ab5541cf..997c043e2 100644 --- a/TLM/TLM/State/Options.cs +++ b/TLM/TLM/State/Options.cs @@ -114,12 +114,6 @@ public enum PersistTo { public static bool turnOnRedEnabled = true; public static bool laneConnectorEnabled = true; - [UsedImplicitly] - public static bool scanForKnownIncompatibleModsEnabled = true; - - [UsedImplicitly] - public static bool ignoreDisabledModsEnabled; - public static VehicleRestrictionsAggression vehicleRestrictionsAggression = VehicleRestrictionsAggression.Medium; From e79c38124183dae70a661d6855b6952f56f5a663 Mon Sep 17 00:00:00 2001 From: aubergine10 Date: Wed, 23 Feb 2022 03:42:02 +0000 Subject: [PATCH 05/10] Standardise event handler param name `value` --- .../OptionsTabs/GeneralTab_InterfaceGroup.cs | 53 ++++++++++--------- .../GeneralTab_LocalisationGroup.cs | 16 +++--- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/TLM/TLM/State/OptionsTabs/GeneralTab_InterfaceGroup.cs b/TLM/TLM/State/OptionsTabs/GeneralTab_InterfaceGroup.cs index 63a3a3787..a94985945 100644 --- a/TLM/TLM/State/OptionsTabs/GeneralTab_InterfaceGroup.cs +++ b/TLM/TLM/State/OptionsTabs/GeneralTab_InterfaceGroup.cs @@ -90,53 +90,53 @@ internal static void AddUI(UIHelperBase tab) { private static string T(string key) => Translation.Options.Get(key); - private static void OnMainMenuButtonPosLockedChanged(bool val) { - if (Main.MainMenuButtonPosLocked == val) return; + private static void OnMainMenuButtonPosLockedChanged(bool value) { + if (Main.MainMenuButtonPosLocked == value) return; - Main.MainMenuButtonPosLocked = val; + Main.MainMenuButtonPosLocked = value; GlobalConfig.WriteConfig(); if (TMPELifecycle.InGameOrEditor()) { - ModUI.Instance.MainMenuButton.SetPosLock(val); + ModUI.Instance.MainMenuButton.SetPosLock(value); } } - private static void OnMainMenuPosLockedChanged(bool val) { - if (Main.MainMenuPosLocked == val) return; + private static void OnMainMenuPosLockedChanged(bool value) { + if (Main.MainMenuPosLocked == value) return; - Main.MainMenuPosLocked = val; + Main.MainMenuPosLocked = value; GlobalConfig.WriteConfig(); if (TMPELifecycle.InGameOrEditor()) { - ModUI.Instance.MainMenu.SetPosLock(val); + ModUI.Instance.MainMenu.SetPosLock(value); } } - private static void OnUseUUIChanged(bool val) { - if (Main.UseUUI == val) return; + private static void OnUseUUIChanged(bool value) { + if (Main.UseUUI == value) return; - Main.UseUUI = val; + Main.UseUUI = value; GlobalConfig.WriteConfig(); if (TMPELifecycle.InGameOrEditor()) { var button = ModUI.GetTrafficManagerTool()?.UUIButton; - if (button) button.isVisible = val; + if (button) button.isVisible = value; ModUI.Instance?.MainMenuButton?.UpdateButtonSkinAndTooltip(); } } - private static void OnGuiScaleChanged(float val) { - if (Main.GuiScale == val) return; + private static void OnGuiScaleChanged(float value) { + if (Main.GuiScale == value) return; - Main.GuiScale = val; + Main.GuiScale = value; GlobalConfig.WriteConfig(); if (TMPELifecycle.InGameOrEditor()) ModUI.Instance?.Events.UiScaleChanged(); } - private static void OnGuiOpacityChanged(float val) { - if (Main.GuiOpacity == val) return; + private static void OnGuiOpacityChanged(float value) { + if (Main.GuiOpacity == value) return; Main.GuiOpacity = GuiOpacity.Save(); GlobalConfig.WriteConfig(); @@ -147,24 +147,25 @@ private static void OnGuiOpacityChanged(float val) { } } - private static void OnOverlayOpacityChanged(float val) { - if (Main.OverlayOpacity == val) return; + private static void OnOverlayOpacityChanged(float value) { + if (Main.OverlayOpacity == value) return; Main.OverlayOpacity = OverlayOpacity.Save(); GlobalConfig.WriteConfig(); } - private static void OnEnableTutorialChanged(bool val) { - if (Main.EnableTutorial == val) return; + private static void OnEnableTutorialChanged(bool value) { + if (Main.EnableTutorial == value) return; - Main.EnableTutorial = val; + Main.EnableTutorial = value; GlobalConfig.WriteConfig(); } - private static void OnOpenUrlsInSteamOverlayChanged(bool val) { - if (Main.OpenUrlsInSteamOverlay == val) return; - - Main.OpenUrlsInSteamOverlay = val; + private static void OnOpenUrlsInSteamOverlayChanged(bool value) + { + if (Main.OpenUrlsInSteamOverlay == value) + return; + Main.OpenUrlsInSteamOverlay = value; GlobalConfig.WriteConfig(); } } diff --git a/TLM/TLM/State/OptionsTabs/GeneralTab_LocalisationGroup.cs b/TLM/TLM/State/OptionsTabs/GeneralTab_LocalisationGroup.cs index 00c06576c..bf33cd581 100644 --- a/TLM/TLM/State/OptionsTabs/GeneralTab_LocalisationGroup.cs +++ b/TLM/TLM/State/OptionsTabs/GeneralTab_LocalisationGroup.cs @@ -123,8 +123,8 @@ private static void OnLanguageChanged(int newLanguageIndex) { Options.RebuildOptions(); } - private static void OnDisplaySpeedLimitsMphChanged(bool newMphValue) { - bool supportedByTheme = newMphValue + private static void OnDisplaySpeedLimitsMphChanged(bool value) { + bool supportedByTheme = value ? RoadSignThemes.ActiveTheme.SupportsMph : RoadSignThemes.ActiveTheme.SupportsKmph; @@ -132,20 +132,20 @@ private static void OnDisplaySpeedLimitsMphChanged(bool newMphValue) { if (!supportedByTheme) { // Reset to German road signs theme - _roadSignsThemeDropdown.selectedIndex = RoadSignThemes.Instance.FindDefaultThemeIndex(newMphValue); - mainConfig.RoadSignTheme = RoadSignThemes.Instance.GetDefaultThemeName(newMphValue); + _roadSignsThemeDropdown.selectedIndex = RoadSignThemes.Instance.FindDefaultThemeIndex(value); + mainConfig.RoadSignTheme = RoadSignThemes.Instance.GetDefaultThemeName(value); Log.Info( - $"Display MPH changed to {newMphValue}, but was not supported by current theme, " + $"Display MPH changed to {value}, but was not supported by current theme, " + $"so theme was also reset to {mainConfig.RoadSignTheme}"); } else { - Log.Info($"Display MPH changed to {newMphValue}"); + Log.Info($"Display MPH changed to {value}"); } - mainConfig.DisplaySpeedLimitsMph = newMphValue; + mainConfig.DisplaySpeedLimitsMph = value; GlobalConfig.WriteConfig(); if (Options.IsGameLoaded(false)) { - ModUI.Instance.Events.DisplayMphChanged(newMphValue); + ModUI.Instance.Events.DisplayMphChanged(value); } } From a19c089ab08e237f2289fdc0c387d351faffef12 Mon Sep 17 00:00:00 2001 From: aubergine10 Date: Wed, 23 Feb 2022 03:42:29 +0000 Subject: [PATCH 06/10] Split out `Compatibility` group --- TLM/TLM/State/OptionsTabs/GeneralTab.cs | 83 +------------------ .../GeneralTab_CompatibilityGroup.cs | 78 +++++++++++++++++ TLM/TLM/TLM.csproj | 1 + TLM/TLM/UI/IncompatibleModsPanel.cs | 3 +- 4 files changed, 81 insertions(+), 84 deletions(-) create mode 100644 TLM/TLM/State/OptionsTabs/GeneralTab_CompatibilityGroup.cs diff --git a/TLM/TLM/State/OptionsTabs/GeneralTab.cs b/TLM/TLM/State/OptionsTabs/GeneralTab.cs index 5256a5909..265605c08 100644 --- a/TLM/TLM/State/OptionsTabs/GeneralTab.cs +++ b/TLM/TLM/State/OptionsTabs/GeneralTab.cs @@ -4,12 +4,9 @@ namespace TrafficManager.State { using CSUtil.Commons; using ICities; using JetBrains.Annotations; - using TrafficManager.U; using TrafficManager.UI; using TrafficManager.UI.Helpers; using TrafficManager.UI.WhatsNew; - using UnityEngine; - using TrafficManager.Lifecycle; public static class GeneralTab { public static ActionButton WhatsNewButton = new() { @@ -22,27 +19,6 @@ public static class GeneralTab { [UsedImplicitly] private static UIDropDown _simulationAccuracyDropdown; - [UsedImplicitly] - private static UICheckBox _lockButtonToggle; - - [UsedImplicitly] - private static UICheckBox _lockMenuToggle; - - private static UISlider _guiOpacitySlider; - private static UISlider _guiScaleSlider; - private static UISlider _overlayTransparencySlider; - - [UsedImplicitly] - private static UICheckBox _enableTutorialToggle; - - [UsedImplicitly] - private static UICheckBox _showCompatibilityCheckErrorToggle; - - private static UICheckBox _scanForKnownIncompatibleModsToggle; - private static UICheckBox _ignoreDisabledModsToggle; - - private static UICheckBox _useUUI; - private static string T(string key) => Translation.Options.Get(key); internal static void MakeSettings_General(ExtUITabstrip tabStrip) { @@ -81,46 +57,7 @@ internal static void MakeSettings_General(ExtUITabstrip tabStrip) { GeneralTab_InterfaceGroup.AddUI(tab); - group = tab.AddGroup(T("General.Group:Compatibility")); - - _scanForKnownIncompatibleModsToggle - = group.AddCheckbox( - Translation.ModConflicts.Get("Checkbox:Scan for known incompatible mods on startup"), - GlobalConfig.Instance.Main.ScanForKnownIncompatibleModsAtStartup, - OnScanForKnownIncompatibleModsChanged) as UICheckBox; - _ignoreDisabledModsToggle = group.AddCheckbox( - text: Translation.ModConflicts.Get("Checkbox:Ignore disabled mods"), - defaultValue: GlobalConfig.Instance.Main.IgnoreDisabledMods, - eventCallback: OnIgnoreDisabledModsChanged) as UICheckBox; - CheckboxOption.ApplyIndent(_ignoreDisabledModsToggle); - _showCompatibilityCheckErrorToggle - = group.AddCheckbox( - T("General.Checkbox:Notify me about TM:PE startup conflicts"), - GlobalConfig.Instance.Main.ShowCompatibilityCheckErrorMessage, - OnShowCompatibilityCheckErrorChanged) as UICheckBox; - } - - private static void OnShowCompatibilityCheckErrorChanged(bool newValue) { - Log._Debug($"Show mod compatibility error changed to {newValue}"); - GlobalConfig.Instance.Main.ShowCompatibilityCheckErrorMessage = newValue; - GlobalConfig.WriteConfig(); - } - - private static void OnScanForKnownIncompatibleModsChanged(bool newValue) { - Log._Debug($"Show incompatible mod checker warnings changed to {newValue}"); - GlobalConfig.Instance.Main.ScanForKnownIncompatibleModsAtStartup = newValue; - if (newValue) { - GlobalConfig.WriteConfig(); - } else { - SetIgnoreDisabledMods(false); - OnIgnoreDisabledModsChanged(false); - } - } - - private static void OnIgnoreDisabledModsChanged(bool newValue) { - Log._Debug($"Ignore disabled mods changed to {newValue}"); - GlobalConfig.Instance.Main.IgnoreDisabledMods = newValue; - GlobalConfig.WriteConfig(); + GeneralTab_CompatibilityGroup.AddUI(tab); } private static void OnSimulationAccuracyChanged(int newAccuracy) { @@ -141,13 +78,6 @@ private static void OnInstantEffectsChanged(bool newValue) { Options.instantEffects = newValue; } - public static void SetIgnoreDisabledMods(bool value) { - Options.ignoreDisabledModsEnabled = value; - if (_ignoreDisabledModsToggle != null) { - _ignoreDisabledModsToggle.isChecked = value; - } - } - public static void SetSimulationAccuracy(SimulationAccuracy newAccuracy) { Options.simulationAccuracy = newAccuracy; if (_simulationAccuracyDropdown != null) { @@ -162,16 +92,5 @@ public static void SetInstantEffects(bool value) { _instantEffectsToggle.isChecked = value; } } - - public static void SetScanForKnownIncompatibleMods(bool value) { - Options.scanForKnownIncompatibleModsEnabled = value; - if (_scanForKnownIncompatibleModsToggle != null) { - _scanForKnownIncompatibleModsToggle.isChecked = value; - } - - if (!value) { - SetIgnoreDisabledMods(false); - } - } } // end class } diff --git a/TLM/TLM/State/OptionsTabs/GeneralTab_CompatibilityGroup.cs b/TLM/TLM/State/OptionsTabs/GeneralTab_CompatibilityGroup.cs new file mode 100644 index 000000000..f0558cd10 --- /dev/null +++ b/TLM/TLM/State/OptionsTabs/GeneralTab_CompatibilityGroup.cs @@ -0,0 +1,78 @@ +namespace TrafficManager.State { + using ICities; + using System; + using TrafficManager.UI; + using TrafficManager.UI.Helpers; + using TrafficManager.Util; + + public static class GeneralTab_CompatibilityGroup { + + public static CheckboxOption ScanForKnownIncompatibleModsAtStartup = + new (nameof(Main.ScanForKnownIncompatibleModsAtStartup), Options.PersistTo.Global) { + Label = "Checkbox:Scan for known incompatible mods on startup", + Translator = Translation.ModConflicts.Get, + Handler = OnScanForKnownIncompatibleModsAtStartupChanged, + }; + + public static CheckboxOption IgnoreDisabledMods = + new (nameof(Main.IgnoreDisabledMods), Options.PersistTo.Global) { + Label = "Checkbox:Ignore disabled mods", + Indent = true, + Translator = Translation.ModConflicts.Get, + Handler = OnIgnoreDisabledModsChanged, + }; + + public static CheckboxOption ShowCompatibilityCheckErrorMessage = + new(nameof(Main.ShowCompatibilityCheckErrorMessage), Options.PersistTo.Global) { + Label = "General.Checkbox:Notify me about TM:PE startup conflicts", + Handler = OnShowCompatibilityCheckErrorMessageChanged, + }; + + static GeneralTab_CompatibilityGroup() { + try { + IgnoreDisabledMods + .PropagateTrueTo(ScanForKnownIncompatibleModsAtStartup); + } + catch (Exception ex) { + ex.LogException(); + } + } + + private static ConfigData.Main Main => GlobalConfig.Instance.Main; + + internal static void AddUI(UIHelperBase tab) { + + var group = tab.AddGroup(T("General.Group:Compatibility")); + + ScanForKnownIncompatibleModsAtStartup.AddUI(group) + .Value = Main.ScanForKnownIncompatibleModsAtStartup; + IgnoreDisabledMods.AddUI(group) + .Value = Main.IgnoreDisabledMods; + ShowCompatibilityCheckErrorMessage.AddUI(group) + .Value = Main.ShowCompatibilityCheckErrorMessage; + } + + private static string T(string key) => Translation.Options.Get(key); + + private static void OnScanForKnownIncompatibleModsAtStartupChanged(bool value) { + if (Main.ScanForKnownIncompatibleModsAtStartup == value) return; + + Main.ScanForKnownIncompatibleModsAtStartup = value; + GlobalConfig.WriteConfig(); + } + + private static void OnIgnoreDisabledModsChanged(bool value) { + if (Main.IgnoreDisabledMods == value) return; + + Main.IgnoreDisabledMods = value; + GlobalConfig.WriteConfig(); + } + + private static void OnShowCompatibilityCheckErrorMessageChanged(bool value) { + if (Main.ShowCompatibilityCheckErrorMessage == value) return; + + Main.ShowCompatibilityCheckErrorMessage = value; + GlobalConfig.WriteConfig(); + } + } +} \ No newline at end of file diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index 08903e33c..f92d163a5 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -148,6 +148,7 @@ + diff --git a/TLM/TLM/UI/IncompatibleModsPanel.cs b/TLM/TLM/UI/IncompatibleModsPanel.cs index 8835bf8f3..602b6ad8f 100644 --- a/TLM/TLM/UI/IncompatibleModsPanel.cs +++ b/TLM/TLM/UI/IncompatibleModsPanel.cs @@ -181,8 +181,7 @@ protected override void OnKeyDown(UIKeyEventParameter p) { /// /// The new value of the checkbox; true if checked, otherwise false. private void RunModsCheckerOnStartup_eventCheckChanged(bool value) { - Log._Debug("Incompatible mods checker run on game launch changed to " + value); - GeneralTab.SetScanForKnownIncompatibleMods(value); + GeneralTab_CompatibilityGroup.ScanForKnownIncompatibleModsAtStartup.Value = value; } /// From b5f95c630584a387141e4154e217fcb0af5c7833 Mon Sep 17 00:00:00 2001 From: aubergine10 Date: Wed, 23 Feb 2022 04:30:01 +0000 Subject: [PATCH 07/10] Split out sim accuracy group, remove debug group --- TLM/TLM/Manager/Impl/OptionsManager.cs | 4 +- TLM/TLM/State/Options.cs | 2 +- TLM/TLM/State/OptionsTabs/GeneralTab.cs | 73 +------------------ .../OptionsTabs/GeneralTab_DebugGroup.cs | 42 ----------- .../OptionsTabs/GeneralTab_SimulationGroup.cs | 57 +++++++++++++++ TLM/TLM/TLM.csproj | 2 +- 6 files changed, 65 insertions(+), 115 deletions(-) delete mode 100644 TLM/TLM/State/OptionsTabs/GeneralTab_DebugGroup.cs create mode 100644 TLM/TLM/State/OptionsTabs/GeneralTab_SimulationGroup.cs diff --git a/TLM/TLM/Manager/Impl/OptionsManager.cs b/TLM/TLM/Manager/Impl/OptionsManager.cs index 7ed753174..ae6778d3f 100644 --- a/TLM/TLM/Manager/Impl/OptionsManager.cs +++ b/TLM/TLM/Manager/Impl/OptionsManager.cs @@ -104,7 +104,7 @@ public bool LoadData(byte[] data) { Log.Info($"OptionsManager.LoadData: {data.Length} bytes"); - GeneralTab.SetSimulationAccuracy(ConvertToSimulationAccuracy(LoadByte(data, idx: 0))); + GeneralTab_SimulationGroup.SetSimulationAccuracy(ConvertToSimulationAccuracy(LoadByte(data, idx: 0))); // skip Options.setLaneChangingRandomization(options[1]); GameplayTab_VehicleBehaviourGroup.SetRecklessDrivers(LoadByte(data, idx: 2)); PoliciesTab.SetRelaxedBusses(LoadBool(data, idx: 3)); @@ -133,7 +133,7 @@ public bool LoadData(byte[] data) { PoliciesTab.SetPreferOuterLane(LoadBool(data, idx: 26)); ToCheckbox(data, idx: 27, GameplayTab_VehicleBehaviourGroup.IndividualDrivingStyle, false); PoliciesTab.SetEvacBussesMayIgnoreRules(LoadBool(data, idx: 28)); - GeneralTab.SetInstantEffects(LoadBool(data, idx: 29)); + ToCheckbox(data, idx: 29, GeneralTab_SimulationGroup.InstantEffects, true); MaintenanceTab.SetParkingRestrictionsEnabled(LoadBool(data, idx: 30)); OverlaysTab.SetParkingRestrictionsOverlay(LoadBool(data, idx: 31)); PoliciesTab.SetBanRegularTrafficOnBusLanes(LoadBool(data, idx: 32)); diff --git a/TLM/TLM/State/Options.cs b/TLM/TLM/State/Options.cs index 997c043e2..6d2b04bd6 100644 --- a/TLM/TLM/State/Options.cs +++ b/TLM/TLM/State/Options.cs @@ -43,7 +43,7 @@ public enum PersistTo { /// public static bool Available = false; - public static bool instantEffects = true; + public static bool instantEffects; public static bool individualDrivingStyle = true; public static int recklessDrivers = 3; diff --git a/TLM/TLM/State/OptionsTabs/GeneralTab.cs b/TLM/TLM/State/OptionsTabs/GeneralTab.cs index 265605c08..7d77c5a6d 100644 --- a/TLM/TLM/State/OptionsTabs/GeneralTab.cs +++ b/TLM/TLM/State/OptionsTabs/GeneralTab.cs @@ -1,9 +1,4 @@ namespace TrafficManager.State { - using TrafficManager.API.Traffic.Enums; - using ColossalFramework.UI; - using CSUtil.Commons; - using ICities; - using JetBrains.Annotations; using TrafficManager.UI; using TrafficManager.UI.Helpers; using TrafficManager.UI.WhatsNew; @@ -14,20 +9,9 @@ public static class GeneralTab { Handler = WhatsNew.OpenModal, }; - private static UICheckBox _instantEffectsToggle; - - [UsedImplicitly] - private static UIDropDown _simulationAccuracyDropdown; - - private static string T(string key) => Translation.Options.Get(key); - internal static void MakeSettings_General(ExtUITabstrip tabStrip) { - UIHelper tab = tabStrip.AddTabPage(T("Tab:General")); - UIHelperBase group; -#if DEBUG - GeneralTab_DebugGroup.AddUI(tab); -#endif + UIHelper tab = tabStrip.AddTabPage(T("Tab:General")); tab.AddSpace(5); WhatsNewButton.AddUI(tab); @@ -35,62 +19,13 @@ internal static void MakeSettings_General(ExtUITabstrip tabStrip) { GeneralTab_LocalisationGroup.AddUI(tab); - group = tab.AddGroup(T("General.Group:Simulation")); - - string[] simPrecisionOptions = new[] { - T("General.Dropdown.Option:Very low"), - T("General.Dropdown.Option:Low"), - T("General.Dropdown.Option:Medium"), - T("General.Dropdown.Option:High"), - T("General.Dropdown.Option:Very high"), - }; - _simulationAccuracyDropdown = group.AddDropdown( - text: T("General.Dropdown:Simulation accuracy") + ":", - options: simPrecisionOptions, - defaultSelection: (int)Options.simulationAccuracy, - eventCallback: OnSimulationAccuracyChanged) as UIDropDown; - - _instantEffectsToggle = group.AddCheckbox( - text: T("General.Checkbox:Apply AI changes right away"), - defaultValue: Options.instantEffects, - eventCallback: OnInstantEffectsChanged) as UICheckBox; + GeneralTab_SimulationGroup.AddUI(tab); GeneralTab_InterfaceGroup.AddUI(tab); GeneralTab_CompatibilityGroup.AddUI(tab); } - private static void OnSimulationAccuracyChanged(int newAccuracy) { - if (!Options.IsGameLoaded()) { - return; - } - - Log._Debug($"Simulation accuracy changed to {newAccuracy}"); - Options.simulationAccuracy = (SimulationAccuracy)newAccuracy; - } - - private static void OnInstantEffectsChanged(bool newValue) { - if (!Options.IsGameLoaded()) { - return; - } - - Log._Debug($"Instant effects changed to {newValue}"); - Options.instantEffects = newValue; - } - - public static void SetSimulationAccuracy(SimulationAccuracy newAccuracy) { - Options.simulationAccuracy = newAccuracy; - if (_simulationAccuracyDropdown != null) { - _simulationAccuracyDropdown.selectedIndex = (int)newAccuracy; - } - } - - public static void SetInstantEffects(bool value) { - Options.instantEffects = value; - - if (_instantEffectsToggle != null) { - _instantEffectsToggle.isChecked = value; - } - } - } // end class + private static string T(string key) => Translation.Options.Get(key); + } } diff --git a/TLM/TLM/State/OptionsTabs/GeneralTab_DebugGroup.cs b/TLM/TLM/State/OptionsTabs/GeneralTab_DebugGroup.cs deleted file mode 100644 index 739390677..000000000 --- a/TLM/TLM/State/OptionsTabs/GeneralTab_DebugGroup.cs +++ /dev/null @@ -1,42 +0,0 @@ -#if DEBUG -namespace TrafficManager.State { - using ICities; - using System; - using System.Diagnostics.CodeAnalysis; - using TrafficManager.Lifecycle; - using TrafficManager.UI.Helpers; - using TrafficManager.Util; - - /// DEBUG-only group for testing checkbox options. - [SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1516:Elements should be separated by blank line", Justification = "Brevity.")] - public static class GeneralTab_DebugGroup { - public static CheckboxOption DebugCheckboxA = - new ("DebugCheckboxA", Options.PersistTo.None) { - Label = "Checkbox A: requires Checkbox B", - }; - public static CheckboxOption DebugCheckboxB = - new ("DebugCheckboxB", Options.PersistTo.None) { - Label = "Checkbox B: is required by Checkbox A", - }; - - static GeneralTab_DebugGroup() { - try { - DebugCheckboxA - .PropagateTrueTo(DebugCheckboxB); - } - catch (Exception ex) { - ex.LogException(); - } - } - - internal static void AddUI(UIHelperBase tab) { - if (TMPELifecycle.InGameOrEditor()) return; - - var group = tab.AddGroup("Debug CheckboxOption"); - - DebugCheckboxA.AddUI(group); - DebugCheckboxB.AddUI(group); - } - } -} -#endif \ No newline at end of file diff --git a/TLM/TLM/State/OptionsTabs/GeneralTab_SimulationGroup.cs b/TLM/TLM/State/OptionsTabs/GeneralTab_SimulationGroup.cs new file mode 100644 index 000000000..79c28cfe6 --- /dev/null +++ b/TLM/TLM/State/OptionsTabs/GeneralTab_SimulationGroup.cs @@ -0,0 +1,57 @@ +namespace TrafficManager.State { + using ColossalFramework.UI; + using CSUtil.Commons; + using ICities; + using TrafficManager.API.Traffic.Enums; + using TrafficManager.UI; + using TrafficManager.UI.Helpers; + + public static class GeneralTab_SimulationGroup { + + private static UIDropDown _simulationAccuracyDropdown; + + public static CheckboxOption InstantEffects = + new (nameof(Options.instantEffects), Options.PersistTo.Savegame) { + Label = "General.Checkbox:Apply AI changes right away", + }; + + public static void SetSimulationAccuracy(SimulationAccuracy value) { + Options.simulationAccuracy = value; + if (_simulationAccuracyDropdown != null) { + _simulationAccuracyDropdown.selectedIndex = (int)value; + } + } + + internal static void AddUI(UIHelperBase tab) { + + var group = tab.AddGroup(T("General.Group:Simulation")); + + AddSimulationAccuracyDropDown(group); + InstantEffects.AddUI(group); + } + + private static string T(string key) => Translation.Options.Get(key); + + private static void AddSimulationAccuracyDropDown(UIHelperBase group) { + string[] simPrecisionOptions = new[] { + T("General.Dropdown.Option:Very low"), + T("General.Dropdown.Option:Low"), + T("General.Dropdown.Option:Medium"), + T("General.Dropdown.Option:High"), + T("General.Dropdown.Option:Very high"), + }; + _simulationAccuracyDropdown = group.AddDropdown( + text: T("General.Dropdown:Simulation accuracy") + ":", + options: simPrecisionOptions, + defaultSelection: (int)Options.simulationAccuracy, + eventCallback: OnSimulationAccuracyChanged) as UIDropDown; + } + + private static void OnSimulationAccuracyChanged(int value) { + if (!Options.IsGameLoaded()) return; + + Log.Info($"Simulation accuracy changed to {value}"); + Options.simulationAccuracy = (SimulationAccuracy)value; + } + } +} \ No newline at end of file diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index f92d163a5..97c1c92db 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -152,7 +152,7 @@ - + From 204bfd51cb3d5175246dcc543b41ed836f3e23ed Mon Sep 17 00:00:00 2001 From: aubergine10 Date: Sat, 26 Feb 2022 06:37:53 +0000 Subject: [PATCH 08/10] Remove pointless `instantEffects` option - Move `MayPublishSegmentChanges()` to `TMPELifecycle` class - Mark `OptionsManager` as obsolete and redirect to `TMPELifecycle` --- TLM/TLM/Lifecycle/TMPELifecycle.cs | 9 ++++++++- TLM/TLM/Manager/Impl/OptionsManager.cs | 13 ++++++------- .../State/OptionsTabs/GeneralTab_SimulationGroup.cs | 6 ------ TLM/TMPE.API/Manager/IOptionsManager.cs | 3 +++ 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/TLM/TLM/Lifecycle/TMPELifecycle.cs b/TLM/TLM/Lifecycle/TMPELifecycle.cs index 139168c75..ab08dc082 100644 --- a/TLM/TLM/Lifecycle/TMPELifecycle.cs +++ b/TLM/TLM/Lifecycle/TMPELifecycle.cs @@ -17,7 +17,7 @@ namespace TrafficManager.Lifecycle { using UnityEngine.SceneManagement; using UnityEngine; using JetBrains.Annotations; - using UI.WhatsNew; + using TrafficManager.UI.WhatsNew; using System.Diagnostics.CodeAnalysis; using TrafficManager.UI.Helpers; using TrafficManager.API.Traffic.Enums; @@ -62,6 +62,13 @@ public static bool InGameOrEditor() => SceneManager.GetActiveScene().name != "MainMenu" && SceneManager.GetActiveScene().name != "Startup"; + /// + /// Determines if modifications to segments may be published in the current state. + /// + /// Returns true if changes may be published, otherwise false. + public bool MayPublishSegmentChanges() + => InGameOrEditor() && !Instance.Deserializing; + public static AppMode? AppMode => SimulationManager.instance.m_ManagersWrapper.loading?.currentMode; public static SimulationManager.UpdateMode UpdateMode => SimulationManager.instance.m_metaData.m_updateMode; diff --git a/TLM/TLM/Manager/Impl/OptionsManager.cs b/TLM/TLM/Manager/Impl/OptionsManager.cs index ae6778d3f..4facc2b2b 100644 --- a/TLM/TLM/Manager/Impl/OptionsManager.cs +++ b/TLM/TLM/Manager/Impl/OptionsManager.cs @@ -55,6 +55,10 @@ public bool TryGetOptionByName(string optionName, out TVal value) { return true; } + [Obsolete("Use TMPELifecycle method of same name instead")] + public bool MayPublishSegmentChanges() + => TMPELifecycle.Instance.MayPublishSegmentChanges(); + /// /// Converts SimulationAccuracy to SimulationAccuracy /// @@ -64,11 +68,6 @@ private static byte ConvertFromSimulationAccuracy(SimulationAccuracy value) { return (byte)(SimulationAccuracy.MaxValue - value); } - public bool MayPublishSegmentChanges() { - return Options.instantEffects && TMPELifecycle.InGameOrEditor() && - !TMPELifecycle.Instance.Deserializing; - } - // Takes a bool from data and sets it in `out result` private static bool LoadBool([NotNull] byte[] data, uint idx, bool defaultVal = false) { if (data.Length > idx) { @@ -133,7 +132,7 @@ public bool LoadData(byte[] data) { PoliciesTab.SetPreferOuterLane(LoadBool(data, idx: 26)); ToCheckbox(data, idx: 27, GameplayTab_VehicleBehaviourGroup.IndividualDrivingStyle, false); PoliciesTab.SetEvacBussesMayIgnoreRules(LoadBool(data, idx: 28)); - ToCheckbox(data, idx: 29, GeneralTab_SimulationGroup.InstantEffects, true); + // skip ToCheckbox(data, idx: 29, GeneralTab_SimulationGroup.InstantEffects, true); MaintenanceTab.SetParkingRestrictionsEnabled(LoadBool(data, idx: 30)); OverlaysTab.SetParkingRestrictionsOverlay(LoadBool(data, idx: 31)); PoliciesTab.SetBanRegularTrafficOnBusLanes(LoadBool(data, idx: 32)); @@ -231,7 +230,7 @@ public byte[] SaveData(ref bool success) { save[26] = (byte)(Options.preferOuterLane ? 1 : 0); save[27] = GameplayTab_VehicleBehaviourGroup.IndividualDrivingStyle.Save(); save[28] = (byte)(Options.evacBussesMayIgnoreRules ? 1 : 0); - save[29] = (byte)(Options.instantEffects ? 1 : 0); + save[29] = 0; // (byte)(Options.instantEffects ? 1 : 0); save[30] = (byte)(Options.parkingRestrictionsEnabled ? 1 : 0); save[31] = (byte)(Options.parkingRestrictionsOverlay ? 1 : 0); save[32] = (byte)(Options.banRegularTrafficOnBusLanes ? 1 : 0); diff --git a/TLM/TLM/State/OptionsTabs/GeneralTab_SimulationGroup.cs b/TLM/TLM/State/OptionsTabs/GeneralTab_SimulationGroup.cs index 79c28cfe6..5c2453c80 100644 --- a/TLM/TLM/State/OptionsTabs/GeneralTab_SimulationGroup.cs +++ b/TLM/TLM/State/OptionsTabs/GeneralTab_SimulationGroup.cs @@ -10,11 +10,6 @@ public static class GeneralTab_SimulationGroup { private static UIDropDown _simulationAccuracyDropdown; - public static CheckboxOption InstantEffects = - new (nameof(Options.instantEffects), Options.PersistTo.Savegame) { - Label = "General.Checkbox:Apply AI changes right away", - }; - public static void SetSimulationAccuracy(SimulationAccuracy value) { Options.simulationAccuracy = value; if (_simulationAccuracyDropdown != null) { @@ -27,7 +22,6 @@ internal static void AddUI(UIHelperBase tab) { var group = tab.AddGroup(T("General.Group:Simulation")); AddSimulationAccuracyDropDown(group); - InstantEffects.AddUI(group); } private static string T(string key) => Translation.Options.Get(key); diff --git a/TLM/TMPE.API/Manager/IOptionsManager.cs b/TLM/TMPE.API/Manager/IOptionsManager.cs index f07341480..bf53ecc30 100644 --- a/TLM/TMPE.API/Manager/IOptionsManager.cs +++ b/TLM/TMPE.API/Manager/IOptionsManager.cs @@ -1,4 +1,6 @@ namespace TrafficManager.API.Manager { + using System; + /// /// Manages mod options /// @@ -7,6 +9,7 @@ public interface IOptionsManager : ICustomDataManager { /// Determines if modifications to segments may be published in the current state. /// /// true if changes may be published, false otherwise + [Obsolete("Use TMPELifecycle method of same name instead")] bool MayPublishSegmentChanges(); /// From 634102d951ac7015710cef4d14f2b8c40a47b5d9 Mon Sep 17 00:00:00 2001 From: aubergine10 Date: Sat, 26 Feb 2022 06:40:03 +0000 Subject: [PATCH 09/10] Missed a bit --- TLM/TLM/State/Options.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/TLM/TLM/State/Options.cs b/TLM/TLM/State/Options.cs index 6d2b04bd6..70d83400d 100644 --- a/TLM/TLM/State/Options.cs +++ b/TLM/TLM/State/Options.cs @@ -43,7 +43,6 @@ public enum PersistTo { /// public static bool Available = false; - public static bool instantEffects; public static bool individualDrivingStyle = true; public static int recklessDrivers = 3; From 3a5f455aeda573b271a8f9abdcd3663463448882 Mon Sep 17 00:00:00 2001 From: aubergine10 Date: Sat, 26 Feb 2022 06:45:39 +0000 Subject: [PATCH 10/10] Update call sites to use new implementation --- TLM/TLM/Manager/Impl/JunctionRestrictionsManager.cs | 3 ++- TLM/TLM/Manager/Impl/LaneArrowManager.cs | 3 ++- TLM/TLM/Manager/Impl/LaneConnectionManager.cs | 9 +++++---- TLM/TLM/Manager/Impl/VehicleRestrictionsManager.cs | 7 ++++--- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/TLM/TLM/Manager/Impl/JunctionRestrictionsManager.cs b/TLM/TLM/Manager/Impl/JunctionRestrictionsManager.cs index f96fbb4a4..c6ca21465 100644 --- a/TLM/TLM/Manager/Impl/JunctionRestrictionsManager.cs +++ b/TLM/TLM/Manager/Impl/JunctionRestrictionsManager.cs @@ -16,6 +16,7 @@ namespace TrafficManager.Manager.Impl { using static CSUtil.Commons.TernaryBoolUtil; using TrafficManager.Util; using TrafficManager.Util.Extensions; + using TrafficManager.Lifecycle; public class JunctionRestrictionsManager : AbstractGeometryObservingManager, @@ -1018,7 +1019,7 @@ private void OnSegmentChange(ushort segmentId, if (requireRecalc) { RoutingManager.Instance.RequestRecalculation(segmentId); - if (OptionsManager.Instance.MayPublishSegmentChanges()) { + if (TMPELifecycle.Instance.MayPublishSegmentChanges()) { ExtSegmentManager.Instance.PublishSegmentChanges(segmentId); } } diff --git a/TLM/TLM/Manager/Impl/LaneArrowManager.cs b/TLM/TLM/Manager/Impl/LaneArrowManager.cs index 71333c3f2..4ad71875c 100644 --- a/TLM/TLM/Manager/Impl/LaneArrowManager.cs +++ b/TLM/TLM/Manager/Impl/LaneArrowManager.cs @@ -12,6 +12,7 @@ namespace TrafficManager.Manager.Impl { using UnityEngine; using static TrafficManager.Util.Shortcuts; using TrafficManager.Util.Extensions; + using TrafficManager.Lifecycle; public class LaneArrowManager : AbstractGeometryObservingManager, @@ -200,7 +201,7 @@ private static void RecalculateFlags(uint laneId) { private void OnLaneChange(uint laneId) { ushort segment = laneId.ToLane().m_segment; RoutingManager.Instance.RequestRecalculation(segment); - if (OptionsManager.Instance.MayPublishSegmentChanges()) { + if (TMPELifecycle.Instance.MayPublishSegmentChanges()) { ExtSegmentManager.Instance.PublishSegmentChanges(segment); } } diff --git a/TLM/TLM/Manager/Impl/LaneConnectionManager.cs b/TLM/TLM/Manager/Impl/LaneConnectionManager.cs index e1bff4ef5..2c16bbcf5 100644 --- a/TLM/TLM/Manager/Impl/LaneConnectionManager.cs +++ b/TLM/TLM/Manager/Impl/LaneConnectionManager.cs @@ -14,6 +14,7 @@ namespace TrafficManager.Manager.Impl { using static TrafficManager.Util.Shortcuts; using TrafficManager.Util; using TrafficManager.Util.Extensions; + using TrafficManager.Lifecycle; public class LaneConnectionManager : AbstractGeometryObservingManager, @@ -247,7 +248,7 @@ internal bool RemoveLaneConnection(uint laneId1, uint laneId2, bool startNode1) ref NetNode commonNode = ref commonNodeId.ToNode(); RoutingManager.Instance.RequestNodeRecalculation(ref commonNode); - if (OptionsManager.Instance.MayPublishSegmentChanges()) { + if (TMPELifecycle.Instance.MayPublishSegmentChanges()) { ExtSegmentManager extSegmentManager = ExtSegmentManager.Instance; extSegmentManager.PublishSegmentChanges(segmentId1); extSegmentManager.PublishSegmentChanges(segmentId2); @@ -310,7 +311,7 @@ internal void RemoveLaneConnectionsFromSegment(ushort segmentId, if (recalcAndPublish) { RoutingManager.Instance.RequestRecalculation(segmentId); - if (OptionsManager.Instance.MayPublishSegmentChanges()) { + if (TMPELifecycle.Instance.MayPublishSegmentChanges()) { ExtSegmentManager.Instance.PublishSegmentChanges(segmentId); } } @@ -364,7 +365,7 @@ internal void RemoveLaneConnections(uint laneId, ushort segment = laneId.ToLane().m_segment; RoutingManager.Instance.RequestRecalculation(segment); - if (OptionsManager.Instance.MayPublishSegmentChanges()) { + if (TMPELifecycle.Instance.MayPublishSegmentChanges()) { ExtSegmentManager.Instance.PublishSegmentChanges(segment); } } @@ -426,7 +427,7 @@ internal bool AddLaneConnection(uint sourceLaneId, RoutingManager.Instance.RequestRecalculation(sourceSegmentId, false); RoutingManager.Instance.RequestRecalculation(targetSegmentId, false); - if (OptionsManager.Instance.MayPublishSegmentChanges()) { + if (TMPELifecycle.Instance.MayPublishSegmentChanges()) { ExtSegmentManager extSegmentManager = ExtSegmentManager.Instance; extSegmentManager.PublishSegmentChanges(sourceSegmentId); extSegmentManager.PublishSegmentChanges(targetSegmentId); diff --git a/TLM/TLM/Manager/Impl/VehicleRestrictionsManager.cs b/TLM/TLM/Manager/Impl/VehicleRestrictionsManager.cs index 3ead3501c..0e35ead68 100644 --- a/TLM/TLM/Manager/Impl/VehicleRestrictionsManager.cs +++ b/TLM/TLM/Manager/Impl/VehicleRestrictionsManager.cs @@ -12,6 +12,7 @@ namespace TrafficManager.Manager.Impl { using TrafficManager.Traffic; using TrafficManager.Util; using TrafficManager.Util.Extensions; + using TrafficManager.Lifecycle; public class VehicleRestrictionsManager : AbstractGeometryObservingManager, @@ -447,7 +448,7 @@ internal bool SetAllowedVehicleTypes(ushort segmentId, NotifyStartEndNode(segmentId); - if (OptionsManager.Instance.MayPublishSegmentChanges()) { + if (TMPELifecycle.Instance.MayPublishSegmentChanges()) { ExtSegmentManager.Instance.PublishSegmentChanges(segmentId); } @@ -494,7 +495,7 @@ public void AddAllowedType(ushort segmentId, Flags.SetLaneAllowedVehicleTypes(segmentId, laneIndex, laneId, allowedTypes); NotifyStartEndNode(segmentId); - if (OptionsManager.Instance.MayPublishSegmentChanges()) { + if (TMPELifecycle.Instance.MayPublishSegmentChanges()) { ExtSegmentManager.Instance.PublishSegmentChanges(segmentId); } } @@ -539,7 +540,7 @@ public void RemoveAllowedType(ushort segmentId, Flags.SetLaneAllowedVehicleTypes(segmentId, laneIndex, laneId, allowedTypes); NotifyStartEndNode(segmentId); - if (OptionsManager.Instance.MayPublishSegmentChanges()) { + if (TMPELifecycle.Instance.MayPublishSegmentChanges()) { ExtSegmentManager.Instance.PublishSegmentChanges(segmentId); } }