|
| 1 | +using System.Numerics; |
| 2 | +using Content.Client.Construction; |
| 3 | +using Content.Client.UserInterface.Controls; |
| 4 | +using Content.Shared._White.ShortConstruction; |
| 5 | +using Content.Shared.Construction.Prototypes; |
| 6 | +using Robust.Client.AutoGenerated; |
| 7 | +using Robust.Client.GameObjects; |
| 8 | +using Robust.Client.Placement; |
| 9 | +using Robust.Client.UserInterface.Controls; |
| 10 | +using Robust.Client.UserInterface.XAML; |
| 11 | +using Robust.Shared.Enums; |
| 12 | +using Robust.Shared.Prototypes; |
| 13 | + |
| 14 | +namespace Content.Client._White.ShortConstruction.UI; |
| 15 | + |
| 16 | +[GenerateTypedNameReferences] |
| 17 | +public sealed partial class ShortConstructionMenu : RadialMenu |
| 18 | +{ |
| 19 | + [Dependency] private readonly EntityManager _entManager = default!; |
| 20 | + [Dependency] private readonly IPrototypeManager _protoManager = default!; |
| 21 | + [Dependency] private readonly IPlacementManager _placementManager = default!; |
| 22 | + |
| 23 | + private readonly ConstructionSystem _construction; |
| 24 | + |
| 25 | + public ShortConstructionMenu(EntityUid owner) |
| 26 | + { |
| 27 | + IoCManager.InjectDependencies(this); |
| 28 | + RobustXamlLoader.Load(this); |
| 29 | + _construction = _entManager.System<ConstructionSystem>(); |
| 30 | + |
| 31 | + if (!_entManager.TryGetComponent<ShortConstructionComponent>(owner, out var crafting)) |
| 32 | + return; |
| 33 | + |
| 34 | + var spriteSystem = _entManager.System<SpriteSystem>(); |
| 35 | + var main = FindControl<RadialContainer>("Main"); |
| 36 | + |
| 37 | + foreach (var protoId in crafting.Prototypes) |
| 38 | + { |
| 39 | + if (!_protoManager.TryIndex(protoId, out var proto)) |
| 40 | + continue; |
| 41 | + |
| 42 | + var button = new RadialMenuTextureButton |
| 43 | + { |
| 44 | + ToolTip = Loc.GetString(proto.Name), |
| 45 | + StyleClasses = { "RadialMenuButton" }, |
| 46 | + SetSize = new Vector2(48f, 48f), |
| 47 | + }; |
| 48 | + |
| 49 | + var texture = new TextureRect |
| 50 | + { |
| 51 | + VerticalAlignment = VAlignment.Center, |
| 52 | + HorizontalAlignment = HAlignment.Center, |
| 53 | + Texture = spriteSystem.Frame0(proto.Icon), |
| 54 | + TextureScale = new Vector2(1.5f, 1.5f), |
| 55 | + }; |
| 56 | + |
| 57 | + button.AddChild(texture); |
| 58 | + |
| 59 | + button.OnButtonUp += _ => |
| 60 | + { |
| 61 | + if (ConstructItem(proto)) |
| 62 | + Close(); |
| 63 | + }; |
| 64 | + |
| 65 | + main.AddChild(button); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Makes an item or places a schematic based on the type of construction recipe. |
| 71 | + /// </summary> |
| 72 | + /// <returns>Whatever the menu should be closed or not. By default crafting items does not close the window</returns> |
| 73 | + private bool ConstructItem(ConstructionPrototype prototype) |
| 74 | + { |
| 75 | + if (prototype.Type == ConstructionType.Item) |
| 76 | + { |
| 77 | + _construction.TryStartItemConstruction(prototype.ID); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + _placementManager.BeginPlacing(new PlacementInformation |
| 82 | + { |
| 83 | + IsTile = false, |
| 84 | + PlacementOption = prototype.PlacementMode |
| 85 | + }, new ConstructionPlacementHijack(_construction, prototype)); |
| 86 | + return true; |
| 87 | + } |
| 88 | +} |
0 commit comments