Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api update #1577

Merged
merged 15 commits into from
May 29, 2022
5 changes: 5 additions & 0 deletions TLM/TLM/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace TrafficManager {
using TrafficManager.API.Manager;
using TrafficManager.API.Notifier;
using TrafficManager.API.UI;
using TrafficManager.U;
using TrafficManager.UI.Textures;

public static class Constants {
/// <summary>
Expand Down Expand Up @@ -35,6 +37,9 @@ public static float ByteToFloat(byte b) {

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

public static IUIFactory UIFactory => UI.UIFactory.Instance;

public static INotifier Notifier => TrafficManager.Notifier.Instance;

}
}
120 changes: 56 additions & 64 deletions TLM/TLM/Manager/Impl/JunctionRestrictionsManager.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class LaneConnectionManager
| VehicleInfo.VehicleType.Trolleybus;

public LaneConnectionSubManager Sub = // TODO #354 divide into Road/Track
new LaneConnectionSubManager(LaneEndTransitionGroup.All);
new LaneConnectionSubManager(LaneEndTransitionGroup.Vehicle);
kianzarrin marked this conversation as resolved.
Show resolved Hide resolved

public NetInfo.LaneType LaneTypes => LANE_TYPES;

Expand Down
26 changes: 26 additions & 0 deletions TLM/TLM/Manager/Impl/TrafficLightManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public void RemoveAllExistingTrafficLights() {
}
}

public bool ToggleTrafficLight(ushort nodeId) => ToggleTrafficLight(nodeId, ref nodeId.ToNode());

public bool ToggleTrafficLight(ushort nodeId, ref NetNode node) {
return SetTrafficLight(nodeId, !HasTrafficLight(nodeId, ref node), ref node);
}
Expand All @@ -127,6 +129,16 @@ public bool ToggleTrafficLight(ushort nodeId, ref NetNode node, out ToggleTraffi
return SetTrafficLight(nodeId, !HasTrafficLight(nodeId, ref node), ref node, out reason);
}

public bool CanToggleTL(ushort nodeId) {
kianzarrin marked this conversation as resolved.
Show resolved Hide resolved
ref NetNode netNode = ref nodeId.ToNode();
return netNode.IsValid() &&
CanToggleTrafficLight(
nodeId,
HasTrafficLight(nodeId, ref netNode),
ref netNode,
out _);
}

