1
0

ExamineSystemShared.Group.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using Content.Shared.Ghost;
  2. using Content.Shared.Verbs;
  3. using Robust.Shared.Utility;
  4. namespace Content.Shared.Examine
  5. {
  6. public abstract partial class ExamineSystemShared : EntitySystem
  7. {
  8. [Dependency] private readonly IComponentFactory _componentFactory = default!;
  9. public const string DefaultIconTexture = "/Textures/Interface/examine-star.png";
  10. public override void Initialize()
  11. {
  12. base.Initialize();
  13. SubscribeLocalEvent<GroupExamineComponent, GetVerbsEvent<ExamineVerb>>(OnGroupExamineVerb);
  14. _ghostQuery = GetEntityQuery<GhostComponent>();
  15. }
  16. /// <summary>
  17. /// Called when getting verbs on an object with the GroupExamine component. <br/>
  18. /// This checks if any of the ExamineGroups are relevant (has 1 or more of the relevant components on the entity)
  19. /// and if so, creates an ExamineVerb details button for the ExamineGroup.
  20. /// </summary>
  21. private void OnGroupExamineVerb(EntityUid uid, GroupExamineComponent component, GetVerbsEvent<ExamineVerb> args)
  22. {
  23. foreach (var group in component.Group)
  24. {
  25. if (!EntityHasComponent(uid, group.Components))
  26. continue;
  27. var examineVerb = new ExamineVerb()
  28. {
  29. Act = () =>
  30. {
  31. SendExamineGroup(args.User, args.Target, group);
  32. group.Entries.Clear();
  33. },
  34. Text = Loc.GetString(group.ContextText),
  35. Message = Loc.GetString(group.HoverMessage),
  36. Category = VerbCategory.Examine,
  37. Icon = group.Icon,
  38. };
  39. args.Verbs.Add(examineVerb);
  40. }
  41. }
  42. /// <summary>
  43. /// Checks if the entity <paramref name="uid"/> has any of the listed <paramref name="components"/>.
  44. /// </summary>
  45. public bool EntityHasComponent(EntityUid uid, List<string> components)
  46. {
  47. foreach (var comp in components)
  48. {
  49. if (!_componentFactory.TryGetRegistration(comp, out var componentRegistration))
  50. continue;
  51. if (!HasComp(uid, componentRegistration.Type))
  52. continue;
  53. return true;
  54. }
  55. return false;
  56. }
  57. /// <summary>
  58. /// Sends an ExamineTooltip based on the contents of <paramref name="group"/>
  59. /// </summary>
  60. public void SendExamineGroup(EntityUid user, EntityUid target, ExamineGroup group)
  61. {
  62. var message = new FormattedMessage();
  63. if (group.Title != null)
  64. {
  65. message.AddMarkupOrThrow(Loc.GetString(group.Title));
  66. message.PushNewline();
  67. }
  68. message.AddMessage(GetFormattedMessageFromExamineEntries(group.Entries));
  69. SendExamineTooltip(user, target, message, false, false);
  70. }
  71. /// <returns>A FormattedMessage based on all <paramref name="entries"/>, sorted.</returns>
  72. public static FormattedMessage GetFormattedMessageFromExamineEntries(List<ExamineEntry> entries)
  73. {
  74. var formattedMessage = new FormattedMessage();
  75. entries.Sort((a, b) => (b.Priority.CompareTo(a.Priority)));
  76. var first = true;
  77. foreach (var entry in entries)
  78. {
  79. if (!first)
  80. {
  81. formattedMessage.PushNewline();
  82. }
  83. else
  84. {
  85. first = false;
  86. }
  87. formattedMessage.AddMessage(entry.Message);
  88. }
  89. return formattedMessage;
  90. }
  91. /// <summary>
  92. /// Either sends the details to a GroupExamineComponent if it finds one, or adds a details examine verb itself.
  93. /// </summary>
  94. public void AddDetailedExamineVerb(GetVerbsEvent<ExamineVerb> verbsEvent, Component component, List<ExamineEntry> entries, string verbText, string iconTexture = DefaultIconTexture, string hoverMessage = "")
  95. {
  96. // If the entity has the GroupExamineComponent
  97. if (TryComp<GroupExamineComponent>(verbsEvent.Target, out var groupExamine))
  98. {
  99. // Make sure we have the component name as a string
  100. var componentName = _componentFactory.GetComponentName(component.GetType());
  101. foreach (var examineGroup in groupExamine.Group)
  102. {
  103. // If any of the examine groups list of components contain this componentname
  104. if (examineGroup.Components.Contains(componentName))
  105. {
  106. foreach (var entry in examineGroup.Entries)
  107. {
  108. // If any of the entries already are from your component, dont do anything else - no doubles!
  109. if (entry.Component == componentName)
  110. return;
  111. }
  112. foreach (var entry in entries)
  113. {
  114. // Otherwise, just add all information to the examine groups entries, and stop there.
  115. examineGroup.Entries.Add(entry);
  116. }
  117. return;
  118. }
  119. }
  120. }
  121. var formattedMessage = GetFormattedMessageFromExamineEntries(entries);
  122. var examineVerb = new ExamineVerb()
  123. {
  124. Act = () =>
  125. {
  126. SendExamineTooltip(verbsEvent.User, verbsEvent.Target, formattedMessage, false, false);
  127. },
  128. Text = verbText,
  129. Message = hoverMessage,
  130. Category = VerbCategory.Examine,
  131. Icon = new SpriteSpecifier.Texture(new(iconTexture)),
  132. };
  133. verbsEvent.Verbs.Add(examineVerb);
  134. }
  135. /// <summary>
  136. /// Either adds a details examine verb, or sends the details to a GroupExamineComponent if it finds one.
  137. /// </summary>
  138. public void AddDetailedExamineVerb(GetVerbsEvent<ExamineVerb> verbsEvent, Component component, ExamineEntry entry, string verbText, string iconTexture = DefaultIconTexture, string hoverMessage = "")
  139. {
  140. AddDetailedExamineVerb(verbsEvent, component, new List<ExamineEntry> { entry }, verbText, iconTexture, hoverMessage);
  141. }
  142. /// <summary>
  143. /// Either adds a details examine verb, or sends the details to a GroupExamineComponent if it finds one.
  144. /// </summary>
  145. public void AddDetailedExamineVerb(GetVerbsEvent<ExamineVerb> verbsEvent, Component component, FormattedMessage message, string verbText, string iconTexture = DefaultIconTexture, string hoverMessage = "")
  146. {
  147. var componentName = _componentFactory.GetComponentName(component.GetType());
  148. AddDetailedExamineVerb(verbsEvent, component, new ExamineEntry(componentName, 0f, message), verbText, iconTexture, hoverMessage);
  149. }
  150. }
  151. }