Skip to content

Commit

Permalink
Fix accidental breaking change to background-color
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Apr 27, 2024
1 parent 9896830 commit 1457e44
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
25 changes: 19 additions & 6 deletions BeatSaberMarkupLanguage/Components/Backgroundable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using HMUI;
using UnityEngine;
using UnityEngine.UI;
Expand Down Expand Up @@ -73,6 +74,7 @@ public void ApplyBackground(string name)
}
}

[Obsolete]
public void ApplyColor(Color color)
{
if (background == null)
Expand All @@ -94,6 +96,7 @@ public void ApplyColor(Color color)
}
}

[Obsolete]
public void ApplyGradient(Color color0, Color color1)
{
if (background is not ImageView imageView)
Expand All @@ -113,34 +116,44 @@ public void ApplyColor0(Color color0)
{
if (background is not ImageView imageView)
{
throw new BSMLException("Can't set gradient on null background!");
throw new BSMLException("Can't set color0 on null background!");
}

ApplyGradient(color0, imageView.color1);
imageView.color0 = color0;
}

public void ApplyColor1(Color color1)
{
if (background is not ImageView imageView)
{
throw new BSMLException("Can't set gradient on null background!");
throw new BSMLException("Can't set color1 on null background!");
}

ApplyGradient(imageView.color0, color1);
imageView.color1 = color1;
}

public void ApplyAlpha(float alpha)
{
if (background == null)
{
throw new BSMLException("Can't set gradient on null background!");
throw new BSMLException("Can't set alpha on null background!");
}

Color color = background.color;
color.a = alpha;
background.color = color;
}

internal void SetGradient(bool active)
{
if (background is not ImageView imageView)
{
throw new BSMLException("Can't set gradient on null background!");
}

imageView.gradient = active;
}

private static ImageView FindTemplate(string name, string backgroundName)
{
string objectName = ObjectNames[name];
Expand Down
9 changes: 8 additions & 1 deletion BeatSaberMarkupLanguage/Parse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,19 @@ public static RectOffset RectOffset(string s)
/// Parse a string as a <see cref="UnityEngine.Color"/>.
/// </summary>
/// <param name="s">String to parse.</param>
/// <param name="defaultAlpha">The default value to use for alpha if not specified.</param>
/// <returns>A <see cref="UnityEngine.Color"/> representation of the string.</returns>
/// <exception cref="ParseException">Thrown if the string cannot be parsed.</exception>
public static Color Color(string s)
public static Color Color(string s, float defaultAlpha = 1)
{
if (ColorUtility.TryParseHtmlString(s, out Color color))
{
// color name or #123 or #123456
if (s[0] != '#' || s.Length is 4 or 7)
{
color.a = defaultAlpha;
}

return color;
}
else
Expand Down
9 changes: 6 additions & 3 deletions BeatSaberMarkupLanguage/TypeHandlers/BackgroundableHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using BeatSaberMarkupLanguage.Components;
using HMUI;

namespace BeatSaberMarkupLanguage.TypeHandlers
{
Expand All @@ -14,15 +15,17 @@ public class BackgroundableHandler : TypeHandler<Backgroundable>
{ "backgroundColor0", new[] { "bg-color0", "background-color0" } },
{ "backgroundColor1", new[] { "bg-color1", "background-color1" } },
{ "backgroundAlpha", new[] { "bg-alpha", "background-alpha" } },
{ "backgroundGradient", new[] { "background-gradient" } },
};

public override Dictionary<string, Action<Backgroundable, string>> Setters => new()
{
{ "background", new Action<Backgroundable, string>((component, value) => component.ApplyBackground(value)) },
{ "backgroundColor", new Action<Backgroundable, string>((component, value) => component.ApplyColor(Parse.Color(value))) },
{ "backgroundColor0", new Action<Backgroundable, string>((component, value) => component.ApplyColor0(Parse.Color(value))) },
{ "backgroundColor1", new Action<Backgroundable, string>((component, value) => component.ApplyColor1(Parse.Color(value))) },
{ "backgroundColor", new Action<Backgroundable, string>((component, value) => component.background.color = Parse.Color(value, component.background.color.a)) },
{ "backgroundColor0", new Action<Backgroundable, string>((component, value) => component.ApplyColor0(Parse.Color(value, (component.background is ImageView imageView) ? imageView.color0.a : 1))) },
{ "backgroundColor1", new Action<Backgroundable, string>((component, value) => component.ApplyColor1(Parse.Color(value, (component.background is ImageView imageView) ? imageView.color1.a : 1))) },
{ "backgroundAlpha", new Action<Backgroundable, string>((component, value) => component.ApplyAlpha(Parse.Float(value))) },
{ "backgroundGradient", new Action<Backgroundable, string>((component, value) => component.SetGradient(Parse.Bool(value))) },
};
}
}

0 comments on commit 1457e44

Please sign in to comment.