Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

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) {
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
38 changes: 36 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,39 @@ public Texture2D GetOtherRestriction(OtherRestriction type, bool allow) {
: this.ParentTheme.GetOtherRestriction(type, allow: false);
}

public Texture2D JunctionRestriction(JunctionRestrictionRules rule, bool allowed) {
bool rht = Shortcuts.RHT;
switch (rule) {
case JunctionRestrictionRules.AllowPedestrianCrossing:
return GetOtherRestriction(OtherRestriction.Crossing, allowed);
case JunctionRestrictionRules.Uturn:
return GetOtherRestriction(OtherRestriction.UTurn, allowed);
case JunctionRestrictionRules.EnterWhenBlocked:
return GetOtherRestriction(OtherRestriction.EnterBlockedJunction, allowed);
case JunctionRestrictionRules.ForwardLaneChange:
return GetOtherRestriction(OtherRestriction.LaneChange, allowed);
case JunctionRestrictionRules.FarTurnOnRed when rht:
case JunctionRestrictionRules.NearTurnOnRed when !rht:
return GetOtherRestriction(OtherRestriction.LeftOnRed, allowed);
case JunctionRestrictionRules.NearTurnOnRed when rht:
case JunctionRestrictionRules.FarTurnOnRed when !rht:
return GetOtherRestriction(OtherRestriction.RightOnRed, allowed);
default:
Log.Error($"could not get texture for {rule}.");
return null;
}
}

public Texture2D TrafficLights(TrafficLightType type) {
return type switch {
TrafficLightType.None => TrafficLightTextures.Instance.TrafficLightDisabled,
TrafficLightType.Vanilla => TrafficLightTextures.Instance.TrafficLightEnabled,
TrafficLightType.Manual => TrafficLightTextures.Instance.TrafficLightEnabled,
TrafficLightType.Paused => TrafficLightTextures.Instance.TrafficLightEnabledTimed,
TrafficLightType.TimedScript => 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
15 changes: 15 additions & 0 deletions TLM/TMPE.API/Manager/ITrafficLightManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,20 @@ 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);
}
}
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);

bool HasActiveTimedSimulation(ushort nodeId);
}
}
4 changes: 4 additions & 0 deletions TLM/TMPE.API/TMPE.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<Compile Include="Traffic\Data\SpeedValue.cs" />
<Compile Include="Traffic\Data\TurnOnRedSegments.cs" />
<Compile Include="Traffic\Enums\CarUsagePolicy.cs" />
<Compile Include="Traffic\Enums\TrafficLightType.cs" />
<Compile Include="Traffic\Enums\ExtParkingSpaceLocation.cs" />
<Compile Include="Traffic\Enums\ExtPathMode.cs" />
<Compile Include="Traffic\Enums\ExtPathState.cs" />
Expand All @@ -144,6 +145,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 +167,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 {
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,
Bicycle = 4,
Pedestrian = 8,
}
}
9 changes: 9 additions & 0 deletions TLM/TMPE.API/Traffic/Enums/TrafficLightType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TrafficManager.API.Traffic.Enums {
public enum TrafficLightType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think TrafficLightType is the right name for this. It includes things like "paused."

It's pretty UI-centric. Maybe its name should reflect that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what should I call it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TrafficLightFlags?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paused traffic light acts like normal vanilla traffic light.
I also have the option of dropping paused.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I consider this to be a specifically UI-related enumeration. Basically, it indicates which traffic light tool has control of the light, and indicates additional state information. So why not call it TrafficLightToolState, and then ITrafficLightManager.GetTrafficLightToolState and ITheme.TrafficLightToolState()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And no, I say don't drop paused. Under my interpretation, it belongs there.

None,
Vanilla,
Manual,
Paused,
TimedScript,
}
}
19 changes: 19 additions & 0 deletions TLM/TMPE.API/UI/ITheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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(TrafficLightType type);
}
}
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; }
}
}