Skip to content

Commit

Permalink
Merge pull request #774 from CitiesSkylinesMods/prompt-helper
Browse files Browse the repository at this point in the history
Small class to display warning and error prompts
  • Loading branch information
originalfoo authored Mar 5, 2020
2 parents 158c739 + 3ad3c5f commit 81e1206
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 42 deletions.
9 changes: 3 additions & 6 deletions TLM/TLM/State/Keybinds/KeybindUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace TrafficManager.State.Keybinds {
using System;
using TrafficManager.UI;
using UnityEngine;
using TrafficManager.UI.Helpers;

/// <summary>
/// Helper for creating keyboard bindings Settings page.
Expand Down Expand Up @@ -219,9 +220,7 @@ private void OnBindingKeyDown(UIComponent comp, UIKeyEventParameter evParam) {
var message = Translation.Options.Get("Keybinds.Dialog.Text:Keybind conflict")
+ "\n\n" + maybeConflict;
Log.Info($"Keybind conflict: {message}");
UIView.library
.ShowModal<ExceptionPanel>("ExceptionPanel")
.SetMessage("Key Conflict", message, false);
Prompt.Warning("Key Conflict", message);
} else {
editedBinding.Value.TargetKey.value = inputKey;
editedBinding.Value.Target.NotifyKeyChanged();
Expand Down Expand Up @@ -257,9 +256,7 @@ private void OnBindingMouseDown(UIComponent comp, UIMouseEventParameter evParam)
var message = Translation.Options.Get("Keybinds.Dialog.Text:Keybind conflict")
+ "\n\n" + maybeConflict;
Log.Info($"Keybind conflict: {message}");
UIView.library
.ShowModal<ExceptionPanel>("ExceptionPanel")
.SetMessage("Key Conflict", message, false);
Prompt.Warning("Key Conflict", message);
} else {
editedBinding.Value.TargetKey.value = inputKey;
editedBinding.Value.Target.NotifyKeyChanged();
Expand Down
5 changes: 2 additions & 3 deletions TLM/TLM/State/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ internal static bool IsGameLoaded(bool warn = true) {
}

if (warn) {
UIView.library.ShowModal<ExceptionPanel>("ExceptionPanel").SetMessage(
Prompt.Warning(
"Nope!",
Translation.Options.Get("Dialog.Text:Settings are stored in savegame")
+ " https://www.viathinksoft.de/tmpe/#options",
false);
+ " https://github.com/CitiesSkylinesMods/TMPE/wiki/Settings");
}

return false;
Expand Down
5 changes: 3 additions & 2 deletions TLM/TLM/TLM.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -150,6 +150,7 @@
<Compile Include="Manager\Impl\ExtSegmentEndManager.cs" />
<Compile Include="Manager\Impl\ExtSegmentManager.cs" />
<Compile Include="UI\Helpers\NodeLaneMarker.cs" />
<Compile Include="UI\Helpers\Prompt.cs" />
<Compile Include="UI\Helpers\SegmentLaneMarker.cs" />
<Compile Include="UI\Helpers\ExtUITabStrip.cs" />
<Compile Include="Manager\Impl\SegmentEndManager.cs" />
Expand Down Expand Up @@ -525,7 +526,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Traffic\Data\" />
<Folder Include="U\MainMenu" />
<Folder Include="U\MainMenu\" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\TrafficLights\pedestrian_mode_1.png" />
Expand Down
12 changes: 2 additions & 10 deletions TLM/TLM/ThreadingExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace TrafficManager {
using TrafficManager.State;
using TrafficManager.UI;
using UnityEngine;
using TrafficManager.UI.Helpers;

[UsedImplicitly]
public sealed class ThreadingExtension : ThreadingExtensionBase {
Expand Down Expand Up @@ -98,16 +99,7 @@ public override void OnBeforeSimulationFrame() {
Log.Info(log);

if (GlobalConfig.Instance.Main.ShowCompatibilityCheckErrorMessage) {
Singleton<SimulationManager>.instance.m_ThreadingWrapper.QueueMainThread(
() => {
UIView
.library
.ShowModal<ExceptionPanel>(
"ExceptionPanel").SetMessage(
"Incompatibility Issue",
error,
true);
});
Prompt.Error("TM:PE Incompatibility Issue", error);
}
}
}
Expand Down
89 changes: 89 additions & 0 deletions TLM/TLM/UI/Helpers/Prompt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
namespace TrafficManager.UI.Helpers {
using ColossalFramework;
using ColossalFramework.UI;
using CSUtil.Commons;
using System;
using UnityEngine.SceneManagement;

/// <summary>
/// Use this class to display small but annoying dialog prompts to the user.
///
/// TODO: At some point add more panels, such as:
/// * ConfirmPanel
/// * ExitConfirmPanel
/// * MessageBoxPanel
/// * TutorialPanel
/// * TutorialAdvisorPanel
/// </summary>
public class Prompt {

/// <summary>
/// Display a warning prompt in the centre of the screen.
/// </summary>
///
/// <param name="title">Dialog title.</param>
/// <param name="message">Dialog body text.</param>
public static void Warning(string title, string message) {
ExceptionPanel(title, message, false);
}

/// <summary>
/// Display a formatted warning prompt in the center of the screen.
/// </summary>
///
/// <param name="title">Dialog title.</param>
/// <param name="messageFormat">Dialog body text format.</param>
/// <param name="args">Values to put in the <paramref name="messageFormat"/>.</param>
public static void WarningFormat(string title, string messageFormat, params object[] args) {
ExceptionPanel(title, string.Format(messageFormat, args), false);
}

/// <summary>
/// Display an error prompt in the centre of the screen.
/// </summary>
/// <param name="title">Dialog title.</param>
/// <param name="message">Dialog body text.</param>
public static void Error(string title, string message) {
ExceptionPanel(title, message, true);
}

/// <summary>
/// Display an formatted error prompt in the center of the screen.
/// </summary>
///
/// <param name="title">Dialog title.</param>
/// <param name="messageFormat">Dialog body text format.</param>
/// <param name="args">Values to put in the <paramref name="messageFormat"/>.</param>
public static void ErrorFormat(string title, string messageFormat, params object[] args) {
ExceptionPanel(title, string.Format(messageFormat, args), true);
}

/// <summary>
/// Display an exception message in the center of the screen, optionally
/// styled as an error.
/// </summary>
///
/// <param name="title">Dialog title.</param>
/// <param name="message">Dialog body text.</param>
/// <param name="isError">If <c>true</c>, the dialog is styled as an error.</param>
internal static void ExceptionPanel(string title, string message, bool isError) {
Action prompt = () => {
UIView.library
.ShowModal<ExceptionPanel>("ExceptionPanel")
.SetMessage(title, message, isError);
};

try {
if (SceneManager.GetActiveScene().name == "Game") {
Singleton<SimulationManager>.instance.m_ThreadingWrapper.QueueMainThread(prompt);
} else {
prompt();
}
} catch (Exception e) {
Log.ErrorFormat(
"Error displaying a Prompt:\n{0}",
e.ToString());
}
}
}
}
6 changes: 2 additions & 4 deletions TLM/TLM/UI/SubTools/ManualTrafficLightsTool.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace TrafficManager.UI.SubTools {
namespace TrafficManager.UI.SubTools {
using ColossalFramework;
using JetBrains.Annotations;
using TrafficLight;
using TrafficManager.API.Manager;
using TrafficManager.API.Traffic.Data;
using TrafficManager.API.Traffic.Enums;
Expand Down Expand Up @@ -62,8 +61,7 @@ ref Singleton<NetManager>.instance.m_nodes.m_buffer[
// }
// }
} else {
MainTool.ShowError(
Translation.TrafficLights.Get("Dialog.Text:Node has timed TL script"));
MainTool.WarningPrompt(Translation.TrafficLights.Get("Dialog.Text:Node has timed TL script"));
}
}

Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/UI/SubTools/PrioritySignsTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ private bool MayNodeHavePrioritySigns(ushort nodeId) {
// Log._Debug($"PrioritySignsTool.MayNodeHavePrioritySigns: Node {nodeId} does not
// allow priority signs: {reason}");
if (reason == SetPrioritySignError.HasTimedLight) {
MainTool.ShowError(
MainTool.WarningPrompt(
Translation.TrafficLights.Get("Dialog.Text:Node has timed TL script"));
}

Expand Down
11 changes: 6 additions & 5 deletions TLM/TLM/UI/SubTools/TimedTrafficLightsTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public override void OnPrimaryClickOverlay() {
bool ctrlDown = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
if(ctrlDown) {
AutoTimedTrafficLights.ErrorResult res = AutoTimedTrafficLights.Setup(HoveredNodeId);
string message = null;
string message = null;

switch (res) {
case AutoTimedTrafficLights.ErrorResult.NotSupported:
MainTool.Guide.Activate("TimedTrafficLightsTool_Auto TL no need");
Expand All @@ -126,7 +127,7 @@ public override void OnPrimaryClickOverlay() {
Translation.TrafficLights.Get("Dialog.Text:Auto TL create failed because") +
"\n" +
Translation.TrafficLights.Get(message);
MainTool.ShowError(message);
MainTool.WarningPrompt(message);
return;
}
RefreshCurrentTimedNodeIds(HoveredNodeId);
Expand Down Expand Up @@ -160,7 +161,7 @@ public override void OnPrimaryClickOverlay() {
MainTool.SetToolMode(ToolMode.TimedLightsShowLights);
}
} else {
MainTool.ShowError(T("Dialog.Text:Node has timed TL script"));
MainTool.WarningPrompt(T("Dialog.Text:Node has timed TL script"));
}
}

Expand Down Expand Up @@ -242,14 +243,14 @@ public override void OnPrimaryClickOverlay() {
.CountSegments();

if (numSourceSegments != numTargetSegments) {
MainTool.ShowError(
MainTool.WarningPrompt(
T("Dialog.Text:Incompatible traffic light script"));
return;
}

// check for existing simulation
if (tlsMan.HasTimedSimulation(HoveredNodeId)) {
MainTool.ShowError(
MainTool.WarningPrompt(
T("Dialog.Text:Node has timed TL script"));
return;
}
Expand Down
4 changes: 2 additions & 2 deletions TLM/TLM/UI/SubTools/ToggleTrafficLightsTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ public void ToggleTrafficLight(ushort nodeId,
if (showMessageOnError) {
switch (reason) {
case ToggleTrafficLightError.HasTimedLight: {
MainTool.ShowError(
MainTool.WarningPrompt(
Translation.TrafficLights.Get("Dialog.Text:Node has timed TL script"));
break;
}

case ToggleTrafficLightError.IsLevelCrossing: {
MainTool.ShowError(
MainTool.WarningPrompt(
Translation.TrafficLights.Get("Dialog.Text:Node is level crossing"));
break;
}
Expand Down
19 changes: 10 additions & 9 deletions TLM/TLM/UI/TrafficManagerTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ namespace TrafficManager.UI {
using JetBrains.Annotations;
using TrafficManager.Manager.Impl;
using TrafficManager.State;
#if DEBUG
using TrafficManager.State.ConfigData;
#endif
using TrafficManager.UI.MainMenu;
using TrafficManager.UI.SubTools;
using TrafficManager.UI.SubTools.SpeedLimits;
using TrafficManager.Util;
using UnityEngine;
using TrafficManager.UI.Helpers;
using GenericGameBridge.Service;
using CitiesGameBridge.Service;

[UsedImplicitly]
public class TrafficManagerTool
Expand Down Expand Up @@ -1871,16 +1871,17 @@ internal bool CheckClicked() {
return false;
}

/// <summary>Displays modal popup with an error</summary>
/// <param name="text">The localized message</param>
public void ShowError(string text) {
if (text == null) {
/// <summary>
/// Displays a warning prompt in center of the screen.
/// </summary>
///
/// <param name="message">The localized body text of the prompt.</param>
public void WarningPrompt(string message) {
if (string.IsNullOrEmpty(message)) {
return;
}

UIView.library
.ShowModal<ExceptionPanel>("ExceptionPanel")
.SetMessage("Info", text, false);
Prompt.Warning("Warning", message);
}
}
}

0 comments on commit 81e1206

Please sign in to comment.