ConstructionMenu.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Numerics;
  3. using Robust.Client.AutoGenerated;
  4. using Robust.Client.Graphics;
  5. using Robust.Client.UserInterface.Controls;
  6. using Robust.Client.UserInterface.CustomControls;
  7. using Robust.Client.UserInterface.XAML;
  8. using Robust.Shared.Graphics;
  9. using Robust.Shared.IoC;
  10. using Robust.Shared.Localization;
  11. namespace Content.Client.Construction.UI
  12. {
  13. /// <summary>
  14. /// This is the interface for a UI View of the construction window. The point of it is to abstract away the actual
  15. /// UI controls and just provide higher level operations on the entire window. This View is completely passive and
  16. /// just raises events to the outside world. This class is controlled by the <see cref="ConstructionMenuPresenter"/>.
  17. /// </summary>
  18. public interface IConstructionMenuView : IDisposable
  19. {
  20. // It isn't optimal to expose UI controls like this, but the UI control design is
  21. // questionable so it can't be helped.
  22. string[] Categories { get; set; }
  23. OptionButton OptionCategories { get; }
  24. bool EraseButtonPressed { get; set; }
  25. bool GridViewButtonPressed { get; set; }
  26. bool BuildButtonPressed { get; set; }
  27. ItemList Recipes { get; }
  28. ItemList RecipeStepList { get; }
  29. ScrollContainer RecipesGridScrollContainer { get; }
  30. GridContainer RecipesGrid { get; }
  31. event EventHandler<(string search, string catagory)> PopulateRecipes;
  32. event EventHandler<ItemList.Item?> RecipeSelected;
  33. event EventHandler RecipeFavorited;
  34. event EventHandler<bool> BuildButtonToggled;
  35. event EventHandler<bool> EraseButtonToggled;
  36. event EventHandler ClearAllGhosts;
  37. void ClearRecipeInfo();
  38. void SetRecipeInfo(string name, string description, Texture iconTexture, bool isItem, bool isFavorite);
  39. void ResetPlacement();
  40. #region Window Control
  41. event Action? OnClose;
  42. bool IsOpen { get; }
  43. void OpenCentered();
  44. void MoveToFront();
  45. bool IsAtFront();
  46. void Close();
  47. #endregion
  48. }
  49. [GenerateTypedNameReferences]
  50. public sealed partial class ConstructionMenu : DefaultWindow, IConstructionMenuView
  51. {
  52. public bool BuildButtonPressed
  53. {
  54. get => BuildButton.Pressed;
  55. set => BuildButton.Pressed = value;
  56. }
  57. public string[] Categories { get; set; } = Array.Empty<string>();
  58. public bool EraseButtonPressed
  59. {
  60. get => EraseButton.Pressed;
  61. set => EraseButton.Pressed = value;
  62. }
  63. public bool GridViewButtonPressed
  64. {
  65. get => MenuGridViewButton.Pressed;
  66. set => MenuGridViewButton.Pressed = value;
  67. }
  68. public ConstructionMenu()
  69. {
  70. SetSize = new Vector2(560, 450);
  71. MinSize = new Vector2(560, 320);
  72. IoCManager.InjectDependencies(this);
  73. RobustXamlLoader.Load(this);
  74. Title = Loc.GetString("construction-menu-title");
  75. BuildButton.Text = Loc.GetString("construction-menu-place-ghost");
  76. Recipes.OnItemSelected += obj => RecipeSelected?.Invoke(this, obj.ItemList[obj.ItemIndex]);
  77. Recipes.OnItemDeselected += _ => RecipeSelected?.Invoke(this, null);
  78. SearchBar.OnTextChanged += _ =>
  79. PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[OptionCategories.SelectedId]));
  80. OptionCategories.OnItemSelected += obj =>
  81. {
  82. OptionCategories.SelectId(obj.Id);
  83. SearchBar.SetText(string.Empty);
  84. PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[obj.Id]));
  85. };
  86. BuildButton.Text = Loc.GetString("construction-menu-place-ghost");
  87. BuildButton.OnToggled += args => BuildButtonToggled?.Invoke(this, args.Pressed);
  88. ClearButton.Text = Loc.GetString("construction-menu-clear-all");
  89. ClearButton.OnPressed += _ => ClearAllGhosts?.Invoke(this, EventArgs.Empty);
  90. EraseButton.Text = Loc.GetString("construction-menu-eraser-mode");
  91. EraseButton.OnToggled += args => EraseButtonToggled?.Invoke(this, args.Pressed);
  92. FavoriteButton.OnPressed += args => RecipeFavorited?.Invoke(this, EventArgs.Empty);
  93. MenuGridViewButton.OnPressed += _ =>
  94. PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[OptionCategories.SelectedId]));
  95. }
  96. public event EventHandler? ClearAllGhosts;
  97. public event EventHandler<(string search, string catagory)>? PopulateRecipes;
  98. public event EventHandler<ItemList.Item?>? RecipeSelected;
  99. public event EventHandler? RecipeFavorited;
  100. public event EventHandler<bool>? BuildButtonToggled;
  101. public event EventHandler<bool>? EraseButtonToggled;
  102. public void ResetPlacement()
  103. {
  104. BuildButton.Pressed = false;
  105. EraseButton.Pressed = false;
  106. }
  107. public void SetRecipeInfo(
  108. string name, string description, Texture iconTexture, bool isItem, bool isFavorite)
  109. {
  110. BuildButton.Disabled = false;
  111. BuildButton.Text = Loc.GetString(isItem ? "construction-menu-place-ghost" : "construction-menu-craft");
  112. TargetName.SetMessage(name);
  113. TargetDesc.SetMessage(description);
  114. TargetTexture.Texture = iconTexture;
  115. FavoriteButton.Visible = true;
  116. FavoriteButton.Text = Loc.GetString(
  117. isFavorite ? "construction-add-favorite-button" : "construction-remove-from-favorite-button");
  118. }
  119. public void ClearRecipeInfo()
  120. {
  121. BuildButton.Disabled = true;
  122. TargetName.SetMessage(string.Empty);
  123. TargetDesc.SetMessage(string.Empty);
  124. TargetTexture.Texture = null;
  125. FavoriteButton.Visible = false;
  126. RecipeStepList.Clear();
  127. }
  128. }
  129. }