2021-07-31 03:14:00 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-06-21 02:21:20 -07:00
|
|
|
using Content.Client.Examine;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Client.Items.UI;
|
|
|
|
|
using Content.Client.Storage;
|
|
|
|
|
using Content.Client.Verbs;
|
|
|
|
|
using Content.Shared.Cooldown;
|
2021-07-31 03:14:00 +02:00
|
|
|
using Content.Shared.Hands.Components;
|
2020-01-17 06:43:20 -08:00
|
|
|
using Content.Shared.Input;
|
2021-08-22 03:20:18 +10:00
|
|
|
using Content.Shared.Interaction;
|
2020-07-26 07:25:38 -05:00
|
|
|
using Robust.Client.GameObjects;
|
2020-01-17 06:43:20 -08:00
|
|
|
using Robust.Client.UserInterface;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
2020-01-17 06:43:20 -08:00
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Map;
|
|
|
|
|
using Robust.Shared.Maths;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Timing;
|
2020-01-17 06:43:20 -08:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Client.Items.Managers
|
2020-01-17 06:43:20 -08:00
|
|
|
{
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class ItemSlotManager : IItemSlotManager
|
2020-01-17 06:43:20 -08:00
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
|
|
|
|
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
|
|
|
|
|
[Dependency] private readonly IEntityManager _entityManager = default!;
|
2021-07-31 03:14:00 +02:00
|
|
|
|
|
|
|
|
private readonly HashSet<EntityUid> _highlightEntities = new();
|
|
|
|
|
|
|
|
|
|
public event Action<EntitySlotHighlightedEventArgs>? EntityHighlightedUpdated;
|
2020-01-17 06:43:20 -08:00
|
|
|
|
2021-12-30 22:56:10 +01:00
|
|
|
public bool SetItemSlot(ItemSlotButton button, EntityUid? entity)
|
2020-01-17 18:41:47 -08:00
|
|
|
{
|
2021-12-30 22:56:10 +01:00
|
|
|
if (entity == null)
|
2020-01-17 18:41:47 -08:00
|
|
|
{
|
|
|
|
|
button.SpriteView.Sprite = null;
|
|
|
|
|
button.StorageButton.Visible = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-07-31 03:14:00 +02:00
|
|
|
ISpriteComponent? sprite;
|
2021-12-05 18:09:01 +01:00
|
|
|
if (_entityManager.TryGetComponent(entity, out HandVirtualItemComponent? virtPull)
|
2021-09-28 13:35:29 +02:00
|
|
|
&& _entityManager.TryGetComponent(virtPull.BlockingEntity, out ISpriteComponent pulledSprite))
|
2021-07-31 03:14:00 +02:00
|
|
|
{
|
|
|
|
|
sprite = pulledSprite;
|
|
|
|
|
}
|
2021-12-05 18:09:01 +01:00
|
|
|
else if (!_entityManager.TryGetComponent(entity, out sprite))
|
2021-07-31 03:14:00 +02:00
|
|
|
{
|
2020-01-17 18:41:47 -08:00
|
|
|
return false;
|
2021-07-31 03:14:00 +02:00
|
|
|
}
|
2020-07-26 17:57:48 +02:00
|
|
|
|
|
|
|
|
button.ClearHover();
|
2020-01-17 18:41:47 -08:00
|
|
|
button.SpriteView.Sprite = sprite;
|
2021-12-05 18:09:01 +01:00
|
|
|
button.StorageButton.Visible = _entityManager.HasComponent<ClientStorageComponent>(entity);
|
2020-01-17 18:41:47 -08:00
|
|
|
}
|
2021-07-31 03:14:00 +02:00
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
button.Entity = entity ?? default;
|
2021-07-31 03:14:00 +02:00
|
|
|
|
|
|
|
|
// im lazy
|
|
|
|
|
button.UpdateSlotHighlighted();
|
2020-01-17 18:41:47 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
public bool OnButtonPressed(GUIBoundKeyEventArgs args, EntityUid? item)
|
2020-01-17 06:43:20 -08:00
|
|
|
{
|
2022-01-05 17:53:08 +13:00
|
|
|
if (item == null)
|
2020-01-17 06:43:20 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (args.Function == ContentKeyFunctions.ExamineEntity)
|
|
|
|
|
{
|
|
|
|
|
_entitySystemManager.GetEntitySystem<ExamineSystem>()
|
2022-01-05 17:53:08 +13:00
|
|
|
.DoExamine(item.Value);
|
2020-01-17 06:43:20 -08:00
|
|
|
}
|
|
|
|
|
else if (args.Function == ContentKeyFunctions.OpenContextMenu)
|
|
|
|
|
{
|
2022-01-05 17:53:08 +13:00
|
|
|
_entitySystemManager.GetEntitySystem<VerbSystem>().VerbMenu.OpenVerbMenu(item.Value);
|
2020-01-17 06:43:20 -08:00
|
|
|
}
|
|
|
|
|
else if (args.Function == ContentKeyFunctions.ActivateItemInWorld)
|
|
|
|
|
{
|
2022-01-05 17:53:08 +13:00
|
|
|
_entityManager.EntityNetManager?.SendSystemNetworkMessage(new InteractInventorySlotEvent(item.Value, altInteract: false));
|
2021-08-22 03:20:18 +10:00
|
|
|
}
|
|
|
|
|
else if (args.Function == ContentKeyFunctions.AltActivateItemInWorld)
|
|
|
|
|
{
|
2022-01-05 17:53:08 +13:00
|
|
|
_entityManager.RaisePredictiveEvent(new InteractInventorySlotEvent(item.Value, altInteract: true));
|
2020-01-17 06:43:20 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-05-11 11:19:41 +02:00
|
|
|
args.Handle();
|
2020-01-17 06:43:20 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
public void UpdateCooldown(ItemSlotButton? button, EntityUid? entity)
|
2020-01-17 06:43:20 -08:00
|
|
|
{
|
2021-03-10 14:48:29 +01:00
|
|
|
var cooldownDisplay = button?.CooldownDisplay;
|
2020-01-17 06:43:20 -08:00
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
if (cooldownDisplay == null)
|
2020-01-17 06:43:20 -08:00
|
|
|
{
|
2021-03-10 14:48:29 +01:00
|
|
|
return;
|
2020-01-17 06:43:20 -08:00
|
|
|
}
|
2021-03-10 14:48:29 +01:00
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
if (entity == null || _entityManager.Deleted(entity) ||
|
2021-12-05 18:09:01 +01:00
|
|
|
!_entityManager.TryGetComponent(entity, out ItemCooldownComponent? cooldown) ||
|
2021-03-10 14:48:29 +01:00
|
|
|
!cooldown.CooldownStart.HasValue ||
|
|
|
|
|
!cooldown.CooldownEnd.HasValue)
|
2020-01-17 06:43:20 -08:00
|
|
|
{
|
2020-05-23 11:26:59 +02:00
|
|
|
cooldownDisplay.Visible = false;
|
2021-03-10 14:48:29 +01:00
|
|
|
return;
|
2020-01-17 06:43:20 -08:00
|
|
|
}
|
2021-03-10 14:48:29 +01:00
|
|
|
|
|
|
|
|
var start = cooldown.CooldownStart.Value;
|
|
|
|
|
var end = cooldown.CooldownEnd.Value;
|
|
|
|
|
|
|
|
|
|
var length = (end - start).TotalSeconds;
|
|
|
|
|
var progress = (_gameTiming.CurTime - start).TotalSeconds / length;
|
|
|
|
|
var ratio = (progress <= 1 ? (1 - progress) : (_gameTiming.CurTime - end).TotalSeconds * -5);
|
|
|
|
|
|
|
|
|
|
cooldownDisplay.Progress = MathHelper.Clamp((float) ratio, -1, 1);
|
|
|
|
|
cooldownDisplay.Visible = ratio > -1f;
|
2020-01-17 06:43:20 -08:00
|
|
|
}
|
2020-07-26 07:25:38 -05:00
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
public void HoverInSlot(ItemSlotButton button, EntityUid? entity, bool fits)
|
2020-07-26 07:25:38 -05:00
|
|
|
{
|
2022-01-05 17:53:08 +13:00
|
|
|
if (entity == null || !button.MouseIsHovering)
|
2020-07-26 07:25:38 -05:00
|
|
|
{
|
2020-07-26 17:57:48 +02:00
|
|
|
button.ClearHover();
|
2020-07-26 07:25:38 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
if (!_entityManager.HasComponent<SpriteComponent>(entity))
|
2020-07-26 07:25:38 -05:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set green / red overlay at 50% transparency
|
|
|
|
|
var hoverEntity = _entityManager.SpawnEntity("hoverentity", MapCoordinates.Nullspace);
|
2021-12-05 18:09:01 +01:00
|
|
|
var hoverSprite = _entityManager.GetComponent<SpriteComponent>(hoverEntity);
|
2022-01-05 17:53:08 +13:00
|
|
|
hoverSprite.CopyFrom(_entityManager.GetComponent<SpriteComponent>(entity.Value));
|
2020-07-26 07:25:38 -05:00
|
|
|
hoverSprite.Color = fits ? new Color(0, 255, 0, 127) : new Color(255, 0, 0, 127);
|
|
|
|
|
|
2020-07-26 17:57:48 +02:00
|
|
|
button.HoverSpriteView.Sprite = hoverSprite;
|
2020-07-26 07:25:38 -05:00
|
|
|
}
|
2021-07-31 03:14:00 +02:00
|
|
|
|
2021-12-30 22:56:10 +01:00
|
|
|
public bool IsHighlighted(EntityUid? uid)
|
2021-07-31 03:14:00 +02:00
|
|
|
{
|
2021-12-30 22:56:10 +01:00
|
|
|
if (uid == null) return false;
|
|
|
|
|
return _highlightEntities.Contains(uid.Value);
|
2021-07-31 03:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HighlightEntity(EntityUid uid)
|
|
|
|
|
{
|
|
|
|
|
if (!_highlightEntities.Add(uid))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
EntityHighlightedUpdated?.Invoke(new EntitySlotHighlightedEventArgs(uid, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnHighlightEntity(EntityUid uid)
|
|
|
|
|
{
|
|
|
|
|
if (!_highlightEntities.Remove(uid))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
EntityHighlightedUpdated?.Invoke(new EntitySlotHighlightedEventArgs(uid, false));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public readonly struct EntitySlotHighlightedEventArgs
|
|
|
|
|
{
|
|
|
|
|
public EntitySlotHighlightedEventArgs(EntityUid entity, bool newHighlighted)
|
|
|
|
|
{
|
|
|
|
|
Entity = entity;
|
|
|
|
|
NewHighlighted = newHighlighted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EntityUid Entity { get; }
|
|
|
|
|
public bool NewHighlighted { get; }
|
2020-01-17 06:43:20 -08:00
|
|
|
}
|
|
|
|
|
}
|