BorgMenu.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using Content.Client.Stylesheets;
  2. using Content.Client.UserInterface.Controls;
  3. using Content.Shared.NameIdentifier;
  4. using Content.Shared.Preferences;
  5. using Content.Shared.Silicons.Borgs;
  6. using Content.Shared.Silicons.Borgs.Components;
  7. using Robust.Client.AutoGenerated;
  8. using Robust.Client.UserInterface.Controls;
  9. using Robust.Client.UserInterface.XAML;
  10. using Robust.Shared.Timing;
  11. namespace Content.Client.Silicons.Borgs;
  12. [GenerateTypedNameReferences]
  13. public sealed partial class BorgMenu : FancyWindow
  14. {
  15. [Dependency] private readonly IEntityManager _entity = default!;
  16. public Action? BrainButtonPressed;
  17. public Action? EjectBatteryButtonPressed;
  18. public Action<string>? NameChanged;
  19. public Action<EntityUid>? RemoveModuleButtonPressed;
  20. public float AccumulatedTime;
  21. private string _lastValidName;
  22. private List<EntityUid> _modules = new();
  23. public EntityUid Entity;
  24. public BorgMenu()
  25. {
  26. RobustXamlLoader.Load(this);
  27. IoCManager.InjectDependencies(this);
  28. _lastValidName = NameLineEdit.Text;
  29. EjectBatteryButton.OnPressed += _ => EjectBatteryButtonPressed?.Invoke();
  30. BrainButton.OnPressed += _ => BrainButtonPressed?.Invoke();
  31. NameLineEdit.OnTextChanged += OnNameChanged;
  32. NameLineEdit.OnTextEntered += OnNameEntered;
  33. NameLineEdit.OnFocusExit += OnNameFocusExit;
  34. UpdateBrainButton();
  35. }
  36. public void SetEntity(EntityUid entity)
  37. {
  38. Entity = entity;
  39. BorgSprite.SetEntity(entity);
  40. if (_entity.TryGetComponent<NameIdentifierComponent>(Entity, out var nameIdentifierComponent))
  41. {
  42. NameIdentifierLabel.Visible = true;
  43. NameIdentifierLabel.Text = nameIdentifierComponent.FullIdentifier;
  44. var fullName = _entity.GetComponent<MetaDataComponent>(Entity).EntityName;
  45. var name = fullName.Substring(0, fullName.Length - nameIdentifierComponent.FullIdentifier.Length - 1);
  46. NameLineEdit.Text = name;
  47. }
  48. else
  49. {
  50. NameIdentifierLabel.Visible = false;
  51. NameLineEdit.Text = _entity.GetComponent<MetaDataComponent>(Entity).EntityName;
  52. }
  53. }
  54. protected override void FrameUpdate(FrameEventArgs args)
  55. {
  56. base.FrameUpdate(args);
  57. AccumulatedTime += args.DeltaSeconds;
  58. BorgSprite.OverrideDirection = (Direction) ((int) AccumulatedTime % 4 * 2);
  59. }
  60. public void UpdateState(BorgBuiState state)
  61. {
  62. EjectBatteryButton.Disabled = !state.HasBattery;
  63. ChargeBar.Value = state.ChargePercent;
  64. ChargeLabel.Text = Loc.GetString("borg-ui-charge-label",
  65. ("charge", (int) MathF.Round(state.ChargePercent * 100)));
  66. UpdateBrainButton();
  67. UpdateModulePanel();
  68. }
  69. private void UpdateBrainButton()
  70. {
  71. if (_entity.TryGetComponent(Entity, out BorgChassisComponent? chassis) && chassis.BrainEntity is { } brain)
  72. {
  73. BrainButton.Text = _entity.GetComponent<MetaDataComponent>(brain).EntityName;
  74. BrainView.Visible = true;
  75. BrainView.SetEntity(brain);
  76. BrainButton.Disabled = false;
  77. BrainButton.AddStyleClass(StyleBase.ButtonOpenLeft);
  78. }
  79. else
  80. {
  81. BrainButton.Text = Loc.GetString("borg-ui-no-brain");
  82. BrainButton.Disabled = true;
  83. BrainView.Visible = false;
  84. BrainButton.RemoveStyleClass(StyleBase.ButtonOpenLeft);
  85. }
  86. }
  87. private void UpdateModulePanel()
  88. {
  89. if (!_entity.TryGetComponent(Entity, out BorgChassisComponent? chassis))
  90. return;
  91. ModuleCounter.Text = Loc.GetString("borg-ui-module-counter",
  92. ("actual", chassis.ModuleCount),
  93. ("max", chassis.MaxModules));
  94. if (chassis.ModuleContainer.Count == _modules.Count)
  95. {
  96. var isSame = true;
  97. foreach (var module in chassis.ModuleContainer.ContainedEntities)
  98. {
  99. if (_modules.Contains(module))
  100. continue;
  101. isSame = false;
  102. break;
  103. }
  104. if (isSame)
  105. return;
  106. }
  107. ModuleContainer.Children.Clear();
  108. _modules.Clear();
  109. foreach (var module in chassis.ModuleContainer.ContainedEntities)
  110. {
  111. var moduleComponent = _entity.GetComponent<BorgModuleComponent>(module);
  112. var control = new BorgModuleControl(module, _entity, !moduleComponent.DefaultModule);
  113. control.RemoveButtonPressed += () =>
  114. {
  115. RemoveModuleButtonPressed?.Invoke(module);
  116. };
  117. ModuleContainer.AddChild(control);
  118. _modules.Add(module);
  119. }
  120. }
  121. private void OnNameChanged(LineEdit.LineEditEventArgs obj)
  122. {
  123. if (obj.Text.Length == 0 ||
  124. string.IsNullOrWhiteSpace(obj.Text) ||
  125. string.IsNullOrEmpty(obj.Text))
  126. {
  127. return;
  128. }
  129. if (obj.Text.Length > HumanoidCharacterProfile.MaxNameLength)
  130. {
  131. obj.Control.Text = obj.Text.Substring(0, HumanoidCharacterProfile.MaxNameLength);
  132. }
  133. _lastValidName = obj.Control.Text;
  134. obj.Control.Text = _lastValidName;
  135. }
  136. private void OnNameEntered(LineEdit.LineEditEventArgs obj)
  137. {
  138. NameChanged?.Invoke(_lastValidName);
  139. }
  140. private void OnNameFocusExit(LineEdit.LineEditEventArgs obj)
  141. {
  142. if (obj.Text.Length > HumanoidCharacterProfile.MaxNameLength ||
  143. obj.Text.Length == 0 ||
  144. string.IsNullOrWhiteSpace(obj.Text) ||
  145. string.IsNullOrEmpty(obj.Text))
  146. {
  147. obj.Control.Text = _lastValidName.Trim();
  148. }
  149. NameChanged?.Invoke(_lastValidName);
  150. }
  151. }