Implement StorageButton on HandsGUI and click empty hand to swap… (#517)

Also moved duplicate sprite view code to ItemSlotManager
This commit is contained in:
ShadowCommander
2020-01-17 18:41:47 -08:00
committed by Pieter-Jan Briers
parent 9a76c70b37
commit e0aaab56e3
9 changed files with 106 additions and 60 deletions

View File

@@ -24,8 +24,8 @@ namespace Content.Client.UserInterface
[Dependency] private readonly IItemSlotManager _itemSlotManager;
#pragma warning restore 0649
private IEntity LeftHand;
private IEntity RightHand;
private IEntity _leftHand;
private IEntity _rightHand;
private readonly TextureRect ActiveHandRect;
@@ -61,7 +61,9 @@ namespace Content.Client.UserInterface
AddChild(hBox);
_leftButton.OnPressed += args => HandKeyBindDown(args.Event, HandNameLeft);
_leftButton.OnStoragePressed += args => _OnStoragePressed(args.Event, HandNameLeft);
_rightButton.OnPressed += args => HandKeyBindDown(args.Event, HandNameRight);
_rightButton.OnStoragePressed += args => _OnStoragePressed(args.Event, HandNameRight);
// Active hand
_leftButton.AddChild(ActiveHandRect = new TextureRect
@@ -105,38 +107,16 @@ namespace Content.Client.UserInterface
parent.AddChild(ActiveHandRect);
ActiveHandRect.SetPositionInParent(1);
if (left != null)
if (left != _leftHand)
{
if (left != LeftHand)
{
LeftHand = left;
if (LeftHand.TryGetComponent(out ISpriteComponent sprite))
{
_leftButton.SpriteView.Sprite = sprite;
}
}
}
else
{
LeftHand = null;
_leftButton.SpriteView.Sprite = null;
_leftHand = left;
_itemSlotManager.SetItemSlot(_leftButton, _leftHand);
}
if (right != null)
if (right != _rightHand)
{
if (right != RightHand)
{
RightHand = right;
if (RightHand.TryGetComponent(out ISpriteComponent sprite))
{
_rightButton.SpriteView.Sprite = sprite;
}
}
}
else
{
RightHand = null;
_rightButton.SpriteView.Sprite = null;
_rightHand = right;
_itemSlotManager.SetItemSlot(_rightButton, _rightHand);
}
}
@@ -180,15 +160,24 @@ namespace Content.Client.UserInterface
}
}
private void _OnStoragePressed(GUIBoundKeyEventArgs args, string handIndex)
{
if (!args.CanFocus)
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);
_itemSlotManager.UpdateCooldown(_leftButton, _leftHand);
_itemSlotManager.UpdateCooldown(_rightButton, _rightHand);
_rightStatusPanel.Update(RightHand);
_leftStatusPanel.Update(LeftHand);
_rightStatusPanel.Update(_rightHand);
_leftStatusPanel.Update(_leftHand);
}
}
}

View File

@@ -9,5 +9,6 @@ namespace Content.Client.UserInterface
void Initialize();
bool OnButtonPressed(GUIBoundKeyEventArgs args, IEntity item);
void UpdateCooldown(ItemSlotButton cooldownTexture, IEntity entity);
bool SetItemSlot(ItemSlotButton button, IEntity entity);
}
}

View File

@@ -1,11 +1,13 @@
using System;
using Content.Client.GameObjects;
using Content.Client.GameObjects.Components.Storage;
using Content.Client.GameObjects.EntitySystems;
using Content.Client.Utility;
using Content.Shared.GameObjects.Components.Items;
using Content.Shared.Input;
using Robust.Client.GameObjects.EntitySystems;
using Robust.Client.Graphics;
using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Client.Interfaces.Graphics.ClientEye;
using Robust.Client.Interfaces.Input;
using Robust.Client.Interfaces.ResourceManagement;
@@ -33,17 +35,34 @@ namespace Content.Client.UserInterface
private const int CooldownLevels = 8;
private readonly Texture[] TexturesCooldownOverlay = new Texture[CooldownLevels];
private readonly Texture[] _texturesCooldownOverlay = new Texture[CooldownLevels];
public void Initialize()
{
for (var i = 0; i < CooldownLevels; i++)
{
TexturesCooldownOverlay[i] =
_texturesCooldownOverlay[i] =
_resourceCache.GetTexture($"/Textures/UserInterface/Inventory/cooldown-{i}.png");
}
}
public bool SetItemSlot(ItemSlotButton button, IEntity entity)
{
if (entity == null)
{
button.SpriteView.Sprite = null;
button.StorageButton.Visible = false;
}
else
{
if (!entity.TryGetComponent(out ISpriteComponent sprite))
return false;
button.SpriteView.Sprite = sprite;
button.StorageButton.Visible = entity.HasComponent<ClientStorageComponent>();
}
return true;
}
public bool OnButtonPressed(GUIBoundKeyEventArgs args, IEntity item)
{
args.Handle();
@@ -107,7 +126,7 @@ namespace Content.Client.UserInterface
else
{
cooldownTexture.Visible = true;
cooldownTexture.Texture = TexturesCooldownOverlay[textureIndex];
cooldownTexture.Texture = _texturesCooldownOverlay[textureIndex];
}
}
else