Files
OldThink/Content.Client/_White/Medical/BodyScanner/GroupDamageCardComponent.xaml.cs
Remuchi fe82255139 [Fix]: Исправлено отображение урона в УИ анализатора здоровья (#77)
* fix: возвращено отображение урона в сканере здоровья

* add: сканер здоровья только те группы, где есть урон
2024-02-15 18:08:30 +00:00

109 lines
3.9 KiB
C#

using Content.Shared.Damage.Prototypes;
using Content.Shared.FixedPoint;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
namespace Content.Client._White.Medical.BodyScanner
{
[GenerateTypedNameReferences]
public sealed partial class GroupDamageCardComponent : Control
{
public GroupDamageCardComponent(string title, string damageGroupId, IReadOnlyDictionary<string, FixedPoint2> damagePerType)
{
RobustXamlLoader.Load(this);
DamageGroupTitle.Text = title;
HashSet<string> shownTypes = new();
var protos = IoCManager.Resolve<IPrototypeManager>();
var group = protos.Index<DamageGroupPrototype>(damageGroupId);
// Show the damage for each type in that group.
foreach (var type in group.DamageTypes)
{
if (!damagePerType.TryGetValue(type, out var typeAmount))
{
continue;
}
if (typeAmount == 0)
{
continue;
}
// If damage types are allowed to belong to more than one damage group, they may appear twice here. Mark them as duplicate.
if (!shownTypes.Add(type))
{
continue;
}
Label damagePerTypeLabel = new()
{
Text = Loc.GetString("health-analyzer-window-damage-type-" + type, ("amount", typeAmount)),
};
SetColorLabel(damagePerTypeLabel, typeAmount.Float());
DamageLabelsContainer.AddChild(damagePerTypeLabel);
}
}
/// <summary>
/// Sets the color of a label depending on the damage.
/// </summary>
/// <param name="label"></param>
/// <param name="damage"></param>
private static void SetColorLabel(Label label, float damage)
{
var startColor = Color.White;
var critColor = Color.Yellow;
var endColor = Color.Red;
const float startDamage = 0f;
const float critDamage = 30f;
const float endDamage = 100f;
switch (damage)
{
case <= startDamage:
label.FontColorOverride = startColor;
break;
case >= endDamage:
label.FontColorOverride = endColor;
break;
case >= startDamage and <= critDamage:
// We need a number from 0 to 100.
damage *= 100f / (critDamage - startDamage);
label.FontColorOverride = GetColorLerp(startColor, critColor, damage);
break;
case >= critDamage and <= endDamage:
// We need a number from 0 to 100.
damage *= 100f / (endDamage - critDamage);
label.FontColorOverride = GetColorLerp(critColor, endColor, damage);
break;
}
}
/// <summary>
/// Smooth transition from one color to another depending on the percentage.
/// </summary>
/// <param name="startColor"></param>
/// <param name="endColor"></param>
/// <param name="percentage"></param>
/// <returns></returns>
private static Color GetColorLerp(Color startColor, Color endColor, float percentage)
{
var t = percentage / 100f;
var r = MathHelper.Lerp(startColor.R, endColor.R, t);
var g = MathHelper.Lerp(startColor.G, endColor.G, t);
var b = MathHelper.Lerp(startColor.B, endColor.B, t);
var a = MathHelper.Lerp(startColor.A, endColor.A, t);
return new Color(r, g, b, a);
}
}
}