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

delete junction restrictions #639

Merged
Show file tree
Hide file tree
Changes from all 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
118 changes: 108 additions & 10 deletions TLM/TLM/Manager/Impl/JunctionRestrictionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace TrafficManager.Manager.Impl {
using TrafficManager.State.ConfigData;
using TrafficManager.State;
using TrafficManager.Traffic;
using static TrafficManager.Util.Shortcuts;
using static CSUtil.Commons.TernaryBoolUtil;
using TrafficManager.Util;

public class JunctionRestrictionsManager
: AbstractGeometryObservingManager,
Expand Down Expand Up @@ -743,6 +746,54 @@ public bool TogglePedestrianCrossingAllowed(ushort segmentId, bool startNode) {
!IsPedestrianCrossingAllowed(segmentId, startNode));
}

public bool SetUturnAllowed(ushort segmentId, bool startNode, bool value) {
return SetUturnAllowed(segmentId, startNode, ToTernaryBool(value));
}

public bool SetNearTurnOnRedAllowed(ushort segmentId, bool startNode, bool value) {
return SetNearTurnOnRedAllowed(segmentId, startNode, ToTernaryBool(value));
}

public bool SetFarTurnOnRedAllowed(ushort segmentId, bool startNode, bool value) {
return SetFarTurnOnRedAllowed(segmentId, startNode, ToTernaryBool(value));
}

public bool SetTurnOnRedAllowed(bool near, ushort segmentId, bool startNode, bool value) {
return SetTurnOnRedAllowed(near, segmentId, startNode, ToTernaryBool(value));
}

public bool SetLaneChangingAllowedWhenGoingStraight(ushort segmentId, bool startNode, bool value) {
return SetLaneChangingAllowedWhenGoingStraight(
segmentId,
startNode,
ToTernaryBool(value));
}

public bool SetEnteringBlockedJunctionAllowed(ushort segmentId, bool startNode, bool value) {
return SetEnteringBlockedJunctionAllowed(
segmentId,
startNode,
ToTernaryBool(value));
}

public bool SetPedestrianCrossingAllowed(ushort segmentId, bool startNode, bool value) {
return SetPedestrianCrossingAllowed(
segmentId,
startNode,
ToTernaryBool(value));
}

public bool ClearSegmentEnd(ushort segmentId, bool startNode) {
bool ret = true;
ret |= SetPedestrianCrossingAllowed(segmentId, startNode, TernaryBool.Undefined);
ret |= SetEnteringBlockedJunctionAllowed(segmentId, startNode, TernaryBool.Undefined);
ret |= SetLaneChangingAllowedWhenGoingStraight(segmentId, startNode, TernaryBool.Undefined);
ret |= SetFarTurnOnRedAllowed(segmentId, startNode, TernaryBool.Undefined);
ret |= SetNearTurnOnRedAllowed(segmentId, startNode, TernaryBool.Undefined);
ret |= SetUturnAllowed(segmentId, startNode, TernaryBool.Undefined);
return ret;
}

