Replaces cooldown circle (#956)

This commit is contained in:
Tomeno
2020-05-23 11:26:59 +02:00
committed by GitHub
parent cad59d2cb4
commit af0ec2aeb9
6 changed files with 46 additions and 54 deletions

View File

@@ -247,7 +247,6 @@ namespace Content.Client
IoCManager.Resolve<IChatManager>().Initialize();
IoCManager.Resolve<ISandboxManager>().Initialize();
IoCManager.Resolve<IClientPreferencesManager>().Initialize();
IoCManager.Resolve<IItemSlotManager>().Initialize();
_baseClient.RunLevelChanged += (sender, args) =>
{

View File

@@ -0,0 +1,33 @@
using Robust.Client.Graphics.Drawing;
using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Client.Interfaces.Graphics;
using Robust.Shared.Maths;
using System;
namespace Robust.Client.UserInterface.Controls
{
public class CooldownGraphic : Control
{
public float Fraction { get; set; }
protected override void Draw(DrawingHandleScreen handle)
{
const int maxSegments = 64;
const float segment = MathHelper.TwoPi / maxSegments;
var segments = (int)Math.Max(2, Math.Ceiling(maxSegments * Fraction)); // ensure that we always have 3 vertices
var max = MathHelper.TwoPi * Fraction;
var radius = (Math.Min(SizeBox.Height, SizeBox.Width) / 2) * 0.875f; // 28/32 = 0.875 - 2 pixels inwards from the edge
Span<Vector2> vertices = stackalloc Vector2[segments + 1];
vertices[0] = PixelPosition + SizeBox.Center;
for (int i = 0; i < segments; i++)
{
var angle = MathHelper.Pi + Math.Min(max, segment * i);
vertices[i + 1] = vertices[0] + new Vector2((float) Math.Sin(angle) * radius, (float) Math.Cos(angle) * radius);
}
handle.DrawPrimitives(DrawPrimitiveTopology.TriangleFan, vertices, new Color(0.3f, 0.3f, 0.4f, 0.5f));
}
}
}

View File

@@ -6,7 +6,6 @@ namespace Content.Client.UserInterface
{
public interface IItemSlotManager
{
void Initialize();
bool OnButtonPressed(GUIBoundKeyEventArgs args, IEntity item);
void UpdateCooldown(ItemSlotButton cooldownTexture, IEntity entity);
bool SetItemSlot(ItemSlotButton button, IEntity entity);

View File

@@ -1,4 +1,5 @@
using System;
using Content.Client.UserInterface;
using Content.Shared.Input;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
@@ -13,7 +14,7 @@ namespace Content.Client.GameObjects
public TextureRect Button { get; }
public SpriteView SpriteView { get; }
public BaseButton StorageButton { get; }
public TextureRect CooldownCircle { get; }
public CooldownGraphic CooldownDisplay { get; }
public Action<GUIBoundKeyEventArgs> OnPressed { get; set; }
public Action<GUIBoundKeyEventArgs> OnStoragePressed { get; set; }
@@ -56,12 +57,10 @@ namespace Content.Client.GameObjects
StorageButton.OnPressed += OnStorageButtonPressed;
AddChild(CooldownCircle = new TextureRect
AddChild(CooldownDisplay = new CooldownGraphic
{
SizeFlagsHorizontal = SizeFlags.ShrinkCenter,
SizeFlagsVertical = SizeFlags.ShrinkCenter,
Stretch = TextureRect.StretchMode.KeepCentered,
TextureScale = (2, 2),
SizeFlagsHorizontal = SizeFlags.Fill,
SizeFlagsVertical = SizeFlags.Fill,
Visible = false,
});
}

View File

@@ -30,22 +30,8 @@ namespace Content.Client.UserInterface
[Dependency] private readonly IInputManager _inputManager;
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
[Dependency] private readonly IEyeManager _eyeManager;
[Dependency] private readonly IResourceCache _resourceCache;
#pragma warning restore 0649
private const int CooldownLevels = 8;
private readonly Texture[] _texturesCooldownOverlay = new Texture[CooldownLevels];
public void Initialize()
{
for (var i = 0; i < CooldownLevels; i++)
{
_texturesCooldownOverlay[i] =
_resourceCache.GetTexture($"/Textures/UserInterface/Inventory/cooldown-{i}.png");
}
}
public bool SetItemSlot(ItemSlotButton button, IEntity entity)
{
if (entity == null)
@@ -103,7 +89,7 @@ namespace Content.Client.UserInterface
public void UpdateCooldown(ItemSlotButton button, IEntity entity)
{
var cooldownTexture = button.CooldownCircle;
var cooldownDisplay = button.CooldownDisplay;
if (entity != null
&& entity.TryGetComponent(out ItemCooldownComponent cooldown)
@@ -115,30 +101,23 @@ namespace Content.Client.UserInterface
var length = (end - start).TotalSeconds;
var progress = (_gameTiming.CurTime - start).TotalSeconds;
var ratio = (float)(progress / length);
var ratio = 1 - (float)(progress / length).Clamp(0, 1);
var textureIndex = CalculateCooldownLevel(ratio);
if (textureIndex == CooldownLevels)
cooldownDisplay.Fraction = ratio;
if (ratio > 0)
{
cooldownTexture.Visible = false;
cooldownDisplay.Visible = true;
}
else
{
cooldownTexture.Visible = true;
cooldownTexture.Texture = _texturesCooldownOverlay[textureIndex];
cooldownDisplay.Visible = false;
}
}
else
{
cooldownTexture.Visible = false;
}
}
internal static int CalculateCooldownLevel(float cooldownValue)
{
var val = cooldownValue.Clamp(0, 1);
val *= CooldownLevels;
return (int)Math.Floor(val);
cooldownDisplay.Visible = false;
}
}
}
}

View File

@@ -1,17 +0,0 @@
using Content.Client.UserInterface;
using NUnit.Framework;
namespace Content.Tests.Client.UserInterface
{
[TestFixture]
public class ItemSlotTest
{
[Test]
public void TestCalculateCooldownLevel()
{
Assert.AreEqual(ItemSlotManager.CalculateCooldownLevel(0.5f), 4);
Assert.AreEqual(ItemSlotManager.CalculateCooldownLevel(1), 8);
Assert.AreEqual(ItemSlotManager.CalculateCooldownLevel(0), 0);
}
}
}