Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add button classes for use in mod options #1424

Merged
merged 2 commits into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions TLM/TLM/State/ConfigData/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public SpeedUnit GetDisplaySpeedUnit() => DisplaySpeedLimitsMph
/// </summary>
public string RoadSignTheme = string.Empty;

/// <summary>
/// If <c>true</c>, <see cref="UI.Helpers.UrlButton"/> links will open in
/// Steam Overlay if it is available.
/// </summary>
public bool OpenUrlsInSteamOverlay = true;

/// <summary>
/// Storing version of What's New panel opened last time
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/State/GlobalConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace TrafficManager.State {
public class GlobalConfig : GenericObservable<GlobalConfig> {
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;
Expand Down
27 changes: 24 additions & 3 deletions TLM/TLM/State/OptionsTabs/GeneralTab.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
namespace TrafficManager.State {
using System;
using System.Collections.Generic;
using System.Linq;
using TrafficManager.API.Traffic.Enums;
Expand All @@ -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;
Expand All @@ -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]
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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[] {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions TLM/TLM/TLM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@
<Compile Include="State\OptionsTabs\GeneralTab_DebugGroup.cs" />
<Compile Include="State\OptionsTabs\MaintenanceTab_DespawnGroup.cs" />
<Compile Include="State\VersionInfo.cs" />
<Compile Include="UI\Helpers\ActionButton.cs" />
<Compile Include="UI\Helpers\OptionButtonBase.cs" />
<Compile Include="UI\Helpers\UrlButton.cs" />
<Compile Include="UI\WhatsNew\MarkupKeyword.cs" />
<Compile Include="UI\WhatsNew\WhatsNewMarkup.cs" />
<Compile Include="Util\Extensions\CitizenUnitExtensions.cs" />
Expand Down
13 changes: 13 additions & 0 deletions TLM/TLM/UI/Helpers/ActionButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace TrafficManager.UI.Helpers {
using ICities;

public class ActionButton : OptionButtonBase {

public OnButtonClicked Handler {
set {
OnClicked -= value;
OnClicked += value;
}
}
}
}
72 changes: 72 additions & 0 deletions TLM/TLM/UI/Helpers/OptionButtonBase.cs
Original file line number Diff line number Diff line change
@@ -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;

public 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);

}
}
53 changes: 53 additions & 0 deletions TLM/TLM/UI/Helpers/UrlButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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;
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);
}
}
}
}