public bool CanToggleTrafficLight(ushort nodeId,
bool flag, // override?
ref NetNode node,
Expand Down Expand Up @@ -242,6 +254,20 @@ public bool CanEnableTrafficLight(ushort nodeId,
return ret;
}

public TrafficLightType GetTrafficLight(ushort nodeId) {
if (!HasTrafficLight(nodeId, ref nodeId.ToNode())) {
return TrafficLightType.None;
} else if (TrafficLightSimulationManager.Instance.HasManualSimulation(nodeId)) {
return TrafficLightType.Manual;
} else if (TrafficLightSimulationManager.Instance.HasActiveTimedSimulation(nodeId)) {
return TrafficLightType.TimedScript;
} else if (TrafficLightSimulationManager.Instance.HasTimedSimulation(nodeId)) {
return TrafficLightType.Paused;
} else {
return TrafficLightType.Vanilla;
}
}

public bool HasTrafficLight(ushort nodeId, ref NetNode node) {
return node.IsValid()
&& node.m_flags.IsFlagSet(NetNode.Flags.TrafficLights);
Expand Down
1 change: 1 addition & 0 deletions TLM/TLM/TLM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
<Compile Include="UI\SubTools\RoutingDetector\Connection.cs" />
<Compile Include="UI\SubTools\RoutingDetector\LaneEnd.cs" />
<Compile Include="UI\SubTools\RoutingDetector\RoutingDetectorTool.cs" />
<Compile Include="UI\UIFactory.cs" />
<Compile Include="UI\WhatsNew\MarkupKeyword.cs" />
<Compile Include="UI\WhatsNew\WhatsNewMarkup.cs" />
<Compile Include="Util\Extensions\CitizenUnitExtensions.cs" />
Expand Down
25 changes: 23 additions & 2 deletions TLM/TLM/UI/Textures/RoadSignTheme.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
namespace TrafficManager.UI.Textures {
namespace TrafficManager.UI.Textures {
using System;
using System.Collections.Generic;
using CSUtil.Commons;
using JetBrains.Annotations;
using TrafficManager.API.Traffic.Data;
using TrafficManager.API.Traffic.Enums;
using TrafficManager.API.UI;
using TrafficManager.State;
using TrafficManager.UI.SubTools;
using TrafficManager.Util;
Expand All @@ -15,7 +16,7 @@
/// Defines one theme for road signs. All themes are accessible via, and stored in
/// <see cref="RoadSignThemeManager"/>.
/// </summary>
public class RoadSignTheme {
public class RoadSignTheme : ITheme {
public enum OtherRestriction {
Crossing,
EnterBlockedJunction,
Expand Down Expand Up @@ -148,6 +149,26 @@ public Texture2D GetOtherRestriction(OtherRestriction type, bool allow) {
: this.ParentTheme.GetOtherRestriction(type, allow: false);
}

public Texture2D JunctionRestriction(JunctionRestrictionRules rule, bool allowed) {
switch (rule) {
case JunctionRestrictionRules.AllowPedestrianCrossing:
return GetOtherRestriction(OtherRestriction.Crossing, allowed);
case JunctionRestrictionRules.Uturn:
return GetOtherRestriction(OtherRestriction.Crossing, allowed);
case JunctionRestrictionRules.AllowPedestrianCrossing:
return GetOtherRestriction(OtherRestriction.Crossing, allowed);
case JunctionRestrictionRules.AllowPedestrianCrossing:
return GetOtherRestriction(OtherRestriction.Crossing, allowed);
case JunctionRestrictionRules.AllowPedestrianCrossing:
return GetOtherRestriction(OtherRestriction.Crossing, allowed);
}
}

public Texture2D TrafficLights(bool enabled) =>
enabled ? TrafficLightTextures.Instance.TrafficLightEnabled : TrafficLightTextures.Instance.TrafficLightDisabled;
public Texture2D TimedTrafficLights(bool paused) =>
paused ? TrafficLightTextures.Instance.ClockPause : TrafficLightTextures.Instance.TrafficLightEnabledTimed;

public RoadSignTheme Load(bool whiteTexture = false) {
if (this.AttemptedToLoad) {
return this;
Expand Down
10 changes: 10 additions & 0 deletions TLM/TLM/UI/UIFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace TrafficManager.UI {
using TrafficManager.API.UI;
using TrafficManager.UI.Textures;

public class UIFactory : IUIFactory {
public static IUIFactory Instance = new UIFactory();

public ITheme ActiveTheme => RoadSignThemeManager.ActiveTheme;
}
}
3 changes: 3 additions & 0 deletions TLM/TMPE.API/Implementations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ namespace TrafficManager.API {
using TrafficManager.API.Manager;
using TrafficManager.API.Notifier;
using System.Linq;
using TrafficManager.API.UI;

public static class Implementations {
private static Type constantsType_;
private static IManagerFactory managerFactory_;
private static INotifier notifier_;
private static IUIFactory uiFactory_;

public static IManagerFactory ManagerFactory => managerFactory_ ??= GetImplementation<IManagerFactory>();
public static INotifier Notifier => notifier_ ??= GetImplementation<INotifier>();
public static IUIFactory ActiveTheme => uiFactory_ ??= GetImplementation<IUIFactory>();

private static T GetImplementation<T>()
where T : class {
Expand Down
23 changes: 23 additions & 0 deletions TLM/TMPE.API/Manager/ITrafficLightManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,28 @@ namespace TrafficManager.API.Manager {
using TrafficManager.API.Traffic.Enums;

public interface ITrafficLightManager {
TrafficLightType GetTrafficLight(ushort nodeId);

/// <summary>
/// Manual/timed traffic light cannot be toggled using <see cref="ITrafficLightManager"/>.
/// Use <see cref="ITrafficLightSimulationManager"/> to do that.
/// Also certain node types cannot have traffic light.
/// </summary>
bool CanToggleTL(ushort nodeId);

/// <summary>
/// if node has no traffic light, vanilla traffic light is set (if possible).
/// if node has vanilla traffic light, it is removed (if possible).
/// this method will fail if node has Manual/timed traffic light.
/// </summary>
bool ToggleTrafficLight(ushort nodeId);
}

public enum TrafficLightType {
None,
Vanilla,
Manual,
Paused,
TimedScript,
}
}
3 changes: 3 additions & 0 deletions TLM/TMPE.API/Manager/ITrafficLightSimulationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ namespace TrafficManager.API.Manager {
using System.Collections.Generic;

public interface ITrafficLightSimulationManager {
bool HasTimedSimulation(ushort nodeId);
Elesbaan70 marked this conversation as resolved.
Show resolved Hide resolved

bool HasActiveTimedSimulation(ushort nodeId);
}
}
3 changes: 3 additions & 0 deletions TLM/TMPE.API/TMPE.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
<Compile Include="Traffic\Enums\ExtVehicleType.cs" />
<Compile Include="Traffic\Enums\FlowWaitCalcMode.cs" />
<Compile Include="Traffic\Enums\GeometryCalculationMode.cs" />
<Compile Include="Traffic\Enums\JunctionRestrictionRules.cs" />
<Compile Include="Traffic\Enums\LocaleKeyAttribute.cs" />
<Compile Include="Traffic\Enums\LaneArrows.cs" />
<Compile Include="Traffic\Enums\LaneEndTransitionGroup.cs" />
Expand All @@ -165,6 +166,8 @@
<Compile Include="Traffic\Enums\VehicleRestrictionsMode.cs" />
<Compile Include="Traffic\ISegmentEnd.cs" />
<Compile Include="Traffic\ISegmentEndId.cs" />
<Compile Include="UI\IUIFactory.cs" />
<Compile Include="UI\ITheme.cs" />
<Compile Include="Util\IObservable.cs" />
<Compile Include="Util\IObserver.cs" />
</ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions TLM/TMPE.API/Traffic/Enums/JunctionRestrictionRules.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace TrafficManager.API.Traffic.Enums {
public enum JunctionRestrictionRules {
Elesbaan70 marked this conversation as resolved.
Show resolved Hide resolved
Uturn = 1 << 0,
NearTurnOnRed = 1 << 1,
FarTurnOnRed = 1 << 2,
ForwardLaneChange = 1 << 3,
EnterWhenBlocked = 1 << 4,
AllowPedestrianCrossing = 1 << 5,
}
}
4 changes: 3 additions & 1 deletion TLM/TMPE.API/Traffic/Enums/LaneEndTransitionGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public enum LaneEndTransitionGroup {
None = 0,
Road = 1,
Track = 2,
All = Road | Track,
Vehicle = Road | Track,
Elesbaan70 marked this conversation as resolved.
Show resolved Hide resolved
Bicycle = 4,
Pedestrian = 8,
}
}
21 changes: 21 additions & 0 deletions TLM/TMPE.API/UI/ITheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace TrafficManager.API.UI {
using TrafficManager.API.Traffic.Enums;
using UnityEngine;

/// <summary>
/// gets the texture for overlay sprite for each traffic rule according to the current theme.
/// </summary>
public interface ITheme {
Texture2D JunctionRestriction(JunctionRestrictionRules rule, bool allowed);

Texture2D Parking(bool allowed);

Texture2D Priority(PriorityType p);

Texture2D VehicleRestriction(ExtVehicleType type, bool allow);

Texture2D TrafficLights(bool enabled);

Texture2D TimedTrafficLights(bool paused);
}
}
9 changes: 9 additions & 0 deletions TLM/TMPE.API/UI/IUIFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TrafficManager.API.UI {

/// <summary>
/// gets the texture for overlay sprite for each traffic rule according to the current theme.
/// </summary>
public interface IUIFactory {
ITheme ActiveTheme { get; }
}
}