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

boost lane marker hovering #626

Closed
Closed
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
12 changes: 4 additions & 8 deletions TLM/TLM/UI/SubTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,11 @@ private Texture2D BorderlessTexture {

private GUIStyle borderlessStyle_;

protected virtual ushort HoveredNodeId {
get => TrafficManagerTool.HoveredNodeId;
set => TrafficManagerTool.HoveredNodeId = value;
}
protected virtual ushort HoveredNodeId => TrafficManagerTool.HoveredNodeId;

protected virtual ushort HoveredSegmentId {
get => TrafficManagerTool.HoveredSegmentId;
set => TrafficManagerTool.HoveredSegmentId = value;
}
protected virtual ushort HoveredSegmentId => TrafficManagerTool.HoveredSegmentId;

protected virtual uint HoveredLaneId => TrafficManagerTool.HoveredLaneId;

protected ushort SelectedNodeId {
get => TrafficManagerTool.SelectedNodeId;
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/UI/SubTools/LaneArrowTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ protected override ushort HoveredNodeId {
}
return base.HoveredNodeId;
}
set => base.HoveredNodeId = value;
}

/// <summary>
Expand Down
19 changes: 2 additions & 17 deletions TLM/TLM/UI/SubTools/LaneConnectorTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace TrafficManager.UI.SubTools {
using TrafficManager.State;
using TrafficManager.Util.Caching;
using UnityEngine;
using Util.Caching;

public class LaneConnectorTool : SubTool {
private enum MarkerSelectionMode {
Expand Down Expand Up @@ -201,8 +202,7 @@ private void ShowOverlay(bool viewOnly, RenderManager.CameraInfo cameraInfo) {

// bounds.center = laneMarker.position;
// bounds.IntersectRay(mouseRay);
bool markerIsHovered = IsLaneMarkerHovered(laneMarker, ref mouseRay);

bool markerIsHovered = laneMarker.LaneId == HoveredLaneId;
// draw source marker in source selection mode,
// draw target marker (if segment turning angles are within bounds) and
// selected source marker in target selection mode
Expand Down Expand Up @@ -262,21 +262,6 @@ bool drawMarker
} // end for node in all nodes
}

private bool IsLaneMarkerHovered(NodeLaneMarker laneMarker, ref Ray mouseRay) {
Bounds bounds = new Bounds(Vector3.zero, Vector3.one) {
center = laneMarker.Position
};

if (bounds.IntersectRay(mouseRay)) {
return true;
}

bounds = new Bounds(Vector3.zero, Vector3.one) {
center = laneMarker.SecondaryPosition
};
return bounds.IntersectRay(mouseRay);
}

/// <summary>
/// Finds the first index for which node.GetSegment(index) != 0 (its possible node.m_segment0 == 0)
/// </summary>
Expand Down
32 changes: 27 additions & 5 deletions TLM/TLM/UI/TrafficManagerTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace TrafficManager.UI {
using TrafficManager.UI.SubTools;
using TrafficManager.Util;
using UnityEngine;
using static Util.Shortcuts;

[UsedImplicitly]
public class TrafficManagerTool
Expand All @@ -31,6 +32,7 @@ public class TrafficManagerTool

internal static ushort HoveredNodeId;
internal static ushort HoveredSegmentId;
internal static uint HoveredLaneId;

private static bool _mouseClickProcessed;

Expand Down Expand Up @@ -825,6 +827,7 @@ public bool DoRayCast(RaycastInput input, out RaycastOutput output) {
private bool DetermineHoveredElements() {
HoveredSegmentId = 0;
HoveredNodeId = 0;
HoveredLaneId = 0;

bool mouseRayValid = !UIView.IsInsideUI() && Cursor.visible &&
(_activeSubTool == null || !_activeSubTool.IsCursorInPanel());
Expand Down Expand Up @@ -941,20 +944,23 @@ private bool DetermineHoveredElements() {
}

if (HoveredNodeId != 0) {
HoveredSegmentId = GetHoveredSegmentFromNode();
HoveredSegmentId = GetHoveredSegmentFromNode(segmentOutput.m_hitPos);
}
}

if(HoveredSegmentId != 0) {
DetermineHoveredLane(segmentOutput.m_hitPos);
}
}
return HoveredNodeId != 0 || HoveredSegmentId != 0;
}

/// <summary>
/// returns the node segment that is closest to the mouse pointer based on angle.
/// </summary>
internal ushort GetHoveredSegmentFromNode() {
internal ushort GetHoveredSegmentFromNode(Vector3 hitPos) {
ushort minSegId = 0;
NetNode node = NetManager.instance.m_nodes.m_buffer[HoveredNodeId];
Vector3 dir0 = m_mousePosition - node.m_position;
Vector3 dir0 = hitPos - node.m_position;
float min_angle = float.MaxValue;
Constants.ServiceFactory.NetService.IterateNodeSegments(
HoveredNodeId,
Expand Down Expand Up @@ -988,8 +994,24 @@ private static float GetAngle(Vector3 v1, Vector3 v2) {
return ret;
}


/// <summary>
/// Determine the
/// </summary>
/// <param name="hitPos"></param>
private void DetermineHoveredLane(Vector3 hitPos) {
NetSegment segment = GetSeg(HoveredSegmentId);
segment.GetClosestLanePosition(
hitPos,
NetInfo.LaneType.All,
VehicleInfo.VehicleType.All,
out Vector3 pos,
out uint laneID,
out int laneIndex,
out float laneOffset);
HoveredLaneId = laneID;
}

/// <summary>
/// Displays lane ids over lanes
/// </summary>
private void GuiDisplayLanes(ushort segmentId,
Expand Down