Files
crystall-punk-14/Content.Client/UserInterface/HandsGui.cs

185 lines
6.1 KiB
C#
Raw Normal View History

using Content.Client.GameObjects;
using Content.Client.Interfaces.GameObjects;
2019-07-20 16:01:01 +02:00
using Content.Client.Utility;
2019-08-04 16:03:51 -07:00
using Content.Shared.Input;
using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Client.Interfaces.ResourceManagement;
using Robust.Client.Player;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
2020-05-10 22:48:14 -07:00
using Robust.Shared.Input;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Timing;
2017-09-30 16:56:19 +02:00
namespace Content.Client.UserInterface
{
public class HandsGui : Control
2017-09-30 16:56:19 +02:00
{
2020-01-09 00:27:52 +01:00
private const string HandNameLeft = "left";
private const string HandNameRight = "right";
2019-07-20 16:01:01 +02:00
#pragma warning disable 0649
[Dependency] private readonly IPlayerManager _playerManager;
[Dependency] private readonly IResourceCache _resourceCache;
[Dependency] private readonly IItemSlotManager _itemSlotManager;
2019-07-20 16:01:01 +02:00
#pragma warning restore 0649
2018-11-21 20:58:11 +01:00
private IEntity _leftHand;
private IEntity _rightHand;
2017-09-30 16:56:19 +02:00
2019-08-14 22:04:35 +02:00
private readonly TextureRect ActiveHandRect;
2019-03-15 19:07:29 +01:00
private readonly ItemSlotButton _leftButton;
private readonly ItemSlotButton _rightButton;
2020-01-09 00:27:52 +01:00
private readonly ItemStatusPanel _rightStatusPanel;
private readonly ItemStatusPanel _leftStatusPanel;
2019-08-14 22:04:35 +02:00
public HandsGui()
2017-09-30 16:56:19 +02:00
{
2019-07-20 16:01:01 +02:00
IoCManager.InjectDependencies(this);
var textureHandLeft = _resourceCache.GetTexture("/Textures/UserInterface/Inventory/hand_l.png");
var textureHandRight = _resourceCache.GetTexture("/Textures/UserInterface/Inventory/hand_r.png");
var textureHandActive = _resourceCache.GetTexture("/Textures/UserInterface/Inventory/hand_active.png");
var storageTexture = _resourceCache.GetTexture("/Textures/UserInterface/Inventory/back.png");
2020-01-09 00:27:52 +01:00
_rightStatusPanel = new ItemStatusPanel(true);
_leftStatusPanel = new ItemStatusPanel(false);
_leftButton = new ItemSlotButton(textureHandLeft, storageTexture);
_rightButton = new ItemSlotButton(textureHandRight, storageTexture);
var hBox = new HBoxContainer
{
SeparationOverride = 0,
Children = {_rightStatusPanel, _rightButton, _leftButton, _leftStatusPanel}
};
AddChild(hBox);
2020-05-05 23:59:31 +02:00
_leftButton.OnPressed += args => HandKeyBindDown(args, HandNameLeft);
_leftButton.OnStoragePressed += args => _OnStoragePressed(args, HandNameLeft);
_rightButton.OnPressed += args => HandKeyBindDown(args, HandNameRight);
_rightButton.OnStoragePressed += args => _OnStoragePressed(args, HandNameRight);
2020-01-09 00:27:52 +01:00
// Active hand
_leftButton.AddChild(ActiveHandRect = new TextureRect
2019-07-20 16:01:01 +02:00
{
Texture = textureHandActive,
2019-07-20 16:01:01 +02:00
TextureScale = (2, 2)
});
2017-09-30 16:56:19 +02:00
}
/// <summary>
/// Gets the hands component controling this gui, returns true if successful and false if failure
/// </summary>
/// <param name="hands"></param>
/// <returns></returns>
private bool TryGetHands(out IHandsComponent hands)
2017-09-30 16:56:19 +02:00
{
2019-07-20 16:01:01 +02:00
hands = default;
2017-09-30 16:56:19 +02:00
2019-07-20 16:01:01 +02:00
var entity = _playerManager?.LocalPlayer?.ControlledEntity;
return entity != null && entity.TryGetComponent(out hands);
}
public void UpdateHandIcons()
{
if (Parent == null)
{
return;
}
2018-11-21 20:58:11 +01:00
UpdateDraw();
2019-03-15 19:07:29 +01:00
if (!TryGetHands(out var hands))
return;
2020-01-09 00:27:52 +01:00
var left = hands.GetEntity(HandNameLeft);
var right = hands.GetEntity(HandNameRight);
2017-09-30 16:56:19 +02:00
ActiveHandRect.Parent.RemoveChild(ActiveHandRect);
var parent = hands.ActiveIndex == HandNameLeft ? _leftButton : _rightButton;
parent.AddChild(ActiveHandRect);
2020-01-09 00:27:52 +01:00
ActiveHandRect.SetPositionInParent(1);
2019-07-20 16:01:01 +02:00
if (left != _leftHand)
2017-09-30 16:56:19 +02:00
{
_leftHand = left;
_itemSlotManager.SetItemSlot(_leftButton, _leftHand);
2017-09-30 16:56:19 +02:00
}
if (right != _rightHand)
2017-09-30 16:56:19 +02:00
{
_rightHand = right;
_itemSlotManager.SetItemSlot(_rightButton, _rightHand);
2017-09-30 16:56:19 +02:00
}
}
private void HandKeyBindDown(GUIBoundKeyEventArgs args, string handIndex)
{
2018-11-21 20:58:11 +01:00
if (!TryGetHands(out var hands))
return;
if (args.Function == ContentKeyFunctions.MouseMiddle)
2017-09-30 16:56:19 +02:00
{
hands.SendChangeHand(handIndex);
args.Handle();
return;
}
var entity = hands.GetEntity(handIndex);
if (entity == null)
{
2020-05-10 22:48:14 -07:00
if (args.Function == EngineKeyFunctions.UIClick && hands.ActiveIndex != handIndex)
2018-11-21 20:58:11 +01:00
{
hands.SendChangeHand(handIndex);
args.Handle();
2018-11-21 20:58:11 +01:00
}
return;
2017-09-30 16:56:19 +02:00
}
if (_itemSlotManager.OnButtonPressed(args, entity))
{
args.Handle();
return;
}
2020-05-10 22:48:14 -07:00
if (args.Function == EngineKeyFunctions.UIClick)
2017-09-30 16:56:19 +02:00
{
if (hands.ActiveIndex == handIndex)
{
hands.UseActiveHand();
}
else
{
hands.AttackByInHand(handIndex);
}
args.Handle();
return;
2017-09-30 16:56:19 +02:00
}
}
private void _OnStoragePressed(GUIBoundKeyEventArgs args, string handIndex)
{
2020-05-10 22:48:14 -07:00
if (args.Function != EngineKeyFunctions.UIClick)
return;
if (!TryGetHands(out var hands))
return;
hands.ActivateItemInHand(handIndex);
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
_itemSlotManager.UpdateCooldown(_leftButton, _leftHand);
_itemSlotManager.UpdateCooldown(_rightButton, _rightHand);
2020-01-09 00:27:52 +01:00
_rightStatusPanel.Update(_rightHand);
_leftStatusPanel.Update(_leftHand);
}
2017-09-30 16:56:19 +02:00
}
}