2021-10-05 14:29:03 +11:00
|
|
|
using System.Linq;
|
2019-11-28 08:38:23 -05:00
|
|
|
using System.Threading;
|
2019-04-09 17:33:53 +02:00
|
|
|
using System.Threading.Tasks;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Examine;
|
2019-04-09 17:33:53 +02:00
|
|
|
using Content.Shared.Input;
|
2021-10-05 14:29:03 +11:00
|
|
|
using Content.Shared.Verbs;
|
2019-04-09 17:33:53 +02:00
|
|
|
using JetBrains.Annotations;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Client.GameObjects;
|
2021-11-22 09:40:09 +13:00
|
|
|
using Robust.Client.Graphics;
|
2019-05-16 14:41:43 -07:00
|
|
|
using Robust.Client.Player;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Client.UserInterface;
|
|
|
|
|
using Robust.Client.UserInterface.Controls;
|
|
|
|
|
using Robust.Shared.GameObjects;
|
2020-05-31 14:32:05 -07:00
|
|
|
using Robust.Shared.Input.Binding;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Shared.IoC;
|
2021-10-05 14:29:03 +11:00
|
|
|
using Robust.Shared.Localization;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Shared.Map;
|
|
|
|
|
using Robust.Shared.Maths;
|
|
|
|
|
using Robust.Shared.Players;
|
2019-11-28 08:38:23 -05:00
|
|
|
using Robust.Shared.Utility;
|
2021-07-21 13:31:00 +02:00
|
|
|
using static Content.Shared.Interaction.SharedInteractionSystem;
|
2021-07-18 18:39:31 +02:00
|
|
|
using static Robust.Client.UserInterface.Controls.BoxContainer;
|
2019-04-09 17:33:53 +02:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Client.Examine
|
2019-04-09 17:33:53 +02:00
|
|
|
{
|
|
|
|
|
[UsedImplicitly]
|
2019-07-19 10:45:04 +02:00
|
|
|
internal sealed class ExamineSystem : ExamineSystemShared
|
2019-04-09 17:33:53 +02:00
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
|
|
|
|
|
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
2021-11-22 09:40:09 +13:00
|
|
|
[Dependency] private readonly IEyeManager _eyeManager = default!;
|
2019-04-09 17:33:53 +02:00
|
|
|
|
2020-08-24 14:10:28 +02:00
|
|
|
public const string StyleClassEntityTooltip = "entity-tooltip";
|
2019-04-09 17:33:53 +02:00
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
private EntityUid _examinedEntity;
|
|
|
|
|
private EntityUid _playerEntity;
|
2021-03-10 14:48:29 +01:00
|
|
|
private Popup? _examineTooltipOpen;
|
|
|
|
|
private CancellationTokenSource? _requestCancelTokenSource;
|
2019-04-09 17:33:53 +02:00
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
2021-12-30 03:12:04 +01:00
|
|
|
UpdatesOutsidePrediction = true;
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
SubscribeLocalEvent<GetOtherVerbsEvent>(AddExamineVerb);
|
|
|
|
|
|
2020-05-31 14:32:05 -07:00
|
|
|
CommandBinds.Builder
|
2021-12-30 03:12:04 +01:00
|
|
|
.Bind(ContentKeyFunctions.ExamineEntity, new PointerInputCmdHandler(HandleExamine, outsidePrediction: true))
|
2020-05-31 14:32:05 -07:00
|
|
|
.Register<ExamineSystem>();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-21 13:31:00 +02:00
|
|
|
public override void Update(float frameTime)
|
|
|
|
|
{
|
2021-12-05 18:09:01 +01:00
|
|
|
if (_examineTooltipOpen is not {Visible: true}) return;
|
|
|
|
|
if (!_examinedEntity.Valid || !_playerEntity.Valid) return;
|
2021-07-21 13:31:00 +02:00
|
|
|
|
2021-11-22 09:40:09 +13:00
|
|
|
if (!CanExamine(_playerEntity, _examinedEntity))
|
2021-07-21 13:31:00 +02:00
|
|
|
CloseTooltip();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-31 14:32:05 -07:00
|
|
|
public override void Shutdown()
|
|
|
|
|
{
|
|
|
|
|
CommandBinds.Unregister<ExamineSystem>();
|
|
|
|
|
base.Shutdown();
|
2019-04-09 17:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
public override bool CanExamine(EntityUid examiner, MapCoordinates target, Ignored? predicate = null)
|
2021-11-22 09:40:09 +13:00
|
|
|
{
|
|
|
|
|
var b = _eyeManager.GetWorldViewbounds();
|
|
|
|
|
if (!b.Contains(target.Position))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return base.CanExamine(examiner, target, predicate);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-30 03:12:04 +01:00
|
|
|
private bool HandleExamine(in PointerInputCmdHandler.PointerInputCmdArgs args)
|
2019-04-09 17:33:53 +02:00
|
|
|
{
|
2021-12-30 03:12:04 +01:00
|
|
|
if (!args.EntityUid.IsValid() || !EntityManager.EntityExists(args.EntityUid))
|
2019-04-09 17:33:53 +02:00
|
|
|
{
|
2019-09-17 16:08:45 -07:00
|
|
|
return false;
|
2019-04-09 17:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
_playerEntity = _playerManager.LocalPlayer?.ControlledEntity ?? default;
|
2019-07-19 10:45:04 +02:00
|
|
|
|
2021-12-30 03:12:04 +01:00
|
|
|
if (_playerEntity == default || !CanExamine(_playerEntity, args.EntityUid))
|
2019-07-19 10:45:04 +02:00
|
|
|
{
|
2019-09-17 16:08:45 -07:00
|
|
|
return false;
|
2019-07-19 10:45:04 +02:00
|
|
|
}
|
2019-05-16 14:41:43 -07:00
|
|
|
|
2021-12-30 03:12:04 +01:00
|
|
|
DoExamine(args.EntityUid);
|
2019-09-17 16:08:45 -07:00
|
|
|
return true;
|
2019-04-09 17:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
private void AddExamineVerb(GetOtherVerbsEvent args)
|
|
|
|
|
{
|
|
|
|
|
if (!CanExamine(args.User, args.Target))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Verb verb = new();
|
|
|
|
|
verb.Act = () => DoExamine(args.Target) ;
|
|
|
|
|
verb.Text = Loc.GetString("examine-verb-name");
|
|
|
|
|
verb.IconTexture = "/Textures/Interface/VerbIcons/examine.svg.192dpi.png";
|
2021-10-28 18:21:19 +13:00
|
|
|
verb.ClientExclusive = true;
|
2021-10-05 14:29:03 +11:00
|
|
|
args.Verbs.Add(verb);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
public async void DoExamine(EntityUid entity)
|
2019-04-09 17:33:53 +02:00
|
|
|
{
|
2021-07-21 13:31:00 +02:00
|
|
|
// Close any examine tooltip that might already be opened
|
2019-04-09 17:33:53 +02:00
|
|
|
CloseTooltip();
|
|
|
|
|
|
2021-11-07 23:33:52 +13:00
|
|
|
// cache entity for Update function
|
|
|
|
|
_examinedEntity = entity;
|
|
|
|
|
|
2021-07-21 13:31:00 +02:00
|
|
|
const float minWidth = 300;
|
2020-12-04 08:24:22 -06:00
|
|
|
var popupPos = _userInterfaceManager.MousePositionScaled;
|
2019-11-23 15:57:44 -05:00
|
|
|
|
2019-04-09 17:33:53 +02:00
|
|
|
// Actually open the tooltip.
|
2021-07-21 13:31:00 +02:00
|
|
|
_examineTooltipOpen = new Popup { MaxWidth = 400 };
|
2020-01-25 17:28:39 +01:00
|
|
|
_userInterfaceManager.ModalRoot.AddChild(_examineTooltipOpen);
|
2019-04-09 17:33:53 +02:00
|
|
|
var panel = new PanelContainer();
|
|
|
|
|
panel.AddStyleClass(StyleClassEntityTooltip);
|
2019-05-16 14:41:43 -07:00
|
|
|
panel.ModulateSelfOverride = Color.LightGray.WithAlpha(0.90f);
|
2019-04-09 17:33:53 +02:00
|
|
|
_examineTooltipOpen.AddChild(panel);
|
2021-07-21 13:31:00 +02:00
|
|
|
|
2021-07-18 18:39:31 +02:00
|
|
|
var vBox = new BoxContainer
|
|
|
|
|
{
|
|
|
|
|
Orientation = LayoutOrientation.Vertical
|
|
|
|
|
};
|
2019-04-09 17:33:53 +02:00
|
|
|
panel.AddChild(vBox);
|
2021-07-21 13:31:00 +02:00
|
|
|
|
2021-07-18 18:39:31 +02:00
|
|
|
var hBox = new BoxContainer
|
|
|
|
|
{
|
|
|
|
|
Orientation = LayoutOrientation.Horizontal,
|
|
|
|
|
SeparationOverride = 5
|
|
|
|
|
};
|
2019-04-09 17:33:53 +02:00
|
|
|
vBox.AddChild(hBox);
|
2021-07-21 13:31:00 +02:00
|
|
|
|
2021-12-08 12:09:43 +01:00
|
|
|
if (EntityManager.TryGetComponent(entity, out ISpriteComponent? sprite))
|
2019-04-09 17:33:53 +02:00
|
|
|
{
|
2021-07-21 13:31:00 +02:00
|
|
|
hBox.AddChild(new SpriteView { Sprite = sprite, OverrideDirection = Direction.South });
|
2019-04-09 17:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hBox.AddChild(new Label
|
|
|
|
|
{
|
2021-12-08 12:09:43 +01:00
|
|
|
Text = EntityManager.GetComponent<MetaDataComponent>(entity).EntityName,
|
2021-02-21 12:38:56 +01:00
|
|
|
HorizontalExpand = true,
|
2019-04-09 17:33:53 +02:00
|
|
|
});
|
|
|
|
|
|
2021-02-21 12:38:56 +01:00
|
|
|
panel.Measure(Vector2.Infinity);
|
|
|
|
|
var size = Vector2.ComponentMax((minWidth, 0), panel.DesiredSize);
|
2019-11-23 15:57:44 -05:00
|
|
|
|
2021-05-02 20:48:00 +02:00
|
|
|
_examineTooltipOpen.Open(UIBox2.FromDimensions(popupPos.Position, size));
|
2019-04-09 17:33:53 +02:00
|
|
|
|
2020-08-01 17:37:12 +02:00
|
|
|
FormattedMessage message;
|
2021-12-03 15:53:09 +01:00
|
|
|
if (entity.IsClientSide())
|
2019-04-09 17:33:53 +02:00
|
|
|
{
|
2021-03-10 14:48:29 +01:00
|
|
|
message = GetExamineText(entity, _playerManager.LocalPlayer?.ControlledEntity);
|
2019-04-09 17:33:53 +02:00
|
|
|
}
|
2020-08-01 17:37:12 +02:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Ask server for extra examine info.
|
2021-12-03 15:53:09 +01:00
|
|
|
RaiseNetworkEvent(new ExamineSystemMessages.RequestExamineInfoMessage(entity));
|
2019-04-09 17:33:53 +02:00
|
|
|
|
2020-08-01 17:37:12 +02:00
|
|
|
ExamineSystemMessages.ExamineInfoResponseMessage response;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_requestCancelTokenSource = new CancellationTokenSource();
|
|
|
|
|
response =
|
|
|
|
|
await AwaitNetworkEvent<ExamineSystemMessages.ExamineInfoResponseMessage>(_requestCancelTokenSource
|
|
|
|
|
.Token);
|
|
|
|
|
}
|
|
|
|
|
catch (TaskCanceledException)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_requestCancelTokenSource = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message = response.Message;
|
2019-04-09 17:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-20 12:42:42 +01:00
|
|
|
foreach (var msg in message.Tags.OfType<FormattedMessage.TagText>())
|
2019-11-28 08:38:23 -05:00
|
|
|
{
|
2021-12-20 12:42:42 +01:00
|
|
|
if (string.IsNullOrWhiteSpace(msg.Text)) continue;
|
2021-07-21 13:31:00 +02:00
|
|
|
|
|
|
|
|
var richLabel = new RichTextLabel();
|
|
|
|
|
richLabel.SetMessage(message);
|
|
|
|
|
vBox.AddChild(richLabel);
|
|
|
|
|
break;
|
2019-11-28 08:38:23 -05:00
|
|
|
}
|
2019-04-09 17:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-21 13:31:00 +02:00
|
|
|
private void CloseTooltip()
|
2019-04-09 17:33:53 +02:00
|
|
|
{
|
|
|
|
|
if (_examineTooltipOpen != null)
|
|
|
|
|
{
|
|
|
|
|
_examineTooltipOpen.Dispose();
|
|
|
|
|
_examineTooltipOpen = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_requestCancelTokenSource != null)
|
|
|
|
|
{
|
|
|
|
|
_requestCancelTokenSource.Cancel();
|
|
|
|
|
_requestCancelTokenSource = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|