-using Content.Shared.Research.Prototypes;
+using Content.Shared.Research.Prototypes;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface;
[GenerateTypedNameReferences]
public sealed partial class MiniTechnologyCardControl : Control
{
+ /// The technology that this control represents
+ public readonly TechnologyPrototype Technology;
+
public MiniTechnologyCardControl(TechnologyPrototype technology, IPrototypeManager prototypeManager, SpriteSystem spriteSys, FormattedMessage description)
{
RobustXamlLoader.Load(this);
var tooltip = new Tooltip();
tooltip.SetMessage(description);
Main.TooltipSupplier = _ => tooltip;
+ Technology = technology;
}
}
+using System.Linq;
using System.Numerics;
using Content.Client.UserInterface.Controls;
using Content.Shared.Access.Components;
public void UpdatePanels(ResearchConsoleBoundInterfaceState state)
{
- var allTech = _research.GetAvailableTechnologies(Entity);
- AvailableCardsContainer.Children.Clear();
TechnologyCardsContainer.Children.Clear();
- UnlockedCardsContainer.Children.Clear();
- foreach (var tech in allTech)
- {
- var mini = new MiniTechnologyCardControl(tech, _prototype, _sprite, _research.GetTechnologyDescription(tech));
- AvailableCardsContainer.AddChild(mini);
- }
+ var availableTech = _research.GetAvailableTechnologies(Entity);
+ SyncTechnologyList(AvailableCardsContainer, availableTech);
if (_technologyDatabase == null)
return;
TechnologyCardsContainer.AddChild(cardControl);
}
- foreach (var unlocked in _technologyDatabase.UnlockedTechnologies)
- {
- var tech = _prototype.Index<TechnologyPrototype>(unlocked);
- var cardControl = new MiniTechnologyCardControl(tech, _prototype, _sprite, _research.GetTechnologyDescription(tech, false));
- UnlockedCardsContainer.AddChild(cardControl);
- }
+ var unlockedTech = _technologyDatabase.UnlockedTechnologies.Select(x => _prototype.Index<TechnologyPrototype>(x));
+ SyncTechnologyList(UnlockedCardsContainer, unlockedTech);
}
public void UpdateInformationPanel(ResearchConsoleBoundInterfaceState state)
TierDisplayContainer.AddChild(control);
}
}
+
+ /// <summary>
+ /// Synchronize a container for technology cards with a list of technologies,
+ /// creating or removing UI cards as appropriate.
+ /// </summary>
+ /// <param name="container">The container which contains the UI cards</param>
+ /// <param name="technologies">The current set of technologies for which there should be cards</param>
+ private void SyncTechnologyList(BoxContainer container, IEnumerable<TechnologyPrototype> technologies)
+ {
+ // For the cards which already exist, build a map from technology prototype to the UI card
+ var currentTechControls = new Dictionary<TechnologyPrototype, Control>();
+ foreach (var child in container.Children)
+ {
+ if (child is MiniTechnologyCardControl)
+ {
+ currentTechControls.Add((child as MiniTechnologyCardControl)!.Technology, child);
+ }
+ }
+
+ foreach (var tech in technologies)
+ {
+ if (!currentTechControls.ContainsKey(tech))
+ {
+ // Create a card for any technology which doesn't already have one.
+ var mini = new MiniTechnologyCardControl(tech, _prototype, _sprite, _research.GetTechnologyDescription(tech));
+ container.AddChild(mini);
+ }
+ else
+ {
+ // The tech already exists in the UI; remove it from the set, so we won't revisit it below
+ currentTechControls.Remove(tech);
+ }
+ }
+
+ // Now, any items left in the dictionary are technologies which were previously
+ // available, but now are not. Remove them.
+ foreach (var (tech, techControl) in currentTechControls)
+ {
+ container.Children.Remove(techControl);
+ }
+ }
}