From 73c92ba309b1e5aa378ea1aac1588869bb7c3392 Mon Sep 17 00:00:00 2001 From: Scott Doxey Date: Sat, 3 Apr 2021 04:02:14 -0400 Subject: [PATCH] Added editor panel. --- Editor/Scripts/EnvironmentFileEditor.cs | 193 +++++++++++++++++++ Editor/Scripts/EnvironmentFileEditor.cs.meta | 3 + 2 files changed, 196 insertions(+) create mode 100644 Editor/Scripts/EnvironmentFileEditor.cs create mode 100644 Editor/Scripts/EnvironmentFileEditor.cs.meta diff --git a/Editor/Scripts/EnvironmentFileEditor.cs b/Editor/Scripts/EnvironmentFileEditor.cs new file mode 100644 index 0000000..f71438e --- /dev/null +++ b/Editor/Scripts/EnvironmentFileEditor.cs @@ -0,0 +1,193 @@ +// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. + +#if UNITY_EDITOR +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using UnityEditor; +using UnityEngine; + +namespace CandyCoded.env.Editor +{ + + public class EnvironmentFileEditor : EditorWindow + { + + private Vector2 scrollPosition; + + private List> tempConfig = new List>(); + + private void Update() + { + + if (EditorApplication.isPlaying && !EditorApplication.isPaused) + { + + Repaint(); + + } + + } + + private void OnEnable() + { + + LoadEnvironmentFile(); + + } + + private void OnGUI() + { + + if (!File.Exists(env.editorFilePath)) + { + + if (GUILayout.Button("Create .env File")) + { + + try + { + + File.WriteAllText(env.editorFilePath, + env.SerializeEnvironmentDictionary( + new Dictionary { { "VARIABLE_NAME", "VALUE" } })); + + LoadEnvironmentFile(); + + } + catch (Exception err) + { + + EditorUtility.DisplayDialog("Error", err.Message, "Ok"); + + } + + } + + return; + + } + + scrollPosition = GUILayout.BeginScrollView(scrollPosition); + + for (var i = 0; i < tempConfig.Count; i += 1) + { + + GUILayout.BeginHorizontal(); + + var key = GUILayout.TextField(tempConfig[i].Item1, GUILayout.ExpandWidth(false), + GUILayout.Width(position.width * 0.25f)); + + var value = GUILayout.TextField(tempConfig[i].Item2, GUILayout.ExpandWidth(true)); + + if (tempConfig.Count == 1) + { + + GUI.enabled = false; + + } + + if (GUILayout.Button("-", GUILayout.ExpandWidth(false))) + { + + tempConfig.RemoveAt(i); + + continue; + + } + + if (tempConfig.Count == 1) + { + + GUI.enabled = true; + + } + + if (GUILayout.Button("+", GUILayout.ExpandWidth(false))) + { + + tempConfig.Insert(i + 1, new Tuple("", "")); + + continue; + + } + + GUILayout.EndHorizontal(); + + if (!key.Equals(tempConfig[i].Item1) || !value.Equals(tempConfig[i].Item2)) + { + + tempConfig[i] = new Tuple(key, value); + + } + + } + + if (GUILayout.Button("Save Changes")) + { + + try + { + + File.WriteAllText(env.editorFilePath, env.SerializeEnvironmentDictionary( + tempConfig.ToDictionary(item => item.Item1, item => item.Item2))); + + } + catch (Exception err) + { + + EditorUtility.DisplayDialog("Error", err.Message, "Ok"); + + } + + } + + if (GUILayout.Button("Revert Changes")) + { + + LoadEnvironmentFile(); + + } + + if (GUILayout.Button("Delete .env File")) + { + + if (EditorUtility.DisplayDialog("Confirm", $"Delete {env.editorFilePath}?", "Ok", "Cancel")) + { + + File.Delete(env.editorFilePath); + + } + + } + + GUILayout.EndScrollView(); + + } + + [MenuItem("Window/CandyCoded/Environment File Editor")] + public static void ShowWindow() + { + + GetWindow(typeof(EnvironmentFileEditor), false, "Environment File Editor", true); + + } + + private void LoadEnvironmentFile() + { + + if (File.Exists(env.editorFilePath)) + { + + tempConfig = env.ParseEnvironmentFile().Select(item => new Tuple(item.Key, item.Value)) + .ToList(); + + } + + } + + } + +} +#endif diff --git a/Editor/Scripts/EnvironmentFileEditor.cs.meta b/Editor/Scripts/EnvironmentFileEditor.cs.meta new file mode 100644 index 0000000..a0e87ee --- /dev/null +++ b/Editor/Scripts/EnvironmentFileEditor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3cff657a75704ca293a63fa3c914a9ab +timeCreated: 1617436287 \ No newline at end of file