GuideMicrowaveEmbed.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Linq;
  3. using Content.Client.Guidebook.Richtext;
  4. using Content.Client.Message;
  5. using Content.Client.UserInterface.ControlExtensions;
  6. using Content.Shared.Chemistry.Reagent;
  7. using Content.Shared.Kitchen;
  8. using JetBrains.Annotations;
  9. using Robust.Client.AutoGenerated;
  10. using Robust.Client.UserInterface.Controls;
  11. using Robust.Client.UserInterface.XAML;
  12. using Robust.Client.UserInterface;
  13. using Robust.Shared.Prototypes;
  14. using Robust.Shared.Utility;
  15. namespace Content.Client.Guidebook.Controls;
  16. /// <summary>
  17. /// Control for embedding a microwave recipe into a guidebook.
  18. /// </summary>
  19. [UsedImplicitly, GenerateTypedNameReferences]
  20. public sealed partial class GuideMicrowaveEmbed : PanelContainer, IDocumentTag, ISearchableControl
  21. {
  22. [Dependency] private readonly IPrototypeManager _prototype = default!;
  23. [Dependency] private readonly ILogManager _logManager = default!;
  24. private ISawmill _sawmill = default!;
  25. public GuideMicrowaveEmbed()
  26. {
  27. RobustXamlLoader.Load(this);
  28. IoCManager.InjectDependencies(this);
  29. MouseFilter = MouseFilterMode.Stop;
  30. _sawmill = _logManager.GetSawmill("guidemicrowaveembed");
  31. }
  32. public GuideMicrowaveEmbed(string recipe) : this()
  33. {
  34. GenerateControl(_prototype.Index<FoodRecipePrototype>(recipe));
  35. }
  36. public GuideMicrowaveEmbed(FoodRecipePrototype recipe) : this()
  37. {
  38. GenerateControl(recipe);
  39. }
  40. public bool CheckMatchesSearch(string query)
  41. {
  42. return this.ChildrenContainText(query);
  43. }
  44. public void SetHiddenState(bool state, string query)
  45. {
  46. Visible = CheckMatchesSearch(query) ? state : !state;
  47. }
  48. public bool TryParseTag(Dictionary<string, string> args, [NotNullWhen(true)] out Control? control)
  49. {
  50. control = null;
  51. if (!args.TryGetValue("Recipe", out var id))
  52. {
  53. _sawmill.Error("Recipe embed tag is missing recipe prototype argument");
  54. return false;
  55. }
  56. if (!_prototype.TryIndex<FoodRecipePrototype>(id, out var recipe))
  57. {
  58. _sawmill.Error($"Specified recipe prototype \"{id}\" is not a valid recipe prototype");
  59. return false;
  60. }
  61. GenerateControl(recipe);
  62. control = this;
  63. return true;
  64. }
  65. private void GenerateHeader(FoodRecipePrototype recipe)
  66. {
  67. var entity = _prototype.Index<EntityPrototype>(recipe.Result);
  68. IconContainer.AddChild(new GuideEntityEmbed(recipe.Result, false, false));
  69. ResultName.SetMarkup(entity.Name);
  70. ResultDescription.SetMarkup(entity.Description);
  71. }
  72. private void GenerateSolidIngredients(FoodRecipePrototype recipe)
  73. {
  74. foreach (var (product, amount) in recipe.IngredientsSolids.OrderByDescending(p => p.Value))
  75. {
  76. var ingredient = _prototype.Index<EntityPrototype>(product);
  77. IngredientsGrid.AddChild(new GuideEntityEmbed(product, false, false));
  78. // solid name
  79. var solidNameMsg = new FormattedMessage();
  80. solidNameMsg.AddMarkupOrThrow(Loc.GetString("guidebook-microwave-solid-name-display", ("ingredient", ingredient.Name)));
  81. solidNameMsg.Pop();
  82. var solidNameLabel = new RichTextLabel();
  83. solidNameLabel.SetMessage(solidNameMsg);
  84. IngredientsGrid.AddChild(solidNameLabel);
  85. // solid quantity
  86. var solidQuantityMsg = new FormattedMessage();
  87. solidQuantityMsg.AddMarkupOrThrow(Loc.GetString("guidebook-microwave-solid-quantity-display", ("amount", amount)));
  88. solidQuantityMsg.Pop();
  89. var solidQuantityLabel = new RichTextLabel();
  90. solidQuantityLabel.SetMessage(solidQuantityMsg);
  91. IngredientsGrid.AddChild(solidQuantityLabel);
  92. }
  93. }
  94. private void GenerateLiquidIngredients(FoodRecipePrototype recipe)
  95. {
  96. foreach (var (product, amount) in recipe.IngredientsReagents.OrderByDescending(p => p.Value))
  97. {
  98. var reagent = _prototype.Index<ReagentPrototype>(product);
  99. // liquid color
  100. var liquidColorMsg = new FormattedMessage();
  101. liquidColorMsg.AddMarkupOrThrow(Loc.GetString("guidebook-microwave-reagent-color-display", ("color", reagent.SubstanceColor)));
  102. liquidColorMsg.Pop();
  103. var liquidColorLabel = new RichTextLabel();
  104. liquidColorLabel.SetMessage(liquidColorMsg);
  105. liquidColorLabel.HorizontalAlignment = Control.HAlignment.Center;
  106. IngredientsGrid.AddChild(liquidColorLabel);
  107. // liquid name
  108. var liquidNameMsg = new FormattedMessage();
  109. liquidNameMsg.AddMarkupOrThrow(Loc.GetString("guidebook-microwave-reagent-name-display", ("reagent", reagent.LocalizedName)));
  110. liquidNameMsg.Pop();
  111. var liquidNameLabel = new RichTextLabel();
  112. liquidNameLabel.SetMessage(liquidNameMsg);
  113. IngredientsGrid.AddChild(liquidNameLabel);
  114. // liquid quantity
  115. var liquidQuantityMsg = new FormattedMessage();
  116. liquidQuantityMsg.AddMarkupOrThrow(Loc.GetString("guidebook-microwave-reagent-quantity-display", ("amount", amount)));
  117. liquidQuantityMsg.Pop();
  118. var liquidQuantityLabel = new RichTextLabel();
  119. liquidQuantityLabel.SetMessage(liquidQuantityMsg);
  120. IngredientsGrid.AddChild(liquidQuantityLabel);
  121. }
  122. }
  123. private void GenerateIngredients(FoodRecipePrototype recipe)
  124. {
  125. GenerateLiquidIngredients(recipe);
  126. GenerateSolidIngredients(recipe);
  127. }
  128. private void GenerateCookTime(FoodRecipePrototype recipe)
  129. {
  130. var msg = new FormattedMessage();
  131. msg.AddMarkupOrThrow(Loc.GetString("guidebook-microwave-cook-time", ("time", recipe.CookTime)));
  132. msg.Pop();
  133. CookTimeLabel.SetMessage(msg);
  134. }
  135. private void GenerateControl(FoodRecipePrototype recipe)
  136. {
  137. GenerateHeader(recipe);
  138. GenerateIngredients(recipe);
  139. GenerateCookTime(recipe);
  140. }
  141. }