|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using UnityEngine; |
| 8 | +using UnityExplorer.UI.Main; |
| 9 | +using UnityExplorer.UI.Main.Home; |
| 10 | + |
| 11 | +namespace UnityExplorer.Core.Config |
| 12 | +{ |
| 13 | + public static class ConfigManager |
| 14 | + { |
| 15 | + // Each Loader has its own ConfigHandler. |
| 16 | + // See the UnityExplorer.Loader namespace for the implementations. |
| 17 | + public static IConfigHandler Handler { get; private set; } |
| 18 | + |
| 19 | + public static ConfigElement<KeyCode> Main_Menu_Toggle; |
| 20 | + public static ConfigElement<int> Default_Page_Limit; |
| 21 | + public static ConfigElement<string> Default_Output_Path; |
| 22 | + public static ConfigElement<bool> Log_Unity_Debug; |
| 23 | + public static ConfigElement<bool> Hide_On_Startup; |
| 24 | + public static ConfigElement<string> Last_Window_Anchors; |
| 25 | + public static ConfigElement<int> Last_Active_Tab; |
| 26 | + public static ConfigElement<bool> Last_DebugConsole_State; |
| 27 | + public static ConfigElement<bool> Last_SceneExplorer_State; |
| 28 | + |
| 29 | + internal static readonly Dictionary<string, IConfigElement> ConfigElements = new Dictionary<string, IConfigElement>(); |
| 30 | + |
| 31 | + public static void Init(IConfigHandler configHandler) |
| 32 | + { |
| 33 | + Handler = configHandler; |
| 34 | + Handler.Init(); |
| 35 | + |
| 36 | + CreateConfigElements(); |
| 37 | + |
| 38 | + Handler.LoadConfig(); |
| 39 | + |
| 40 | + SceneExplorer.OnToggleShow += SceneExplorer_OnToggleShow; |
| 41 | + PanelDragger.OnFinishResize += PanelDragger_OnFinishResize; |
| 42 | + MainMenu.OnActiveTabChanged += MainMenu_OnActiveTabChanged; |
| 43 | + DebugConsole.OnToggleShow += DebugConsole_OnToggleShow; |
| 44 | + } |
| 45 | + |
| 46 | + internal static void RegisterConfigElement<T>(ConfigElement<T> configElement) |
| 47 | + { |
| 48 | + Handler.RegisterConfigElement(configElement); |
| 49 | + ConfigElements.Add(configElement.Name, configElement); |
| 50 | + } |
| 51 | + |
| 52 | + private static void CreateConfigElements() |
| 53 | + { |
| 54 | + Main_Menu_Toggle = new ConfigElement<KeyCode>("Main Menu Toggle", |
| 55 | + "The UnityEngine.KeyCode to toggle the UnityExplorer Menu.", |
| 56 | + KeyCode.F7, |
| 57 | + false); |
| 58 | + |
| 59 | + Default_Page_Limit = new ConfigElement<int>("Default Page Limit", |
| 60 | + "The default maximum number of elements per 'page' in UnityExplorer.", |
| 61 | + 25, |
| 62 | + false); |
| 63 | + |
| 64 | + Default_Output_Path = new ConfigElement<string>("Default Output Path", |
| 65 | + "The default output path when exporting things from UnityExplorer.", |
| 66 | + Path.Combine(ExplorerCore.Loader.ExplorerFolder, "Output"), |
| 67 | + false); |
| 68 | + |
| 69 | + Log_Unity_Debug = new ConfigElement<bool>("Log Unity Debug", |
| 70 | + "Should UnityEngine.Debug.Log messages be printed to UnityExplorer's log?", |
| 71 | + false, |
| 72 | + false); |
| 73 | + |
| 74 | + Hide_On_Startup = new ConfigElement<bool>("Hide On Startup", |
| 75 | + "Should UnityExplorer be hidden on startup?", |
| 76 | + false, |
| 77 | + false); |
| 78 | + |
| 79 | + Last_Window_Anchors = new ConfigElement<string>("Last_Window_Anchors", |
| 80 | + "For internal use, the last anchors of the UnityExplorer window.", |
| 81 | + DEFAULT_WINDOW_ANCHORS, |
| 82 | + true); |
| 83 | + |
| 84 | + Last_Active_Tab = new ConfigElement<int>("Last_Active_Tab", |
| 85 | + "For internal use, the last active tab index.", |
| 86 | + 0, |
| 87 | + true); |
| 88 | + |
| 89 | + Last_DebugConsole_State = new ConfigElement<bool>("Last_DebugConsole_State", |
| 90 | + "For internal use, the collapsed state of the Debug Console.", |
| 91 | + true, |
| 92 | + true); |
| 93 | + |
| 94 | + Last_SceneExplorer_State = new ConfigElement<bool>("Last_SceneExplorer_State", |
| 95 | + "For internal use, the collapsed state of the Scene Explorer.", |
| 96 | + true, |
| 97 | + true); |
| 98 | + } |
| 99 | + |
| 100 | + // Internal config callback listeners |
| 101 | + |
| 102 | + private static void PanelDragger_OnFinishResize(RectTransform rect) |
| 103 | + { |
| 104 | + Last_Window_Anchors.Value = RectAnchorsToString(rect); |
| 105 | + Handler.SaveConfig(); |
| 106 | + } |
| 107 | + |
| 108 | + private static void MainMenu_OnActiveTabChanged(int page) |
| 109 | + { |
| 110 | + Last_Active_Tab.Value = page; |
| 111 | + Handler.SaveConfig(); |
| 112 | + } |
| 113 | + |
| 114 | + private static void DebugConsole_OnToggleShow(bool showing) |
| 115 | + { |
| 116 | + Last_DebugConsole_State.Value = showing; |
| 117 | + Handler.SaveConfig(); |
| 118 | + } |
| 119 | + |
| 120 | + private static void SceneExplorer_OnToggleShow(bool showing) |
| 121 | + { |
| 122 | + Last_SceneExplorer_State.Value = showing; |
| 123 | + Handler.SaveConfig(); |
| 124 | + } |
| 125 | + |
| 126 | + // Window Anchors helpers |
| 127 | + |
| 128 | + private const string DEFAULT_WINDOW_ANCHORS = "0.25,0.10,0.78,0.95"; |
| 129 | + |
| 130 | + internal static CultureInfo _enCulture = new CultureInfo("en-US"); |
| 131 | + |
| 132 | + internal static string RectAnchorsToString(this RectTransform rect) |
| 133 | + { |
| 134 | + try |
| 135 | + { |
| 136 | + return string.Format(_enCulture, "{0},{1},{2},{3}", new object[] |
| 137 | + { |
| 138 | + rect.anchorMin.x, |
| 139 | + rect.anchorMin.y, |
| 140 | + rect.anchorMax.x, |
| 141 | + rect.anchorMax.y |
| 142 | + }); |
| 143 | + } |
| 144 | + catch |
| 145 | + { |
| 146 | + return DEFAULT_WINDOW_ANCHORS; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + internal static void SetAnchorsFromString(this RectTransform panel, string stringAnchors) |
| 151 | + { |
| 152 | + Vector4 anchors; |
| 153 | + try |
| 154 | + { |
| 155 | + var split = stringAnchors.Split(','); |
| 156 | + |
| 157 | + if (split.Length != 4) |
| 158 | + throw new Exception(); |
| 159 | + |
| 160 | + anchors.x = float.Parse(split[0], _enCulture); |
| 161 | + anchors.y = float.Parse(split[1], _enCulture); |
| 162 | + anchors.z = float.Parse(split[2], _enCulture); |
| 163 | + anchors.w = float.Parse(split[3], _enCulture); |
| 164 | + } |
| 165 | + catch |
| 166 | + { |
| 167 | + anchors = new Vector4(0.25f, 0.1f, 0.78f, 0.95f); |
| 168 | + } |
| 169 | + |
| 170 | + panel.anchorMin = new Vector2(anchors.x, anchors.y); |
| 171 | + panel.anchorMax = new Vector2(anchors.z, anchors.w); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments