-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1424 from CitiesSkylinesMods/option-button
Add button classes for use in mod options
- Loading branch information
Showing
7 changed files
with
172 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |