Skip to content

Commit

Permalink
Add ability to switch vessel by clicking on the connections in the ve…
Browse files Browse the repository at this point in the history
…ssel report
  • Loading branch information
rockfactory committed Feb 24, 2024
1 parent 6774cab commit 90e9581
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 24 deletions.
Binary file modified plugin_template/assets/bundles/commnext_ui.bundle
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@
padding-right: 6px;
padding-top: 6px;
padding-bottom: 6px;
transition: background-color 0.18s;
}

.row__container:hover {
background-color: rgb(67, 72, 155);
}

.icon__small {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<Style src="project://database/Assets/UI/CommNextStyles.uss?fileID=7433441132597879392&amp;guid=a2dd25c9df311c044b3d4c1d0b58a080&amp;type=3#CommNextStyles" />
<ui:VisualElement class="row__spacer" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0);">
<ui:VisualElement class="row__container" style="flex-grow: 1; flex-direction: row; align-items: center; padding-left: 6px; padding-right: 6px; padding-top: 6px; padding-bottom: 6px; flex-shrink: 0;">
<ui:VisualElement name="row__container" class="row__container" style="flex-grow: 1; flex-direction: row; align-items: center; padding-left: 6px; padding-right: 6px; padding-top: 6px; padding-bottom: 6px; flex-shrink: 0;">
<ui:VisualElement style="flex-grow: 0; background-color: rgba(0, 0, 0, 0);">
<ui:VisualElement name="connection-icon" class="relay__icon" style="flex-grow: 0; background-color: rgba(0, 0, 0, 0);" />
</ui:VisualElement>
Expand Down
35 changes: 32 additions & 3 deletions src/CommNext/UI/Components/NetworkConnectionViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace CommNext.UI.Components;

public class NetworkConnectionViewController : UIToolkitElement
public class NetworkConnectionViewController : UIToolkitElement, IPoolingElement
{
private Label _nameLabel;
Expand All @@ -25,7 +24,10 @@ public class NetworkConnectionViewController : UIToolkitElement, IPoolingElement
private VisualElement _powerIcon;
private SignalStrengthIcon _signalStrengthIcon;
private TooltipManipulator _signalStrengthTooltip;
private VisualElement _rowContainer;

private NetworkNode _currentNode = null!;
private NetworkConnection _connection = null!;

private static Color ActiveColor => new(0f, 1f, 0.4f, 1f);
private static Color InactiveColor => new(1f, 0.36f, 0.28f, 1f);
Expand All @@ -43,10 +45,37 @@ public NetworkConnectionViewController() : base("Components/NetworkConnectionVie
_signalStrengthIcon = _root.Q<SignalStrengthIcon>("signal-strength-icon");
_signalStrengthTooltip = new TooltipManipulator("");
_signalStrengthIcon.AddManipulator(_signalStrengthTooltip);
_rowContainer = _root.Q<VisualElement>("row__container");
_rowContainer.AddManipulator(new Clickable(OnClick));
}

/// <summary>
/// See `MapUISelectableItem.HandleVesselControl`. This implementation
/// is pretty much the same (except for the 3D Map view part).
/// </summary>
private void OnClick()
{
var game = GameManager.Instance.Game;
if (!game.ViewController.CanObserverLeaveTheActiveVessel()) return;

game.UI.SetCurtainContext(CurtainContext.EnterGamePlay);
game.UI.SetCurtainVisibility(true, () =>
{
game.GlobalGameState.SetState(GameState.Map3DView);
Mouse.EnableVirtualCursor(false);
var targetVessel = game.UniverseModel.FindVesselComponent(_connection.GetOther(_currentNode).Owner);
game.ViewController.SetActiveVehicle(targetVessel, true, true);
game.UI.SetCurtainVisibility(false);
});
}


public void Bind(NetworkNode currentNode, NetworkConnection connection)
{
_currentNode = currentNode;
_connection = connection;

var otherNode = connection.GetOther(currentNode);
_nameLabel.text = otherNode.DebugVesselName;
_directionLabel.text = connection.IsSource(currentNode)
Expand All @@ -69,8 +98,8 @@ public void Bind(NetworkNode currentNode, NetworkConnection connection)
var occludedText = connection.OccludingBody.HasValue
? " | " +
LocalizationManager.GetTranslation(LocalizedStrings.OccludedByKey, [
CelestialBodiesHelper.GetBodyName(connection.OccludingBody).UIColored("#E7CA76")
]).UIColored("#FF5B48")
CelestialBodiesHelper.GetBodyName(connection.OccludingBody).RTEColor("#E7CA76")
]).RTEColor("#FF5B48")
: "";

_detailsLabel.text = distanceText + occludedText;
Expand Down
6 changes: 3 additions & 3 deletions src/CommNext/UI/Utils/UIToolkitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ public static void PoolChildren<TItem, TElement>(
IEnumerable<TItem> items,
Action<TItem, TElement> binder) where TElement : IPoolingElement, new()
{
var children = parent.Children().ToList();
var children = parent.Children().ToArray();
var itemsArray = items as TItem[] ?? items.ToArray();
for (var i = 0; i < itemsArray.Length; i++)
{
TElement itemElement;
if (i < itemsArray.Length)
if (i < children.Length)
{
itemElement = (TElement)children[i].userData;
}
Expand All @@ -136,7 +136,7 @@ public static void PoolChildren<TItem, TElement>(
binder(itemsArray[i], itemElement);
}

for (var i = itemsArray.Length; i < children.Count; i++)
for (var i = itemsArray.Length; i < children.Length; i++)
parent.Remove(children[i]);
}
}
24 changes: 7 additions & 17 deletions src/CommNext/UI/VesselReportWindowController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private void BuildUI()
_nameLabel.text = _vessel!.Name;
_rangeLabel.text = LocalizationManager.GetTranslation(LocalizedStrings.RangeLabelKey, [
Units.PrintSI(vesselMaxRange, Units.SymbolMeters)
.UIColored("#E7CA76")
.RTEColor("#E7CA76")
]);

var connections = NetworkManager.Instance.GetNodeConnections(networkNode, _query.Filter);
Expand All @@ -173,24 +173,14 @@ private void BuildUI()
// Cache the connections for MapView
ReportVesselConnections = connections;

var connectionElements = _connectionsList.Children().ToList();

// Some basic pooling
for (var i = 0; i < connections.Count; i++)
if (i < connectionElements.Count)
// Pool the connections
_connectionsList.PoolChildren<NetworkConnection, NetworkConnectionViewController>(connections,
(connection, connectionRow) =>
{
var connectionRow = (NetworkConnectionViewController)connectionElements[i].userData;
connectionRow.Bind(networkNode, connections[i]);
}
else
{
var connectionRow = new NetworkConnectionViewController();
connectionRow.Bind(networkNode, connections[i]);
_connectionsList.Add(connectionRow.Root);
}
// Link the connection to the row
connectionRow.Bind(networkNode, connection);
});

for (var i = connections.Count; i < connectionElements.Count; i++)
_connectionsList.Remove(connectionElements[i]);
}

private void Update()
Expand Down

0 comments on commit 90e9581

Please sign in to comment.