From 241aa353e993f1cf8f7303d780eb6544660715e2 Mon Sep 17 00:00:00 2001 From: moneyl <8206401+Moneyl@users.noreply.github.com> Date: Mon, 27 May 2019 18:15:13 -0400 Subject: [PATCH] Apc variable charge bar color (#240) * Make Apc chargebar color dependent on charge amount * Adjust orange color and increase brightness --- .../Components/Power/ApcBoundUserInterface.cs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Content.Client/GameObjects/Components/Power/ApcBoundUserInterface.cs b/Content.Client/GameObjects/Components/Power/ApcBoundUserInterface.cs index 9b4aa21396..b308ad3425 100644 --- a/Content.Client/GameObjects/Components/Power/ApcBoundUserInterface.cs +++ b/Content.Client/GameObjects/Components/Power/ApcBoundUserInterface.cs @@ -3,12 +3,14 @@ using Content.Shared.GameObjects.Components.Power; using NJsonSchema.Validation; using OpenTK.Graphics.OpenGL4; using Robust.Client.GameObjects.Components.UserInterface; +using Robust.Client.Graphics.Drawing; using Robust.Client.Interfaces.Graphics; using Robust.Client.UserInterface; using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.CustomControls; using Robust.Shared.GameObjects.Components.UserInterface; using Robust.Shared.IoC; +using Robust.Shared.Maths; using Robust.Shared.Utility; namespace Content.Client.GameObjects.Components.Power @@ -65,6 +67,45 @@ namespace Content.Client.GameObjects.Components.Power } _chargeBar.Value = castState.Charge; + UpdateChargeBarColor(castState.Charge); + } + + private void UpdateChargeBarColor(float charge) + { + float normalizedCharge = charge / _chargeBar.MaxValue; + + float leftHue = 0.0f;// Red + float middleHue = 0.066f;// Orange + float rightHue = 0.33f;// Green + float saturation = 1.0f;// Uniform saturation + float value = 0.8f;// Uniform value / brightness + float alpha = 1.0f;// Uniform alpha + + // These should add up to 1.0 or your transition won't be smooth + float leftSideSize = 0.5f;// Fraction of _chargeBar lerped from leftHue to middleHue + float rightSideSize = 0.5f;// Fraction of _chargeBar lerped from middleHue to rightHue + + float finalHue; + if (normalizedCharge <= leftSideSize) + { + normalizedCharge /= leftSideSize;// Adjust range to 0.0 to 1.0 + finalHue = FloatMath.Lerp(leftHue, middleHue, normalizedCharge); + } + else + { + normalizedCharge = (normalizedCharge - leftSideSize) / rightSideSize;// Adjust range to 0.0 to 1.0. + finalHue = FloatMath.Lerp(middleHue, rightHue, normalizedCharge); + } + + // Check if null first to avoid repeatedly creating this. + if (_chargeBar.ForegroundStyleBoxOverride == null) + { + _chargeBar.ForegroundStyleBoxOverride = new StyleBoxFlat(); + } + + var foregroundStyleBoxOverride = (StyleBoxFlat)_chargeBar.ForegroundStyleBoxOverride; + foregroundStyleBoxOverride.BackgroundColor = + Color.FromHsv(new Vector4(finalHue, saturation, value, alpha)); } protected override void Dispose(bool disposing)