MicrowaveBoundUserInterface.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Content.Shared.Chemistry.Reagent;
  2. using Content.Shared.Kitchen.Components;
  3. using JetBrains.Annotations;
  4. using Robust.Client.GameObjects;
  5. using Robust.Client.Graphics;
  6. using Robust.Client.UserInterface;
  7. using Robust.Client.UserInterface.Controls;
  8. using Robust.Shared.Timing;
  9. namespace Content.Client.Kitchen.UI
  10. {
  11. [UsedImplicitly]
  12. public sealed class MicrowaveBoundUserInterface : BoundUserInterface
  13. {
  14. [ViewVariables]
  15. private MicrowaveMenu? _menu;
  16. [ViewVariables]
  17. private readonly Dictionary<int, EntityUid> _solids = new();
  18. [ViewVariables]
  19. private readonly Dictionary<int, ReagentQuantity> _reagents = new();
  20. public MicrowaveBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
  21. {
  22. }
  23. protected override void Open()
  24. {
  25. base.Open();
  26. _menu = this.CreateWindow<MicrowaveMenu>();
  27. _menu.StartButton.OnPressed += _ => SendPredictedMessage(new MicrowaveStartCookMessage());
  28. _menu.EjectButton.OnPressed += _ => SendPredictedMessage(new MicrowaveEjectMessage());
  29. _menu.IngredientsList.OnItemSelected += args =>
  30. {
  31. SendPredictedMessage(new MicrowaveEjectSolidIndexedMessage(EntMan.GetNetEntity(_solids[args.ItemIndex])));
  32. };
  33. _menu.OnCookTimeSelected += (args, buttonIndex) =>
  34. {
  35. var selectedCookTime = (uint) 0;
  36. if (args.Button is MicrowaveMenu.MicrowaveCookTimeButton microwaveCookTimeButton)
  37. {
  38. // args.Button is a MicrowaveCookTimeButton
  39. var actualButton = (MicrowaveMenu.MicrowaveCookTimeButton) args.Button;
  40. selectedCookTime = actualButton.CookTime == 0 ? 0 : actualButton.CookTime;
  41. // SendMessage(new MicrowaveSelectCookTimeMessage((int) selectedCookTime / 5, actualButton.CookTime));
  42. SendPredictedMessage(new MicrowaveSelectCookTimeMessage((int) selectedCookTime / 5, actualButton.CookTime));
  43. _menu.CookTimeInfoLabel.Text = Loc.GetString("microwave-bound-user-interface-cook-time-label",
  44. ("time", selectedCookTime));
  45. }
  46. else
  47. {
  48. // args.Button is a normal button aka instant cook button
  49. SendPredictedMessage(new MicrowaveSelectCookTimeMessage((int) selectedCookTime, 0));
  50. _menu.CookTimeInfoLabel.Text = Loc.GetString("microwave-bound-user-interface-cook-time-label",
  51. ("time", Loc.GetString("microwave-menu-instant-button")));
  52. }
  53. };
  54. }
  55. protected override void UpdateState(BoundUserInterfaceState state)
  56. {
  57. base.UpdateState(state);
  58. if (state is not MicrowaveUpdateUserInterfaceState cState || _menu == null)
  59. {
  60. return;
  61. }
  62. _menu.IsBusy = cState.IsMicrowaveBusy;
  63. _menu.CurrentCooktimeEnd = cState.CurrentCookTimeEnd;
  64. _menu.ToggleBusyDisableOverlayPanel(cState.IsMicrowaveBusy || cState.ContainedSolids.Length == 0);
  65. // TODO move this to a component state and ensure the net ids.
  66. RefreshContentsDisplay(EntMan.GetEntityArray(cState.ContainedSolids));
  67. //Set the cook time info label
  68. var cookTime = cState.ActiveButtonIndex == 0
  69. ? Loc.GetString("microwave-menu-instant-button")
  70. : cState.CurrentCookTime.ToString();
  71. _menu.CookTimeInfoLabel.Text = Loc.GetString("microwave-bound-user-interface-cook-time-label",
  72. ("time", cookTime));
  73. _menu.StartButton.Disabled = cState.IsMicrowaveBusy || cState.ContainedSolids.Length == 0;
  74. _menu.EjectButton.Disabled = cState.IsMicrowaveBusy || cState.ContainedSolids.Length == 0;
  75. //Set the correct button active button
  76. if (cState.ActiveButtonIndex == 0)
  77. {
  78. _menu.InstantCookButton.Pressed = true;
  79. }
  80. else
  81. {
  82. var currentlySelectedTimeButton = (Button) _menu.CookTimeButtonVbox.GetChild(cState.ActiveButtonIndex - 1);
  83. currentlySelectedTimeButton.Pressed = true;
  84. }
  85. //Set the "micowave light" ui color to indicate if the microwave is busy or not
  86. if (cState.IsMicrowaveBusy && cState.ContainedSolids.Length > 0)
  87. {
  88. _menu.IngredientsPanel.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex("#947300") };
  89. }
  90. else
  91. {
  92. _menu.IngredientsPanel.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex("#1B1B1E") };
  93. }
  94. }
  95. private void RefreshContentsDisplay(EntityUid[] containedSolids)
  96. {
  97. _reagents.Clear();
  98. if (_menu == null) return;
  99. _solids.Clear();
  100. _menu.IngredientsList.Clear();
  101. foreach (var entity in containedSolids)
  102. {
  103. if (EntMan.Deleted(entity))
  104. {
  105. return;
  106. }
  107. // TODO just use sprite view
  108. Texture? texture;
  109. if (EntMan.TryGetComponent<IconComponent>(entity, out var iconComponent))
  110. {
  111. texture = EntMan.System<SpriteSystem>().GetIcon(iconComponent);
  112. }
  113. else if (EntMan.TryGetComponent<SpriteComponent>(entity, out var spriteComponent))
  114. {
  115. texture = spriteComponent.Icon?.Default;
  116. }
  117. else
  118. {
  119. continue;
  120. }
  121. var solidItem = _menu.IngredientsList.AddItem(EntMan.GetComponent<MetaDataComponent>(entity).EntityName, texture);
  122. var solidIndex = _menu.IngredientsList.IndexOf(solidItem);
  123. _solids.Add(solidIndex, entity);
  124. }
  125. }
  126. }
  127. }