Skip to content

Commit

Permalink
Add button stuff from #1424
Browse files Browse the repository at this point in the history
  • Loading branch information
originalfoo committed Feb 19, 2022
1 parent 01dffb6 commit 11a14ca
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 65 deletions.
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 = false;

/// <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
2 changes: 2 additions & 0 deletions TLM/TLM/TLM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@
<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
65 changes: 1 addition & 64 deletions TLM/TLM/UI/Helpers/ActionButton.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,10 @@
namespace TrafficManager.UI.Helpers {
using ColossalFramework.UI;
using ICities;

public class ActionButton {
private string _label;
private string _tooltip;
private bool _readOnly;

private UIButton _ui;

public event OnButtonClicked OnClicked;
public class ActionButton : OptionButtonBase {

public OnButtonClicked Handler {
set => OnClicked += value;
}

public bool HasUI => _ui != null;

public string Label {
get => _label;
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();
}

private void UpdateLabel() {
if (!HasUI) return;

_ui.text = T(_label);
}

private void UpdateTooltip() {
if (!HasUI) return;

_ui.tooltip = T(_tooltip);
}

private void UpdateReadOnly() {
if (!HasUI) return;

_ui.isInteractive = !_readOnly;
_ui.opacity = _readOnly ? 0.3f : 1f;
}

private string T(string key)
=> Translation.Options.Get(key);
}
}
70 changes: 70 additions & 0 deletions TLM/TLM/UI/Helpers/OptionButtonBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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;
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 = 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);

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

0 comments on commit 11a14ca

Please sign in to comment.