Improve ExaminedEvent to handle newlines for you with helper methods.

This commit is contained in:
Pieter-Jan Briers
2021-09-15 16:58:15 +02:00
parent 9c7b061d13
commit cd6c2bb373
13 changed files with 76 additions and 38 deletions

View File

@@ -65,7 +65,7 @@ namespace Content.Client.Construction
{
if (component.Prototype == null) return;
args.Message.AddMarkup(Loc.GetString(
args.PushMarkup(Loc.GetString(
"construction-ghost-examine-message",
("name", component.Prototype.Name)));

View File

@@ -58,10 +58,9 @@ namespace Content.Server.Construction
{
if (component.Target != null)
{
args.Message.AddMarkup(
Loc.GetString(
"construction-component-to-create-header",
("targetName", component.Target.Name)) + "\n");
args.PushMarkup(Loc.GetString(
"construction-component-to-create-header",
("targetName", component.Target.Name)));
}
if (component.Edge == null && component.TargetNextEdge != null)

View File

@@ -59,10 +59,8 @@ namespace Content.Server.Dice
private void OnExamined(EntityUid uid, DiceComponent dice, ExaminedEvent args)
{
//No details check, since the sprite updates to show the side.
args.Message.PushNewline();
args.Message.AddMarkup(Loc.GetString("dice-component-on-examine-message-part-1", ("sidesAmount", dice.Sides)));
args.Message.PushNewline();
args.Message.AddMarkup(Loc.GetString("dice-component-on-examine-message-part-2", ("currentSide", dice.CurrentSide)));
args.PushMarkup(Loc.GetString("dice-component-on-examine-message-part-1", ("sidesAmount", dice.Sides)));
args.PushMarkup(Loc.GetString("dice-component-on-examine-message-part-2", ("currentSide", dice.CurrentSide)));
}
}
}

View File

@@ -169,15 +169,13 @@ namespace Content.Server.Flash
{
if (!comp.HasUses)
{
args.Message.AddText("\n");
args.Message.AddText(Loc.GetString("flash-component-examine-empty"));
args.PushText(Loc.GetString("flash-component-examine-empty"));
return;
}
if (args.IsInDetailsRange)
{
args.Message.AddText("\n");
args.Message.AddMarkup(
args.PushMarkup(
Loc.GetString(
"flash-component-examine-detail-count",
("count", comp.Uses),

View File

@@ -32,7 +32,7 @@ namespace Content.Server.Fluids
{
if (ComponentManager.TryGetComponent<SlipperyComponent>(uid, out var slippery) && slippery.Slippery)
{
args.Message.AddText(Loc.GetString("puddle-component-examine-is-slipper-text"));
args.PushText(Loc.GetString("puddle-component-examine-is-slipper-text"));
}
}

View File

@@ -86,7 +86,7 @@ namespace Content.Server.Ghost
? Loc.GetString("comp-ghost-examine-time-minutes", ("minutes", timeSinceDeath.Minutes))
: Loc.GetString("comp-ghost-examine-time-seconds", ("seconds", timeSinceDeath.Seconds));
args.Message.AddMarkup(deathTimeInfo);
args.PushMarkup(deathTimeInfo);
}
private void OnMindRemovedMessage(EntityUid uid, GhostComponent component, MindRemovedMessage args)

View File

@@ -192,7 +192,7 @@ namespace Content.Server.Hands
{
foreach (var inhand in component.GetAllHeldItems())
{
args.Message.AddText($"\n{Loc.GetString("comp-hands-examine", ("user", component.Owner), ("item", inhand.Owner))}");
args.PushText(Loc.GetString("comp-hands-examine", ("user", component.Owner), ("item", inhand.Owner)));
}
}

View File

@@ -52,11 +52,10 @@ namespace Content.Server.Lock
private void OnExamined(EntityUid eUI, LockComponent lockComp, ExaminedEvent args)
{
args.Message.AddText("\n");
args.Message.AddText(Loc.GetString(lockComp.Locked
? "lock-comp-on-examined-is-locked"
: "lock-comp-on-examined-is-unlocked",
("entityName", lockComp.Owner.Name)));
args.PushText(Loc.GetString(lockComp.Locked
? "lock-comp-on-examined-is-locked"
: "lock-comp-on-examined-is-unlocked",
("entityName", lockComp.Owner.Name)));
}
public void DoLock(LockComponent lockComp, ActivateInWorldEvent args)
@@ -72,7 +71,7 @@ namespace Content.Server.Lock
{
SoundSystem.Play(Filter.Pvs(lockComp.Owner), lockComp.LockSound.GetSound(), lockComp.Owner, AudioParams.Default.WithVolume(-5));
}
if (lockComp.Owner.TryGetComponent(out AppearanceComponent? appearanceComp))
{
appearanceComp.SetData(StorageVisuals.Locked, true);
@@ -96,7 +95,7 @@ namespace Content.Server.Lock
{
SoundSystem.Play(Filter.Pvs(lockComp.Owner), lockComp.UnlockSound.GetSound(), lockComp.Owner, AudioParams.Default.WithVolume(-5));
}
if (lockComp.Owner.TryGetComponent(out AppearanceComponent? appearanceComp))
{
appearanceComp.SetData(StorageVisuals.Locked, false);

View File

@@ -108,11 +108,10 @@ namespace Content.Server.Stunnable
private void OnExamined(EntityUid uid, StunbatonComponent comp, ExaminedEvent args)
{
args.Message.AddText("\n");
var msg = comp.Activated
? Loc.GetString("comp-stunbaton-examined-on")
: Loc.GetString("comp-stunbaton-examined-off");
args.Message.AddMarkup(msg);
args.PushMarkup(msg);
}
private void StunEntity(IEntity entity, StunbatonComponent comp)

View File

@@ -28,16 +28,16 @@ namespace Content.Server.Tools
{
if (component.WelderLit)
{
args.Message.AddMarkup(Loc.GetString("welder-component-on-examine-welder-lit-message") + "\n");
args.PushMarkup(Loc.GetString("welder-component-on-examine-welder-lit-message"));
}
else
{
args.Message.AddText(Loc.GetString("welder-component-on-examine-welder-not-lit-message") + "\n");
args.PushMarkup(Loc.GetString("welder-component-on-examine-welder-not-lit-message"));
}
if (args.IsInDetailsRange)
{
args.Message.AddMarkup(Loc.GetString("welder-component-on-examine-detailed-message",
args.PushMarkup(Loc.GetString("welder-component-on-examine-detailed-message",
("colorName", component.Fuel < component.FuelCapacity / 4f ? "darkorange" : "orange"),
("fuelLeft", Math.Round(component.Fuel)),
("fuelCapacity", component.FuelCapacity)));

View File

@@ -69,7 +69,7 @@ namespace Content.Shared.Chemistry.EntitySystems
if (solutionHolder.Contents.Count == 0)
{
args.Message.AddText(Loc.GetString("shared-solution-container-component-on-examine-empty-container"));
args.PushText(Loc.GetString("shared-solution-container-component-on-examine-empty-container"));
return;
}
@@ -86,7 +86,7 @@ namespace Content.Shared.Chemistry.EntitySystems
.ToHexNoAlpha(); //TODO: If the chem has a dark color, the examine text becomes black on a black background, which is unreadable.
var messageString = "shared-solution-container-component-on-examine-main-text";
args.Message.AddMarkup(Loc.GetString(messageString,
args.PushMarkup(Loc.GetString(messageString,
("color", colorHex),
("wordedAmount", Loc.GetString(solutionHolder.Contents.Count == 1
? "shared-solution-container-component-on-examine-worded-amount-one-reagent"

View File

@@ -189,13 +189,15 @@ namespace Content.Shared.Examine
message.PushColor(Color.DarkGray);
// Raise the event and let things that subscribe to it change the message...
RaiseLocalEvent(entity.Uid, new ExaminedEvent(message, entity, examiner, IsInDetailsRange(examiner, entity)));
var isInDetailsRange = IsInDetailsRange(examiner, entity);
var examinedEvent = new ExaminedEvent(message, entity, examiner, isInDetailsRange, doNewline);
RaiseLocalEvent(entity.Uid, examinedEvent);
//Add component statuses from components that report one
foreach (var examineComponent in entity.GetAllComponents<IExamine>())
{
var subMessage = new FormattedMessage();
examineComponent.Examine(subMessage, IsInDetailsRange(examiner, entity));
examineComponent.Examine(subMessage, isInDetailsRange);
if (subMessage.Tags.Count == 0)
continue;
@@ -214,15 +216,17 @@ namespace Content.Shared.Examine
/// <summary>
/// Raised when an entity is examined.
/// You have to manually add a newline at the start, and perform cleanup (popping state) at the end.
/// </summary>
public class ExaminedEvent : EntityEventArgs
{
/// <summary>
/// The message that will be displayed as the examine text.
/// Use the methods it exposes to change it, and don't forget to add a newline at the start!
/// Input/Output parameter.
/// For most use cases, you probably want to use <see cref="PushMarkup"/> and similar instead to modify this,
/// since it handles newlines and such correctly.
/// </summary>
/// <seealso cref="PushMessage"/>
/// <seealso cref="PushMarkup"/>
/// <seealso cref="PushText"/>
public FormattedMessage Message { get; }
/// <summary>
@@ -240,12 +244,54 @@ namespace Content.Shared.Examine
/// </summary>
public bool IsInDetailsRange { get; }
public ExaminedEvent(FormattedMessage message, IEntity examined, IEntity examiner, bool isInDetailsRange)
private bool _doNewLine;
public ExaminedEvent(FormattedMessage message, IEntity examined, IEntity examiner, bool isInDetailsRange, bool doNewLine)
{
Message = message;
Examined = examined;
Examiner = examiner;
IsInDetailsRange = isInDetailsRange;
_doNewLine = doNewLine;
}
/// <summary>
/// Push another message into this examine result, on its own line.
/// </summary>
/// <seealso cref="PushMarkup"/>
/// <seealso cref="PushText"/>
public void PushMessage(FormattedMessage message)
{
if (message.Tags.Count == 0)
return;
if (_doNewLine)
Message.AddText("\n");
Message.AddMessage(message);
_doNewLine = true;
}
/// <summary>
/// Push another message parsed from markup into this examine result, on its own line.
/// </summary>
/// <seealso cref="PushText"/>
/// <seealso cref="PushMessage"/>
public void PushMarkup(string markup)
{
PushMessage(FormattedMessage.FromMarkup(markup));
}
/// <summary>
/// Push another message containing raw text into this examine result, on its own line.
/// </summary>
/// <seealso cref="PushMarkup"/>
/// <seealso cref="PushMessage"/>
public void PushText(string text)
{
var msg = new FormattedMessage();
msg.AddText(text);
PushMessage(msg);
}
}
}

View File

@@ -100,8 +100,7 @@ namespace Content.Shared.Stacks
if (!args.IsInDetailsRange)
return;
args.Message.AddText("\n");
args.Message.AddMarkup(
args.PushMarkup(
Loc.GetString("comp-stack-examine-detail-count",
("count", component.Count),
("markupCountColor", "lightgray")