Files
crystall-punk-14/Content.Client/Chemistry/UI/ChemMasterWindow.cs

434 lines
17 KiB
C#
Raw Normal View History

using System;
2020-07-17 15:41:19 -05:00
using System.Linq;
2021-06-09 22:19:39 +02:00
using Content.Client.Stylesheets;
using Content.Client.UserInterface;
2021-06-09 22:19:39 +02:00
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Robust.Client.Graphics;
2020-07-17 15:41:19 -05:00
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.GameObjects;
2020-07-17 15:41:19 -05:00
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
2021-06-09 22:19:39 +02:00
using static Content.Shared.Chemistry.Components.SharedChemMasterComponent;
using static Robust.Client.UserInterface.Controls.BaseButton;
using static Robust.Client.UserInterface.Controls.BoxContainer;
2020-07-17 15:41:19 -05:00
2021-06-09 22:19:39 +02:00
namespace Content.Client.Chemistry.UI
2020-07-17 15:41:19 -05:00
{
/// <summary>
/// Client-side UI used to control a <see cref="SharedChemMasterComponent"/>
/// </summary>
public class ChemMasterWindow : SS14Window
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
2020-07-17 15:41:19 -05:00
/// <summary>Contains info about the reagent container such as it's contents, if one is loaded into the dispenser.</summary>
private readonly BoxContainer ContainerInfo;
2020-07-17 15:41:19 -05:00
private readonly BoxContainer BufferInfo;
2020-07-17 15:41:19 -05:00
private readonly BoxContainer PackagingInfo;
2020-07-17 15:41:19 -05:00
/// <summary>Ejects the reagent container from the dispenser.</summary>
public Button EjectButton { get; }
public Button BufferTransferButton { get; }
public Button BufferDiscardButton { get; }
public bool BufferModeTransfer = true;
public event Action<ButtonEventArgs, ChemButton>? OnChemButtonPressed;
2020-07-17 15:41:19 -05:00
public BoxContainer PillInfo { get; set; }
public BoxContainer BottleInfo { get; set; }
2020-07-17 15:41:19 -05:00
public SpinBox PillAmount { get; set; }
public SpinBox BottleAmount { get; set; }
public Button CreatePills { get; }
public Button CreateBottles { get; }
/// <summary>
/// Create and initialize the chem master UI client-side. Creates the basic layout,
/// actual data isn't filled in until the server sends data about the chem master.
/// </summary>
public ChemMasterWindow()
{
MinSize = SetSize = (400, 525);
2020-07-17 15:41:19 -05:00
IoCManager.InjectDependencies(this);
Contents.AddChild(new BoxContainer
2020-07-17 15:41:19 -05:00
{
Orientation = LayoutOrientation.Vertical,
2020-07-17 15:41:19 -05:00
Children =
{
//Container
new BoxContainer
2020-07-17 15:41:19 -05:00
{
Orientation = LayoutOrientation.Horizontal,
2020-07-17 15:41:19 -05:00
Children =
{
new Label {Text = Loc.GetString("chem-master-window-container-label")},
2021-02-21 12:38:56 +01:00
new Control {HorizontalExpand = true},
(EjectButton = new Button {Text = Loc.GetString("chem-master-window-eject-button")})
2020-07-17 15:41:19 -05:00
}
},
//Wrap the container info in a PanelContainer so we can color it's background differently.
new PanelContainer
{
2021-02-21 12:38:56 +01:00
VerticalExpand = true,
2020-07-17 15:41:19 -05:00
SizeFlagsStretchRatio = 6,
2021-02-21 12:38:56 +01:00
MinSize = (0, 200),
2020-07-17 15:41:19 -05:00
PanelOverride = new StyleBoxFlat
{
BackgroundColor = new Color(27, 27, 30)
},
Children =
{
//Currently empty, when server sends state data this will have container contents and fill volume.
(ContainerInfo = new BoxContainer
2020-07-17 15:41:19 -05:00
{
Orientation = LayoutOrientation.Vertical,
2021-02-21 12:38:56 +01:00
HorizontalExpand = true,
2020-07-17 15:41:19 -05:00
Children =
{
new Label
{
Text = Loc.GetString("chem-master-window-no-container-loaded-text")
2020-07-17 15:41:19 -05:00
}
}
}),
}
},
//Padding
2021-02-21 12:38:56 +01:00
new Control {MinSize = (0.0f, 10.0f)},
2020-07-17 15:41:19 -05:00
//Buffer
new BoxContainer
2020-07-17 15:41:19 -05:00
{
Orientation = LayoutOrientation.Horizontal,
2020-07-17 15:41:19 -05:00
Children =
{
new Label {Text = Loc.GetString("chem-master-window-buffer-text")},
2021-02-21 12:38:56 +01:00
new Control {HorizontalExpand = true},
(BufferTransferButton = new Button {Text = Loc.GetString("chem-master-window-transfer-button"), Pressed = BufferModeTransfer, StyleClasses = { StyleBase.ButtonOpenRight }}),
(BufferDiscardButton = new Button {Text = Loc.GetString("chem-master-window-discard-button"), Pressed = !BufferModeTransfer, StyleClasses = { StyleBase.ButtonOpenLeft }})
2020-07-17 15:41:19 -05:00
}
},
//Wrap the buffer info in a PanelContainer so we can color it's background differently.
new PanelContainer
{
2021-02-21 12:38:56 +01:00
VerticalExpand = true,
2020-07-17 15:41:19 -05:00
SizeFlagsStretchRatio = 6,
2021-02-21 12:38:56 +01:00
MinSize = (0, 100),
2020-07-17 15:41:19 -05:00
PanelOverride = new StyleBoxFlat
{
BackgroundColor = new Color(27, 27, 30)
},
Children =
{
//Buffer reagent list
(BufferInfo = new BoxContainer
2020-07-17 15:41:19 -05:00
{
Orientation = LayoutOrientation.Vertical,
2021-02-21 12:38:56 +01:00
HorizontalExpand = true,
2020-07-17 15:41:19 -05:00
Children =
{
new Label
{
Text = Loc.GetString("chem-master-window-buffer-empty-text")
2020-07-17 15:41:19 -05:00
}
}
}),
}
},
//Padding
2021-02-21 12:38:56 +01:00
new Control {MinSize = (0.0f, 10.0f)},
2020-07-17 15:41:19 -05:00
//Packaging
new BoxContainer
2020-07-17 15:41:19 -05:00
{
Orientation = LayoutOrientation.Horizontal,
2020-07-17 15:41:19 -05:00
Children =
{
new Label {Text = $"{Loc.GetString("chem-master-window-packaging-text")} "},
2020-07-17 15:41:19 -05:00
}
},
//Wrap the packaging info in a PanelContainer so we can color it's background differently.
new PanelContainer
{
2021-02-21 12:38:56 +01:00
VerticalExpand = true,
2020-07-17 15:41:19 -05:00
SizeFlagsStretchRatio = 6,
2021-02-21 12:38:56 +01:00
MinSize = (0, 100),
2020-07-17 15:41:19 -05:00
PanelOverride = new StyleBoxFlat
{
BackgroundColor = new Color(27, 27, 30)
},
Children =
{
//Packaging options
(PackagingInfo = new BoxContainer
2020-07-17 15:41:19 -05:00
{
Orientation = LayoutOrientation.Vertical,
2021-02-21 12:38:56 +01:00
HorizontalExpand = true,
2020-07-17 15:41:19 -05:00
}),
}
},
}
});
//Pills
PillInfo = new BoxContainer
2020-07-17 15:41:19 -05:00
{
Orientation = LayoutOrientation.Horizontal,
2020-07-17 15:41:19 -05:00
Children =
{
new Label
{
Text = $"{Loc.GetString("chem-master-window-pills-label")} "
2020-07-17 15:41:19 -05:00
},
},
};
PackagingInfo.AddChild(PillInfo);
2021-02-21 12:38:56 +01:00
var pillPadding = new Control {HorizontalExpand = true};
2020-07-17 15:41:19 -05:00
PillInfo.AddChild(pillPadding);
PillAmount = new SpinBox
{
2021-02-21 12:38:56 +01:00
HorizontalExpand = true,
2020-07-17 15:41:19 -05:00
Value = 1
};
PillAmount.InitDefaultButtons();
PillAmount.IsValid = (n) => (n > 0 && n <= 10);
PillInfo.AddChild(PillAmount);
var pillVolume = new Label
{
Text = $" {Loc.GetString("chem-master-window-max-pills-volume-text")} ",
2020-07-17 15:41:19 -05:00
StyleClasses = {StyleNano.StyleClassLabelSecondaryColor}
};
PillInfo.AddChild((pillVolume));
CreatePills = new Button {Text = Loc.GetString("chem-master-window-create-pill-button") };
2020-07-17 15:41:19 -05:00
PillInfo.AddChild(CreatePills);
//Bottles
BottleInfo = new BoxContainer
2020-07-17 15:41:19 -05:00
{
Orientation = LayoutOrientation.Horizontal,
2020-07-17 15:41:19 -05:00
Children =
{
new Label
{
Text = Loc.GetString("cham-master-window-bottles-label")
2020-07-17 15:41:19 -05:00
},
},
};
PackagingInfo.AddChild(BottleInfo);
2021-02-21 12:38:56 +01:00
var bottlePadding = new Control {HorizontalExpand = true};
2020-07-17 15:41:19 -05:00
BottleInfo.AddChild(bottlePadding);
BottleAmount = new SpinBox
{
2021-02-21 12:38:56 +01:00
HorizontalExpand = true,
2020-07-17 15:41:19 -05:00
Value = 1
};
BottleAmount.InitDefaultButtons();
BottleAmount.IsValid = (n) => (n > 0 && n <= 10);
BottleInfo.AddChild(BottleAmount);
var bottleVolume = new Label
{
Text = $" {Loc.GetString("chem-master-window-max-bottle-volume-text")} ",
2020-07-17 15:41:19 -05:00
StyleClasses = {StyleNano.StyleClassLabelSecondaryColor}
};
BottleInfo.AddChild((bottleVolume));
CreateBottles = new Button {Text = Loc.GetString("chem-master-window-create-bottle-button") };
2020-07-17 15:41:19 -05:00
BottleInfo.AddChild(CreateBottles);
}
private ChemButton MakeChemButton(string text, ReagentUnit amount, string id, bool isBuffer, string styleClass)
{
var button = new ChemButton(text, amount, id, isBuffer, styleClass);
button.OnPressed += args
=> OnChemButtonPressed?.Invoke(args, button);
return button;
}
/// <summary>
/// Update the UI state when new state data is received from the server.
/// </summary>
/// <param name="state">State data sent by the server.</param>
public void UpdateState(BoundUserInterfaceState state)
{
var castState = (ChemMasterBoundUserInterfaceState) state;
Title = castState.DispenserName;
UpdatePanelInfo(castState);
if (Contents.Children != null)
{
ButtonHelpers.SetButtonDisabledRecursive(Contents, !castState.HasPower);
EjectButton.Disabled = !castState.HasBeaker;
}
}
2020-07-17 15:41:19 -05:00
/// <summary>
/// Update the container, buffer, and packaging panels.
/// </summary>
/// <param name="state">State data for the dispenser.</param>
private void UpdatePanelInfo(ChemMasterBoundUserInterfaceState state)
{
BufferModeTransfer = state.BufferModeTransfer;
BufferTransferButton.Pressed = BufferModeTransfer;
BufferDiscardButton.Pressed = !BufferModeTransfer;
ContainerInfo.Children.Clear();
if (!state.HasBeaker)
{
ContainerInfo.Children.Add(new Label {Text = Loc.GetString("chem-master-window-no-container-loaded-text") });
2020-07-17 15:41:19 -05:00
return;
}
ContainerInfo.Children.Add(new BoxContainer // Name of the container and its fill status (Ex: 44/100u)
2020-07-17 15:41:19 -05:00
{
Orientation = LayoutOrientation.Horizontal,
2020-07-17 15:41:19 -05:00
Children =
{
new Label {Text = $"{state.ContainerName}: "},
new Label
{
Text = $"{state.BeakerCurrentVolume}/{state.BeakerMaxVolume}",
StyleClasses = {StyleNano.StyleClassLabelSecondaryColor}
}
}
});
foreach (var reagent in state.ContainerReagents)
{
var name = Loc.GetString("chem-master-window-unknown-reagent-text");
2020-07-17 15:41:19 -05:00
//Try to the prototype for the given reagent. This gives us it's name.
if (_prototypeManager.TryIndex(reagent.ReagentId, out ReagentPrototype? proto))
2020-07-17 15:41:19 -05:00
{
name = proto.Name;
}
if (proto != null)
{
ContainerInfo.Children.Add(new BoxContainer
2020-07-17 15:41:19 -05:00
{
Orientation = LayoutOrientation.Horizontal,
2020-07-17 15:41:19 -05:00
Children =
{
new Label {Text = $"{name}: "},
new Label
{
Text = $"{reagent.Quantity}u",
StyleClasses = {StyleNano.StyleClassLabelSecondaryColor}
},
//Padding
2021-02-21 12:38:56 +01:00
new Control {HorizontalExpand = true},
2020-07-17 15:41:19 -05:00
MakeChemButton("1", ReagentUnit.New(1), reagent.ReagentId, false, StyleBase.ButtonOpenRight),
MakeChemButton("5", ReagentUnit.New(5), reagent.ReagentId, false, StyleBase.ButtonOpenBoth),
MakeChemButton("10", ReagentUnit.New(10), reagent.ReagentId, false, StyleBase.ButtonOpenBoth),
MakeChemButton("25", ReagentUnit.New(25), reagent.ReagentId, false, StyleBase.ButtonOpenBoth),
MakeChemButton(Loc.GetString("chem-master-window-buffer-all-amount"), ReagentUnit.New(-1), reagent.ReagentId, false, StyleBase.ButtonOpenLeft),
2020-07-17 15:41:19 -05:00
}
});
}
}
BufferInfo.Children.Clear();
if (!state.BufferReagents.Any())
{
BufferInfo.Children.Add(new Label {Text = Loc.GetString("chem-master-window-buffer-empty-text") });
2020-07-17 15:41:19 -05:00
return;
}
var bufferHBox = new BoxContainer
{
Orientation = LayoutOrientation.Horizontal
};
2020-07-17 15:41:19 -05:00
BufferInfo.AddChild(bufferHBox);
var bufferLabel = new Label { Text = $"{Loc.GetString("chem-master-window-buffer-label")} " };
2020-07-17 15:41:19 -05:00
bufferHBox.AddChild(bufferLabel);
var bufferVol = new Label
{
Text = $"{state.BufferCurrentVolume}",
2020-07-17 15:41:19 -05:00
StyleClasses = {StyleNano.StyleClassLabelSecondaryColor}
};
bufferHBox.AddChild(bufferVol);
foreach (var reagent in state.BufferReagents)
{
var name = Loc.GetString("chem-master-window-unknown-reagent-text");
2020-07-17 15:41:19 -05:00
//Try to the prototype for the given reagent. This gives us it's name.
if (_prototypeManager.TryIndex(reagent.ReagentId, out ReagentPrototype? proto))
2020-07-17 15:41:19 -05:00
{
name = proto.Name;
}
if (proto != null)
{
BufferInfo.Children.Add(new BoxContainer
2020-07-17 15:41:19 -05:00
{
Orientation = LayoutOrientation.Horizontal,
2020-07-17 15:41:19 -05:00
//SizeFlagsHorizontal = SizeFlags.ShrinkEnd,
Children =
{
new Label {Text = $"{name}: "},
new Label
{
Text = $"{reagent.Quantity}u",
StyleClasses = {StyleNano.StyleClassLabelSecondaryColor}
},
//Padding
2021-02-21 12:38:56 +01:00
new Control {HorizontalExpand = true},
2020-07-17 15:41:19 -05:00
MakeChemButton("1", ReagentUnit.New(1), reagent.ReagentId, true, StyleBase.ButtonOpenRight),
MakeChemButton("5", ReagentUnit.New(5), reagent.ReagentId, true, StyleBase.ButtonOpenBoth),
MakeChemButton("10", ReagentUnit.New(10), reagent.ReagentId, true, StyleBase.ButtonOpenBoth),
MakeChemButton("25", ReagentUnit.New(25), reagent.ReagentId, true, StyleBase.ButtonOpenBoth),
MakeChemButton(Loc.GetString("chem-master-window-buffer-all-amount"), ReagentUnit.New(-1), reagent.ReagentId, true, StyleBase.ButtonOpenLeft),
2020-07-17 15:41:19 -05:00
}
});
}
}
}
}
public class ChemButton : Button
{
public ReagentUnit Amount { get; set; }
public bool isBuffer = true;
public string Id { get; set; }
public ChemButton(string _text, ReagentUnit _amount, string _id, bool _isBuffer, string _styleClass)
{
AddStyleClass(_styleClass);
Text = _text;
Amount = _amount;
Id = _id;
isBuffer = _isBuffer;
}
}
}