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 damagePerType) { RobustXamlLoader.Load(this); DamageGroupTitle.Text = title; HashSet shownTypes = new(); var protos = IoCManager.Resolve(); var group = protos.Index(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); } } /// /// Sets the color of a label depending on the damage. /// /// /// 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; } } /// /// Smooth transition from one color to another depending on the percentage. /// /// /// /// /// 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); } } }