private void SetSegmentEndFlags(ushort segmentId, bool startNode, SegmentEndFlags flags) {
if (flags.uturnAllowed != TernaryBool.Undefined) {
SetUturnAllowed(segmentId, startNode, flags.IsUturnAllowed());
Expand All @@ -753,7 +804,7 @@ private void SetSegmentEndFlags(ushort segmentId, bool startNode, SegmentEndFlag
}

if (flags.nearTurnOnRedAllowed != TernaryBool.Undefined) {
SetFarTurnOnRedAllowed(segmentId, startNode, flags.IsFarTurnOnRedAllowed());
SetFarTurnOnRedAllowed(segmentId, startNode, flags.IsNearTurnOnRedAllowed());
}

if (flags.straightLaneChangingAllowed != TernaryBool.Undefined) {
Expand All @@ -778,12 +829,27 @@ private void SetSegmentEndFlags(ushort segmentId, bool startNode, SegmentEndFlag
}
}

public bool SetUturnAllowed(ushort segmentId, bool startNode, bool value) {

private static ref NetNode GetNode(ushort segmentId, bool startNode) {
ref NetSegment segment = ref GetSeg(segmentId);
ushort nodeId = startNode ? segment.m_startNode : segment.m_endNode;
return ref Shortcuts.GetNode(nodeId);
}

#region Set<Traffic Rule>Allowed: TernaryBool

public bool SetUturnAllowed(ushort segmentId, bool startNode, TernaryBool value) {
if (!Services.NetService.IsSegmentValid(segmentId)) {
return false;
}
if(GetUturnAllowed(segmentId,startNode) == value) {
return true;
}
if(!IsUturnAllowedConfigurable(segmentId,startNode, ref GetNode(segmentId, startNode))) {
return false;
}

if (!value && Constants.ManagerFactory.LaneConnectionManager.HasUturnConnections(
if (value == TernaryBool.False && Constants.ManagerFactory.LaneConnectionManager.HasUturnConnections(
segmentId,
startNode)) {
return false;
Expand All @@ -798,18 +864,30 @@ public bool SetUturnAllowed(ushort segmentId, bool startNode, bool value) {
return true;
}

public bool SetNearTurnOnRedAllowed(ushort segmentId, bool startNode, bool value) {
public bool SetNearTurnOnRedAllowed(ushort segmentId, bool startNode, TernaryBool value) {
return SetTurnOnRedAllowed(true, segmentId, startNode, value);
}

public bool SetFarTurnOnRedAllowed(ushort segmentId, bool startNode, bool value) {
public bool SetFarTurnOnRedAllowed(ushort segmentId, bool startNode, TernaryBool value) {
return SetTurnOnRedAllowed(false, segmentId, startNode, value);
}

public bool SetTurnOnRedAllowed(bool near, ushort segmentId, bool startNode, bool value) {
public bool SetTurnOnRedAllowed(bool near, ushort segmentId, bool startNode, TernaryBool value) {
if (!Services.NetService.IsSegmentValid(segmentId)) {
return false;
}
if (GetTurnOnRedAllowed(near, segmentId, startNode) == value) {
return true;
}
if (!IsTurnOnRedAllowedConfigurable(near, segmentId, startNode, ref GetNode(segmentId, startNode))) {
return false;
}

if (value == TernaryBool.False && Constants.ManagerFactory.LaneConnectionManager.HasUturnConnections(
segmentId,
startNode)) {
return false;
}

if (near) {
segmentFlags_[segmentId].SetNearTurnOnRedAllowed(startNode, value);
Expand All @@ -823,10 +901,16 @@ public bool SetTurnOnRedAllowed(bool near, ushort segmentId, bool startNode, boo
public bool SetLaneChangingAllowedWhenGoingStraight(
ushort segmentId,
bool startNode,
bool value) {
TernaryBool value) {
if (!Services.NetService.IsSegmentValid(segmentId)) {
return false;
}
if (GetLaneChangingAllowedWhenGoingStraight(segmentId, startNode) == value) {
return true;
}
if (!IsLaneChangingAllowedWhenGoingStraightConfigurable(segmentId, startNode, ref GetNode(segmentId, startNode))) {
return false;
}

segmentFlags_[segmentId].SetLaneChangingAllowedWhenGoingStraight(startNode, value);
OnSegmentChange(
Expand All @@ -837,11 +921,16 @@ public bool SetLaneChangingAllowedWhenGoingStraight(
return true;
}

public bool
SetEnteringBlockedJunctionAllowed(ushort segmentId, bool startNode, bool value) {
public bool SetEnteringBlockedJunctionAllowed(ushort segmentId, bool startNode, TernaryBool value) {
if (!Services.NetService.IsSegmentValid(segmentId)) {
return false;
}
if (GetEnteringBlockedJunctionAllowed(segmentId, startNode) == value) {
return true;
}
if (!IsEnteringBlockedJunctionAllowedConfigurable(segmentId, startNode, ref GetNode(segmentId, startNode))) {
return false;
}

segmentFlags_[segmentId].SetEnteringBlockedJunctionAllowed(startNode, value);

Expand All @@ -854,10 +943,17 @@ public bool
return true;
}

public bool SetPedestrianCrossingAllowed(ushort segmentId, bool startNode, bool value) {
public bool SetPedestrianCrossingAllowed(ushort segmentId, bool startNode, TernaryBool value) {
if (!Services.NetService.IsSegmentValid(segmentId)) {
return false;
}
if(GetPedestrianCrossingAllowed(segmentId, startNode) == value) {
return true;
}
if(!IsPedestrianCrossingAllowedConfigurable(segmentId, startNode, ref GetNode(segmentId, startNode))) {
return false;
}


segmentFlags_[segmentId].SetPedestrianCrossingAllowed(startNode, value);
OnSegmentChange(
Expand All @@ -868,6 +964,8 @@ public bool SetPedestrianCrossingAllowed(ushort segmentId, bool startNode, bool
return true;
}

#endregion

private void OnSegmentChange(ushort segmentId,
bool startNode,
ref ExtSegment seg,
Expand Down
19 changes: 19 additions & 0 deletions TLM/TLM/State/Keybinds/KeybindSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,30 @@ public string ToLocalizedString(string prefix = "") {
return result + Keybind.ToLocalizedString(AlternateKey);
}

/// <param name="e"></param>
/// <returns>true for as long as user holds the key</returns>
public bool IsPressed(Event e) {
return Key.IsPressed(e)
|| (AlternateKey != null && AlternateKey.IsPressed(e));
}

private bool prev_value = false;

/// <summary>
/// Determines when user first presses the key. the event is consumed first time
/// this function is called.
/// </summary>
/// <param name="e"></param>
/// <returns>true once when user presses the key.</returns>
public bool KeyDown(Event e) {
bool value = Key.IsPressed(e);
bool ret = value && !prev_value;
if (ret || !value) {
prev_value = value;
}
return ret;
}

/// <summary>
/// Check whether main or alt key are the same as k
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions TLM/TLM/UI/SubTools/JunctionRestrictionsTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace TrafficManager.UI.SubTools {
using TrafficManager.State;
using TrafficManager.UI.Textures;
using UnityEngine;
using TrafficManager.State.Keybinds;
using static TrafficManager.Util.Shortcuts;

public class JunctionRestrictionsTool : SubTool {
private readonly HashSet<ushort> currentRestrictedNodeIds;
Expand All @@ -24,6 +26,18 @@ public override void OnToolGUI(Event e) {
// overlayHandleHovered = false;
// }
// ShowSigns(false);

// handle delete
if (KeybindSettingsBase.LaneConnectorDelete.KeyDown(e)) {
netService.IterateNodeSegments(
SelectedNodeId,
(ushort segmmentId, ref NetSegment segment) => {
// TODO: #568 provide unified delete key for all managers.
bool startNode = (bool)netService.IsStartNode(segmmentId, SelectedNodeId);
JunctionRestrictionsManager.Instance.ClearSegmentEnd(segmmentId, startNode);
return true;
});
}
}

public override void RenderInfoOverlay(RenderManager.CameraInfo cameraInfo) { }
Expand Down
Loading