A game about forced loneliness, made by TACStudios
1using System;
2
3namespace UnityEditor.Rendering
4{
5 /// <summary>Bool flag saved in EditorPref</summary>
6 /// <typeparam name="T">Underlying enum type</typeparam>
7 public struct EditorPrefBoolFlags<T> : IEquatable<T>, IEquatable<EditorPrefBoolFlags<T>>
8 where T : struct, IConvertible
9 {
10 readonly string m_Key;
11
12 /// <summary>The value as the underlying enum type used</summary>
13 public T value
14 { get => (T)(object)EditorPrefs.GetInt(m_Key); set => EditorPrefs.SetInt(m_Key, (int)(object)value); }
15
16 /// <summary>The raw value</summary>
17 public uint rawValue
18 {
19 get => (uint)EditorPrefs.GetInt(m_Key);
20 set => EditorPrefs.SetInt(m_Key, (int)value);
21 }
22
23 /// <summary>Constructor</summary>
24 /// <param name="key">Name of the Key in EditorPrefs to save the value</param>
25 public EditorPrefBoolFlags(string key)
26 {
27 m_Key = key;
28 }
29
30 /// <summary>Test if saved value is equal to the one given</summary>
31 /// <param name="other">Given value</param>
32 /// <returns>True if value are the same</returns>
33 public bool Equals(T other) => (int)(object)value == (int)(object)other;
34
35 /// <summary>Test if this EditorPrefBoolFlags is the same than the given one</summary>
36 /// <param name="other">Given EditorPrefBoolFlags</param>
37 /// <returns>True if they use the same value</returns>
38 public bool Equals(EditorPrefBoolFlags<T> other) => m_Key == other.m_Key;
39
40 /// <summary>Test if the given flags are set</summary>
41 /// <param name="v">Given flags</param>
42 /// <returns>True: all the given flags are set</returns>
43 public bool HasFlag(T v) => ((uint)(int)(object)v & rawValue) == (uint)(int)(object)v;
44 /// <summary>Set or unset the flags</summary>
45 /// <param name="f">Flags to edit</param>
46 /// <param name="v">Boolean value to set to the given flags</param>
47 public void SetFlag(T f, bool v)
48 {
49 if (v) rawValue |= (uint)(int)(object)f;
50 else rawValue &= ~(uint)(int)(object)f;
51 }
52
53 /// <summary>Explicit conversion operator to the underlying type</summary>
54 /// <param name="v">The EditorPrefBoolFlags to convert</param>
55 /// <returns>The converted value</returns>
56 public static explicit operator T(EditorPrefBoolFlags<T> v) => v.value;
57 /// <summary>Or operator between a EditorPrefBoolFlags and a value</summary>
58 /// <param name="l">The EditorPrefBoolFlags</param>
59 /// <param name="r">The value</param>
60 /// <returns>A EditorPrefBoolFlags with OR operator performed</returns>
61 public static EditorPrefBoolFlags<T> operator |(EditorPrefBoolFlags<T> l, T r)
62 {
63 l.rawValue |= (uint)(int)(object)r;
64 return l;
65 }
66
67 /// <summary>And operator between a EditorPrefBoolFlags and a value</summary>
68 /// <param name="l">The EditorPrefBoolFlags</param>
69 /// <param name="r">The value</param>
70 /// <returns>A EditorPrefBoolFlags with AND operator performed</returns>
71 public static EditorPrefBoolFlags<T> operator &(EditorPrefBoolFlags<T> l, T r)
72 {
73 l.rawValue &= (uint)(int)(object)r;
74 return l;
75 }
76
77 /// <summary>Xor operator between a EditorPrefBoolFlags and a value</summary>
78 /// <param name="l">The EditorPrefBoolFlags</param>
79 /// <param name="r">The value</param>
80 /// <returns>A EditorPrefBoolFlags with XOR operator performed</returns>
81 public static EditorPrefBoolFlags<T> operator ^(EditorPrefBoolFlags<T> l, T r)
82 {
83 l.rawValue ^= (uint)(int)(object)r;
84 return l;
85 }
86 }
87}