Skip to content

Commit c286caa

Browse files
authored
Merge pull request #639 from kianzarrin/623-delete-junction-restrictions
#623 I provided Interface for resting junction values. to do so provide ternarybool.undefined to a set<traffic rule>() function provided documentation explaining what I explained in description of #623. User Interface: choose Junction restriction tool -> select a junction -> press delete added KeyDown keybind settings mimicking Input.KeyDown()
2 parents ca6dee7 + 8fe40b9 commit c286caa

File tree

6 files changed

+358
-64
lines changed

6 files changed

+358
-64
lines changed

TLM/TLM/Manager/Impl/JunctionRestrictionsManager.cs

+108-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace TrafficManager.Manager.Impl {
1212
using TrafficManager.State.ConfigData;
1313
using TrafficManager.State;
1414
using TrafficManager.Traffic;
15+
using static TrafficManager.Util.Shortcuts;
16+
using static CSUtil.Commons.TernaryBoolUtil;
17+
using TrafficManager.Util;
1518

1619
public class JunctionRestrictionsManager
1720
: AbstractGeometryObservingManager,
@@ -743,6 +746,54 @@ public bool TogglePedestrianCrossingAllowed(ushort segmentId, bool startNode) {
743746
!IsPedestrianCrossingAllowed(segmentId, startNode));
744747
}
745748

