Skip to content

Commit d2fbafa

Browse files
committed
New YAML-serializable object alternatives
1 parent 7327084 commit d2fbafa

2 files changed

Lines changed: 108 additions & 1 deletion

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System.ComponentModel;
2+
3+
using UnityEngine;
4+
5+
using YamlDotNet.Serialization;
6+
7+
namespace LabExtended.Core.Configs.Objects
8+
{
9+
/// <summary>
10+
/// Represents a YAML-serializable <see cref="UnityEngine.Color"/> alternative.
11+
/// </summary>
12+
public class YamlColor
13+
{
14+
private Color? cachedColor;
15+
16+
/// <summary>
17+
/// Gets or sets the value of the color's alpha.
18+
/// </summary>
19+
[Description("The alpha component of the color.")]
20+
public float A { get; set; }
21+
22+
/// <summary>
23+
/// Gets or sets the red component of the color.
24+
/// </summary>
25+
[Description("The red component of the color.")]
26+
public float R { get; set; }
27+
28+
/// <summary>
29+
/// Gets or sets the green component of the color.
30+
/// </summary>
31+
[Description("The green component of the color.")]
32+
public float G { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets the blue component of the color.
36+
/// </summary>
37+
[Description("The blue component of the color.")]
38+
public float B { get; set; }
39+
40+
/// <summary>
41+
/// Gets the converted Unity <see cref="Color"/>.
42+
/// </summary>
43+
[YamlIgnore]
44+
public Color Color
45+
{
46+
get
47+
{
48+
if (!cachedColor.HasValue
49+
|| cachedColor.Value.a != A
50+
|| cachedColor.Value.r != R
51+
|| cachedColor.Value.g != G
52+
|| cachedColor.Value.b != B)
53+
cachedColor = new Color(R, G, B, A);
54+
55+
return cachedColor.Value;
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Initializes a new instance of the <see cref="YamlColor"/> class.
61+
/// </summary>
62+
public YamlColor()
63+
{
64+
65+
}
66+
67+
/// <summary>
68+
/// Initializes a new instance of the <see cref="YamlColor"/> class using the specified <see cref="Color"/>.
69+
/// </summary>
70+
/// <param name="color">The <see cref="Color"/> instance from which to initialize the <see cref="YamlColor"/>. The alpha, red,
71+
/// green, and blue components of the color are copied to the corresponding properties of the <see
72+
/// cref="YamlColor"/>.</param>
73+
public YamlColor(Color color)
74+
{
75+
A = color.a;
76+
R = color.r;
77+
G = color.g;
78+
B = color.b;
79+
80+
cachedColor = color;
81+
}
82+
83+
/// <summary>
84+
/// Initializes a new instance of the <see cref="YamlColor"/> struct with the specified color components.
85+
/// </summary>
86+
/// <param name="a">The alpha component of the color, representing transparency.</param>
87+
/// <param name="r">The red component of the color.</param>
88+
/// <param name="g">The green component of the color.</param>
89+
/// <param name="b">The blue component of the color.</param>
90+
public YamlColor(float a, float r, float g, float b)
91+
{
92+
A = a;
93+
R = r;
94+
G = g;
95+
B = b;
96+
97+
cachedColor = new Color(r, g, b, a);
98+
}
99+
100+
/// <summary>
101+
/// Performs an implicit conversion from <see cref="YamlColor"/> to <see cref="Color"/>.
102+
/// </summary>
103+
/// <param name="yamlColor">The <see cref="YamlColor"/> instance to convert. Cannot be <see langword="null"/>.</param>
104+
public static implicit operator Color(YamlColor yamlColor)
105+
=> yamlColor?.Color ?? throw new ArgumentNullException(nameof(yamlColor));
106+
}
107+
}

LabExtended/Core/Configs/Objects/YamlVector3.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,5 @@ public YamlVector3(float x, float y = 0f, float z = 0f)
8282
/// <param name="vector">The instance to convert.</param>
8383
/// <returns>The converted Vector3.</returns>
8484
public static implicit operator Vector3(YamlVector3 vector)
85-
=> vector.Vector;
85+
=> vector?.Vector ?? throw new ArgumentNullException(nameof(vector));
8686
}

0 commit comments

Comments
 (0)