1
0

AlertPrototype.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Robust.Shared.Prototypes;
  2. using Robust.Shared.Utility;
  3. namespace Content.Shared.Alert;
  4. /// <summary>
  5. /// An alert popup with associated icon, tooltip, and other data.
  6. /// </summary>
  7. [Prototype]
  8. public sealed partial class AlertPrototype : IPrototype
  9. {
  10. /// <summary>
  11. /// Type of alert, no 2 alert prototypes should have the same one.
  12. /// </summary>
  13. [IdDataField]
  14. public string ID { get; private set; } = default!;
  15. /// <summary>
  16. /// List of icons to use for this alert. Each entry corresponds to a different severity level, starting from the
  17. /// minimum and incrementing upwards. If severities are not supported, the first entry is used.
  18. /// </summary>
  19. [DataField(required: true)]
  20. public List<SpriteSpecifier> Icons = new();
  21. /// <summary>
  22. /// An entity used for displaying the <see cref="Icons"/> in the UI control.
  23. /// </summary>
  24. [DataField]
  25. public EntProtoId AlertViewEntity = "AlertSpriteView";
  26. /// <summary>
  27. /// Name to show in tooltip window. Accepts formatting.
  28. /// </summary>
  29. [DataField]
  30. public string Name { get; private set; } = string.Empty;
  31. /// <summary>
  32. /// Description to show in tooltip window. Accepts formatting.
  33. /// </summary>
  34. [DataField]
  35. public string Description { get; private set; } = string.Empty;
  36. /// <summary>
  37. /// Category the alert belongs to. Only one alert of a given category
  38. /// can be shown at a time. If one is shown while another is already being shown,
  39. /// it will be replaced. This can be useful for categories of alerts which should naturally
  40. /// replace each other and are mutually exclusive, for example lowpressure / highpressure,
  41. /// hot / cold. If left unspecified, the alert will not replace or be replaced by any other alerts.
  42. /// </summary>
  43. [DataField]
  44. public ProtoId<AlertCategoryPrototype>? Category { get; private set; }
  45. /// <summary>
  46. /// Key which is unique w.r.t category semantics (alerts with same category have equal keys,
  47. /// alerts with no category have different keys).
  48. /// </summary>
  49. public AlertKey AlertKey => new(ID, Category);
  50. /// <summary>
  51. /// -1 (no effect) unless MaxSeverity is specified. Defaults to 1. Minimum severity level supported by this state.
  52. /// </summary>
  53. public short MinSeverity => MaxSeverity == -1 ? (short) -1 : _minSeverity;
  54. [DataField("minSeverity")] private short _minSeverity = 1;
  55. /// <summary>
  56. /// Maximum severity level supported by this state. -1 (default) indicates
  57. /// no severity levels are supported by the state.
  58. /// </summary>
  59. [DataField]
  60. public short MaxSeverity = -1;
  61. /// <summary>
  62. /// Indicates whether this state support severity levels
  63. /// </summary>
  64. public bool SupportsSeverity => MaxSeverity != -1;
  65. /// <summary>
  66. /// Event raised on the user when they click on this alert.
  67. /// Can be null.
  68. /// </summary>
  69. [DataField]
  70. public BaseAlertEvent? ClickEvent;
  71. /// <param name="severity">severity level, if supported by this alert</param>
  72. /// <returns>the icon path to the texture for the provided severity level</returns>
  73. public SpriteSpecifier GetIcon(short? severity = null)
  74. {
  75. var minIcons = SupportsSeverity
  76. ? MaxSeverity - MinSeverity
  77. : 1;
  78. if (Icons.Count < minIcons)
  79. throw new InvalidOperationException($"Insufficient number of icons given for alert {ID}");
  80. if (!SupportsSeverity)
  81. return Icons[0];
  82. if (severity == null)
  83. {
  84. throw new ArgumentException($"No severity specified but this alert ({AlertKey}) has severity.", nameof(severity));
  85. }
  86. if (severity < MinSeverity)
  87. {
  88. throw new ArgumentOutOfRangeException(nameof(severity), $"Severity below minimum severity in {AlertKey}.");
  89. }
  90. if (severity > MaxSeverity)
  91. {
  92. throw new ArgumentOutOfRangeException(nameof(severity), $"Severity above maximum severity in {AlertKey}.");
  93. }
  94. return Icons[severity.Value - _minSeverity];
  95. }
  96. }
  97. [ImplicitDataDefinitionForInheritors]
  98. public abstract partial class BaseAlertEvent : HandledEntityEventArgs
  99. {
  100. public EntityUid User;
  101. public ProtoId<AlertPrototype> AlertId;
  102. protected BaseAlertEvent(EntityUid user, ProtoId<AlertPrototype> alertId)
  103. {
  104. User = user;
  105. AlertId = alertId;
  106. }
  107. }