Files
OldThink/Content.Client/Kitchen/UI/MicrowaveBoundUserInterface.cs

139 lines
5.0 KiB
C#
Raw Normal View History

2020-05-03 03:09:54 -05:00
using System.Collections.Generic;
using Content.Shared.Chemistry.Components;
2021-06-09 22:19:39 +02:00
using Content.Shared.Chemistry.Reagent;
using Content.Shared.Kitchen.Components;
using JetBrains.Annotations;
2020-05-03 03:09:54 -05:00
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using static Content.Shared.Kitchen.Components.SharedMicrowaveComponent;
2021-06-09 22:19:39 +02:00
namespace Content.Client.Kitchen.UI
{
[UsedImplicitly]
public class MicrowaveBoundUserInterface : BoundUserInterface
{
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private MicrowaveMenu? _menu;
private readonly Dictionary<int, EntityUid> _solids = new();
private readonly Dictionary<int, Solution.ReagentQuantity> _reagents =new();
2020-05-03 03:09:54 -05:00
public MicrowaveBoundUserInterface(ClientUserInterfaceComponent owner, object uiKey) : base(owner,uiKey)
{
}
2020-05-01 23:34:04 -05:00
protected override void Open()
{
base.Open();
_menu = new MicrowaveMenu(this);
_menu.OpenCentered();
_menu.OnClose += Close;
_menu.StartButton.OnPressed += _ => SendMessage(new MicrowaveStartCookMessage());
_menu.EjectButton.OnPressed += _ => SendMessage(new MicrowaveEjectMessage());
_menu.IngredientsList.OnItemSelected += args =>
{
SendMessage(new MicrowaveEjectSolidIndexedMessage(_solids[args.ItemIndex]));
};
_menu.IngredientsListReagents.OnItemSelected += args =>
{
SendMessage(new MicrowaveVaporizeReagentIndexedMessage(_reagents[args.ItemIndex]));
};
_menu.OnCookTimeSelected += (args,buttonIndex) =>
{
var actualButton = (MicrowaveMenu.MicrowaveCookTimeButton) args.Button ;
SendMessage(new MicrowaveSelectCookTimeMessage(buttonIndex,actualButton.CookTime));
};
2020-05-01 23:34:04 -05:00
}
2020-05-03 03:09:54 -05:00
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
2020-05-03 03:09:54 -05:00
if (!disposing)
{
return;
}
_solids.Clear();
2020-05-03 03:09:54 -05:00
_menu?.Dispose();
}
2020-05-01 23:34:04 -05:00
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
if (state is not MicrowaveUpdateUserInterfaceState cState)
2020-05-03 03:09:54 -05:00
{
2020-05-01 23:34:04 -05:00
return;
2020-05-03 03:09:54 -05:00
}
_menu?.ToggleBusyDisableOverlayPanel(cState.IsMicrowaveBusy);
RefreshContentsDisplay(cState.ReagentQuantities, cState.ContainedSolids);
if (_menu == null) return;
var currentlySelectedTimeButton = (Button) _menu.CookTimeButtonVbox.GetChild(cState.ActiveButtonIndex);
currentlySelectedTimeButton.Pressed = true;
var cookTime = cState.ActiveButtonIndex == 0
? Loc.GetString("microwave-menu-instant-button")
: cState.CurrentCookTime.ToString();
_menu.CookTimeInfoLabel.Text = Loc.GetString("microwave-bound-user-interface-cook-time-label",
("time", cookTime));
2020-05-01 23:34:04 -05:00
}
private void RefreshContentsDisplay(Solution.ReagentQuantity[] reagents, EntityUid[] containedSolids)
2020-05-03 01:34:00 -05:00
{
_reagents.Clear();
if (_menu == null) return;
_menu.IngredientsListReagents.Clear();
for (var i = 0; i < reagents.Length; i++)
2020-05-03 03:09:54 -05:00
{
if (!_prototypeManager.TryIndex(reagents[i].ReagentId, out ReagentPrototype? proto)) continue;
var reagentAdded = _menu.IngredientsListReagents.AddItem($"{reagents[i].Quantity} {proto.Name}");
var reagentIndex = _menu.IngredientsListReagents.IndexOf(reagentAdded);
_reagents.Add(reagentIndex, reagents[i]);
2020-05-03 03:09:54 -05:00
}
_solids.Clear();
_menu.IngredientsList.Clear();
2021-12-05 18:09:01 +01:00
foreach (var entity in containedSolids)
2020-05-03 03:09:54 -05:00
{
2021-12-09 12:29:27 +01:00
if (_entityManager.Deleted(entity))
{
return;
}
Texture? texture;
2021-12-05 18:09:01 +01:00
if (_entityManager.TryGetComponent(entity, out IconComponent? iconComponent))
{
texture = iconComponent.Icon?.Default;
}
2021-12-05 18:09:01 +01:00
else if (_entityManager.TryGetComponent(entity, out SpriteComponent? spriteComponent))
{
texture = spriteComponent.Icon?.Default;
}
else
{
continue;
}
2021-12-05 18:09:01 +01:00
var solidItem = _menu.IngredientsList.AddItem(_entityManager.GetComponent<MetaDataComponent>(entity).EntityName, texture);
var solidIndex = _menu.IngredientsList.IndexOf(solidItem);
2021-12-05 18:09:01 +01:00
_solids.Add(solidIndex, entity);
}
2020-05-03 01:34:00 -05:00
}
}
}