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

358 lines
11 KiB
C#
Raw Normal View History

using System;
using Content.Client.GameObjects;
2018-11-21 20:58:11 +01:00
using Content.Client.GameObjects.EntitySystems;
using Content.Client.Interfaces.GameObjects;
2019-07-20 16:01:01 +02:00
using Content.Client.Utility;
using Content.Shared.GameObjects.Components.Items;
2019-08-04 16:03:51 -07:00
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;
2019-08-04 16:03:51 -07:00
using Robust.Shared.Input;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
2019-07-20 16:01:01 +02:00
using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Maths;
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
{
private const int CooldownLevels = 8;
2019-07-20 16:01:01 +02:00
private const int BoxSpacing = 0;
private const int BoxSize = 64;
2018-11-21 20:58:11 +01:00
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 ILocalizationManager _loc;
[Dependency] private readonly IGameTiming _gameTiming;
2019-07-20 16:01:01 +02:00
#pragma warning restore 0649
2018-11-21 20:58:11 +01:00
private readonly Texture TextureHandLeft;
private readonly Texture TextureHandRight;
private readonly Texture TextureHandActive;
private readonly Texture[] TexturesCooldownOverlay;
2017-09-30 16:56:19 +02:00
2019-03-15 19:07:29 +01:00
private IEntity LeftHand;
private IEntity RightHand;
2019-05-11 16:05:41 +02:00
private UIBox2i _handL;
private UIBox2i _handR;
2017-09-30 16:56:19 +02:00
2019-08-14 22:04:35 +02:00
private readonly SpriteView LeftSpriteView;
private readonly SpriteView RightSpriteView;
private readonly TextureRect ActiveHandRect;
2019-03-15 19:07:29 +01:00
private readonly TextureRect CooldownCircleLeft;
private readonly TextureRect CooldownCircleRight;
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);
_handR = new UIBox2i(0, 0, BoxSize, BoxSize);
_handL = _handR.Translated((BoxSize + BoxSpacing, 0));
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");
}
2019-07-20 16:01:01 +02:00
AddChild(new TextureRect
{
MouseFilter = MouseFilterMode.Ignore,
2019-07-20 16:01:01 +02:00
Texture = TextureHandLeft,
Size = _handL.Size,
Position = _handL.TopLeft,
TextureScale = (2, 2)
});
AddChild(new TextureRect
{
MouseFilter = MouseFilterMode.Ignore,
2019-07-20 16:01:01 +02:00
Texture = TextureHandRight,
Size = _handR.Size,
Position = _handR.TopLeft,
TextureScale = (2, 2)
});
2019-07-20 16:01:01 +02:00
AddChild(ActiveHandRect = new TextureRect
{
MouseFilter = MouseFilterMode.Ignore,
2019-07-20 16:01:01 +02:00
Texture = TextureHandActive,
Size = _handL.Size,
Position = _handL.TopLeft,
TextureScale = (2, 2)
});
2019-03-15 19:07:29 +01:00
2019-07-20 16:01:01 +02:00
LeftSpriteView = new SpriteView
{
MouseFilter = MouseFilterMode.Ignore,
Scale = (2, 2)
};
2019-03-15 19:07:29 +01:00
AddChild(LeftSpriteView);
2019-05-11 16:05:41 +02:00
LeftSpriteView.Size = _handL.Size;
LeftSpriteView.Position = _handL.TopLeft;
2019-03-15 19:07:29 +01:00
2019-07-20 16:01:01 +02:00
RightSpriteView = new SpriteView
{
MouseFilter = MouseFilterMode.Ignore,
Scale = (2, 2)
};
2019-03-15 19:07:29 +01:00
AddChild(RightSpriteView);
2019-05-11 16:05:41 +02:00
RightSpriteView.Size = _handR.Size;
RightSpriteView.Position = _handR.TopLeft;
// Cooldown circles.
AddChild(CooldownCircleLeft = new TextureRect
{
MouseFilter = MouseFilterMode.Ignore,
Position = _handL.TopLeft + (8, 8),
TextureScale = (2, 2),
Visible = false,
});
AddChild(CooldownCircleRight = new TextureRect
{
MouseFilter = MouseFilterMode.Ignore,
Position = _handR.TopLeft + (8, 8),
TextureScale = (2, 2),
Visible = false
});
2017-09-30 16:56:19 +02:00
}
protected override Vector2 CalculateMinimumSize()
2017-09-30 16:56:19 +02:00
{
2019-07-20 16:01:01 +02:00
return new Vector2(BoxSize * 2 + BoxSpacing, BoxSize) * UIScale;
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;
2017-09-30 16:56:19 +02:00
var left = hands.GetEntity("left");
var right = hands.GetEntity("right");
2019-07-20 16:01:01 +02:00
ActiveHandRect.Position = hands.ActiveIndex == "left" ? _handL.TopLeft : _handR.TopLeft;
2017-09-30 16:56:19 +02:00
if (left != null)
{
2019-03-15 19:07:29 +01:00
if (left != LeftHand)
2017-09-30 16:56:19 +02:00
{
2019-03-15 19:07:29 +01:00
LeftHand = left;
if (LeftHand.TryGetComponent(out ISpriteComponent sprite))
{
LeftSpriteView.Sprite = sprite;
}
2017-09-30 16:56:19 +02:00
}
}
else
{
2019-03-15 19:07:29 +01:00
LeftHand = null;
LeftSpriteView.Sprite = null;
2017-09-30 16:56:19 +02:00
}
if (right != null)
{
2019-03-15 19:07:29 +01:00
RightHand = right;
if (RightHand.TryGetComponent(out ISpriteComponent sprite))
2017-09-30 16:56:19 +02:00
{
2019-03-15 19:07:29 +01:00
RightSpriteView.Sprite = sprite;
2017-09-30 16:56:19 +02:00
}
}
else
{
2019-03-15 19:07:29 +01:00
RightHand = null;
RightSpriteView.Sprite = null;
2017-09-30 16:56:19 +02:00
}
}
private void SendSwitchHandTo(string index)
{
if (!TryGetHands(out IHandsComponent hands))
2017-09-30 16:56:19 +02:00
return;
2017-09-30 16:56:19 +02:00
hands.SendChangeHand(index);
}
private void UseActiveHand()
{
if (!TryGetHands(out IHandsComponent hands))
return;
//Todo: remove hands interface, so weird
2018-11-21 20:58:11 +01:00
((HandsComponent) hands).UseActiveHand();
}
private void AttackByInHand(string hand)
{
if (!TryGetHands(out var hands))
return;
hands.AttackByInHand(hand);
}
protected override bool HasPoint(Vector2 point)
{
2019-05-11 16:05:41 +02:00
return _handL.Contains((Vector2i) point) || _handR.Contains((Vector2i) point);
}
2019-08-04 16:03:51 -07:00
protected override void KeyBindDown(GUIBoundKeyEventArgs args)
2017-09-30 16:56:19 +02:00
{
2019-08-04 16:03:51 -07:00
base.KeyBindDown(args);
2019-08-04 16:03:51 -07:00
if (!args.CanFocus)
{
return;
}
var leftHandContains = _handL.Contains((Vector2i) args.RelativePosition);
var rightHandContains = _handR.Contains((Vector2i) args.RelativePosition);
2018-11-21 20:58:11 +01:00
string handIndex;
if (leftHandContains)
{
handIndex = "left";
}
else if (rightHandContains)
{
handIndex = "right";
}
else
{
return;
}
2019-08-04 16:03:51 -07:00
if (args.Function == EngineKeyFunctions.Use)
2017-09-30 16:56:19 +02:00
{
2018-11-21 20:58:11 +01:00
if (!TryGetHands(out var hands))
return;
2018-11-21 20:58:11 +01:00
if (hands.ActiveIndex == handIndex)
{
UseActiveHand();
2018-11-21 20:58:11 +01:00
}
else
{
AttackByInHand(handIndex);
}
2017-09-30 16:56:19 +02:00
}
2018-11-21 20:58:11 +01:00
else if (args.Function == ContentKeyFunctions.ExamineEntity)
{
var examine = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<ExamineSystem>();
if (leftHandContains)
examine.DoExamine(LeftHand);
else if (rightHandContains)
examine.DoExamine(RightHand);
}
2019-08-04 16:03:51 -07:00
else if (args.Function == ContentKeyFunctions.MouseMiddle)
2018-11-21 20:58:11 +01:00
{
SendSwitchHandTo(handIndex);
}
2019-08-04 16:03:51 -07:00
else if (args.Function == ContentKeyFunctions.OpenContextMenu)
2017-09-30 16:56:19 +02:00
{
2018-11-21 20:58:11 +01:00
if (!TryGetHands(out var hands))
{
2018-11-21 20:58:11 +01:00
return;
}
2018-11-21 20:58:11 +01:00
var entity = hands.GetEntity(handIndex);
if (entity == null)
{
2018-11-21 20:58:11 +01:00
return;
}
2018-11-21 20:58:11 +01:00
var esm = IoCManager.Resolve<IEntitySystemManager>();
esm.GetEntitySystem<VerbSystem>()
.OpenContextMenu(entity, new ScreenCoordinates(args.PointerLocation.Position));
2017-09-30 16:56:19 +02:00
}
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
UpdateCooldown(CooldownCircleLeft, LeftHand);
UpdateCooldown(CooldownCircleRight, RightHand);
}
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);
}
2017-09-30 16:56:19 +02:00
}
}