mirror of
https://github.com/wega-team/ss14-wega.git
synced 2026-02-14 19:30:01 +01:00
* unify * cleanup and merge conflicts * floating points --------- Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
200 lines
6.3 KiB
C#
200 lines
6.3 KiB
C#
using Content.Client.Stylesheets;
|
|
using Content.Client.UserInterface.Controls;
|
|
using Content.Shared.CCVar;
|
|
using Content.Shared.NameIdentifier;
|
|
using Content.Shared.NameModifier.EntitySystems;
|
|
using Content.Shared.Power.EntitySystems;
|
|
using Content.Shared.PowerCell;
|
|
using Content.Shared.Silicons.Borgs.Components;
|
|
using Robust.Client.AutoGenerated;
|
|
using Robust.Client.UserInterface.Controls;
|
|
using Robust.Client.UserInterface.XAML;
|
|
using Robust.Shared.Configuration;
|
|
using Robust.Shared.Timing;
|
|
|
|
namespace Content.Client.Silicons.Borgs;
|
|
|
|
[GenerateTypedNameReferences]
|
|
public sealed partial class BorgMenu : FancyWindow
|
|
{
|
|
[Dependency] private readonly IConfigurationManager _cfgManager = default!;
|
|
[Dependency] private readonly IEntityManager _entity = default!;
|
|
private readonly NameModifierSystem _nameModifier;
|
|
private readonly PowerCellSystem _powerCell;
|
|
private readonly SharedBatterySystem _battery;
|
|
|
|
public Action? BrainButtonPressed;
|
|
public Action? EjectBatteryButtonPressed;
|
|
public Action<string>? NameChanged;
|
|
public Action<EntityUid>? RemoveModuleButtonPressed;
|
|
|
|
public float AccumulatedTime;
|
|
private string _lastValidName;
|
|
private List<EntityUid> _modules = new();
|
|
|
|
// CCVar.
|
|
private int _maxNameLength;
|
|
|
|
public EntityUid Entity;
|
|
|
|
public BorgMenu()
|
|
{
|
|
RobustXamlLoader.Load(this);
|
|
IoCManager.InjectDependencies(this);
|
|
|
|
_nameModifier = _entity.System<NameModifierSystem>();
|
|
_powerCell = _entity.System<PowerCellSystem>();
|
|
_battery = _entity.System<SharedBatterySystem>();
|
|
|
|
_maxNameLength = _cfgManager.GetCVar(CCVars.MaxNameLength);
|
|
|
|
_lastValidName = NameLineEdit.Text;
|
|
|
|
EjectBatteryButton.OnPressed += _ => EjectBatteryButtonPressed?.Invoke();
|
|
BrainButton.OnPressed += _ => BrainButtonPressed?.Invoke();
|
|
|
|
NameLineEdit.OnTextChanged += OnNameChanged;
|
|
NameLineEdit.OnTextEntered += OnNameEntered;
|
|
NameLineEdit.OnFocusExit += OnNameFocusExit;
|
|
}
|
|
|
|
public void SetEntity(EntityUid entity)
|
|
{
|
|
Entity = entity;
|
|
BorgSprite.SetEntity(entity);
|
|
|
|
if (_entity.TryGetComponent<NameIdentifierComponent>(Entity, out var nameIdentifierComponent))
|
|
{
|
|
NameIdentifierLabel.Visible = true;
|
|
NameIdentifierLabel.Text = nameIdentifierComponent.FullIdentifier;
|
|
|
|
NameLineEdit.Text = _nameModifier.GetBaseName(entity);
|
|
}
|
|
else
|
|
{
|
|
NameIdentifierLabel.Visible = false;
|
|
NameLineEdit.Text = _entity.GetComponent<MetaDataComponent>(Entity).EntityName;
|
|
}
|
|
|
|
UpdateBatteryButton();
|
|
UpdateBrainButton();
|
|
UpdateModulePanel();
|
|
}
|
|
|
|
protected override void FrameUpdate(FrameEventArgs args)
|
|
{
|
|
base.FrameUpdate(args);
|
|
|
|
AccumulatedTime += args.DeltaSeconds;
|
|
BorgSprite.OverrideDirection = (Direction)((int)AccumulatedTime % 4 * 2);
|
|
|
|
var chargeFraction = 0f;
|
|
|
|
if (_powerCell.TryGetBatteryFromSlot(Entity, out var battery))
|
|
chargeFraction = _battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge;
|
|
|
|
ChargeBar.Value = chargeFraction;
|
|
ChargeLabel.Text = Loc.GetString("borg-ui-charge-label",
|
|
("charge", (int)MathF.Round(chargeFraction * 100)));
|
|
}
|
|
|
|
public void UpdateBatteryButton()
|
|
{
|
|
EjectBatteryButton.Disabled = !_powerCell.HasBattery(Entity);
|
|
}
|
|
|
|
public void UpdateBrainButton()
|
|
{
|
|
if (_entity.TryGetComponent(Entity, out BorgChassisComponent? chassis) && chassis.BrainEntity is { } brain)
|
|
{
|
|
BrainButton.Text = _entity.GetComponent<MetaDataComponent>(brain).EntityName;
|
|
BrainView.Visible = true;
|
|
BrainView.SetEntity(brain);
|
|
BrainButton.Disabled = false;
|
|
BrainButton.AddStyleClass(StyleClass.ButtonOpenLeft);
|
|
}
|
|
else
|
|
{
|
|
BrainButton.Text = Loc.GetString("borg-ui-no-brain");
|
|
BrainButton.Disabled = true;
|
|
BrainView.Visible = false;
|
|
BrainButton.RemoveStyleClass(StyleClass.ButtonOpenLeft);
|
|
}
|
|
}
|
|
|
|
public void UpdateModulePanel()
|
|
{
|
|
if (!_entity.TryGetComponent(Entity, out BorgChassisComponent? chassis))
|
|
return;
|
|
|
|
ModuleCounter.Text = Loc.GetString("borg-ui-module-counter",
|
|
("actual", chassis.ModuleCount),
|
|
("max", chassis.MaxModules));
|
|
|
|
if (chassis.ModuleContainer.Count == _modules.Count)
|
|
{
|
|
var isSame = true;
|
|
foreach (var module in chassis.ModuleContainer.ContainedEntities)
|
|
{
|
|
if (_modules.Contains(module))
|
|
continue;
|
|
isSame = false;
|
|
break;
|
|
}
|
|
|
|
if (isSame)
|
|
return;
|
|
}
|
|
|
|
ModuleContainer.Children.Clear();
|
|
_modules.Clear();
|
|
foreach (var module in chassis.ModuleContainer.ContainedEntities)
|
|
{
|
|
var moduleComponent = _entity.GetComponent<BorgModuleComponent>(module);
|
|
var control = new BorgModuleControl(module, _entity, !moduleComponent.DefaultModule);
|
|
control.RemoveButtonPressed += () =>
|
|
{
|
|
RemoveModuleButtonPressed?.Invoke(module);
|
|
};
|
|
ModuleContainer.AddChild(control);
|
|
_modules.Add(module);
|
|
}
|
|
}
|
|
|
|
private void OnNameChanged(LineEdit.LineEditEventArgs obj)
|
|
{
|
|
if (obj.Text.Length == 0 ||
|
|
string.IsNullOrWhiteSpace(obj.Text) ||
|
|
string.IsNullOrEmpty(obj.Text))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (obj.Text.Length > _maxNameLength)
|
|
{
|
|
obj.Control.Text = obj.Text.Substring(0, _maxNameLength);
|
|
}
|
|
|
|
_lastValidName = obj.Control.Text;
|
|
obj.Control.Text = _lastValidName;
|
|
}
|
|
|
|
private void OnNameEntered(LineEdit.LineEditEventArgs obj)
|
|
{
|
|
NameChanged?.Invoke(_lastValidName);
|
|
}
|
|
|
|
private void OnNameFocusExit(LineEdit.LineEditEventArgs obj)
|
|
{
|
|
if (obj.Text.Length > _maxNameLength ||
|
|
obj.Text.Length == 0 ||
|
|
string.IsNullOrWhiteSpace(obj.Text) ||
|
|
string.IsNullOrEmpty(obj.Text))
|
|
{
|
|
obj.Control.Text = _lastValidName.Trim();
|
|
}
|
|
|
|
NameChanged?.Invoke(_lastValidName);
|
|
}
|
|
}
|