Skip to content

Commit 2a13f80

Browse files
kianzarrinkrzychu124
authored andcommitted
Api update (#1577)
API update for compatibility with NC/DCR/HUT * TrafficLights/JunctionRestrictions: updated API * LaneEndTransitionGroup : Added Bicycle and Pedestrian for future proofing. * Expose Road sign theme in API. (cherry picked from commit 2a4c104)
1 parent f72ad76 commit 2a13f80

14 files changed

+355
-98
lines changed

TLM/TLM/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
namespace TrafficManager {
22
using TrafficManager.API.Manager;
33
using TrafficManager.API.Notifier;
4+
using TrafficManager.API.UI;
45
using TrafficManager.U;
6+
using TrafficManager.UI.Textures;
57

68
public static class Constants {
79
/// <summary>
@@ -35,6 +37,9 @@ public static float ByteToFloat(byte b) {
3537

3638
public static IManagerFactory ManagerFactory => Manager.Impl.ManagerFactory.Instance;
3739

40+
public static IUIFactory UIFactory => UI.UIFactory.Instance;
41+
3842
public static INotifier Notifier => TrafficManager.Notifier.Instance;
43+
3944
}
4045
}

TLM/TLM/Manager/Impl/JunctionRestrictionsManager.cs

Lines changed: 220 additions & 95 deletions
Large diffs are not rendered by default.

TLM/TLM/Manager/Impl/TrafficLightManager.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ public void RemoveAllExistingTrafficLights() {
119119
}
120120
}
121121

122+
public bool ToggleTrafficLight(ushort nodeId) => ToggleTrafficLight(nodeId, ref nodeId.ToNode());
123+
122124
public bool ToggleTrafficLight(ushort nodeId, ref NetNode node) {
123125
return SetTrafficLight(nodeId, !HasTrafficLight(nodeId, ref node), ref node);
124126
}
@@ -127,6 +129,16 @@ public bool ToggleTrafficLight(ushort nodeId, ref NetNode node, out ToggleTraffi
127129
return SetTrafficLight(nodeId, !HasTrafficLight(nodeId, ref node), ref node, out reason);
128130
}
129131

132+
bool ITrafficLightManager.CanToggleTrafficLight(ushort nodeId) {
133+
ref NetNode netNode = ref nodeId.ToNode();
134+
return netNode.IsValid() &&
135+
CanToggleTrafficLight(
136+
nodeId,
137+
HasTrafficLight(nodeId, ref netNode),
138+
ref netNode,
139+
out _);
140+
}
141+
130142
public bool CanToggleTrafficLight(ushort nodeId,
131143
bool flag, // override?
132144
ref NetNode node,
@@ -242,6 +254,8 @@ public bool CanEnableTrafficLight(ushort nodeId,
242254
return ret;
243255
}
244256

257+
public bool HasTrafficLight(ushort nodeId) => HasTrafficLight(nodeId, ref nodeId.ToNode());
258+
245259
public bool HasTrafficLight(ushort nodeId, ref NetNode node) {
246260
return node.IsValid()
247261
&& node.m_flags.IsFlagSet(NetNode.Flags.TrafficLights);

TLM/TLM/TLM.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
<Compile Include="UI\SubTools\RoutingDetector\Connection.cs" />
183183
<Compile Include="UI\SubTools\RoutingDetector\LaneEnd.cs" />
184184
<Compile Include="UI\SubTools\RoutingDetector\RoutingDetectorTool.cs" />
185+
<Compile Include="UI\UIFactory.cs" />
185186
<Compile Include="UI\WhatsNew\MarkupKeyword.cs" />
186187
<Compile Include="UI\WhatsNew\WhatsNewMarkup.cs" />
187188
<Compile Include="Util\Extensions\CitizenUnitExtensions.cs" />

TLM/TLM/UI/Textures/RoadSignTheme.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
namespace TrafficManager.UI.Textures {
1+
namespace TrafficManager.UI.Textures {
22
using System;
33
using System.Collections.Generic;
44
using CSUtil.Commons;
55
using JetBrains.Annotations;
66
using TrafficManager.API.Traffic.Data;
77
using TrafficManager.API.Traffic.Enums;
8+
using TrafficManager.API.UI;
9+
using TrafficManager.Manager.Impl;
810
using TrafficManager.State;
911
using TrafficManager.UI.SubTools;
1012
using TrafficManager.Util;
@@ -15,7 +17,7 @@
1517
/// Defines one theme for road signs. All themes are accessible via, and stored in
1618
/// <see cref="RoadSignThemeManager"/>.
1719
/// </summary>
18-
public class RoadSignTheme {
20+
public class RoadSignTheme : ITheme {
1921
public enum OtherRestriction {
2022
Crossing,
2123
EnterBlockedJunction,
@@ -148,6 +150,39 @@ public Texture2D GetOtherRestriction(OtherRestriction type, bool allow) {
148150
: this.ParentTheme.GetOtherRestriction(type, allow: false);
149151
}
150152

153+
public Texture2D JunctionRestriction(JunctionRestrictionFlags rule, bool allowed) {
154+
bool rht = Shortcuts.RHT;
155+
switch (rule) {
156+
case JunctionRestrictionFlags.AllowPedestrianCrossing:
157+
return GetOtherRestriction(OtherRestriction.Crossing, allowed);
158+
case JunctionRestrictionFlags.AllowUTurn:
159+
return GetOtherRestriction(OtherRestriction.UTurn, allowed);
160+
case JunctionRestrictionFlags.AllowEnterWhenBlocked:
161+
return GetOtherRestriction(OtherRestriction.EnterBlockedJunction, allowed);
162+
case JunctionRestrictionFlags.AllowForwardLaneChange:
163+
return GetOtherRestriction(OtherRestriction.LaneChange, allowed);
164+
case JunctionRestrictionFlags.AllowFarTurnOnRed when rht:
165+
case JunctionRestrictionFlags.AllowNearTurnOnRed when !rht:
166+
return GetOtherRestriction(OtherRestriction.LeftOnRed, allowed);
167+
case JunctionRestrictionFlags.AllowNearTurnOnRed when rht:
168+
case JunctionRestrictionFlags.AllowFarTurnOnRed when !rht:
169+
return GetOtherRestriction(OtherRestriction.RightOnRed, allowed);
170+
default:
171+
Log.Error($"could not get texture for {rule}.");
172+
return null;
173+
}
174+
}
175+
176+
public Texture2D TrafficLightIcon(ushort nodeId) {
177+
if (!TrafficLightManager.Instance.HasTrafficLight(nodeId)) {
178+
return TrafficLightTextures.Instance.TrafficLightDisabled;
179+
} else if (TrafficLightSimulationManager.Instance.HasSimulation(nodeId)) {
180+
return TrafficLightTextures.Instance.TrafficLightEnabledTimed;
181+
} else {
182+
return TrafficLightTextures.Instance.TrafficLightEnabled;
183+
}
184+
}
185+
151186
public RoadSignTheme Load(bool whiteTexture = false) {
152187
if (this.AttemptedToLoad) {
153188
return this;

TLM/TLM/UI/UIFactory.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace TrafficManager.UI {
2+
using TrafficManager.API.UI;
3+
using TrafficManager.UI.Textures;
4+
5+
public class UIFactory : IUIFactory {
6+
public static IUIFactory Instance = new UIFactory();
7+
8+
public ITheme ActiveTheme => RoadSignThemeManager.ActiveTheme;
9+
}
10+
}

TLM/TMPE.API/Implementations.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ namespace TrafficManager.API {
33
using TrafficManager.API.Manager;
44
using TrafficManager.API.Notifier;
55
using System.Linq;
6+
using TrafficManager.API.UI;
67

78
public static class Implementations {
89
private static Type constantsType_;
910
private static IManagerFactory managerFactory_;
1011
private static INotifier notifier_;
12+
private static IUIFactory uiFactory_;
1113

1214
public static IManagerFactory ManagerFactory => managerFactory_ ??= GetImplementation<IManagerFactory>();
1315
public static INotifier Notifier => notifier_ ??= GetImplementation<INotifier>();
16+
public static IUIFactory UIFactory => uiFactory_ ??= GetImplementation<IUIFactory>();
1417

1518
private static T GetImplementation<T>()
1619
where T : class {

TLM/TMPE.API/Manager/ITrafficLightManager.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,23 @@ namespace TrafficManager.API.Manager {
22
using TrafficManager.API.Traffic.Enums;
33

44
public interface ITrafficLightManager {
5+
/// <summary>
6+
/// returns if the node as any kind of traffic light (vanilla, manual, timed).
7+
/// </summary>
8+
public bool HasTrafficLight(ushort nodeId);
9+
10+
/// <summary>
11+
/// Manual/timed traffic light cannot be toggled using <see cref="ITrafficLightManager"/>.
12+
/// Use <see cref="ITrafficLightSimulationManager"/> to do that.
13+
/// Also certain node types cannot have traffic light.
14+
/// </summary>
15+
bool CanToggleTrafficLight(ushort nodeId);
16+
17+
/// <summary>
18+
/// if node has no traffic light, vanilla traffic light is set (if possible).
19+
/// if node has vanilla traffic light, it is removed (if possible).
20+
/// this method will fail if node has Manual/timed traffic light.
21+
/// </summary>
22+
bool ToggleTrafficLight(ushort nodeId);
523
}
624
}

TLM/TMPE.API/Manager/ITrafficLightSimulationManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ namespace TrafficManager.API.Manager {
22
using System.Collections.Generic;
33

44
public interface ITrafficLightSimulationManager {
5+
bool HasTimedSimulation(ushort nodeId);
6+
7+
bool HasActiveTimedSimulation(ushort nodeId);
58
}
69
}

TLM/TMPE.API/TMPE.API.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
<Compile Include="Traffic\Enums\ExtVehicleType.cs" />
147147
<Compile Include="Traffic\Enums\FlowWaitCalcMode.cs" />
148148
<Compile Include="Traffic\Enums\GeometryCalculationMode.cs" />
149+
<Compile Include="Traffic\Enums\JunctionRestrictionFlags.cs" />
149150
<Compile Include="Traffic\Enums\LocaleKeyAttribute.cs" />
150151
<Compile Include="Traffic\Enums\LaneArrows.cs" />
151152
<Compile Include="Traffic\Enums\LaneEndTransitionGroup.cs" />
@@ -167,6 +168,8 @@
167168
<Compile Include="Traffic\Enums\VehicleRestrictionsMode.cs" />
168169
<Compile Include="Traffic\ISegmentEnd.cs" />
169170
<Compile Include="Traffic\ISegmentEndId.cs" />
171+
<Compile Include="UI\IUIFactory.cs" />
172+
<Compile Include="UI\ITheme.cs" />
170173
<Compile Include="Util\IObservable.cs" />
171174
<Compile Include="Util\IObserver.cs" />
172175
</ItemGroup>

0 commit comments

Comments
 (0)