208 lines
7.1 KiB
C#
208 lines
7.1 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using Content.Client.Chemistry.EntitySystems;
|
|
using Content.Client.Guidebook.Richtext;
|
|
using Content.Client.Message;
|
|
using Content.Client.UserInterface.ControlExtensions;
|
|
using Content.Shared.Chemistry.Reaction;
|
|
using Content.Shared.Chemistry.Reagent;
|
|
using JetBrains.Annotations;
|
|
using Robust.Client.AutoGenerated;
|
|
using Robust.Client.Graphics;
|
|
using Robust.Client.UserInterface;
|
|
using Robust.Client.UserInterface.Controls;
|
|
using Robust.Client.UserInterface.XAML;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Utility;
|
|
|
|
namespace Content.Client.Guidebook.Controls;
|
|
|
|
/// <summary>
|
|
/// Control for embedding a reagent into a guidebook.
|
|
/// </summary>
|
|
[UsedImplicitly, GenerateTypedNameReferences]
|
|
public sealed partial class GuideReagentEmbed : BoxContainer, IDocumentTag, ISearchableControl
|
|
{
|
|
[Dependency] private readonly IEntitySystemManager _systemManager = default!;
|
|
[Dependency] private readonly IPrototypeManager _prototype = default!;
|
|
|
|
private readonly ChemistryGuideDataSystem _chemistryGuideData;
|
|
|
|
public GuideReagentEmbed()
|
|
{
|
|
RobustXamlLoader.Load(this);
|
|
IoCManager.InjectDependencies(this);
|
|
_chemistryGuideData = _systemManager.GetEntitySystem<ChemistryGuideDataSystem>();
|
|
MouseFilter = MouseFilterMode.Stop;
|
|
}
|
|
|
|
public GuideReagentEmbed(string reagent) : this()
|
|
{
|
|
GenerateControl(_prototype.Index<ReagentPrototype>(reagent));
|
|
}
|
|
|
|
public GuideReagentEmbed(ReagentPrototype reagent) : this()
|
|
{
|
|
GenerateControl(reagent);
|
|
}
|
|
|
|
public bool CheckMatchesSearch(string query)
|
|
{
|
|
return this.ChildrenContainText(query);
|
|
}
|
|
|
|
public void SetHiddenState(bool state, string query)
|
|
{
|
|
this.Visible = CheckMatchesSearch(query) ? state : !state;
|
|
}
|
|
|
|
public bool TryParseTag(Dictionary<string, string> args, [NotNullWhen(true)] out Control? control)
|
|
{
|
|
control = null;
|
|
if (!args.TryGetValue("Reagent", out var id))
|
|
{
|
|
Logger.Error("Reagent embed tag is missing reagent prototype argument");
|
|
return false;
|
|
}
|
|
|
|
if (!_prototype.TryIndex<ReagentPrototype>(id, out var reagent))
|
|
{
|
|
Logger.Error($"Specified reagent prototype \"{id}\" is not a valid reagent prototype");
|
|
return false;
|
|
}
|
|
|
|
GenerateControl(reagent);
|
|
|
|
control = this;
|
|
return true;
|
|
}
|
|
|
|
private void GenerateControl(ReagentPrototype reagent)
|
|
{
|
|
NameBackground.PanelOverride = new StyleBoxFlat
|
|
{
|
|
BackgroundColor = reagent.SubstanceColor
|
|
};
|
|
|
|
var r = reagent.SubstanceColor.R;
|
|
var g = reagent.SubstanceColor.G;
|
|
var b = reagent.SubstanceColor.B;
|
|
|
|
var textColor = 0.2126f * r + 0.7152f * g + 0.0722f * b > 0.5
|
|
? Color.Black
|
|
: Color.White;
|
|
|
|
ReagentName.SetMarkup(Loc.GetString("guidebook-reagent-name",
|
|
("color", textColor), ("name", reagent.LocalizedName)));
|
|
|
|
#region Recipe
|
|
var reactions = _prototype.EnumeratePrototypes<ReactionPrototype>()
|
|
.Where(p => p.Products.ContainsKey(reagent.ID))
|
|
.OrderBy(p => p.Priority)
|
|
.ThenBy(p => p.Products.Count)
|
|
.ToList();
|
|
|
|
if (reactions.Any())
|
|
{
|
|
foreach (var reactionPrototype in reactions)
|
|
{
|
|
var ctrl = GetRecipeGuide(reactionPrototype);
|
|
RecipesDescriptionContainer.AddChild(ctrl);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
RecipesContainer.Visible = false;
|
|
}
|
|
#endregion
|
|
|
|
#region Effects
|
|
if (_chemistryGuideData.ReagentGuideRegistry.TryGetValue(reagent.ID, out var guideEntryRegistry) &&
|
|
guideEntryRegistry.GuideEntries != null &&
|
|
guideEntryRegistry.GuideEntries.Values.Any(pair => pair.EffectDescriptions.Any()))
|
|
{
|
|
EffectsDescriptionContainer.Children.Clear();
|
|
foreach (var (group, effect) in guideEntryRegistry.GuideEntries)
|
|
{
|
|
if (!effect.EffectDescriptions.Any())
|
|
continue;
|
|
|
|
var groupLabel = new RichTextLabel();
|
|
groupLabel.SetMarkup(Loc.GetString("guidebook-reagent-effects-metabolism-group-rate",
|
|
("group", group), ("rate", effect.MetabolismRate)));
|
|
var descriptionLabel = new RichTextLabel
|
|
{
|
|
Margin = new Thickness(25, 0, 10, 0)
|
|
};
|
|
|
|
var descMsg = new FormattedMessage();
|
|
var descriptionsCount = effect.EffectDescriptions.Length;
|
|
var i = 0;
|
|
foreach (var effectString in effect.EffectDescriptions)
|
|
{
|
|
descMsg.AddMarkup(effectString);
|
|
i++;
|
|
if (i < descriptionsCount)
|
|
descMsg.PushNewline();
|
|
}
|
|
descriptionLabel.SetMessage(descMsg);
|
|
|
|
EffectsDescriptionContainer.AddChild(groupLabel);
|
|
EffectsDescriptionContainer.AddChild(descriptionLabel);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
EffectsContainer.Visible = false;
|
|
}
|
|
#endregion
|
|
|
|
FormattedMessage description = new();
|
|
description.AddText(reagent.LocalizedDescription);
|
|
description.PushNewline();
|
|
description.AddMarkup(Loc.GetString("guidebook-reagent-physical-description",
|
|
("description", reagent.LocalizedPhysicalDescription)));
|
|
ReagentDescription.SetMessage(description);
|
|
}
|
|
|
|
private GuideReagentReaction GetRecipeGuide(ReactionPrototype reactionPrototype)
|
|
{
|
|
var control = new GuideReagentReaction();
|
|
|
|
var reactantMsg = new FormattedMessage();
|
|
var reactantsCount = reactionPrototype.Reactants.Count;
|
|
var i = 0;
|
|
foreach (var (product, reactant) in reactionPrototype.Reactants)
|
|
{
|
|
reactantMsg.AddMarkup(Loc.GetString("guidebook-reagent-recipes-reagent-display",
|
|
("reagent", _prototype.Index<ReagentPrototype>(product).LocalizedName), ("ratio", reactant.Amount)));
|
|
i++;
|
|
if (i < reactantsCount)
|
|
reactantMsg.PushNewline();
|
|
}
|
|
reactantMsg.Pop();
|
|
control.ReactantsLabel.SetMessage(reactantMsg);
|
|
|
|
if (reactionPrototype.MinimumTemperature > 0.0f)
|
|
{
|
|
control.MixLabel.Text = Loc.GetString("guidebook-reagent-recipes-mix-and-heat",
|
|
("temperature", reactionPrototype.MinimumTemperature));
|
|
}
|
|
|
|
var productMsg = new FormattedMessage();
|
|
var productCount = reactionPrototype.Products.Count;
|
|
var u = 0;
|
|
foreach (var (product, ratio) in reactionPrototype.Products)
|
|
{
|
|
productMsg.AddMarkup(Loc.GetString("guidebook-reagent-recipes-reagent-display",
|
|
("reagent", _prototype.Index<ReagentPrototype>(product).LocalizedName), ("ratio", ratio)));
|
|
u++;
|
|
if (u < productCount)
|
|
productMsg.PushNewline();
|
|
}
|
|
productMsg.Pop();
|
|
control.ProductsLabel.SetMessage(productMsg);
|
|
return control;
|
|
}
|
|
}
|