Skip to content

Commit 2a4c104

Browse files
authored
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.
1 parent d0f98b9 commit 2a4c104

15 files changed

Lines changed: 137 additions & 13 deletions

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: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace TrafficManager.Manager.Impl {
1717
using TrafficManager.Util;
1818
using TrafficManager.Util.Extensions;
1919
using TrafficManager.Lifecycle;
20+
using TrafficManager.API.Traffic.Enums;
2021

2122
public class JunctionRestrictionsManager
2223
: AbstractGeometryObservingManager,
@@ -1318,15 +1319,6 @@ public bool LoadData(List<Configuration.SegmentNodeConf> data) {
13181319
return ret;
13191320
}
13201321

1321-
private enum JunctionRestrictionFlags {
1322-
AllowUTurn = 1 << 0,
1323-
AllowNearTurnOnRed = 1 << 1,
1324-
AllowFarTurnOnRed = 1 << 2,
1325-
AllowForwardLaneChange = 1 << 3,
1326-
AllowEnterWhenBlocked = 1 << 4,
1327-
AllowPedestrianCrossing = 1 << 5,
1328-
}
1329-
13301322
private struct SegmentJunctionRestrictions {
13311323
public JunctionRestrictions startNodeRestrictions;
13321324
public JunctionRestrictions endNodeRestrictions;

TLM/TLM/Manager/Impl/LaneConnection/LaneConnectionManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class LaneConnectionManager
2424
| VehicleInfo.VehicleType.Trolleybus;
2525

2626
public LaneConnectionSubManager Sub = // TODO #354 divide into Road/Track
27-
new LaneConnectionSubManager(LaneEndTransitionGroup.All);
27+
new LaneConnectionSubManager(LaneEndTransitionGroup.Vehicle);
2828

2929
public NetInfo.LaneType LaneTypes => LANE_TYPES;
3030

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
@@ -186,6 +186,7 @@
186186
<Compile Include="UI\SubTools\RoutingDetector\Connection.cs" />
187187
<Compile Include="UI\SubTools\RoutingDetector\LaneEnd.cs" />
188188
<Compile Include="UI\SubTools\RoutingDetector\RoutingDetectorTool.cs" />
189+
<Compile Include="UI\UIFactory.cs" />
189190
<Compile Include="UI\WhatsNew\MarkupKeyword.cs" />
190191
<Compile Include="UI\WhatsNew\WhatsNewMarkup.cs" />
191192
<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
}

0 commit comments

Comments
 (0)