CharacterUIController.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System.Linq;
  2. using Content.Client.CharacterInfo;
  3. using Content.Client.Gameplay;
  4. using Content.Client.Stylesheets;
  5. using Content.Client.UserInterface.Controls;
  6. using Content.Client.UserInterface.Systems.Character.Controls;
  7. using Content.Client.UserInterface.Systems.Character.Windows;
  8. using Content.Client.UserInterface.Systems.Objectives.Controls;
  9. using Content.Shared.Input;
  10. using Content.Shared.Mind;
  11. using Content.Shared.Mind.Components;
  12. using Content.Shared.Roles;
  13. using JetBrains.Annotations;
  14. using Robust.Client.GameObjects;
  15. using Robust.Client.Player;
  16. using Robust.Client.UserInterface;
  17. using Robust.Client.UserInterface.Controllers;
  18. using Robust.Client.UserInterface.Controls;
  19. using Robust.Shared.Input.Binding;
  20. using Robust.Shared.Prototypes;
  21. using Robust.Shared.Utility;
  22. using static Content.Client.CharacterInfo.CharacterInfoSystem;
  23. using static Robust.Client.UserInterface.Controls.BaseButton;
  24. namespace Content.Client.UserInterface.Systems.Character;
  25. [UsedImplicitly]
  26. public sealed class CharacterUIController : UIController, IOnStateEntered<GameplayState>, IOnStateExited<GameplayState>, IOnSystemChanged<CharacterInfoSystem>
  27. {
  28. [Dependency] private readonly IEntityManager _ent = default!;
  29. [Dependency] private readonly ILogManager _logMan = default!;
  30. [Dependency] private readonly IPlayerManager _player = default!;
  31. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  32. [UISystemDependency] private readonly CharacterInfoSystem _characterInfo = default!;
  33. [UISystemDependency] private readonly SpriteSystem _sprite = default!;
  34. private ISawmill _sawmill = default!;
  35. public override void Initialize()
  36. {
  37. base.Initialize();
  38. _sawmill = _logMan.GetSawmill("character");
  39. SubscribeNetworkEvent<MindRoleTypeChangedEvent>(OnRoleTypeChanged);
  40. }
  41. private CharacterWindow? _window;
  42. private MenuButton? CharacterButton => UIManager.GetActiveUIWidgetOrNull<MenuBar.Widgets.GameTopMenuBar>()?.CharacterButton;
  43. public void OnStateEntered(GameplayState state)
  44. {
  45. DebugTools.Assert(_window == null);
  46. _window = UIManager.CreateWindow<CharacterWindow>();
  47. LayoutContainer.SetAnchorPreset(_window, LayoutContainer.LayoutPreset.CenterTop);
  48. _window.OnClose += DeactivateButton;
  49. _window.OnOpen += ActivateButton;
  50. CommandBinds.Builder
  51. .Bind(ContentKeyFunctions.OpenCharacterMenu,
  52. InputCmdHandler.FromDelegate(_ => ToggleWindow()))
  53. .Register<CharacterUIController>();
  54. }
  55. public void OnStateExited(GameplayState state)
  56. {
  57. if (_window != null)
  58. {
  59. _window.Close();
  60. _window = null;
  61. }
  62. CommandBinds.Unregister<CharacterUIController>();
  63. }
  64. public void OnSystemLoaded(CharacterInfoSystem system)
  65. {
  66. system.OnCharacterUpdate += CharacterUpdated;
  67. _player.LocalPlayerDetached += CharacterDetached;
  68. }
  69. public void OnSystemUnloaded(CharacterInfoSystem system)
  70. {
  71. system.OnCharacterUpdate -= CharacterUpdated;
  72. _player.LocalPlayerDetached -= CharacterDetached;
  73. }
  74. public void UnloadButton()
  75. {
  76. if (CharacterButton == null)
  77. {
  78. return;
  79. }
  80. CharacterButton.OnPressed -= CharacterButtonPressed;
  81. }
  82. public void LoadButton()
  83. {
  84. if (CharacterButton == null)
  85. {
  86. return;
  87. }
  88. CharacterButton.OnPressed += CharacterButtonPressed;
  89. }
  90. private void DeactivateButton()
  91. {
  92. if (CharacterButton == null)
  93. {
  94. return;
  95. }
  96. CharacterButton.Pressed = false;
  97. }
  98. private void ActivateButton()
  99. {
  100. if (CharacterButton == null)
  101. {
  102. return;
  103. }
  104. CharacterButton.Pressed = true;
  105. }
  106. private void CharacterUpdated(CharacterData data)
  107. {
  108. if (_window == null)
  109. {
  110. return;
  111. }
  112. var (entity, job, objectives, briefing, entityName) = data;
  113. _window.SpriteView.SetEntity(entity);
  114. UpdateRoleType();
  115. _window.NameLabel.Text = entityName;
  116. _window.SubText.Text = job;
  117. _window.Objectives.RemoveAllChildren();
  118. _window.ObjectivesLabel.Visible = objectives.Any();
  119. foreach (var (groupId, conditions) in objectives)
  120. {
  121. var objectiveControl = new CharacterObjectiveControl
  122. {
  123. Orientation = BoxContainer.LayoutOrientation.Vertical,
  124. Modulate = Color.Gray
  125. };
  126. var objectiveText = new FormattedMessage();
  127. objectiveText.TryAddMarkup(groupId, out _);
  128. var objectiveLabel = new RichTextLabel
  129. {
  130. StyleClasses = { StyleNano.StyleClassTooltipActionTitle }
  131. };
  132. objectiveLabel.SetMessage(objectiveText);
  133. objectiveControl.AddChild(objectiveLabel);
  134. foreach (var condition in conditions)
  135. {
  136. var conditionControl = new ObjectiveConditionsControl();
  137. conditionControl.ProgressTexture.Texture = _sprite.Frame0(condition.Icon);
  138. conditionControl.ProgressTexture.Progress = condition.Progress;
  139. var titleMessage = new FormattedMessage();
  140. var descriptionMessage = new FormattedMessage();
  141. titleMessage.AddText(condition.Title);
  142. descriptionMessage.AddText(condition.Description);
  143. conditionControl.Title.SetMessage(titleMessage);
  144. conditionControl.Description.SetMessage(descriptionMessage);
  145. objectiveControl.AddChild(conditionControl);
  146. }
  147. _window.Objectives.AddChild(objectiveControl);
  148. }
  149. if (briefing != null)
  150. {
  151. var briefingControl = new ObjectiveBriefingControl();
  152. var text = new FormattedMessage();
  153. text.PushColor(Color.Yellow);
  154. text.AddText(briefing);
  155. briefingControl.Label.SetMessage(text);
  156. _window.Objectives.AddChild(briefingControl);
  157. }
  158. var controls = _characterInfo.GetCharacterInfoControls(entity);
  159. foreach (var control in controls)
  160. {
  161. _window.Objectives.AddChild(control);
  162. }
  163. _window.RolePlaceholder.Visible = briefing == null && !controls.Any() && !objectives.Any();
  164. }
  165. private void OnRoleTypeChanged(MindRoleTypeChangedEvent ev, EntitySessionEventArgs _)
  166. {
  167. UpdateRoleType();
  168. }
  169. private void UpdateRoleType()
  170. {
  171. if (_window == null || !_window.IsOpen)
  172. return;
  173. if (!_ent.TryGetComponent<MindContainerComponent>(_player.LocalEntity, out var container)
  174. || container.Mind is null)
  175. return;
  176. if (!_ent.TryGetComponent<MindComponent>(container.Mind.Value, out var mind))
  177. return;
  178. var roleText = Loc.GetString("role-type-crew-aligned-name");
  179. var color = Color.White;
  180. if (_prototypeManager.TryIndex(mind.RoleType, out var proto))
  181. {
  182. roleText = Loc.GetString(proto.Name);
  183. color = proto.Color;
  184. }
  185. else
  186. _sawmill.Error($"{_player.LocalEntity} has invalid Role Type '{mind.RoleType}'. Displaying '{roleText}' instead");
  187. _window.RoleType.Text = roleText;
  188. _window.RoleType.FontColorOverride = color;
  189. }
  190. private void CharacterDetached(EntityUid uid)
  191. {
  192. CloseWindow();
  193. }
  194. private void CharacterButtonPressed(ButtonEventArgs args)
  195. {
  196. ToggleWindow();
  197. }
  198. private void CloseWindow()
  199. {
  200. _window?.Close();
  201. }
  202. private void ToggleWindow()
  203. {
  204. if (_window == null)
  205. return;
  206. CharacterButton?.SetClickPressed(!_window.IsOpen);
  207. if (_window.IsOpen)
  208. {
  209. CloseWindow();
  210. }
  211. else
  212. {
  213. _characterInfo.RequestCharacterInfo();
  214. _window.Open();
  215. }
  216. }
  217. }