using Sandbox.Graphics; using Sandbox.Graphics.GUI; using VRage.Utils; using VRageMath; namespace PluginLoader.GUI.GuiControls; // From Sandbox.Game.Screens.Helpers.MyGuiControlRating internal class RatingControl : MyGuiControlBase { private readonly float m_space = 8f; public string EmptyTexture = "Textures\\GUI\\Icons\\Rating\\NoStar.png"; public string FilledTexture = "Textures\\GUI\\Icons\\Rating\\FullStar.png"; public string HalfFilledTexture = "Textures\\GUI\\Icons\\Rating\\HalfStar.png"; private int m_maxValue; private readonly Vector2 m_textureSize = new(32f); public RatingControl(int value = 0, int maxValue = 10) { Value = value; m_maxValue = maxValue; BackgroundTexture = null; ColorMask = Vector4.One; } public int MaxValue { get => m_maxValue; set { m_maxValue = value; RecalculateSize(); } } public int Value { get; set; } private void RecalculateSize() { var vector = MyGuiManager.GetHudNormalizedSizeFromPixelSize(m_textureSize) * new Vector2(0.75f, 1f); var hudNormalizedSizeFromPixelSize = MyGuiManager.GetHudNormalizedSizeFromPixelSize(new(m_space * 0.75f, 0f)); Size = new((vector.X + hudNormalizedSizeFromPixelSize.X) * m_maxValue, vector.Y); } public float GetWidth() { var num = MyGuiManager.GetHudNormalizedSizeFromPixelSize(m_textureSize).X * 0.75f; var num2 = MyGuiManager.GetHudNormalizedSizeFromPixelSize(new(m_space * 0.75f, 0f)).X; return (num + num2) * MaxValue / 2f; } public override void Draw(float transitionAlpha, float backgroundTransitionAlpha) { base.Draw(transitionAlpha, backgroundTransitionAlpha); if (MaxValue <= 0) return; var normalizedSize = MyGuiManager.GetHudNormalizedSizeFromPixelSize(m_textureSize) * new Vector2(0.75f, 1f); var hudNormalizedSizeFromPixelSize = MyGuiManager.GetHudNormalizedSizeFromPixelSize(new(m_space * 0.75f, 0f)); var vector = GetPositionAbsoluteTopLeft() + new Vector2(0f, (Size.Y - normalizedSize.Y) / 2f); var vector2 = new Vector2((normalizedSize.X + hudNormalizedSizeFromPixelSize.X) * 0.5f, normalizedSize.Y); for (var i = 0; i < MaxValue; i += 2) { var normalizedCoord = vector + new Vector2(vector2.X * i, 0f); if (i == Value - 1) MyGuiManager.DrawSpriteBatch(HalfFilledTexture, normalizedCoord, normalizedSize, ApplyColorMaskModifiers(ColorMask, Enabled, transitionAlpha), MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, false, false); else if (i < Value) MyGuiManager.DrawSpriteBatch(FilledTexture, normalizedCoord, normalizedSize, ApplyColorMaskModifiers(ColorMask, Enabled, transitionAlpha), MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, false, false); else MyGuiManager.DrawSpriteBatch(EmptyTexture, normalizedCoord, normalizedSize, ApplyColorMaskModifiers(ColorMask, Enabled, transitionAlpha), MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, false, false); } } }