committed by
GitHub
parent
ea60a81fdf
commit
103bc19508
99
Content.Client/NodeContainer/NodeGroupSystem.cs
Normal file
99
Content.Client/NodeContainer/NodeGroupSystem.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Shared.NodeContainer;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.ResourceManagement;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
|
||||
namespace Content.Client.NodeContainer
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public sealed class NodeGroupSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IOverlayManager _overlayManager = default!;
|
||||
[Dependency] private readonly IEntityLookup _entityLookup = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly IInputManager _inputManager = default!;
|
||||
[Dependency] private readonly IEyeManager _eyeManager = default!;
|
||||
[Dependency] private readonly IResourceCache _resourceCache = default!;
|
||||
|
||||
public bool VisEnabled { get; private set; }
|
||||
|
||||
public Dictionary<int, NodeVis.GroupData> Groups { get; } = new();
|
||||
public HashSet<string> Filtered { get; } = new();
|
||||
|
||||
public Dictionary<EntityUid, (NodeVis.GroupData group, NodeVis.NodeDatum node)[]>
|
||||
Entities { get; private set; } = new();
|
||||
|
||||
public Dictionary<(int group, int node), NodeVis.NodeDatum> NodeLookup { get; private set; } = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeNetworkEvent<NodeVis.MsgData>(DataMsgHandler);
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
|
||||
_overlayManager.RemoveOverlay<NodeVisualizationOverlay>();
|
||||
}
|
||||
|
||||
private void DataMsgHandler(NodeVis.MsgData ev)
|
||||
{
|
||||
if (!VisEnabled)
|
||||
return;
|
||||
|
||||
foreach (var deletion in ev.GroupDeletions)
|
||||
{
|
||||
Groups.Remove(deletion);
|
||||
}
|
||||
|
||||
foreach (var group in ev.Groups)
|
||||
{
|
||||
Groups.Add(group.NetId, group);
|
||||
}
|
||||
|
||||
Entities = Groups.Values
|
||||
.SelectMany(g => g.Nodes, (data, nodeData) => (data, nodeData))
|
||||
.GroupBy(n => n.nodeData.Entity)
|
||||
.ToDictionary(g => g.Key, g => g.ToArray());
|
||||
|
||||
NodeLookup = Groups.Values
|
||||
.SelectMany(g => g.Nodes, (data, nodeData) => (data, nodeData))
|
||||
.ToDictionary(n => (n.data.NetId, n.nodeData.NetId), n => n.nodeData);
|
||||
}
|
||||
|
||||
public void SetVisEnabled(bool enabled)
|
||||
{
|
||||
VisEnabled = enabled;
|
||||
|
||||
RaiseNetworkEvent(new NodeVis.MsgEnable(enabled));
|
||||
|
||||
if (enabled)
|
||||
{
|
||||
var overlay = new NodeVisualizationOverlay(
|
||||
this,
|
||||
_entityLookup,
|
||||
_mapManager,
|
||||
_inputManager,
|
||||
_eyeManager,
|
||||
_resourceCache,
|
||||
EntityManager);
|
||||
|
||||
_overlayManager.AddOverlay(overlay);
|
||||
}
|
||||
else
|
||||
{
|
||||
Groups.Clear();
|
||||
Entities.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
56
Content.Client/NodeContainer/NodeVisCommand.cs
Normal file
56
Content.Client/NodeContainer/NodeVisCommand.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Content.Client.Administration.Managers;
|
||||
using Content.Shared.Administration;
|
||||
using Robust.Shared.Console;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
|
||||
namespace Content.Client.NodeContainer
|
||||
{
|
||||
public sealed class NodeVisCommand : IConsoleCommand
|
||||
{
|
||||
public string Command => "nodevis";
|
||||
public string Description => "Toggles node group visualization";
|
||||
public string Help => "";
|
||||
|
||||
public void Execute(IConsoleShell shell, string argStr, string[] args)
|
||||
{
|
||||
var adminMan = IoCManager.Resolve<IClientAdminManager>();
|
||||
if (!adminMan.HasFlag(AdminFlags.Debug))
|
||||
{
|
||||
shell.WriteError("You need +DEBUG for this command");
|
||||
return;
|
||||
}
|
||||
|
||||
var sys = EntitySystem.Get<NodeGroupSystem>();
|
||||
sys.SetVisEnabled(!sys.VisEnabled);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NodeVisFilterCommand : IConsoleCommand
|
||||
{
|
||||
public string Command => "nodevisfilter";
|
||||
public string Description => "Toggles showing a specific group on nodevis";
|
||||
public string Help => "Usage: nodevis [filter]\nOmit filter to list currently masked-off";
|
||||
|
||||
public void Execute(IConsoleShell shell, string argStr, string[] args)
|
||||
{
|
||||
var sys = EntitySystem.Get<NodeGroupSystem>();
|
||||
|
||||
if (args.Length == 0)
|
||||
{
|
||||
foreach (var filtered in sys.Filtered)
|
||||
{
|
||||
shell.WriteLine(filtered);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var filter = args[0];
|
||||
if (!sys.Filtered.Add(filter))
|
||||
{
|
||||
sys.Filtered.Remove(filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
232
Content.Client/NodeContainer/NodeVisualizationOverlay.cs
Normal file
232
Content.Client/NodeContainer/NodeVisualizationOverlay.cs
Normal file
@@ -0,0 +1,232 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Content.Client.Resources;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.ResourceManagement;
|
||||
using Robust.Client.UserInterface.CustomControls;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.Utility;
|
||||
using static Content.Shared.NodeContainer.NodeVis;
|
||||
|
||||
namespace Content.Client.NodeContainer
|
||||
{
|
||||
public sealed class NodeVisualizationOverlay : Overlay
|
||||
{
|
||||
private readonly NodeGroupSystem _system;
|
||||
private readonly IEntityLookup _lookup;
|
||||
private readonly IMapManager _mapManager;
|
||||
private readonly IInputManager _inputManager;
|
||||
private readonly IEyeManager _eyeManager;
|
||||
private readonly IEntityManager _entityManager;
|
||||
|
||||
private readonly Dictionary<(int, int), NodeRenderData> _nodeIndex = new();
|
||||
private readonly Dictionary<GridId, Dictionary<Vector2i, List<(GroupData, NodeDatum)>>> _gridIndex = new ();
|
||||
|
||||
private readonly Font _font;
|
||||
|
||||
private (int group, int node)? _hovered;
|
||||
private float _time;
|
||||
|
||||
public override OverlaySpace Space => OverlaySpace.ScreenSpace | OverlaySpace.WorldSpace;
|
||||
|
||||
public NodeVisualizationOverlay(
|
||||
NodeGroupSystem system,
|
||||
IEntityLookup lookup,
|
||||
IMapManager mapManager,
|
||||
IInputManager inputManager,
|
||||
IEyeManager eyeManager,
|
||||
IResourceCache cache,
|
||||
IEntityManager entityManager)
|
||||
{
|
||||
_system = system;
|
||||
_lookup = lookup;
|
||||
_mapManager = mapManager;
|
||||
_inputManager = inputManager;
|
||||
_eyeManager = eyeManager;
|
||||
_entityManager = entityManager;
|
||||
|
||||
_font = cache.GetFont("/Fonts/NotoSans/NotoSans-Regular.ttf", 12);
|
||||
}
|
||||
|
||||
protected override void Draw(in OverlayDrawArgs args)
|
||||
{
|
||||
if ((args.Space & OverlaySpace.WorldSpace) != 0)
|
||||
{
|
||||
DrawWorld(args);
|
||||
}
|
||||
else if ((args.Space & OverlaySpace.ScreenSpace) != 0)
|
||||
{
|
||||
DrawScreen(args);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawScreen(in OverlayDrawArgs args)
|
||||
{
|
||||
if (_hovered == null)
|
||||
return;
|
||||
|
||||
var (groupId, nodeId) = _hovered.Value;
|
||||
|
||||
var group = _system.Groups[groupId];
|
||||
var node = _system.NodeLookup[(groupId, nodeId)];
|
||||
|
||||
var mousePos = _inputManager.MouseScreenPosition.Position;
|
||||
|
||||
var entity = _entityManager.GetEntity(node.Entity);
|
||||
|
||||
var gridId = entity.Transform.GridID;
|
||||
var grid = _mapManager.GetGrid(gridId);
|
||||
var gridTile = grid.TileIndicesFor(entity.Transform.Coordinates);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.Append($"entity: {entity}\n");
|
||||
sb.Append($"group id: {group.GroupId}\n");
|
||||
sb.Append($"node: {node.Name}\n");
|
||||
sb.Append($"type: {node.Type}\n");
|
||||
sb.Append($"grid pos: {gridTile}\n");
|
||||
|
||||
args.ScreenHandle.DrawString(_font, mousePos + (20, -20), sb.ToString());
|
||||
}
|
||||
|
||||
private void DrawWorld(in OverlayDrawArgs overlayDrawArgs)
|
||||
{
|
||||
const float nodeSize = 8f / 32;
|
||||
const float nodeOffset = 6f / 32;
|
||||
|
||||
var handle = overlayDrawArgs.WorldHandle;
|
||||
|
||||
var map = overlayDrawArgs.Viewport.Eye?.Position.MapId ?? default;
|
||||
if (map == MapId.Nullspace)
|
||||
return;
|
||||
|
||||
var mouseScreenPos = _inputManager.MouseScreenPosition;
|
||||
var mouseWorldPos = _eyeManager.ScreenToMap(mouseScreenPos).Position;
|
||||
|
||||
_hovered = default;
|
||||
|
||||
var cursorBox = Box2.CenteredAround(mouseWorldPos, (nodeSize, nodeSize));
|
||||
|
||||
// Group visible nodes by grid tiles.
|
||||
var worldBounds = overlayDrawArgs.WorldBounds;
|
||||
_lookup.FastEntitiesIntersecting(map, ref worldBounds, entity =>
|
||||
{
|
||||
if (!_system.Entities.TryGetValue(entity.Uid, out var nodeData))
|
||||
return;
|
||||
|
||||
var gridId = entity.Transform.GridID;
|
||||
var grid = _mapManager.GetGrid(gridId);
|
||||
var gridDict = _gridIndex.GetOrNew(gridId);
|
||||
var coords = entity.Transform.Coordinates;
|
||||
|
||||
// TODO: This probably shouldn't be capable of returning NaN...
|
||||
if (float.IsNaN(coords.Position.X) || float.IsNaN(coords.Position.Y))
|
||||
return;
|
||||
|
||||
var tile = gridDict.GetOrNew(grid.TileIndicesFor(coords));
|
||||
|
||||
foreach (var (group, nodeDatum) in nodeData)
|
||||
{
|
||||
if (!_system.Filtered.Contains(group.GroupId))
|
||||
{
|
||||
tile.Add((group, nodeDatum));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
foreach (var (gridId, gridDict) in _gridIndex)
|
||||
{
|
||||
var grid = _mapManager.GetGrid(gridId);
|
||||
foreach (var (pos, list) in gridDict)
|
||||
{
|
||||
var centerPos = grid.GridTileToWorld(pos).Position;
|
||||
list.Sort(NodeDisplayComparer.Instance);
|
||||
|
||||
var offset = -(list.Count - 1) * nodeOffset / 2;
|
||||
|
||||
foreach (var (group, node) in list)
|
||||
{
|
||||
var nodePos = centerPos + (offset, offset);
|
||||
if (cursorBox.Contains(nodePos))
|
||||
_hovered = (group.NetId, node.NetId);
|
||||
|
||||
_nodeIndex[(group.NetId, node.NetId)] = new NodeRenderData(group, node, nodePos);
|
||||
offset += nodeOffset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var nodeRenderData in _nodeIndex.Values)
|
||||
{
|
||||
var pos = nodeRenderData.WorldPos;
|
||||
var bounds = Box2.CenteredAround(pos, (nodeSize, nodeSize));
|
||||
|
||||
var groupData = nodeRenderData.GroupData;
|
||||
var color = groupData.Color;
|
||||
|
||||
if (!_hovered.HasValue)
|
||||
color.A = 0.5f;
|
||||
else if (_hovered.Value.group != groupData.NetId)
|
||||
color.A = 0.2f;
|
||||
else
|
||||
color.A = 0.75f + MathF.Sin(_time * 4) * 0.25f;
|
||||
|
||||
handle.DrawRect(bounds, color);
|
||||
|
||||
foreach (var reachable in nodeRenderData.NodeDatum.Reachable)
|
||||
{
|
||||
if (_nodeIndex.TryGetValue((groupData.NetId, reachable), out var reachDat))
|
||||
{
|
||||
handle.DrawLine(pos, reachDat.WorldPos, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_nodeIndex.Clear();
|
||||
_gridIndex.Clear();
|
||||
}
|
||||
|
||||
protected override void FrameUpdate(FrameEventArgs args)
|
||||
{
|
||||
base.FrameUpdate(args);
|
||||
|
||||
_time += args.DeltaSeconds;
|
||||
}
|
||||
|
||||
private sealed class NodeDisplayComparer : IComparer<(GroupData, NodeDatum)>
|
||||
{
|
||||
public static readonly NodeDisplayComparer Instance = new();
|
||||
|
||||
public int Compare((GroupData, NodeDatum) x, (GroupData, NodeDatum) y)
|
||||
{
|
||||
var (groupX, nodeX) = x;
|
||||
var (groupY, nodeY) = y;
|
||||
|
||||
var cmp = groupX.NetId.CompareTo(groupY.NetId);
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
|
||||
return nodeX.NetId.CompareTo(nodeY.NetId);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class NodeRenderData
|
||||
{
|
||||
public GroupData GroupData;
|
||||
public NodeDatum NodeDatum;
|
||||
public Vector2 WorldPos;
|
||||
|
||||
public NodeRenderData(GroupData groupData, NodeDatum nodeDatum, Vector2 worldPos)
|
||||
{
|
||||
GroupData = groupData;
|
||||
NodeDatum = nodeDatum;
|
||||
WorldPos = worldPos;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user