A game about forced loneliness, made by TACStudios
at master 160 lines 5.6 kB view raw
1using System; 2using System.Collections.Generic; 3 4namespace UnityEngine.Rendering 5{ 6 /// <summary> 7 /// Templated class for <see cref="IDebugDisplaySettings"/> 8 /// </summary> 9 /// <typeparam name="T">The specific type of Debug Display Settings that is inheriting from the IDebugDisplaySettings interface.</typeparam> 10 public abstract class DebugDisplaySettings<T> : IDebugDisplaySettings 11 where T : IDebugDisplaySettings, new() 12 { 13 class IDebugDisplaySettingsDataComparer : IEqualityComparer<IDebugDisplaySettingsData> 14 { 15 public bool Equals(IDebugDisplaySettingsData x, IDebugDisplaySettingsData y) 16 { 17 if (ReferenceEquals(x, y)) 18 return true; 19 20 if (x == null || y == null) 21 return false; 22 23 return x.GetType() == y.GetType(); 24 } 25 26 public int GetHashCode(IDebugDisplaySettingsData obj) 27 { 28 // Define your custom hashing logic based on the properties you want to include. 29 unchecked 30 { 31 int hash = 17; 32 hash = hash * 23 + obj.GetType().GetHashCode(); 33 return hash; 34 } 35 } 36 } 37 38 /// <summary> 39 /// The set of <see cref="IDebugDisplaySettingsData"/> containing the settings for this debug display 40 /// </summary> 41 protected readonly HashSet<IDebugDisplaySettingsData> m_Settings = new HashSet<IDebugDisplaySettingsData>(new IDebugDisplaySettingsDataComparer()); 42 43 private static readonly Lazy<T> s_Instance = new Lazy<T>(() => 44 { 45 var instance = new T(); 46 instance.Reset(); 47 return instance; 48 }); 49 50 /// <summary> 51 /// The singleton instance that contains the current settings of Rendering Debugger. 52 /// </summary> 53 public static T Instance => s_Instance.Value; 54 55 #region IDebugDisplaySettingsQuery 56 57 /// <summary> 58 /// Returns true if any of the debug settings are currently active. 59 /// </summary> 60 public virtual bool AreAnySettingsActive 61 { 62 get 63 { 64 foreach (IDebugDisplaySettingsData setting in m_Settings) 65 { 66 if (setting.AreAnySettingsActive) 67 return true; 68 } 69 70 return false; 71 } 72 } 73 74 /// <summary> 75 /// Checks whether the current state of these settings allows post-processing. 76 /// </summary> 77 public virtual bool IsPostProcessingAllowed 78 { 79 get 80 { 81 // Only enable post-processing if we aren't using certain debug-views. 82 bool postProcessingAllowed = true; 83 foreach (IDebugDisplaySettingsData setting in m_Settings) 84 postProcessingAllowed &= setting.IsPostProcessingAllowed; 85 return postProcessingAllowed; 86 } 87 } 88 89 /// <summary> 90 /// Returns true if lighting is active for current state of debug settings. 91 /// </summary> 92 public virtual bool IsLightingActive 93 { 94 get 95 { 96 bool lightingActive = true; 97 foreach (IDebugDisplaySettingsData setting in m_Settings) 98 lightingActive &= setting.IsLightingActive; 99 return lightingActive; 100 } 101 } 102 #endregion 103 104 /// <summary> 105 /// Adds a new <see cref="TData"/> to this settings 106 /// </summary> 107 /// <typeparam name="TData">The type of <see cref="TData"/> to be added</typeparam> 108 /// <param name="newData">The <see cref="TData"/> to be added</param> 109 /// <returns>The type of <see cref="TData"/> that has been added</returns> 110 protected TData Add<TData>(TData newData) 111 where TData : IDebugDisplaySettingsData 112 { 113 m_Settings.Add(newData); 114 return newData; 115 } 116 117 /// <inheritdoc/> 118 IDebugDisplaySettingsData IDebugDisplaySettings.Add(IDebugDisplaySettingsData newData) 119 { 120 m_Settings.Add(newData); 121 return newData; 122 } 123 124 /// <summary> 125 /// Executes an action for each element 126 /// </summary> 127 /// <param name="onExecute">The action to be executed on each element in the Debug Display Settings data.</param> 128 public void ForEach(Action<IDebugDisplaySettingsData> onExecute) 129 { 130 foreach (IDebugDisplaySettingsData setting in m_Settings) 131 { 132 onExecute(setting); 133 } 134 } 135 136 /// <summary> 137 /// Reset the stored debug settings 138 /// </summary> 139 public virtual void Reset() 140 { 141 m_Settings.Clear(); 142 } 143 144 /// <summary> 145 /// Attempts to get the color that should be used to clear the screen according to current debug settings. 146 /// </summary> 147 /// <param name="color">A reference to the screen clear color to use.</param> 148 /// <returns>True if the color reference was updated, and false otherwise.</returns> 149 public virtual bool TryGetScreenClearColor(ref Color color) 150 { 151 foreach (IDebugDisplaySettingsData setting in m_Settings) 152 { 153 if (setting.TryGetScreenClearColor(ref color)) 154 return true; 155 } 156 157 return false; 158 } 159 } 160}