From 69d7d1cdec650f43383539fc5ad1b5cf90ef4e0a Mon Sep 17 00:00:00 2001 From: aubergine10 Date: Fri, 18 Feb 2022 23:22:36 +0000 Subject: [PATCH 1/2] Add button classes for use in mod options - `ActionButton` triggers handler on click - `UrlButton` opens a url on click - `OptionButtonBase` is base for both buttons - `OpenUrlsInSteamOverlay` - new global option - ...configurable via General tab option --- TLM/TLM/State/ConfigData/Main.cs | 6 +++ TLM/TLM/State/GlobalConfig.cs | 2 +- TLM/TLM/State/OptionsTabs/GeneralTab.cs | 27 ++++++++-- TLM/TLM/TLM.csproj | 3 ++ TLM/TLM/UI/Helpers/ActionButton.cs | 10 ++++ TLM/TLM/UI/Helpers/OptionButtonBase.cs | 72 +++++++++++++++++++++++++ TLM/TLM/UI/Helpers/UrlButton.cs | 52 ++++++++++++++++++ 7 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 TLM/TLM/UI/Helpers/ActionButton.cs create mode 100644 TLM/TLM/UI/Helpers/OptionButtonBase.cs create mode 100644 TLM/TLM/UI/Helpers/UrlButton.cs diff --git a/TLM/TLM/State/ConfigData/Main.cs b/TLM/TLM/State/ConfigData/Main.cs index 894c9e44f..7339f024d 100644 --- a/TLM/TLM/State/ConfigData/Main.cs +++ b/TLM/TLM/State/ConfigData/Main.cs @@ -105,6 +105,12 @@ public SpeedUnit GetDisplaySpeedUnit() => DisplaySpeedLimitsMph /// public string RoadSignTheme = string.Empty; + /// + /// If true, links will open in + /// Steam Overlay if it is available. + /// + public bool OpenUrlsInSteamOverlay = true; + /// /// Storing version of What's New panel opened last time /// diff --git a/TLM/TLM/State/GlobalConfig.cs b/TLM/TLM/State/GlobalConfig.cs index 36526ac94..5d7f6f948 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 = 18; + private static int LATEST_VERSION = 19; public static GlobalConfig Instance { get => instance; diff --git a/TLM/TLM/State/OptionsTabs/GeneralTab.cs b/TLM/TLM/State/OptionsTabs/GeneralTab.cs index 82940895c..d0043923e 100644 --- a/TLM/TLM/State/OptionsTabs/GeneralTab.cs +++ b/TLM/TLM/State/OptionsTabs/GeneralTab.cs @@ -1,5 +1,4 @@ namespace TrafficManager.State { - using System; using System.Collections.Generic; using System.Linq; using TrafficManager.API.Traffic.Enums; @@ -9,7 +8,6 @@ namespace TrafficManager.State { using JetBrains.Annotations; using TrafficManager.U; using TrafficManager.UI.Helpers; - using TrafficManager.UI.SubTools.SpeedLimits; using TrafficManager.UI; using UnityEngine; using TrafficManager.Lifecycle; @@ -18,6 +16,19 @@ namespace TrafficManager.State { using UI.WhatsNew; public static class GeneralTab { + public static ActionButton WhatsNewButton = new() { + Label = "What's New?", + 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] @@ -60,7 +71,7 @@ internal static void MakeSettings_General(ExtUITabstrip tabStrip) { #endif tab.AddSpace(5); - tab.AddButton("What's New?", WhatsNew.OpenModal); + WhatsNewButton.AddUI(tab); tab.AddSpace(5); group = tab.AddGroup(T("General.Group:Localisation")); @@ -141,6 +152,8 @@ internal static void MakeSettings_General(ExtUITabstrip tabStrip) { GlobalConfig.Instance.Main.EnableTutorial, OnEnableTutorialsChanged) as UICheckBox; + OpenUrlsInSteamOverlay.AddUI(group); + group = tab.AddGroup(T("General.Group:Simulation")); string[] simPrecisionOptions = new[] { @@ -428,6 +441,14 @@ 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; diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index f413e0cab..06edb7de3 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -151,6 +151,9 @@ + + + diff --git a/TLM/TLM/UI/Helpers/ActionButton.cs b/TLM/TLM/UI/Helpers/ActionButton.cs new file mode 100644 index 000000000..f6750bdb4 --- /dev/null +++ b/TLM/TLM/UI/Helpers/ActionButton.cs @@ -0,0 +1,10 @@ +namespace TrafficManager.UI.Helpers { + using ICities; + + public class ActionButton : OptionButtonBase { + + public OnButtonClicked Handler { + set => OnClicked += value; + } + } +} diff --git a/TLM/TLM/UI/Helpers/OptionButtonBase.cs b/TLM/TLM/UI/Helpers/OptionButtonBase.cs new file mode 100644 index 000000000..b4be31c98 --- /dev/null +++ b/TLM/TLM/UI/Helpers/OptionButtonBase.cs @@ -0,0 +1,72 @@ +namespace TrafficManager.UI.Helpers { + using ColossalFramework.UI; + using ICities; + + public abstract class OptionButtonBase { + protected string _label; + protected string _tooltip; + protected bool _readOnly; + + protected UIButton _ui; + + protected event OnButtonClicked OnClicked; + + public bool HasUI => _ui != null; + + public string Label { + get => _label ?? string.Empty; + set { + _label = value; + UpdateLabel(); + } + } + + public string Tooltip { + get => _tooltip; + set { + _tooltip = value; + UpdateTooltip(); + } + } + + public bool ReadOnly { + get => _readOnly; + set { + _readOnly = value; + UpdateReadOnly(); + } + } + + public void AddUI(UIHelperBase container) { + _ui = container.AddButton(T(_label), OnClicked) as UIButton; + + UpdateTooltip(); + UpdateReadOnly(); + } + + protected virtual void UpdateLabel() { + if (!HasUI) return; + + _ui.text = T(_label); + } + + protected virtual void UpdateTooltip() { + if (!HasUI) return; + + _ui.tooltip = string.IsNullOrEmpty(_tooltip) + ? string.Empty + : T(_tooltip); + } + + protected virtual void UpdateReadOnly() { + if (!HasUI) return; + + _ui.isInteractive = !_readOnly; + _ui.opacity = _readOnly ? 0.3f : 1f; + } + + protected virtual string T(string key) + => Translation.Options.Get(key); + + } +} diff --git a/TLM/TLM/UI/Helpers/UrlButton.cs b/TLM/TLM/UI/Helpers/UrlButton.cs new file mode 100644 index 000000000..c0b835d3f --- /dev/null +++ b/TLM/TLM/UI/Helpers/UrlButton.cs @@ -0,0 +1,52 @@ +namespace TrafficManager.UI.Helpers { + using ColossalFramework.PlatformServices; + using System.Diagnostics; + using TrafficManager.Lifecycle; + using TrafficManager.State; + + public class UrlButton : OptionButtonBase { + private string _url; + + public UrlButton() { + OnClicked += OpenURL; + } + + public static bool SteamOverlayAvailable + => PlatformService.platformType == PlatformType.Steam && + PlatformService.IsOverlayEnabled(); + + public string URL { + get => _url; + set { + _url = value; + UpdateTooltip(); + } + } + + protected override void UpdateTooltip() { + if (!HasUI) return; + + _ui.tooltip = string.IsNullOrEmpty(_tooltip) + ? _url + : $"{T(_tooltip)}:\n{_url}"; + } + + private void OpenURL() { + if (string.IsNullOrEmpty(_url)) return; + + if (TMPELifecycle.InGameOrEditor()) + SimulationManager.instance.SimulationPaused = true; + + bool useSteamOverlay = + SteamOverlayAvailable && + GlobalConfig.Instance.Main.OpenUrlsInSteamOverlay; + + if (useSteamOverlay) { + PlatformService.ActivateGameOverlayToWebPage(_url); + } else { + //Application.OpenURL(_url); + Process.Start(_url); + } + } + } +} From 30299d7236c98adfa43614bf396b3d8640ac2c5c Mon Sep 17 00:00:00 2001 From: aubergine10 Date: Sun, 20 Feb 2022 20:37:48 +0000 Subject: [PATCH 2/2] Update based on review feedback https://github.com/CitiesSkylinesMods/TMPE/pull/1424#pullrequestreview-888054744 --- TLM/TLM/UI/Helpers/ActionButton.cs | 5 ++++- TLM/TLM/UI/Helpers/OptionButtonBase.cs | 2 +- TLM/TLM/UI/Helpers/UrlButton.cs | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/TLM/TLM/UI/Helpers/ActionButton.cs b/TLM/TLM/UI/Helpers/ActionButton.cs index f6750bdb4..40ada4ba6 100644 --- a/TLM/TLM/UI/Helpers/ActionButton.cs +++ b/TLM/TLM/UI/Helpers/ActionButton.cs @@ -4,7 +4,10 @@ namespace TrafficManager.UI.Helpers { public class ActionButton : OptionButtonBase { public OnButtonClicked Handler { - set => OnClicked += value; + set { + OnClicked -= value; + OnClicked += value; + } } } } diff --git a/TLM/TLM/UI/Helpers/OptionButtonBase.cs b/TLM/TLM/UI/Helpers/OptionButtonBase.cs index b4be31c98..4c90e22e4 100644 --- a/TLM/TLM/UI/Helpers/OptionButtonBase.cs +++ b/TLM/TLM/UI/Helpers/OptionButtonBase.cs @@ -9,7 +9,7 @@ public abstract class OptionButtonBase { protected UIButton _ui; - protected event OnButtonClicked OnClicked; + public event OnButtonClicked OnClicked; public bool HasUI => _ui != null; diff --git a/TLM/TLM/UI/Helpers/UrlButton.cs b/TLM/TLM/UI/Helpers/UrlButton.cs index c0b835d3f..b253d92dc 100644 --- a/TLM/TLM/UI/Helpers/UrlButton.cs +++ b/TLM/TLM/UI/Helpers/UrlButton.cs @@ -8,6 +8,7 @@ public class UrlButton : OptionButtonBase { private string _url; public UrlButton() { + OnClicked -= OpenURL; OnClicked += OpenURL; }