VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_Settings_Editor.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEditor;
5 
6 using AVR.Core;
7 
8 namespace AVR.UEditor.Core {
9  public class AVR_Settings_Editor : EditorWindow
10  {
11  private static SettingsTreeNode root;
12  Vector2 scrollPos;
13 
14  [MenuItem("AVR/Settings", false, 999)]
15  public static void ShowWindow()
16  {
17  root = AVR_Settings.build_tree();
18  EditorWindow.GetWindow(typeof(AVR_Settings_Editor), true, "AVR Settings");
19  }
20 
21  void OnGUI()
22  {
23  EditorGUILayout.LabelField("AVR SETTINGS");
24 
25  EditorGUILayout.BeginVertical();
26  scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
27  foreach (SettingsTreeNode n in root.get_children()) OnGUISettingsNode(n);
28  EditorGUILayout.EndScrollView();
29  EditorGUILayout.EndVertical();
30 
31  if(GUILayout.Button("Save")) {
32  // All settings excluding any changes made now as well as changes in ~overridesettings.avr
33  Dictionary<string, string> entries_pre = AVR_SettingsParser.AutoParseSettingFiles(true);
34 
35  // This will be all settings entries *including* any changes made now as well as in ~overridesettings.avr
36  Dictionary<string, string> entries_post = AVR_SettingsParser.AutoParseSettingFiles(false);
37 
38  // Scan SettingsTree for any changes in entries_post. Apply these if found.
39  root.traverse((SettingsTreeNode n) => { if(n.is_leaf() && entries_post[n.fullname]!=n.value) entries_post[n.fullname] = n.value; });
40 
41  // Keys that differ between pre and post
42  Dictionary<string, string> entries_diff = new Dictionary<string, string>();
43 
44  // Look for differences
45  foreach(string k in entries_post.Keys) {
46  //if k is not a key in entries or the entry is different
47  if(!entries_pre.ContainsKey(k) || entries_pre[k]!=entries_post[k]) {
48  entries_diff.Add(k, entries_post[k]);
49  }
50  }
51 
52 
53  // Re-insert these into ~overridesettings.avr
54  string content = "";
55  string filepath = "";
56  foreach (string k in entries_diff.Keys)
57  {
58  content += k.Substring(1) + " = \"" + entries_diff[k] + "\"\n";
59  }
60  try {
61  filepath = System.IO.Directory.GetFiles(Application.dataPath+"/..", "~overridesettings.avr", System.IO.SearchOption.AllDirectories)[0];
62  } catch(System.Exception) {
63  AVR_DevConsole.cwarn("Could not find ~overridesettings.avr. Creating new file at directory: " + Application.dataPath, "AVR_Settings_Editor");
64  filepath = Application.dataPath + "/~overridesettings.avr";
65  System.IO.File.Create(filepath);
66  }
67  System.IO.File.WriteAllText(filepath, content);
68 
69  // reload settings
70  AVR_Settings.settings = AVR_SettingsParser.AutoParseSettingFiles();
71  }
72  }
73 
75  if(node.is_leaf()) {
76  string data = node.value;
77  if(bool.TryParse(data, out bool b)) {
78  node.value = EditorGUILayout.Toggle(node.name, b).ToString();
79  }
80  else if(float.TryParse(data, out float f)) {
81  node.value = EditorGUILayout.FloatField(node.name, f).ToString();
82  }
83  else {
84  node.value = EditorGUILayout.TextField(node.name, data);
85  }
86  }
87  else {
88  node.foldout = EditorGUILayout.Foldout(node.foldout, node.name, true);
89  EditorGUI.indentLevel++;
90  if(node.foldout) foreach(SettingsTreeNode n in node.get_children()) OnGUISettingsNode(n);
91  EditorGUI.indentLevel--;
92  }
93  }
94  }
95 }
SettingsTreeNode[] get_children()
void OnGUISettingsNode(SettingsTreeNode node)
Class representing a node from a settings-tree returned by AVR_Settings.build_tree() ...