Inventory Input (#503)

* Create ItemSlotButton

* Refactor inventory buttons

Refactor so that KeyBind handling of inventory and hands are the same.

* Refactor InventoryInterfaceController to call ItemSlotManager with entities

This change allows HandsGUI and InventoryInterfaceController to just call ItemSlotManager.OnButtonPressed with an entity instead of a slot.

* Add CooldownCircle to ItemSlotButton

This allows Hands and Inventory to have cooldown circles on their ItemSlots.

* Refactor HandsGUI to use ItemSlots

This allows functionality and GUI to be the same between Inventory and Hands.

Added clicking empty hand to switch ActiveHand to clicked hand.

* Implement CooldownCircle in ItemSlotManager

Reorganize files
This commit is contained in:
ShadowCommander
2020-01-17 06:43:20 -08:00
committed by Pieter-Jan Briers
parent c20ba98a1e
commit d03da83fda
13 changed files with 368 additions and 323 deletions

View File

@@ -1,22 +1,14 @@
using System;
using Content.Client.GameObjects;
using Content.Client.GameObjects.EntitySystems;
using Content.Client.GameObjects;
using Content.Client.Interfaces.GameObjects;
using Content.Client.Utility;
using Content.Shared.GameObjects.Components.Items;
using Content.Shared.Input;
using Robust.Client.Graphics;
using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Client.Interfaces.ResourceManagement;
using Robust.Client.Player;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Input;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Timing;
namespace Content.Client.UserInterface
@@ -26,31 +18,19 @@ namespace Content.Client.UserInterface
private const string HandNameLeft = "left";
private const string HandNameRight = "right";
private const int CooldownLevels = 8;
#pragma warning disable 0649
[Dependency] private readonly IPlayerManager _playerManager;
[Dependency] private readonly IResourceCache _resourceCache;
[Dependency] private readonly IGameTiming _gameTiming;
[Dependency] private readonly IItemSlotManager _itemSlotManager;
#pragma warning restore 0649
private readonly Texture TextureHandLeft;
private readonly Texture TextureHandRight;
private readonly Texture TextureHandActive;
private readonly Texture[] TexturesCooldownOverlay;
private IEntity LeftHand;
private IEntity RightHand;
private readonly SpriteView LeftSpriteView;
private readonly SpriteView RightSpriteView;
private readonly TextureRect ActiveHandRect;
private readonly TextureRect CooldownCircleLeft;
private readonly TextureRect CooldownCircleRight;
private readonly Control _leftContainer;
private readonly Control _rightContainer;
private readonly ItemSlotButton _leftButton;
private readonly ItemSlotButton _rightButton;
private readonly ItemStatusPanel _rightStatusPanel;
private readonly ItemStatusPanel _leftStatusPanel;
@@ -61,91 +41,34 @@ namespace Content.Client.UserInterface
MouseFilter = MouseFilterMode.Stop;
TextureHandLeft = _resourceCache.GetTexture("/Textures/UserInterface/Inventory/hand_l.png");
TextureHandRight = _resourceCache.GetTexture("/Textures/UserInterface/Inventory/hand_r.png");
TextureHandActive = _resourceCache.GetTexture("/Textures/UserInterface/Inventory/hand_active.png");
TexturesCooldownOverlay = new Texture[CooldownLevels];
for (var i = 0; i < CooldownLevels; i++)
{
TexturesCooldownOverlay[i] =
_resourceCache.GetTexture($"/Textures/UserInterface/Inventory/cooldown-{i}.png");
}
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");
_rightStatusPanel = new ItemStatusPanel(true);
_leftStatusPanel = new ItemStatusPanel(false);
_leftContainer = new Control {MouseFilter = MouseFilterMode.Ignore};
_rightContainer = new Control {MouseFilter = MouseFilterMode.Ignore};
_leftButton = new ItemSlotButton(textureHandLeft, storageTexture);
_rightButton = new ItemSlotButton(textureHandRight, storageTexture);
var hBox = new HBoxContainer
{
SeparationOverride = 0,
Children = {_rightStatusPanel, _rightContainer, _leftContainer, _leftStatusPanel},
Children = {_rightStatusPanel, _rightButton, _leftButton, _leftStatusPanel},
MouseFilter = MouseFilterMode.Ignore
};
AddChild(hBox);
var textureLeft = new TextureRect
_leftButton.OnPressed += args => HandKeyBindDown(args.Event, HandNameLeft);
_rightButton.OnPressed += args => HandKeyBindDown(args.Event, HandNameRight);
// Active hand
_leftButton.AddChild(ActiveHandRect = new TextureRect
{
Texture = TextureHandLeft,
MouseFilter = MouseFilterMode.Ignore,
Texture = textureHandActive,
TextureScale = (2, 2)
};
textureLeft.OnKeyBindDown += args => HandKeyBindDown(args, HandNameLeft);
_leftContainer.AddChild(textureLeft);
var textureRight = new TextureRect
{
Texture = TextureHandRight,
TextureScale = (2, 2)
};
textureRight.OnKeyBindDown += args => HandKeyBindDown(args, HandNameRight);
_rightContainer.AddChild(textureRight);
_leftContainer.AddChild(ActiveHandRect = new TextureRect
{
MouseFilter = MouseFilterMode.Ignore,
Texture = TextureHandActive,
TextureScale = (2, 2)
});
LeftSpriteView = new SpriteView
{
MouseFilter = MouseFilterMode.Ignore,
Scale = (2, 2),
OverrideDirection = Direction.South
};
_leftContainer.AddChild(LeftSpriteView);
RightSpriteView = new SpriteView
{
MouseFilter = MouseFilterMode.Ignore,
Scale = (2, 2),
OverrideDirection = Direction.South
};
_rightContainer.AddChild(RightSpriteView);
// Cooldown circles.
_leftContainer.AddChild(CooldownCircleLeft = new TextureRect
{
SizeFlagsHorizontal = SizeFlags.ShrinkCenter,
SizeFlagsVertical = SizeFlags.ShrinkCenter,
MouseFilter = MouseFilterMode.Ignore,
Stretch = TextureRect.StretchMode.KeepCentered,
TextureScale = (2, 2),
Visible = false,
});
_rightContainer.AddChild(CooldownCircleRight = new TextureRect
{
SizeFlagsHorizontal = SizeFlags.ShrinkCenter,
SizeFlagsVertical = SizeFlags.ShrinkCenter,
MouseFilter = MouseFilterMode.Ignore,
Stretch = TextureRect.StretchMode.KeepCentered,
TextureScale = (2, 2),
Visible = false
});
}
@@ -178,7 +101,7 @@ namespace Content.Client.UserInterface
var right = hands.GetEntity(HandNameRight);
ActiveHandRect.Parent.RemoveChild(ActiveHandRect);
var parent = hands.ActiveIndex == HandNameLeft ? _leftContainer : _rightContainer;
var parent = hands.ActiveIndex == HandNameLeft ? _leftButton : _rightButton;
parent.AddChild(ActiveHandRect);
ActiveHandRect.SetPositionInParent(1);
@@ -189,100 +112,71 @@ namespace Content.Client.UserInterface
LeftHand = left;
if (LeftHand.TryGetComponent(out ISpriteComponent sprite))
{
LeftSpriteView.Sprite = sprite;
_leftButton.SpriteView.Sprite = sprite;
}
}
}
else
{
LeftHand = null;
LeftSpriteView.Sprite = null;
_leftButton.SpriteView.Sprite = null;
}
if (right != null)
{
RightHand = right;
if (RightHand.TryGetComponent(out ISpriteComponent sprite))
if (right != RightHand)
{
RightSpriteView.Sprite = sprite;
RightHand = right;
if (RightHand.TryGetComponent(out ISpriteComponent sprite))
{
_rightButton.SpriteView.Sprite = sprite;
}
}
}
else
{
RightHand = null;
RightSpriteView.Sprite = null;
_rightButton.SpriteView.Sprite = null;
}
}
private void SendSwitchHandTo(string index)
{
if (!TryGetHands(out IHandsComponent hands))
return;
hands.SendChangeHand(index);
}
private void UseActiveHand()
{
if (!TryGetHands(out IHandsComponent hands))
return;
//Todo: remove hands interface, so weird
((HandsComponent) hands).UseActiveHand();
}
private void AttackByInHand(string hand)
{
if (!TryGetHands(out var hands))
return;
hands.AttackByInHand(hand);
}
private void HandKeyBindDown(GUIBoundKeyEventArgs args, string handIndex)
{
if (args.Function == EngineKeyFunctions.Use)
{
if (!TryGetHands(out var hands))
return;
args.Handle();
if (!TryGetHands(out var hands))
return;
if (args.Function == ContentKeyFunctions.MouseMiddle)
{
hands.SendChangeHand(handIndex);
return;
}
var entity = hands.GetEntity(handIndex);
if (entity == null)
{
if (args.CanFocus && hands.ActiveIndex != handIndex)
{
hands.SendChangeHand(handIndex);
}
return;
}
if (_itemSlotManager.OnButtonPressed(args, entity))
return;
if (args.CanFocus)
{
if (hands.ActiveIndex == handIndex)
{
UseActiveHand();
hands.UseActiveHand();
}
else
{
AttackByInHand(handIndex);
hands.AttackByInHand(handIndex);
}
}
else if (args.Function == ContentKeyFunctions.ExamineEntity)
{
var examine = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<ExamineSystem>();
if (handIndex == HandNameLeft)
examine.DoExamine(LeftHand);
else if (handIndex == HandNameRight)
examine.DoExamine(RightHand);
}
else if (args.Function == ContentKeyFunctions.MouseMiddle)
{
SendSwitchHandTo(handIndex);
}
else if (args.Function == ContentKeyFunctions.OpenContextMenu)
{
if (!TryGetHands(out var hands))
{
return;
}
var entity = hands.GetEntity(handIndex);
if (entity == null)
{
return;
}
var esm = IoCManager.Resolve<IEntitySystemManager>();
esm.GetEntitySystem<VerbSystem>()
.OpenContextMenu(entity, new ScreenCoordinates(args.PointerLocation.Position));
return;
}
}
@@ -290,49 +184,11 @@ namespace Content.Client.UserInterface
{
base.FrameUpdate(args);
UpdateCooldown(CooldownCircleLeft, LeftHand);
UpdateCooldown(CooldownCircleRight, RightHand);
_itemSlotManager.UpdateCooldown(_leftButton, LeftHand);
_itemSlotManager.UpdateCooldown(_rightButton, RightHand);
_rightStatusPanel.Update(RightHand);
_leftStatusPanel.Update(LeftHand);
}
private void UpdateCooldown(TextureRect cooldownTexture, IEntity entity)
{
if (entity != null
&& entity.TryGetComponent(out ItemCooldownComponent cooldown)
&& cooldown.CooldownStart.HasValue
&& cooldown.CooldownEnd.HasValue)
{
var start = cooldown.CooldownStart.Value;
var end = cooldown.CooldownEnd.Value;
var length = (end - start).TotalSeconds;
var progress = (_gameTiming.CurTime - start).TotalSeconds;
var ratio = (float) (progress / length);
var textureIndex = CalculateCooldownLevel(ratio);
if (textureIndex == CooldownLevels)
{
cooldownTexture.Visible = false;
}
else
{
cooldownTexture.Visible = true;
cooldownTexture.Texture = TexturesCooldownOverlay[textureIndex];
}
}
else
{
cooldownTexture.Visible = false;
}
}
internal static int CalculateCooldownLevel(float cooldownValue)
{
var val = cooldownValue.Clamp(0, 1);
val *= CooldownLevels;
return (int) Math.Floor(val);
}
}
}