749+
public bool SetUturnAllowed(ushort segmentId, bool startNode, bool value) {
750+
return SetUturnAllowed(segmentId, startNode, ToTernaryBool(value));
751+
}
752+
753+
public bool SetNearTurnOnRedAllowed(ushort segmentId, bool startNode, bool value) {
754+
return SetNearTurnOnRedAllowed(segmentId, startNode, ToTernaryBool(value));
755+
}
756+
757+
public bool SetFarTurnOnRedAllowed(ushort segmentId, bool startNode, bool value) {
758+
return SetFarTurnOnRedAllowed(segmentId, startNode, ToTernaryBool(value));
759+
}
760+
761+
public bool SetTurnOnRedAllowed(bool near, ushort segmentId, bool startNode, bool value) {
762+
return SetTurnOnRedAllowed(near, segmentId, startNode, ToTernaryBool(value));
763+
}
764+
765+
public bool SetLaneChangingAllowedWhenGoingStraight(ushort segmentId, bool startNode, bool value) {
766+
return SetLaneChangingAllowedWhenGoingStraight(
767+
segmentId,
768+
startNode,
769+
ToTernaryBool(value));
770+
}
771+
772+
public bool SetEnteringBlockedJunctionAllowed(ushort segmentId, bool startNode, bool value) {
773+
return SetEnteringBlockedJunctionAllowed(
774+
segmentId,
775+
startNode,
776+
ToTernaryBool(value));
777+
}
778+
779+
public bool SetPedestrianCrossingAllowed(ushort segmentId, bool startNode, bool value) {
780+
return SetPedestrianCrossingAllowed(
781+
segmentId,
782+
startNode,
783+
ToTernaryBool(value));
784+
}
785+
786+
public bool ClearSegmentEnd(ushort segmentId, bool startNode) {
787+
bool ret = true;
788+
ret |= SetPedestrianCrossingAllowed(segmentId, startNode, TernaryBool.Undefined);
789+
ret |= SetEnteringBlockedJunctionAllowed(segmentId, startNode, TernaryBool.Undefined);
790+
ret |= SetLaneChangingAllowedWhenGoingStraight(segmentId, startNode, TernaryBool.Undefined);
791+
ret |= SetFarTurnOnRedAllowed(segmentId, startNode, TernaryBool.Undefined);
792+
ret |= SetNearTurnOnRedAllowed(segmentId, startNode, TernaryBool.Undefined);
793+
ret |= SetUturnAllowed(segmentId, startNode, TernaryBool.Undefined);
794+
return ret;
795+
}
796+
746797
private void SetSegmentEndFlags(ushort segmentId, bool startNode, SegmentEndFlags flags) {
747798
if (flags.uturnAllowed != TernaryBool.Undefined) {
748799
SetUturnAllowed(segmentId, startNode, flags.IsUturnAllowed());
@@ -753,7 +804,7 @@ private void SetSegmentEndFlags(ushort segmentId, bool startNode, SegmentEndFlag
753804
}
754805

755806
if (flags.nearTurnOnRedAllowed != TernaryBool.Undefined) {
756-
SetFarTurnOnRedAllowed(segmentId, startNode, flags.IsFarTurnOnRedAllowed());
807+
SetFarTurnOnRedAllowed(segmentId, startNode, flags.IsNearTurnOnRedAllowed());
757808
}
758809

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

781-
public bool SetUturnAllowed(ushort segmentId, bool startNode, bool value) {
832+
833+
private static ref NetNode GetNode(ushort segmentId, bool startNode) {
834+
ref NetSegment segment = ref GetSeg(segmentId);
835+
ushort nodeId = startNode ? segment.m_startNode : segment.m_endNode;
836+
return ref Shortcuts.GetNode(nodeId);
837+
}
838+
839+
#region Set<Traffic Rule>Allowed: TernaryBool
840+
841+
public bool SetUturnAllowed(ushort segmentId, bool startNode, TernaryBool value) {
782842
if (!Services.NetService.IsSegmentValid(segmentId)) {
783843
return false;
784844
}
845+
if(GetUturnAllowed(segmentId,startNode) == value) {
846+
return true;
847+
}
848+
if(!IsUturnAllowedConfigurable(segmentId,startNode, ref GetNode(segmentId, startNode))) {
849+
return false;
850+
}
785851

786-
if (!value && Constants.ManagerFactory.LaneConnectionManager.HasUturnConnections(
852+
if (value == TernaryBool.False && Constants.ManagerFactory.LaneConnectionManager.HasUturnConnections(
787853
segmentId,
788854
startNode)) {
789855
return false;
@@ -798,18 +864,30 @@ public bool SetUturnAllowed(ushort segmentId, bool startNode, bool value) {
798864
return true;
799865
}
800866

801-
public bool SetNearTurnOnRedAllowed(ushort segmentId, bool startNode, bool value) {
867+
public bool SetNearTurnOnRedAllowed(ushort segmentId, bool startNode, TernaryBool value) {
802868
return SetTurnOnRedAllowed(true, segmentId, startNode, value);
803869
}
804870

805-
public bool SetFarTurnOnRedAllowed(ushort segmentId, bool startNode, bool value) {
871+
public bool SetFarTurnOnRedAllowed(ushort segmentId, bool startNode, TernaryBool value) {
806872
return SetTurnOnRedAllowed(false, segmentId, startNode, value);
807873
}
808874

809-
public bool SetTurnOnRedAllowed(bool near, ushort segmentId, bool startNode, bool value) {
875+
public bool SetTurnOnRedAllowed(bool near, ushort segmentId, bool startNode, TernaryBool value) {
810876
if (!Services.NetService.IsSegmentValid(segmentId)) {
811877
return false;
812878
}
879+
if (GetTurnOnRedAllowed(near, segmentId, startNode) == value) {
880+
return true;
881+
}
882+
if (!IsTurnOnRedAllowedConfigurable(near, segmentId, startNode, ref GetNode(segmentId, startNode))) {
883+
return false;
884+
}
885+
886+
if (value == TernaryBool.False && Constants.ManagerFactory.LaneConnectionManager.HasUturnConnections(
887+
segmentId,
888+
startNode)) {
889+
return false;
890+
}
813891

814892
if (near) {
815893
segmentFlags_[segmentId].SetNearTurnOnRedAllowed(startNode, value);
@@ -823,10 +901,16 @@ public bool SetTurnOnRedAllowed(bool near, ushort segmentId, bool startNode, boo
823901
public bool SetLaneChangingAllowedWhenGoingStraight(
824902
ushort segmentId,
825903
bool startNode,
826-
bool value) {
904+
TernaryBool value) {
827905
if (!Services.NetService.IsSegmentValid(segmentId)) {
828906
return false;
829907
}
908+
if (GetLaneChangingAllowedWhenGoingStraight(segmentId, startNode) == value) {
909+
return true;
910+
}
911+
if (!IsLaneChangingAllowedWhenGoingStraightConfigurable(segmentId, startNode, ref GetNode(segmentId, startNode))) {
912+
return false;
913+
}
830914

831915
segmentFlags_[segmentId].SetLaneChangingAllowedWhenGoingStraight(startNode, value);
832916
OnSegmentChange(
@@ -837,11 +921,16 @@ public bool SetLaneChangingAllowedWhenGoingStraight(
837921
return true;
838922
}
839923

840-
public bool
841-
SetEnteringBlockedJunctionAllowed(ushort segmentId, bool startNode, bool value) {
924+
public bool SetEnteringBlockedJunctionAllowed(ushort segmentId, bool startNode, TernaryBool value) {
842925
if (!Services.NetService.IsSegmentValid(segmentId)) {
843926
return false;
844927
}
928+
if (GetEnteringBlockedJunctionAllowed(segmentId, startNode) == value) {
929+
return true;
930+
}
931+
if (!IsEnteringBlockedJunctionAllowedConfigurable(segmentId, startNode, ref GetNode(segmentId, startNode))) {
932+
return false;
933+
}
845934

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

@@ -854,10 +943,17 @@ public bool
854943
return true;
855944
}
856945

857-
public bool SetPedestrianCrossingAllowed(ushort segmentId, bool startNode, bool value) {
946+
public bool SetPedestrianCrossingAllowed(ushort segmentId, bool startNode, TernaryBool value) {
858947
if (!Services.NetService.IsSegmentValid(segmentId)) {
859948
return false;
860949
}
950+
if(GetPedestrianCrossingAllowed(segmentId, startNode) == value) {
951+
return true;
952+
}
953+
if(!IsPedestrianCrossingAllowedConfigurable(segmentId, startNode, ref GetNode(segmentId, startNode))) {
954+
return false;
955+
}
956+
861957

862958
segmentFlags_[segmentId].SetPedestrianCrossingAllowed(startNode, value);
863959
OnSegmentChange(
@@ -868,6 +964,8 @@ public bool SetPedestrianCrossingAllowed(ushort segmentId, bool startNode, bool
868964
return true;
869965
}
870966

967+
#endregion
968+
871969
private void OnSegmentChange(ushort segmentId,
872970
bool startNode,
873971
ref ExtSegment seg,

TLM/TLM/State/Keybinds/KeybindSetting.cs

+19
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,30 @@ public string ToLocalizedString(string prefix = "") {
9595
return result + Keybind.ToLocalizedString(AlternateKey);
9696
}
9797

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

105+
private bool prev_value = false;
106+
107+
/// <summary>
108+
/// Determines when user first presses the key. the event is consumed first time
109+
/// this function is called.
110+
/// </summary>
111+
/// <param name="e"></param>
112+
/// <returns>true once when user presses the key.</returns>
113+
public bool KeyDown(Event e) {
114+
bool value = Key.IsPressed(e);
115+
bool ret = value && !prev_value;
116+
if (ret || !value) {
117+
prev_value = value;
118+
}
119+
return ret;
120+
}
121+
103122
/// <summary>
104123
/// Check whether main or alt key are the same as k
105124
/// </summary>

TLM/TLM/UI/SubTools/JunctionRestrictionsTool.cs

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace TrafficManager.UI.SubTools {
88
using TrafficManager.State;
99
using TrafficManager.UI.Textures;
1010
using UnityEngine;
11+
using TrafficManager.State.Keybinds;
12+
using static TrafficManager.Util.Shortcuts;
1113

1214
public class JunctionRestrictionsTool : SubTool {
1315
private readonly HashSet<ushort> currentRestrictedNodeIds;
@@ -24,6 +26,18 @@ public override void OnToolGUI(Event e) {
2426
// overlayHandleHovered = false;
2527
// }
2628
// ShowSigns(false);
29+
30+
// handle delete
31+
if (KeybindSettingsBase.LaneConnectorDelete.KeyDown(e)) {
32+
netService.IterateNodeSegments(
33+
SelectedNodeId,
34+
(ushort segmmentId, ref NetSegment segment) => {
35+
// TODO: #568 provide unified delete key for all managers.
36+
bool startNode = (bool)netService.IsStartNode(segmmentId, SelectedNodeId);
37+
JunctionRestrictionsManager.Instance.ClearSegmentEnd(segmmentId, startNode);
38+
return true;
39+
});
40+
}
2741
}
2842

2943
public override void RenderInfoOverlay(RenderManager.CameraInfo cameraInfo) { }

0 commit comments

Comments
 (0)